blob: bf03e0ae9a17b33ee994c1eb1634242294984693 [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Clayton8c7f0da2021-06-17 15:48:39 +000025#include "src/ast/atomic.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000026#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000027#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000028#include "src/ast/bool_literal.h"
29#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000030#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000031#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000032#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000033#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000035#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000036#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000037#include "src/ast/if_statement.h"
38#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000039#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000040#include "src/ast/member_accessor_expression.h"
41#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000043#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000045#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000046#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000047#include "src/ast/scalar_constructor_expression.h"
48#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000049#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000050#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000051#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000054#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000055#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000056#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000057#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000058#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000059#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000060#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000061#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000062#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000063#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000064#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000065#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000066#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000067#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000068#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000069#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000070#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000071#include "src/sem/f32_type.h"
72#include "src/sem/i32_type.h"
73#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000075#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000076#include "src/sem/sampled_texture_type.h"
77#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000078#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000079#include "src/sem/u32_type.h"
80#include "src/sem/vector_type.h"
81#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000082
Ben Clayton03d10722021-06-05 12:45:50 +000083#ifdef INCLUDE_TINT_TINT_H_
84#error "internal tint header being #included from tint.h"
85#endif
86
Ben Claytona6b9a8e2021-01-26 16:57:10 +000087namespace tint {
88
Ben Clayton401b96b2021-02-03 17:19:59 +000089// Forward declarations
90namespace ast {
91class VariableDeclStatement;
92} // namespace ast
93
Ben Claytona6b9a8e2021-01-26 16:57:10 +000094class CloneContext;
95
96/// ProgramBuilder is a mutable builder for a Program.
97/// To construct a Program, populate the builder and then `std::move` it to a
98/// Program.
99class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +0000100 /// VarOptionals is a helper for accepting a number of optional, extra
101 /// arguments for Var() and Global().
102 struct VarOptionals {
103 template <typename... ARGS>
104 explicit VarOptionals(ARGS&&... args) {
105 Apply(std::forward<ARGS>(args)...);
106 }
107 ~VarOptionals();
108
109 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000110 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000111 ast::Expression* constructor = nullptr;
112 ast::DecorationList decorations = {};
113
114 private:
115 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000116 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000117 void Set(ast::Expression* c) { constructor = c; }
118 void Set(const ast::DecorationList& l) { decorations = l; }
119
120 template <typename FIRST, typename... ARGS>
121 void Apply(FIRST&& first, ARGS&&... args) {
122 Set(std::forward<FIRST>(first));
123 Apply(std::forward<ARGS>(args)...);
124 }
125 void Apply() {}
126 };
127
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000128 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000129 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
130 using ASTNodeAllocator = BlockAllocator<ast::Node>;
131
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000132 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
133 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000134
135 /// `i32` is a type alias to `int`.
136 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
137 /// WGSL syntax.
138 /// Note: this is intentionally not aliased to uint32_t as we want integer
139 /// literals passed to the builder to match WGSL's integer literal types.
140 using i32 = decltype(1);
141 /// `u32` is a type alias to `unsigned int`.
142 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
143 /// WGSL syntax.
144 /// Note: this is intentionally not aliased to uint32_t as we want integer
145 /// literals passed to the builder to match WGSL's integer literal types.
146 using u32 = decltype(1u);
147 /// `f32` is a type alias to `float`
148 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
149 /// WGSL syntax.
150 using f32 = float;
151
152 /// Constructor
153 ProgramBuilder();
154
155 /// Move constructor
156 /// @param rhs the builder to move
157 ProgramBuilder(ProgramBuilder&& rhs);
158
159 /// Destructor
160 virtual ~ProgramBuilder();
161
162 /// Move assignment operator
163 /// @param rhs the builder to move
164 /// @return this builder
165 ProgramBuilder& operator=(ProgramBuilder&& rhs);
166
Ben Claytone43c8302021-01-29 11:59:32 +0000167 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
168 /// making a deep clone of the Program contents.
169 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
170 /// existing immutable program.
171 /// As the returned ProgramBuilder wraps `program`, `program` must not be
172 /// destructed or assigned while using the returned ProgramBuilder.
173 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
174 /// function. See crbug.com/tint/460.
175 /// @param program the immutable Program to wrap
176 /// @return the ProgramBuilder that wraps `program`
177 static ProgramBuilder Wrap(const Program* program);
178
Ben Claytone6995de2021-04-13 23:27:27 +0000179 /// @returns the unique identifier for this program
180 ProgramID ID() const { return id_; }
181
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000182 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000183 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000184 AssertNotMoved();
185 return types_;
186 }
187
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000188 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000189 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000190 AssertNotMoved();
191 return types_;
192 }
193
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000194 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000195 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000196 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000197 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000198 }
199
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000200 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000201 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000202 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000203 return ast_nodes_;
204 }
205
206 /// @returns a reference to the program's semantic nodes storage
207 SemNodeAllocator& SemNodes() {
208 AssertNotMoved();
209 return sem_nodes_;
210 }
211
212 /// @returns a reference to the program's semantic nodes storage
213 const SemNodeAllocator& SemNodes() const {
214 AssertNotMoved();
215 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000216 }
217
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000218 /// @returns a reference to the program's AST root Module
219 ast::Module& AST() {
220 AssertNotMoved();
221 return *ast_;
222 }
223
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000224 /// @returns a reference to the program's AST root Module
225 const ast::Module& AST() const {
226 AssertNotMoved();
227 return *ast_;
228 }
229
230 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000231 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000232 AssertNotMoved();
233 return sem_;
234 }
235
236 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000237 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000238 AssertNotMoved();
239 return sem_;
240 }
241
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000242 /// @returns a reference to the program's SymbolTable
243 SymbolTable& Symbols() {
244 AssertNotMoved();
245 return symbols_;
246 }
247
Ben Clayton708dc2d2021-01-29 11:22:40 +0000248 /// @returns a reference to the program's SymbolTable
249 const SymbolTable& Symbols() const {
250 AssertNotMoved();
251 return symbols_;
252 }
253
Ben Clayton844217f2021-01-27 18:49:05 +0000254 /// @returns a reference to the program's diagnostics
255 diag::List& Diagnostics() {
256 AssertNotMoved();
257 return diagnostics_;
258 }
259
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000260 /// @returns a reference to the program's diagnostics
261 const diag::List& Diagnostics() const {
262 AssertNotMoved();
263 return diagnostics_;
264 }
265
Ben Clayton5f0ea112021-03-09 10:54:37 +0000266 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000267 /// @param enable the new flag value (defaults to true)
268 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
269
Ben Clayton5f0ea112021-03-09 10:54:37 +0000270 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000271 /// built.
272 bool ResolveOnBuild() const { return resolve_on_build_; }
273
Ben Clayton844217f2021-01-27 18:49:05 +0000274 /// @returns true if the program has no error diagnostics and is not missing
275 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000276 bool IsValid() const;
277
Ben Clayton708dc2d2021-01-29 11:22:40 +0000278 /// Writes a representation of the node to the output stream
279 /// @note unlike str(), to_str() does not automatically demangle the string.
280 /// @param node the AST node
281 /// @param out the stream to write to
282 /// @param indent number of spaces to indent the node when writing
283 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
284 node->to_str(Sem(), out, indent);
285 }
286
287 /// Returns a demangled, string representation of `node`.
288 /// @param node the AST node
289 /// @returns a string representation of the node
290 std::string str(const ast::Node* node) const;
291
Ben Clayton7fdfff12021-01-29 15:17:30 +0000292 /// Creates a new ast::Node owned by the ProgramBuilder. When the
293 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000294 /// @param source the Source of the node
295 /// @param args the arguments to pass to the type constructor
296 /// @returns the node pointer
297 template <typename T, typename... ARGS>
298 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
299 ARGS&&... args) {
300 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000301 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000302 }
303
Ben Clayton7fdfff12021-01-29 15:17:30 +0000304 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
305 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000306 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000307 /// When the ProgramBuilder is destructed, the ast::Node will also be
308 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000309 /// @returns the node pointer
310 template <typename T>
311 traits::EnableIfIsType<T, ast::Node>* create() {
312 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000313 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000314 }
315
Ben Clayton7fdfff12021-01-29 15:17:30 +0000316 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
317 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000318 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000319 /// When the ProgramBuilder is destructed, the ast::Node will also be
320 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000321 /// @param arg0 the first arguments to pass to the type constructor
322 /// @param args the remaining arguments to pass to the type constructor
323 /// @returns the node pointer
324 template <typename T, typename ARG0, typename... ARGS>
325 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
326 traits::IsTypeOrDerived<T, ast::Node>::value &&
327 !traits::IsTypeOrDerived<ARG0, Source>::value,
328 T>*
329 create(ARG0&& arg0, ARGS&&... args) {
330 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000331 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000332 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000333 }
334
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000335 /// Creates a new sem::Node owned by the ProgramBuilder.
336 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000337 /// destructed.
338 /// @param args the arguments to pass to the type constructor
339 /// @returns the node pointer
340 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000341 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
342 !traits::IsTypeOrDerived<T, sem::Type>::value,
343 T>*
344 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000345 AssertNotMoved();
346 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
347 }
348
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000349 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000350 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
351 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000352 /// Types are unique (de-aliased), and so calling create() for the same `T`
353 /// and arguments will return the same pointer.
354 /// @warning Use this method to acquire a type only if all of its type
355 /// information is provided in the constructor arguments `args`.<br>
356 /// If the type requires additional configuration after construction that
357 /// affect its fundamental type, build the type with `std::make_unique`, make
358 /// any necessary alterations and then call unique_type() instead.
359 /// @param args the arguments to pass to the type constructor
360 /// @returns the de-aliased type pointer
361 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000362 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
363 static_assert(std::is_base_of<sem::Type, T>::value,
364 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000365 AssertNotMoved();
366 return types_.Get<T>(std::forward<ARGS>(args)...);
367 }
368
369 /// Marks this builder as moved, preventing any further use of the builder.
370 void MarkAsMoved();
371
372 //////////////////////////////////////////////////////////////////////////////
373 // TypesBuilder
374 //////////////////////////////////////////////////////////////////////////////
375
376 /// TypesBuilder holds basic `tint` types and methods for constructing
377 /// complex types.
378 class TypesBuilder {
379 public:
380 /// Constructor
381 /// @param builder the program builder
382 explicit TypesBuilder(ProgramBuilder* builder);
383
384 /// @return the tint AST type for the C type `T`.
385 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000386 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000387 return CToAST<T>::get(this);
388 }
389
390 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000391 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000392
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000393 /// @param source the Source of the node
394 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000395 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000396 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000397 }
398
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000399 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000400 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000401
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000402 /// @param source the Source of the node
403 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000404 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000405 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000406 }
407
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000408 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000409 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000410
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000411 /// @param source the Source of the node
412 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000413 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000414 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000415 }
416
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000417 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000418 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000419
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 /// @param source the Source of the node
421 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000422 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000423 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000424 }
425
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000426 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000427 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000428
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000429 /// @param source the Source of the node
430 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000431 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000432 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000433 }
434
Antonio Maiorano25436862021-04-13 13:32:33 +0000435 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000436 /// @param n vector width in elements
437 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000438 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000439 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000440 }
441
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000442 /// @param source the Source of the node
443 /// @param type vector subtype
444 /// @param n vector width in elements
445 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000446 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000447 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000448 }
449
Antonio Maiorano25436862021-04-13 13:32:33 +0000450 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000451 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000452 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000453
454 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000455 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000456 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000457
458 /// @param type vector subtype
459 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000460 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000461
462 /// @param n vector width in elements
463 /// @return the tint AST type for a `n`-element vector of `type`.
464 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000465 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000466 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000467 }
468
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000469 /// @return the tint AST type for a 2-element vector of the C type `T`.
470 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000471 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000472 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000473 }
474
475 /// @return the tint AST type for a 3-element vector of the C type `T`.
476 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000477 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000478 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000479 }
480
481 /// @return the tint AST type for a 4-element vector of the C type `T`.
482 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000483 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000484 return vec4(Of<T>());
485 }
486
487 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000488 /// @param columns number of columns for the matrix
489 /// @param rows number of rows for the matrix
490 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000491 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000492 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000493 }
494
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000495 /// @param source the Source of the node
496 /// @param type matrix subtype
497 /// @param columns number of columns for the matrix
498 /// @param rows number of rows for the matrix
499 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000500 ast::Matrix* mat(const Source& source,
501 ast::Type* type,
502 uint32_t columns,
503 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000504 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000505 }
506
Ben Claytone204f272021-04-22 14:40:23 +0000507 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000508 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000509 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000510
511 /// @param type matrix subtype
512 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000513 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000514
515 /// @param type matrix subtype
516 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000517 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000518
519 /// @param type matrix subtype
520 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000521 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000522
523 /// @param type matrix subtype
524 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000525 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000526
527 /// @param type matrix subtype
528 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000529 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000530
531 /// @param type matrix subtype
532 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000533 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000534
535 /// @param type matrix subtype
536 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000537 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000538
539 /// @param type matrix subtype
540 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000541 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000542
Ben Claytone204f272021-04-22 14:40:23 +0000543 /// @param columns number of columns for the matrix
544 /// @param rows number of rows for the matrix
545 /// @return the tint AST type for a matrix of `type`
546 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000547 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000548 return mat(Of<T>(), columns, rows);
549 }
550
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000551 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
552 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000553 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000554 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 }
556
557 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
558 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000559 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000560 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 }
562
563 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
564 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000565 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000566 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 }
568
569 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
570 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000571 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000572 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
575 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
576 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000577 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000578 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 }
580
581 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
582 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000583 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000584 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000585 }
586
587 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
588 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000589 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000590 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000591 }
592
593 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
594 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000595 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000596 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000597 }
598
599 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
600 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000601 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000602 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000603 }
604
605 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000606 /// @param n the array size. 0 represents a runtime-array
607 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000608 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000609 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000610 uint32_t n = 0,
611 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000612 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000613 }
614
615 /// @param source the Source of the node
616 /// @param subtype the array element type
617 /// @param n the array size. 0 represents a runtime-array
618 /// @param decos the optional decorations for the array
619 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000620 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000621 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000622 uint32_t n = 0,
623 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000624 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000625 }
626
Ben Claytonbab31972021-02-08 21:16:21 +0000627 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000628 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000629 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000630 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000631 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000632 ast::DecorationList decos;
633 if (stride) {
634 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
635 }
636 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000637 }
638
Ben Clayton0f88b312021-05-04 17:36:31 +0000639 /// @param source the Source of the node
640 /// @param subtype the array element type
641 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000642 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000643 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000644 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000645 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000646 uint32_t n,
647 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000648 ast::DecorationList decos;
649 if (stride) {
650 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
651 }
652 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000653 }
654
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000655 /// @return the tint AST type for an array of size `N` of type `T`
656 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000657 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000658 return array(Of<T>(), N);
659 }
660
Ben Claytonbab31972021-02-08 21:16:21 +0000661 /// @param stride the array stride
662 /// @return the tint AST type for an array of size `N` of type `T`
663 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000664 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000665 return array(Of<T>(), N, stride);
666 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000667
Ben Clayton0f88b312021-05-04 17:36:31 +0000668 /// Creates a type name
669 /// @param name the name
670 /// @returns the type name
671 template <typename NAME>
672 ast::TypeName* type_name(NAME&& name) const {
673 return builder->create<ast::TypeName>(
674 builder->Sym(std::forward<NAME>(name)));
675 }
676
677 /// Creates a type name
678 /// @param source the Source of the node
679 /// @param name the name
680 /// @returns the type name
681 template <typename NAME>
682 ast::TypeName* type_name(const Source& source, NAME&& name) const {
683 return builder->create<ast::TypeName>(
684 source, builder->Sym(std::forward<NAME>(name)));
685 }
686
Ben Clayton42d1e092021-02-02 14:29:15 +0000687 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000688 /// @param name the alias name
689 /// @param type the alias type
690 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000691 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000692 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000693 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000694 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000695 }
696
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000697 /// Creates an alias type
698 /// @param source the Source of the node
699 /// @param name the alias name
700 /// @param type the alias type
701 /// @returns the alias pointer
702 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000703 ast::Alias* alias(const Source& source,
704 NAME&& name,
705 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000706 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000707 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000708 }
709
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000710 /// @param type the type of the pointer
711 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000712 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000713 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000714 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000715 ast::StorageClass storage_class,
716 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000717 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000718 }
719
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000720 /// @param source the Source of the node
721 /// @param type the type of the pointer
722 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000723 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000724 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000725 ast::Pointer* pointer(const Source& source,
726 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000727 ast::StorageClass storage_class,
728 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000729 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000730 }
731
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000732 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000733 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000734 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000735 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000736 ast::Pointer* pointer(ast::StorageClass storage_class,
737 ast::Access access = ast::Access::kUndefined) const {
738 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000739 }
740
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000741 /// @param source the Source of the node
742 /// @param type the type of the atomic
743 /// @return the atomic to `type`
744 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
745 return builder->create<ast::Atomic>(source, type);
746 }
747
Ben Clayton313e6182021-06-17 19:56:14 +0000748 /// @param type the type of the atomic
749 /// @return the atomic to `type`
750 ast::Atomic* atomic(ast::Type* type) const {
751 return builder->create<ast::Atomic>(type);
752 }
753
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000754 /// @return the atomic to type `T`
755 template <typename T>
756 ast::Atomic* atomic() const {
757 return atomic(Of<T>());
758 }
759
Ben Claytone204f272021-04-22 14:40:23 +0000760 /// @param kind the kind of sampler
761 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000762 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000763 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000764 }
765
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000766 /// @param source the Source of the node
767 /// @param kind the kind of sampler
768 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000769 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000770 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000771 }
772
Ben Claytone204f272021-04-22 14:40:23 +0000773 /// @param dims the dimensionality of the texture
774 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000775 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000776 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000777 }
778
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000779 /// @param source the Source of the node
780 /// @param dims the dimensionality of the texture
781 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000782 ast::DepthTexture* depth_texture(const Source& source,
783 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000784 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000785 }
786
Ben Claytone204f272021-04-22 14:40:23 +0000787 /// @param dims the dimensionality of the texture
788 /// @param subtype the texture subtype.
789 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000790 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
791 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000792 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000793 }
794
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000795 /// @param source the Source of the node
796 /// @param dims the dimensionality of the texture
797 /// @param subtype the texture subtype.
798 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000799 ast::SampledTexture* sampled_texture(const Source& source,
800 ast::TextureDimension dims,
801 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000802 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000803 }
804
Ben Claytone204f272021-04-22 14:40:23 +0000805 /// @param dims the dimensionality of the texture
806 /// @param subtype the texture subtype.
807 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000808 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
809 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000810 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000811 }
812
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000813 /// @param source the Source of the node
814 /// @param dims the dimensionality of the texture
815 /// @param subtype the texture subtype.
816 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000817 ast::MultisampledTexture* multisampled_texture(const Source& source,
818 ast::TextureDimension dims,
819 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000820 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000821 }
822
Ben Claytone204f272021-04-22 14:40:23 +0000823 /// @param dims the dimensionality of the texture
824 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000825 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000826 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000827 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000828 ast::ImageFormat format,
829 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000830 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000831 return builder->create<ast::StorageTexture>(dims, format, subtype,
832 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000833 }
834
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000835 /// @param source the Source of the node
836 /// @param dims the dimensionality of the texture
837 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000838 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000839 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000840 ast::StorageTexture* storage_texture(const Source& source,
841 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000842 ast::ImageFormat format,
843 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000844 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000845 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
846 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000847 }
848
Brandon Jones7b257692021-05-17 17:40:17 +0000849 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000850 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000851 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000852 }
853
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000854 /// @param source the Source of the node
855 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000856 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000857 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000858 }
859
Ben Clayton19b03192021-05-20 15:04:08 +0000860 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000861 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000862 /// @param type the type
863 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000864 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000865
Ben Clayton8758f102021-06-09 14:32:14 +0000866 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000867 /// @param type the type
868 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000869 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000870
Ben Claytonf5f311e2021-04-28 14:31:23 +0000871 /// The ProgramBuilder
872 ProgramBuilder* const builder;
873
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000874 private:
875 /// CToAST<T> is specialized for various `T` types and each specialization
876 /// contains a single static `get()` method for obtaining the corresponding
877 /// AST type for the C type `T`.
878 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000879 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000880 template <typename T>
881 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000882 };
883
884 //////////////////////////////////////////////////////////////////////////////
885 // AST helper methods
886 //////////////////////////////////////////////////////////////////////////////
887
James Price65ae64d2021-04-29 12:59:14 +0000888 /// @return a new unnamed symbol
889 Symbol Sym() { return Symbols().New(); }
890
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000891 /// @param name the symbol string
892 /// @return a Symbol with the given name
893 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
894
895 /// @param sym the symbol
896 /// @return `sym`
897 Symbol Sym(Symbol sym) { return sym; }
898
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 /// @param expr the expression
900 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000901 template <typename T>
902 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
903 return expr;
904 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000905
Ben Claytonb8ea5912021-04-19 16:52:42 +0000906 /// Passthrough for nullptr
907 /// @return nullptr
908 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
909
Ben Claytonfe0910f2021-05-17 15:51:47 +0000910 /// @param source the source information
911 /// @param symbol the identifier symbol
912 /// @return an ast::IdentifierExpression with the given symbol
913 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
914 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000915 }
916
Ben Clayton46d78d72021-02-10 21:34:26 +0000917 /// @param symbol the identifier symbol
918 /// @return an ast::IdentifierExpression with the given symbol
919 ast::IdentifierExpression* Expr(Symbol symbol) {
920 return create<ast::IdentifierExpression>(symbol);
921 }
922
Ben Claytonfe0910f2021-05-17 15:51:47 +0000923 /// @param source the source information
924 /// @param variable the AST variable
925 /// @return an ast::IdentifierExpression with the variable's symbol
926 ast::IdentifierExpression* Expr(const Source& source,
927 ast::Variable* variable) {
928 return create<ast::IdentifierExpression>(source, variable->symbol());
929 }
930
Ben Claytonb8ea5912021-04-19 16:52:42 +0000931 /// @param variable the AST variable
932 /// @return an ast::IdentifierExpression with the variable's symbol
933 ast::IdentifierExpression* Expr(ast::Variable* variable) {
934 return create<ast::IdentifierExpression>(variable->symbol());
935 }
936
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000937 /// @param source the source information
938 /// @param name the identifier name
939 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000940 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
941 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
942 }
943
944 /// @param name the identifier name
945 /// @return an ast::IdentifierExpression with the given name
946 ast::IdentifierExpression* Expr(const char* name) {
947 return create<ast::IdentifierExpression>(Symbols().Register(name));
948 }
949
950 /// @param source the source information
951 /// @param name the identifier name
952 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000953 ast::IdentifierExpression* Expr(const Source& source,
954 const std::string& name) {
955 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
956 }
957
958 /// @param name the identifier name
959 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000960 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000961 return create<ast::IdentifierExpression>(Symbols().Register(name));
962 }
963
Ben Claytonfe0910f2021-05-17 15:51:47 +0000964 /// @param source the source information
965 /// @param value the boolean value
966 /// @return a Scalar constructor for the given value
967 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
968 return create<ast::ScalarConstructorExpression>(source, Literal(value));
969 }
970
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000971 /// @param value the boolean value
972 /// @return a Scalar constructor for the given value
973 ast::ScalarConstructorExpression* Expr(bool value) {
974 return create<ast::ScalarConstructorExpression>(Literal(value));
975 }
976
Ben Claytonfe0910f2021-05-17 15:51:47 +0000977 /// @param source the source information
978 /// @param value the float value
979 /// @return a Scalar constructor for the given value
980 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
981 return create<ast::ScalarConstructorExpression>(source, Literal(value));
982 }
983
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000984 /// @param value the float value
985 /// @return a Scalar constructor for the given value
986 ast::ScalarConstructorExpression* Expr(f32 value) {
987 return create<ast::ScalarConstructorExpression>(Literal(value));
988 }
989
Ben Claytonfe0910f2021-05-17 15:51:47 +0000990 /// @param source the source information
991 /// @param value the integer value
992 /// @return a Scalar constructor for the given value
993 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
994 return create<ast::ScalarConstructorExpression>(source, Literal(value));
995 }
996
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000997 /// @param value the integer value
998 /// @return a Scalar constructor for the given value
999 ast::ScalarConstructorExpression* Expr(i32 value) {
1000 return create<ast::ScalarConstructorExpression>(Literal(value));
1001 }
1002
Ben Claytonfe0910f2021-05-17 15:51:47 +00001003 /// @param source the source information
1004 /// @param value the unsigned int value
1005 /// @return a Scalar constructor for the given value
1006 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1007 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1008 }
1009
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001010 /// @param value the unsigned int value
1011 /// @return a Scalar constructor for the given value
1012 ast::ScalarConstructorExpression* Expr(u32 value) {
1013 return create<ast::ScalarConstructorExpression>(Literal(value));
1014 }
1015
1016 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1017 /// `list`.
1018 /// @param list the list to append too
1019 /// @param arg the arg to create
1020 template <typename ARG>
1021 void Append(ast::ExpressionList& list, ARG&& arg) {
1022 list.emplace_back(Expr(std::forward<ARG>(arg)));
1023 }
1024
1025 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1026 /// then appends them to `list`.
1027 /// @param list the list to append too
1028 /// @param arg0 the first argument
1029 /// @param args the rest of the arguments
1030 template <typename ARG0, typename... ARGS>
1031 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1032 Append(list, std::forward<ARG0>(arg0));
1033 Append(list, std::forward<ARGS>(args)...);
1034 }
1035
1036 /// @return an empty list of expressions
1037 ast::ExpressionList ExprList() { return {}; }
1038
1039 /// @param args the list of expressions
1040 /// @return the list of expressions converted to `ast::Expression`s using
1041 /// `Expr()`,
1042 template <typename... ARGS>
1043 ast::ExpressionList ExprList(ARGS&&... args) {
1044 ast::ExpressionList list;
1045 list.reserve(sizeof...(args));
1046 Append(list, std::forward<ARGS>(args)...);
1047 return list;
1048 }
1049
1050 /// @param list the list of expressions
1051 /// @return `list`
1052 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1053
1054 /// @param val the boolan value
1055 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001056 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001057
1058 /// @param val the float value
1059 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001060 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001061
1062 /// @param val the unsigned int value
1063 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001064 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001065
1066 /// @param val the integer value
1067 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001068 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001069
1070 /// @param args the arguments for the type constructor
1071 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1072 /// of `args` converted to `ast::Expression`s using `Expr()`
1073 template <typename T, typename... ARGS>
1074 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001075 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001076 }
1077
1078 /// @param type the type to construct
1079 /// @param args the arguments for the constructor
1080 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1081 /// values `args`.
1082 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001083 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001084 return create<ast::TypeConstructorExpression>(
1085 type, ExprList(std::forward<ARGS>(args)...));
1086 }
1087
Ben Clayton313e6182021-06-17 19:56:14 +00001088 /// @param source the source information
1089 /// @param type the type to construct
1090 /// @param args the arguments for the constructor
1091 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1092 /// values `args`.
1093 template <typename... ARGS>
1094 ast::TypeConstructorExpression* Construct(const Source& source,
1095 ast::Type* type,
1096 ARGS&&... args) {
1097 return create<ast::TypeConstructorExpression>(
1098 source, type, ExprList(std::forward<ARGS>(args)...));
1099 }
1100
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001101 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001102 /// @param type the vector type
1103 /// @param size the vector size
1104 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1105 /// type `type`, constructed with the values `args`.
1106 template <typename... ARGS>
1107 ast::TypeConstructorExpression* vec(ast::Type* type,
1108 uint32_t size,
1109 ARGS&&... args) {
1110 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1111 }
1112
1113 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1115 /// `T`, constructed with the values `args`.
1116 template <typename T, typename... ARGS>
1117 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001118 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 }
1120
1121 /// @param args the arguments for the vector constructor
1122 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1123 /// `T`, constructed with the values `args`.
1124 template <typename T, typename... ARGS>
1125 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001126 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 }
1128
1129 /// @param args the arguments for the vector constructor
1130 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1131 /// `T`, constructed with the values `args`.
1132 template <typename T, typename... ARGS>
1133 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001134 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001135 }
1136
1137 /// @param args the arguments for the matrix constructor
1138 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1139 /// `T`, constructed with the values `args`.
1140 template <typename T, typename... ARGS>
1141 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001142 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001143 }
1144
1145 /// @param args the arguments for the matrix constructor
1146 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1147 /// `T`, constructed with the values `args`.
1148 template <typename T, typename... ARGS>
1149 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001150 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001151 }
1152
1153 /// @param args the arguments for the matrix constructor
1154 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1155 /// `T`, constructed with the values `args`.
1156 template <typename T, typename... ARGS>
1157 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001158 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001159 }
1160
1161 /// @param args the arguments for the matrix constructor
1162 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1163 /// `T`, constructed with the values `args`.
1164 template <typename T, typename... ARGS>
1165 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001166 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001167 }
1168
1169 /// @param args the arguments for the matrix constructor
1170 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1171 /// `T`, constructed with the values `args`.
1172 template <typename T, typename... ARGS>
1173 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001174 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001175 }
1176
1177 /// @param args the arguments for the matrix constructor
1178 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1179 /// `T`, constructed with the values `args`.
1180 template <typename T, typename... ARGS>
1181 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001182 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001183 }
1184
1185 /// @param args the arguments for the matrix constructor
1186 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1187 /// `T`, constructed with the values `args`.
1188 template <typename T, typename... ARGS>
1189 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001190 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001191 }
1192
1193 /// @param args the arguments for the matrix constructor
1194 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1195 /// `T`, constructed with the values `args`.
1196 template <typename T, typename... ARGS>
1197 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001198 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 }
1200
1201 /// @param args the arguments for the matrix constructor
1202 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1203 /// `T`, constructed with the values `args`.
1204 template <typename T, typename... ARGS>
1205 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001206 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 }
1208
1209 /// @param args the arguments for the array constructor
1210 /// @return an `ast::TypeConstructorExpression` of an array with element type
1211 /// `T`, constructed with the values `args`.
1212 template <typename T, int N = 0, typename... ARGS>
1213 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001214 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 }
1216
1217 /// @param subtype the array element type
1218 /// @param n the array size. 0 represents a runtime-array.
1219 /// @param args the arguments for the array constructor
1220 /// @return an `ast::TypeConstructorExpression` of an array with element type
1221 /// `subtype`, constructed with the values `args`.
1222 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001223 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001224 uint32_t n,
1225 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001226 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227 }
1228
1229 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001231 /// @param optional the optional variable settings.
1232 /// Can be any of the following, in any order:
1233 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001234 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001235 /// * ast::Expression* - specifies the variable's initializer expression
1236 /// * ast::DecorationList - specifies the variable's decorations
1237 /// Note that repeated arguments of the same type will use the last argument's
1238 /// value.
1239 /// @returns a `ast::Variable` with the given name, type and additional
1240 /// options
1241 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001242 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001243 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001244 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001245 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1246 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001247 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001248 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001249 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250
1251 /// @param source the variable source
1252 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001254 /// @param optional the optional variable settings.
1255 /// Can be any of the following, in any order:
1256 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001257 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001258 /// * ast::Expression* - specifies the variable's initializer expression
1259 /// * ast::DecorationList - specifies the variable's decorations
1260 /// Note that repeated arguments of the same type will use the last argument's
1261 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001263 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001264 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001265 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001266 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001267 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001268 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1269 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001270 opts.storage, opts.access, type, false,
1271 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001272 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001273
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001274 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001275 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001276 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001277 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001278 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001279 template <typename NAME>
1280 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001281 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001282 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001283 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001284 return create<ast::Variable>(
1285 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1286 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001287 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001288
1289 /// @param source the variable source
1290 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001292 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001293 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001294 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001295 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001296 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001297 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001298 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001299 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001300 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001301 return create<ast::Variable>(
1302 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1303 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001304 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001305
James Pricedaf1f1c2021-04-08 22:15:48 +00001306 /// @param name the parameter name
1307 /// @param type the parameter type
1308 /// @param decorations optional parameter decorations
1309 /// @returns a constant `ast::Variable` with the given name and type
1310 template <typename NAME>
1311 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001312 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001313 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001314 return create<ast::Variable>(
1315 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1316 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001317 }
1318
1319 /// @param source the parameter source
1320 /// @param name the parameter name
1321 /// @param type the parameter type
1322 /// @param decorations optional parameter decorations
1323 /// @returns a constant `ast::Variable` with the given name and type
1324 template <typename NAME>
1325 ast::Variable* Param(const Source& source,
1326 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001327 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001328 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001329 return create<ast::Variable>(
1330 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1331 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001332 }
1333
Ben Clayton42708342021-04-21 17:55:12 +00001334 /// @param name the variable name
1335 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001336 /// @param optional the optional variable settings.
1337 /// Can be any of the following, in any order:
1338 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001339 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001340 /// * ast::Expression* - specifies the variable's initializer expression
1341 /// * ast::DecorationList - specifies the variable's decorations
1342 /// Note that repeated arguments of the same type will use the last argument's
1343 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001344 /// @returns a new `ast::Variable`, which is automatically registered as a
1345 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001346 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001347 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001348 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001349 OPTIONAL&&... optional) {
1350 auto* var = Var(std::forward<NAME>(name), type,
1351 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001352 AST().AddGlobalVariable(var);
1353 return var;
1354 }
1355
1356 /// @param source the variable source
1357 /// @param name the variable name
1358 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001359 /// @param optional the optional variable settings.
1360 /// Can be any of the following, in any order:
1361 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001362 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001363 /// * ast::Expression* - specifies the variable's initializer expression
1364 /// * ast::DecorationList - specifies the variable's decorations
1365 /// Note that repeated arguments of the same type will use the last argument's
1366 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001367 /// @returns a new `ast::Variable`, which is automatically registered as a
1368 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001369 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001370 ast::Variable* Global(const Source& source,
1371 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001372 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001373 OPTIONAL&&... optional) {
1374 auto* var = Var(source, std::forward<NAME>(name), type,
1375 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001376 AST().AddGlobalVariable(var);
1377 return var;
1378 }
1379
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001380 /// @param name the variable name
1381 /// @param type the variable type
1382 /// @param constructor constructor expression
1383 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001384 /// @returns a const `ast::Variable` constructed by calling Var() with the
1385 /// arguments of `args`, which is automatically registered as a global
1386 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001387 template <typename NAME>
1388 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001389 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001390 ast::Expression* constructor,
1391 ast::DecorationList decorations = {}) {
1392 auto* var = Const(std::forward<NAME>(name), type, constructor,
1393 std::move(decorations));
1394 AST().AddGlobalVariable(var);
1395 return var;
1396 }
1397
1398 /// @param source the variable source
1399 /// @param name the variable name
1400 /// @param type the variable type
1401 /// @param constructor constructor expression
1402 /// @param decorations optional variable decorations
1403 /// @returns a const `ast::Variable` constructed by calling Var() with the
1404 /// arguments of `args`, which is automatically registered as a global
1405 /// variable with the ast::Module.
1406 template <typename NAME>
1407 ast::Variable* GlobalConst(const Source& source,
1408 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001409 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001410 ast::Expression* constructor,
1411 ast::DecorationList decorations = {}) {
1412 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1413 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001414 AST().AddGlobalVariable(var);
1415 return var;
1416 }
1417
Ben Claytonfe0910f2021-05-17 15:51:47 +00001418 /// @param source the source information
1419 /// @param expr the expression to take the address of
1420 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1421 template <typename EXPR>
1422 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1423 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1424 Expr(std::forward<EXPR>(expr)));
1425 }
1426
1427 /// @param expr the expression to take the address of
1428 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1429 template <typename EXPR>
1430 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1431 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1432 Expr(std::forward<EXPR>(expr)));
1433 }
1434
1435 /// @param source the source information
1436 /// @param expr the expression to perform an indirection on
1437 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1438 template <typename EXPR>
1439 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1440 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1441 Expr(std::forward<EXPR>(expr)));
1442 }
1443
1444 /// @param expr the expression to perform an indirection on
1445 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1446 template <typename EXPR>
1447 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1448 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1449 Expr(std::forward<EXPR>(expr)));
1450 }
1451
Antonio Maiorano14b34032021-06-09 20:17:59 +00001452 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001453 /// @param func the function name
1454 /// @param args the function call arguments
1455 /// @returns a `ast::CallExpression` to the function `func`, with the
1456 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1457 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001458 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1459 return create<ast::CallExpression>(source, Expr(func),
1460 ExprList(std::forward<ARGS>(args)...));
1461 }
1462
1463 /// @param func the function name
1464 /// @param args the function call arguments
1465 /// @returns a `ast::CallExpression` to the function `func`, with the
1466 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1467 template <typename NAME,
1468 typename... ARGS,
1469 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001470 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1471 return create<ast::CallExpression>(Expr(func),
1472 ExprList(std::forward<ARGS>(args)...));
1473 }
1474
Ben Clayton7fe01062021-06-11 13:22:27 +00001475 /// @param expr the expression to ignore
1476 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1477 /// passed the single `expr` argument
1478 template <typename EXPR>
1479 ast::CallStatement* Ignore(EXPR&& expr) {
1480 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1481 }
1482
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001483 /// @param lhs the left hand argument to the addition operation
1484 /// @param rhs the right hand argument to the addition operation
1485 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1486 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001487 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001488 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1489 Expr(std::forward<LHS>(lhs)),
1490 Expr(std::forward<RHS>(rhs)));
1491 }
1492
James Pricec32e8f62021-06-22 20:08:29 +00001493 /// @param lhs the left hand argument to the and operation
1494 /// @param rhs the right hand argument to the and operation
1495 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1496 template <typename LHS, typename RHS>
1497 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1498 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1499 Expr(std::forward<LHS>(lhs)),
1500 Expr(std::forward<RHS>(rhs)));
1501 }
1502
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001503 /// @param lhs the left hand argument to the subtraction operation
1504 /// @param rhs the right hand argument to the subtraction operation
1505 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1506 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001507 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001508 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1509 Expr(std::forward<LHS>(lhs)),
1510 Expr(std::forward<RHS>(rhs)));
1511 }
1512
1513 /// @param lhs the left hand argument to the multiplication operation
1514 /// @param rhs the right hand argument to the multiplication operation
1515 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1516 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001517 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001518 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1519 Expr(std::forward<LHS>(lhs)),
1520 Expr(std::forward<RHS>(rhs)));
1521 }
1522
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001523 /// @param source the source information
1524 /// @param lhs the left hand argument to the multiplication operation
1525 /// @param rhs the right hand argument to the multiplication operation
1526 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1527 template <typename LHS, typename RHS>
1528 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1529 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1530 Expr(std::forward<LHS>(lhs)),
1531 Expr(std::forward<RHS>(rhs)));
1532 }
1533
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001534 /// @param lhs the left hand argument to the division operation
1535 /// @param rhs the right hand argument to the division operation
1536 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1537 template <typename LHS, typename RHS>
1538 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1539 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1540 Expr(std::forward<LHS>(lhs)),
1541 Expr(std::forward<RHS>(rhs)));
1542 }
1543
Ben Clayton0597a2b2021-06-16 09:19:36 +00001544 /// @param source the source information
1545 /// @param arr the array argument for the array accessor expression
1546 /// @param idx the index argument for the array accessor expression
1547 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1548 template <typename ARR, typename IDX>
1549 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1550 ARR&& arr,
1551 IDX&& idx) {
1552 return create<ast::ArrayAccessorExpression>(
1553 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1554 }
1555
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001556 /// @param arr the array argument for the array accessor expression
1557 /// @param idx the index argument for the array accessor expression
1558 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1559 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001560 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001561 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1562 Expr(std::forward<IDX>(idx)));
1563 }
1564
1565 /// @param obj the object for the member accessor expression
1566 /// @param idx the index argument for the array accessor expression
1567 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1568 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001569 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001570 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1571 Expr(std::forward<IDX>(idx)));
1572 }
1573
Ben Clayton42d1e092021-02-02 14:29:15 +00001574 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001575 /// @param val the offset value
1576 /// @returns the offset decoration pointer
1577 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1578 return create<ast::StructMemberOffsetDecoration>(source_, val);
1579 }
1580
Ben Claytond614dd52021-03-15 10:43:11 +00001581 /// Creates a ast::StructMemberSizeDecoration
1582 /// @param source the source information
1583 /// @param val the size value
1584 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001585 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1586 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001587 return create<ast::StructMemberSizeDecoration>(source, val);
1588 }
1589
1590 /// Creates a ast::StructMemberSizeDecoration
1591 /// @param val the size value
1592 /// @returns the size decoration pointer
1593 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1594 return create<ast::StructMemberSizeDecoration>(source_, val);
1595 }
1596
1597 /// Creates a ast::StructMemberAlignDecoration
1598 /// @param source the source information
1599 /// @param val the align value
1600 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001601 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1602 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001603 return create<ast::StructMemberAlignDecoration>(source, val);
1604 }
1605
1606 /// Creates a ast::StructMemberAlignDecoration
1607 /// @param val the align value
1608 /// @returns the align decoration pointer
1609 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1610 return create<ast::StructMemberAlignDecoration>(source_, val);
1611 }
1612
Ben Clayton42d1e092021-02-02 14:29:15 +00001613 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001614 /// @param source the source information
1615 /// @param name the function name
1616 /// @param params the function parameters
1617 /// @param type the function return type
1618 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001619 /// @param decorations the optional function decorations
1620 /// @param return_type_decorations the optional function return type
1621 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001622 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001623 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001624 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001625 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001626 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001627 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001628 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001629 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001630 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001631 auto* func =
1632 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1633 type, create<ast::BlockStatement>(body),
1634 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001635 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001636 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001637 }
1638
Ben Clayton42d1e092021-02-02 14:29:15 +00001639 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001640 /// @param name the function name
1641 /// @param params the function parameters
1642 /// @param type the function return type
1643 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001644 /// @param decorations the optional function decorations
1645 /// @param return_type_decorations the optional function return type
1646 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001647 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001648 template <typename NAME>
1649 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001650 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001651 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001652 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001653 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001654 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001655 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1656 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001657 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001658 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001659 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001660 }
1661
Ben Clayton49668bb2021-04-17 05:32:01 +00001662 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001663 /// @param source the source information
1664 /// @returns the return statement pointer
1665 ast::ReturnStatement* Return(const Source& source) {
1666 return create<ast::ReturnStatement>(source);
1667 }
1668
1669 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001670 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001671 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1672
1673 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001674 /// @param source the source information
1675 /// @param val the return value
1676 /// @returns the return statement pointer
1677 template <typename EXPR>
1678 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1679 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1680 }
1681
1682 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001683 /// @param val the return value
1684 /// @returns the return statement pointer
1685 template <typename EXPR>
1686 ast::ReturnStatement* Return(EXPR&& val) {
1687 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001688 }
1689
Ben Clayton950809f2021-06-09 14:32:14 +00001690 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001691 /// @param source the source information
1692 /// @param name the alias name
1693 /// @param type the alias target type
1694 /// @returns the alias type
1695 template <typename NAME>
1696 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1697 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001698 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001699 return out;
1700 }
1701
Ben Clayton950809f2021-06-09 14:32:14 +00001702 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001703 /// @param name the alias name
1704 /// @param type the alias target type
1705 /// @returns the alias type
1706 template <typename NAME>
1707 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1708 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001709 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001710 return out;
1711 }
1712
Ben Clayton950809f2021-06-09 14:32:14 +00001713 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001714 /// @param source the source information
1715 /// @param name the struct name
1716 /// @param members the struct members
1717 /// @param decorations the optional struct decorations
1718 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001719 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001720 ast::Struct* Structure(const Source& source,
1721 NAME&& name,
1722 ast::StructMemberList members,
1723 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001724 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001725 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001726 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001727 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001728 return type;
1729 }
1730
Ben Clayton950809f2021-06-09 14:32:14 +00001731 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001732 /// @param name the struct name
1733 /// @param members the struct members
1734 /// @param decorations the optional struct decorations
1735 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001736 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001737 ast::Struct* Structure(NAME&& name,
1738 ast::StructMemberList members,
1739 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001740 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001741 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001742 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001743 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001744 return type;
1745 }
1746
Ben Clayton42d1e092021-02-02 14:29:15 +00001747 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001748 /// @param source the source information
1749 /// @param name the struct member name
1750 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001751 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001752 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001753 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001754 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001755 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001756 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001757 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001758 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1759 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001760 }
1761
Ben Clayton42d1e092021-02-02 14:29:15 +00001762 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001763 /// @param name the struct member name
1764 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001765 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001766 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001767 template <typename NAME>
1768 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001769 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001770 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001771 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1772 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001773 }
1774
Ben Claytonbab31972021-02-08 21:16:21 +00001775 /// Creates a ast::StructMember with the given byte offset
1776 /// @param offset the offset to use in the StructMemberOffsetDecoration
1777 /// @param name the struct member name
1778 /// @param type the struct member type
1779 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001780 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001781 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001782 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001783 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001784 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001785 create<ast::StructMemberOffsetDecoration>(offset),
1786 });
1787 }
1788
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001789 /// Creates a ast::BlockStatement with input statements
1790 /// @param statements statements of block
1791 /// @returns the block statement pointer
1792 template <typename... Statements>
1793 ast::BlockStatement* Block(Statements&&... statements) {
1794 return create<ast::BlockStatement>(
1795 ast::StatementList{std::forward<Statements>(statements)...});
1796 }
1797
1798 /// Creates a ast::ElseStatement with input condition and body
1799 /// @param condition the else condition expression
1800 /// @param body the else body
1801 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001802 template <typename CONDITION>
1803 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1804 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1805 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001806 }
1807
1808 /// Creates a ast::IfStatement with input condition, body, and optional
1809 /// variadic else statements
1810 /// @param condition the if statement condition expression
1811 /// @param body the if statement body
1812 /// @param elseStatements optional variadic else statements
1813 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001814 template <typename CONDITION, typename... ELSE_STATEMENTS>
1815 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001816 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001817 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001818 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001819 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001820 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001821 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001822 }
1823
1824 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001825 /// @param source the source information
1826 /// @param lhs the left hand side expression initializer
1827 /// @param rhs the right hand side expression initializer
1828 /// @returns the assignment statement pointer
1829 template <typename LhsExpressionInit, typename RhsExpressionInit>
1830 ast::AssignmentStatement* Assign(const Source& source,
1831 LhsExpressionInit&& lhs,
1832 RhsExpressionInit&& rhs) {
1833 return create<ast::AssignmentStatement>(
1834 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1835 Expr(std::forward<RhsExpressionInit>(rhs)));
1836 }
1837
1838 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001839 /// @param lhs the left hand side expression initializer
1840 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001841 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001842 template <typename LhsExpressionInit, typename RhsExpressionInit>
1843 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1844 RhsExpressionInit&& rhs) {
1845 return create<ast::AssignmentStatement>(
1846 Expr(std::forward<LhsExpressionInit>(lhs)),
1847 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001848 }
1849
1850 /// Creates a ast::LoopStatement with input body and optional continuing
1851 /// @param body the loop body
1852 /// @param continuing the optional continuing block
1853 /// @returns the loop statement pointer
1854 ast::LoopStatement* Loop(ast::BlockStatement* body,
1855 ast::BlockStatement* continuing = nullptr) {
1856 return create<ast::LoopStatement>(body, continuing);
1857 }
1858
1859 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001860 /// @param source the source information
1861 /// @param var the variable to wrap in a decl statement
1862 /// @returns the variable decl statement pointer
1863 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1864 return create<ast::VariableDeclStatement>(source, var);
1865 }
1866
1867 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001868 /// @param var the variable to wrap in a decl statement
1869 /// @returns the variable decl statement pointer
1870 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1871 return create<ast::VariableDeclStatement>(var);
1872 }
1873
Antonio Maioranocea744d2021-03-25 12:55:27 +00001874 /// Creates a ast::SwitchStatement with input expression and cases
1875 /// @param condition the condition expression initializer
1876 /// @param cases case statements
1877 /// @returns the switch statement pointer
1878 template <typename ExpressionInit, typename... Cases>
1879 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1880 return create<ast::SwitchStatement>(
1881 Expr(std::forward<ExpressionInit>(condition)),
1882 ast::CaseStatementList{std::forward<Cases>(cases)...});
1883 }
1884
1885 /// Creates a ast::CaseStatement with input list of selectors, and body
1886 /// @param selectors list of selectors
1887 /// @param body the case body
1888 /// @returns the case statement pointer
1889 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1890 ast::BlockStatement* body = nullptr) {
1891 return create<ast::CaseStatement>(std::move(selectors),
1892 body ? body : Block());
1893 }
1894
1895 /// Convenient overload that takes a single selector
1896 /// @param selector a single case selector
1897 /// @param body the case body
1898 /// @returns the case statement pointer
1899 ast::CaseStatement* Case(ast::IntLiteral* selector,
1900 ast::BlockStatement* body = nullptr) {
1901 return Case(ast::CaseSelectorList{selector}, body);
1902 }
1903
1904 /// Convenience function that creates a 'default' ast::CaseStatement
1905 /// @param body the case body
1906 /// @returns the case statement pointer
1907 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1908 return Case(ast::CaseSelectorList{}, body);
1909 }
1910
James Price68f558f2021-04-06 15:51:47 +00001911 /// Creates an ast::BuiltinDecoration
1912 /// @param source the source information
1913 /// @param builtin the builtin value
1914 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001915 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001916 return create<ast::BuiltinDecoration>(source, builtin);
1917 }
1918
1919 /// Creates an ast::BuiltinDecoration
1920 /// @param builtin the builtin value
1921 /// @returns the builtin decoration pointer
1922 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1923 return create<ast::BuiltinDecoration>(source_, builtin);
1924 }
1925
1926 /// Creates an ast::LocationDecoration
1927 /// @param source the source information
1928 /// @param location the location value
1929 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001930 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001931 return create<ast::LocationDecoration>(source, location);
1932 }
1933
1934 /// Creates an ast::LocationDecoration
1935 /// @param location the location value
1936 /// @returns the location decoration pointer
1937 ast::LocationDecoration* Location(uint32_t location) {
1938 return create<ast::LocationDecoration>(source_, location);
1939 }
1940
James Pricef2f3bfc2021-05-13 20:32:32 +00001941 /// Creates an ast::OverrideDecoration with a specific constant ID
1942 /// @param source the source information
1943 /// @param id the id value
1944 /// @returns the override decoration pointer
1945 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1946 return create<ast::OverrideDecoration>(source, id);
1947 }
1948
1949 /// Creates an ast::OverrideDecoration with a specific constant ID
1950 /// @param id the optional id value
1951 /// @returns the override decoration pointer
1952 ast::OverrideDecoration* Override(uint32_t id) {
1953 return Override(source_, id);
1954 }
1955
1956 /// Creates an ast::OverrideDecoration without a constant ID
1957 /// @param source the source information
1958 /// @returns the override decoration pointer
1959 ast::OverrideDecoration* Override(const Source& source) {
1960 return create<ast::OverrideDecoration>(source);
1961 }
1962
1963 /// Creates an ast::OverrideDecoration without a constant ID
1964 /// @returns the override decoration pointer
1965 ast::OverrideDecoration* Override() { return Override(source_); }
1966
James Price68f558f2021-04-06 15:51:47 +00001967 /// Creates an ast::StageDecoration
1968 /// @param source the source information
1969 /// @param stage the pipeline stage
1970 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001971 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001972 return create<ast::StageDecoration>(source, stage);
1973 }
1974
1975 /// Creates an ast::StageDecoration
1976 /// @param stage the pipeline stage
1977 /// @returns the stage decoration pointer
1978 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1979 return create<ast::StageDecoration>(source_, stage);
1980 }
1981
James Price70f80bb2021-05-19 13:40:08 +00001982 /// Creates an ast::WorkgroupDecoration
1983 /// @param x the x dimension expression
1984 /// @returns the workgroup decoration pointer
1985 template <typename EXPR_X>
1986 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1987 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1988 }
1989
1990 /// Creates an ast::WorkgroupDecoration
1991 /// @param x the x dimension expression
1992 /// @param y the y dimension expression
1993 /// @returns the workgroup decoration pointer
1994 template <typename EXPR_X, typename EXPR_Y>
1995 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1996 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1997 nullptr);
1998 }
1999
2000 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002001 /// @param source the source information
2002 /// @param x the x dimension expression
2003 /// @param y the y dimension expression
2004 /// @param z the z dimension expression
2005 /// @returns the workgroup decoration pointer
2006 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2007 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2008 EXPR_X&& x,
2009 EXPR_Y&& y,
2010 EXPR_Z&& z) {
2011 return create<ast::WorkgroupDecoration>(
2012 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2013 Expr(std::forward<EXPR_Z>(z)));
2014 }
2015
2016 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002017 /// @param x the x dimension expression
2018 /// @param y the y dimension expression
2019 /// @param z the z dimension expression
2020 /// @returns the workgroup decoration pointer
2021 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2022 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2023 return create<ast::WorkgroupDecoration>(
2024 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2025 Expr(std::forward<EXPR_Z>(z)));
2026 }
2027
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002028 /// Sets the current builder source to `src`
2029 /// @param src the Source used for future create() calls
2030 void SetSource(const Source& src) {
2031 AssertNotMoved();
2032 source_ = src;
2033 }
2034
2035 /// Sets the current builder source to `loc`
2036 /// @param loc the Source used for future create() calls
2037 void SetSource(const Source::Location& loc) {
2038 AssertNotMoved();
2039 source_ = Source(loc);
2040 }
2041
Ben Clayton33352542021-01-29 16:43:41 +00002042 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002043 /// @note As the Resolver is run when the Program is built, this will only be
2044 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002045 /// @param expr the AST expression
2046 /// @return the resolved semantic type for the expression, or nullptr if the
2047 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002048 sem::Type* TypeOf(const ast::Expression* expr) const;
2049
Ben Claytonfe0910f2021-05-17 15:51:47 +00002050 /// Helper for returning the resolved semantic type of the variable `var`.
2051 /// @note As the Resolver is run when the Program is built, this will only be
2052 /// useful for the Resolver itself and tests that use their own Resolver.
2053 /// @param var the AST variable
2054 /// @return the resolved semantic type for the variable, or nullptr if the
2055 /// variable has no resolved type.
2056 sem::Type* TypeOf(const ast::Variable* var) const;
2057
Ben Claytonfbec46f2021-04-30 20:20:19 +00002058 /// Helper for returning the resolved semantic type of the AST type `type`.
2059 /// @note As the Resolver is run when the Program is built, this will only be
2060 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002061 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002062 /// @return the resolved semantic type for the type, or nullptr if the type
2063 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002064 const sem::Type* TypeOf(const ast::Type* type) const;
2065
2066 /// Helper for returning the resolved semantic type of the AST type
2067 /// declaration `type_decl`.
2068 /// @note As the Resolver is run when the Program is built, this will only be
2069 /// useful for the Resolver itself and tests that use their own Resolver.
2070 /// @param type_decl the AST type declaration
2071 /// @return the resolved semantic type for the type declaration, or nullptr if
2072 /// the type declaration has no resolved type.
2073 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002074
Ben Clayton169512e2021-04-17 05:52:11 +00002075 /// Wraps the ast::Literal in a statement. This is used by tests that
2076 /// construct a partial AST and require the Resolver to reach these
2077 /// nodes.
2078 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2079 /// @return the ast::Statement that wraps the ast::Statement
2080 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002081 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002082 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002083 /// nodes.
2084 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2085 /// @return the ast::Statement that wraps the ast::Expression
2086 ast::Statement* WrapInStatement(ast::Expression* expr);
2087 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002088 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002089 /// these nodes.
2090 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2091 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2092 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2093 /// Returns the statement argument. Used as a passthrough-overload by
2094 /// WrapInFunction().
2095 /// @param stmt the ast::Statement
2096 /// @return `stmt`
2097 ast::Statement* WrapInStatement(ast::Statement* stmt);
2098 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002099 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002100 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002101 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002102 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002103 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002104 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002105 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002106 }
2107 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002108 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002109 /// @returns the function
2110 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002111
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002112 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002113 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002114
2115 protected:
2116 /// Asserts that the builder has not been moved.
2117 void AssertNotMoved() const;
2118
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002119 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002120 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002121 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002122 ASTNodeAllocator ast_nodes_;
2123 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002124 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002125 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002126 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002127 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002128
2129 /// The source to use when creating AST nodes without providing a Source as
2130 /// the first argument.
2131 Source source_;
2132
Ben Clayton5f0ea112021-03-09 10:54:37 +00002133 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002134 /// program when built.
2135 bool resolve_on_build_ = true;
2136
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002137 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2138 bool moved_ = false;
2139};
2140
2141//! @cond Doxygen_Suppress
2142// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2143template <>
2144struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002145 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002146 return t->i32();
2147 }
2148};
2149template <>
2150struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002151 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002152 return t->u32();
2153 }
2154};
2155template <>
2156struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002157 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002158 return t->f32();
2159 }
2160};
2161template <>
2162struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002163 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002164 return t->bool_();
2165 }
2166};
2167template <>
2168struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002169 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002170 return t->void_();
2171 }
2172};
2173//! @endcond
2174
Ben Clayton917b14b2021-04-19 16:50:23 +00002175/// @param builder the ProgramBuilder
2176/// @returns the ProgramID of the ProgramBuilder
2177inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2178 return builder->ID();
2179}
2180
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002181} // namespace tint
2182
2183#endif // SRC_PROGRAM_BUILDER_H_