blob: ae6f5d27bac69a48a5065e0bfc243e22b24468d2 [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
1102 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1103 /// `T`, constructed with the values `args`.
1104 template <typename T, typename... ARGS>
1105 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001106 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001107 }
1108
1109 /// @param args the arguments for the vector constructor
1110 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1111 /// `T`, constructed with the values `args`.
1112 template <typename T, typename... ARGS>
1113 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001114 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001115 }
1116
1117 /// @param args the arguments for the vector constructor
1118 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1119 /// `T`, constructed with the values `args`.
1120 template <typename T, typename... ARGS>
1121 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001122 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001123 }
1124
1125 /// @param args the arguments for the matrix constructor
1126 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1127 /// `T`, constructed with the values `args`.
1128 template <typename T, typename... ARGS>
1129 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001130 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001131 }
1132
1133 /// @param args the arguments for the matrix constructor
1134 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1135 /// `T`, constructed with the values `args`.
1136 template <typename T, typename... ARGS>
1137 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001138 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001139 }
1140
1141 /// @param args the arguments for the matrix constructor
1142 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1143 /// `T`, constructed with the values `args`.
1144 template <typename T, typename... ARGS>
1145 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001146 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001147 }
1148
1149 /// @param args the arguments for the matrix constructor
1150 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1151 /// `T`, constructed with the values `args`.
1152 template <typename T, typename... ARGS>
1153 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001154 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001155 }
1156
1157 /// @param args the arguments for the matrix constructor
1158 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1159 /// `T`, constructed with the values `args`.
1160 template <typename T, typename... ARGS>
1161 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001162 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001163 }
1164
1165 /// @param args the arguments for the matrix constructor
1166 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1167 /// `T`, constructed with the values `args`.
1168 template <typename T, typename... ARGS>
1169 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001170 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001171 }
1172
1173 /// @param args the arguments for the matrix constructor
1174 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1175 /// `T`, constructed with the values `args`.
1176 template <typename T, typename... ARGS>
1177 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001178 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001179 }
1180
1181 /// @param args the arguments for the matrix constructor
1182 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1183 /// `T`, constructed with the values `args`.
1184 template <typename T, typename... ARGS>
1185 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001186 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001187 }
1188
1189 /// @param args the arguments for the matrix constructor
1190 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1191 /// `T`, constructed with the values `args`.
1192 template <typename T, typename... ARGS>
1193 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001194 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001195 }
1196
1197 /// @param args the arguments for the array constructor
1198 /// @return an `ast::TypeConstructorExpression` of an array with element type
1199 /// `T`, constructed with the values `args`.
1200 template <typename T, int N = 0, typename... ARGS>
1201 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001202 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001203 }
1204
1205 /// @param subtype the array element type
1206 /// @param n the array size. 0 represents a runtime-array.
1207 /// @param args the arguments for the array constructor
1208 /// @return an `ast::TypeConstructorExpression` of an array with element type
1209 /// `subtype`, constructed with the values `args`.
1210 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001211 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001212 uint32_t n,
1213 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001214 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 }
1216
1217 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001219 /// @param optional the optional variable settings.
1220 /// Can be any of the following, in any order:
1221 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001222 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001223 /// * ast::Expression* - specifies the variable's initializer expression
1224 /// * ast::DecorationList - specifies the variable's decorations
1225 /// Note that repeated arguments of the same type will use the last argument's
1226 /// value.
1227 /// @returns a `ast::Variable` with the given name, type and additional
1228 /// options
1229 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001230 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001231 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001232 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001233 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1234 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001235 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001236 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001237 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238
1239 /// @param source the variable source
1240 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001241 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001242 /// @param optional the optional variable settings.
1243 /// Can be any of the following, in any order:
1244 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001245 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001246 /// * ast::Expression* - specifies the variable's initializer expression
1247 /// * ast::DecorationList - specifies the variable's decorations
1248 /// Note that repeated arguments of the same type will use the last argument's
1249 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001251 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001252 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001253 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001254 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001255 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001256 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1257 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001258 opts.storage, opts.access, type, false,
1259 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001260 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001261
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001263 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001264 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001266 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001267 template <typename NAME>
1268 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001269 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001270 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001271 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001272 return create<ast::Variable>(
1273 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1274 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001275 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001276
1277 /// @param source the variable source
1278 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001279 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001280 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001281 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001282 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001283 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001284 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001285 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001286 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001287 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001288 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001289 return create<ast::Variable>(
1290 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1291 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001292 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001293
James Pricedaf1f1c2021-04-08 22:15:48 +00001294 /// @param name the parameter name
1295 /// @param type the parameter type
1296 /// @param decorations optional parameter decorations
1297 /// @returns a constant `ast::Variable` with the given name and type
1298 template <typename NAME>
1299 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001300 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001301 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001302 return create<ast::Variable>(
1303 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1304 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001305 }
1306
1307 /// @param source the parameter source
1308 /// @param name the parameter name
1309 /// @param type the parameter type
1310 /// @param decorations optional parameter decorations
1311 /// @returns a constant `ast::Variable` with the given name and type
1312 template <typename NAME>
1313 ast::Variable* Param(const Source& source,
1314 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001315 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001316 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001317 return create<ast::Variable>(
1318 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1319 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001320 }
1321
Ben Clayton42708342021-04-21 17:55:12 +00001322 /// @param name the variable name
1323 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001324 /// @param optional the optional variable settings.
1325 /// Can be any of the following, in any order:
1326 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001327 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001328 /// * ast::Expression* - specifies the variable's initializer expression
1329 /// * ast::DecorationList - specifies the variable's decorations
1330 /// Note that repeated arguments of the same type will use the last argument's
1331 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001332 /// @returns a new `ast::Variable`, which is automatically registered as a
1333 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001334 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001335 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001336 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001337 OPTIONAL&&... optional) {
1338 auto* var = Var(std::forward<NAME>(name), type,
1339 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001340 AST().AddGlobalVariable(var);
1341 return var;
1342 }
1343
1344 /// @param source the variable source
1345 /// @param name the variable name
1346 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001347 /// @param optional the optional variable settings.
1348 /// Can be any of the following, in any order:
1349 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001350 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001351 /// * ast::Expression* - specifies the variable's initializer expression
1352 /// * ast::DecorationList - specifies the variable's decorations
1353 /// Note that repeated arguments of the same type will use the last argument's
1354 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001355 /// @returns a new `ast::Variable`, which is automatically registered as a
1356 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001357 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001358 ast::Variable* Global(const Source& source,
1359 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001360 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001361 OPTIONAL&&... optional) {
1362 auto* var = Var(source, std::forward<NAME>(name), type,
1363 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001364 AST().AddGlobalVariable(var);
1365 return var;
1366 }
1367
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001368 /// @param name the variable name
1369 /// @param type the variable type
1370 /// @param constructor constructor expression
1371 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001372 /// @returns a const `ast::Variable` constructed by calling Var() with the
1373 /// arguments of `args`, which is automatically registered as a global
1374 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001375 template <typename NAME>
1376 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001377 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001378 ast::Expression* constructor,
1379 ast::DecorationList decorations = {}) {
1380 auto* var = Const(std::forward<NAME>(name), type, constructor,
1381 std::move(decorations));
1382 AST().AddGlobalVariable(var);
1383 return var;
1384 }
1385
1386 /// @param source the variable source
1387 /// @param name the variable name
1388 /// @param type the variable type
1389 /// @param constructor constructor expression
1390 /// @param decorations optional variable decorations
1391 /// @returns a const `ast::Variable` constructed by calling Var() with the
1392 /// arguments of `args`, which is automatically registered as a global
1393 /// variable with the ast::Module.
1394 template <typename NAME>
1395 ast::Variable* GlobalConst(const Source& source,
1396 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001397 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001398 ast::Expression* constructor,
1399 ast::DecorationList decorations = {}) {
1400 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1401 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001402 AST().AddGlobalVariable(var);
1403 return var;
1404 }
1405
Ben Claytonfe0910f2021-05-17 15:51:47 +00001406 /// @param source the source information
1407 /// @param expr the expression to take the address of
1408 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1409 template <typename EXPR>
1410 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1411 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1412 Expr(std::forward<EXPR>(expr)));
1413 }
1414
1415 /// @param expr the expression to take the address of
1416 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1417 template <typename EXPR>
1418 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1419 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1420 Expr(std::forward<EXPR>(expr)));
1421 }
1422
1423 /// @param source the source information
1424 /// @param expr the expression to perform an indirection on
1425 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1426 template <typename EXPR>
1427 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1428 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1429 Expr(std::forward<EXPR>(expr)));
1430 }
1431
1432 /// @param expr the expression to perform an indirection on
1433 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1434 template <typename EXPR>
1435 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1436 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1437 Expr(std::forward<EXPR>(expr)));
1438 }
1439
Antonio Maiorano14b34032021-06-09 20:17:59 +00001440 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001441 /// @param func the function name
1442 /// @param args the function call arguments
1443 /// @returns a `ast::CallExpression` to the function `func`, with the
1444 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1445 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001446 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1447 return create<ast::CallExpression>(source, Expr(func),
1448 ExprList(std::forward<ARGS>(args)...));
1449 }
1450
1451 /// @param func the function name
1452 /// @param args the function call arguments
1453 /// @returns a `ast::CallExpression` to the function `func`, with the
1454 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1455 template <typename NAME,
1456 typename... ARGS,
1457 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001458 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1459 return create<ast::CallExpression>(Expr(func),
1460 ExprList(std::forward<ARGS>(args)...));
1461 }
1462
Ben Clayton7fe01062021-06-11 13:22:27 +00001463 /// @param expr the expression to ignore
1464 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1465 /// passed the single `expr` argument
1466 template <typename EXPR>
1467 ast::CallStatement* Ignore(EXPR&& expr) {
1468 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1469 }
1470
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001471 /// @param lhs the left hand argument to the addition operation
1472 /// @param rhs the right hand argument to the addition operation
1473 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1474 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001475 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001476 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1477 Expr(std::forward<LHS>(lhs)),
1478 Expr(std::forward<RHS>(rhs)));
1479 }
1480
1481 /// @param lhs the left hand argument to the subtraction operation
1482 /// @param rhs the right hand argument to the subtraction operation
1483 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1484 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001485 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001486 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1487 Expr(std::forward<LHS>(lhs)),
1488 Expr(std::forward<RHS>(rhs)));
1489 }
1490
1491 /// @param lhs the left hand argument to the multiplication operation
1492 /// @param rhs the right hand argument to the multiplication operation
1493 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1494 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001495 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001496 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1497 Expr(std::forward<LHS>(lhs)),
1498 Expr(std::forward<RHS>(rhs)));
1499 }
1500
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001501 /// @param source the source information
1502 /// @param lhs the left hand argument to the multiplication operation
1503 /// @param rhs the right hand argument to the multiplication operation
1504 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1505 template <typename LHS, typename RHS>
1506 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1507 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1508 Expr(std::forward<LHS>(lhs)),
1509 Expr(std::forward<RHS>(rhs)));
1510 }
1511
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001512 /// @param lhs the left hand argument to the division operation
1513 /// @param rhs the right hand argument to the division operation
1514 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1515 template <typename LHS, typename RHS>
1516 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1517 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1518 Expr(std::forward<LHS>(lhs)),
1519 Expr(std::forward<RHS>(rhs)));
1520 }
1521
Ben Clayton0597a2b2021-06-16 09:19:36 +00001522 /// @param source the source information
1523 /// @param arr the array argument for the array accessor expression
1524 /// @param idx the index argument for the array accessor expression
1525 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1526 template <typename ARR, typename IDX>
1527 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1528 ARR&& arr,
1529 IDX&& idx) {
1530 return create<ast::ArrayAccessorExpression>(
1531 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1532 }
1533
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001534 /// @param arr the array argument for the array accessor expression
1535 /// @param idx the index argument for the array accessor expression
1536 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1537 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001538 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001539 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1540 Expr(std::forward<IDX>(idx)));
1541 }
1542
1543 /// @param obj the object for the member accessor expression
1544 /// @param idx the index argument for the array accessor expression
1545 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1546 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001547 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001548 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1549 Expr(std::forward<IDX>(idx)));
1550 }
1551
Ben Clayton42d1e092021-02-02 14:29:15 +00001552 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001553 /// @param val the offset value
1554 /// @returns the offset decoration pointer
1555 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1556 return create<ast::StructMemberOffsetDecoration>(source_, val);
1557 }
1558
Ben Claytond614dd52021-03-15 10:43:11 +00001559 /// Creates a ast::StructMemberSizeDecoration
1560 /// @param source the source information
1561 /// @param val the size value
1562 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001563 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1564 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001565 return create<ast::StructMemberSizeDecoration>(source, val);
1566 }
1567
1568 /// Creates a ast::StructMemberSizeDecoration
1569 /// @param val the size value
1570 /// @returns the size decoration pointer
1571 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1572 return create<ast::StructMemberSizeDecoration>(source_, val);
1573 }
1574
1575 /// Creates a ast::StructMemberAlignDecoration
1576 /// @param source the source information
1577 /// @param val the align value
1578 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001579 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1580 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001581 return create<ast::StructMemberAlignDecoration>(source, val);
1582 }
1583
1584 /// Creates a ast::StructMemberAlignDecoration
1585 /// @param val the align value
1586 /// @returns the align decoration pointer
1587 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1588 return create<ast::StructMemberAlignDecoration>(source_, val);
1589 }
1590
Ben Clayton42d1e092021-02-02 14:29:15 +00001591 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001592 /// @param source the source information
1593 /// @param name the function name
1594 /// @param params the function parameters
1595 /// @param type the function return type
1596 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001597 /// @param decorations the optional function decorations
1598 /// @param return_type_decorations the optional function return type
1599 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001600 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001601 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001602 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001603 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001604 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001605 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001606 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001607 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001608 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001609 auto* func =
1610 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1611 type, create<ast::BlockStatement>(body),
1612 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001613 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001614 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001615 }
1616
Ben Clayton42d1e092021-02-02 14:29:15 +00001617 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001618 /// @param name the function name
1619 /// @param params the function parameters
1620 /// @param type the function return type
1621 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001622 /// @param decorations the optional function decorations
1623 /// @param return_type_decorations the optional function return type
1624 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001625 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001626 template <typename NAME>
1627 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001628 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001629 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001630 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001631 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001632 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001633 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1634 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001635 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001636 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001637 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001638 }
1639
Ben Clayton49668bb2021-04-17 05:32:01 +00001640 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001641 /// @param source the source information
1642 /// @returns the return statement pointer
1643 ast::ReturnStatement* Return(const Source& source) {
1644 return create<ast::ReturnStatement>(source);
1645 }
1646
1647 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001648 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001649 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1650
1651 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001652 /// @param source the source information
1653 /// @param val the return value
1654 /// @returns the return statement pointer
1655 template <typename EXPR>
1656 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1657 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1658 }
1659
1660 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001661 /// @param val the return value
1662 /// @returns the return statement pointer
1663 template <typename EXPR>
1664 ast::ReturnStatement* Return(EXPR&& val) {
1665 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001666 }
1667
Ben Clayton950809f2021-06-09 14:32:14 +00001668 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001669 /// @param source the source information
1670 /// @param name the alias name
1671 /// @param type the alias target type
1672 /// @returns the alias type
1673 template <typename NAME>
1674 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1675 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001676 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001677 return out;
1678 }
1679
Ben Clayton950809f2021-06-09 14:32:14 +00001680 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001681 /// @param name the alias name
1682 /// @param type the alias target type
1683 /// @returns the alias type
1684 template <typename NAME>
1685 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1686 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001687 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001688 return out;
1689 }
1690
Ben Clayton950809f2021-06-09 14:32:14 +00001691 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001692 /// @param source the source information
1693 /// @param name the struct name
1694 /// @param members the struct members
1695 /// @param decorations the optional struct decorations
1696 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001697 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001698 ast::Struct* Structure(const Source& source,
1699 NAME&& name,
1700 ast::StructMemberList members,
1701 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001702 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001703 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001704 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001705 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001706 return type;
1707 }
1708
Ben Clayton950809f2021-06-09 14:32:14 +00001709 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001710 /// @param name the struct name
1711 /// @param members the struct members
1712 /// @param decorations the optional struct decorations
1713 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001714 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001715 ast::Struct* Structure(NAME&& name,
1716 ast::StructMemberList members,
1717 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001718 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001719 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001720 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001721 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001722 return type;
1723 }
1724
Ben Clayton42d1e092021-02-02 14:29:15 +00001725 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001726 /// @param source the source information
1727 /// @param name the struct member name
1728 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001729 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001730 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001731 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001732 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001733 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001734 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001735 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001736 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1737 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001738 }
1739
Ben Clayton42d1e092021-02-02 14:29:15 +00001740 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001741 /// @param name the struct member name
1742 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001743 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001744 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001745 template <typename NAME>
1746 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001747 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001748 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001749 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1750 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001751 }
1752
Ben Claytonbab31972021-02-08 21:16:21 +00001753 /// Creates a ast::StructMember with the given byte offset
1754 /// @param offset the offset to use in the StructMemberOffsetDecoration
1755 /// @param name the struct member name
1756 /// @param type the struct member type
1757 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001758 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001759 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001760 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001761 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001762 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001763 create<ast::StructMemberOffsetDecoration>(offset),
1764 });
1765 }
1766
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001767 /// Creates a ast::BlockStatement with input statements
1768 /// @param statements statements of block
1769 /// @returns the block statement pointer
1770 template <typename... Statements>
1771 ast::BlockStatement* Block(Statements&&... statements) {
1772 return create<ast::BlockStatement>(
1773 ast::StatementList{std::forward<Statements>(statements)...});
1774 }
1775
1776 /// Creates a ast::ElseStatement with input condition and body
1777 /// @param condition the else condition expression
1778 /// @param body the else body
1779 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001780 template <typename CONDITION>
1781 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1782 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1783 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001784 }
1785
1786 /// Creates a ast::IfStatement with input condition, body, and optional
1787 /// variadic else statements
1788 /// @param condition the if statement condition expression
1789 /// @param body the if statement body
1790 /// @param elseStatements optional variadic else statements
1791 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001792 template <typename CONDITION, typename... ELSE_STATEMENTS>
1793 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001794 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001795 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001796 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001797 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001798 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001799 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001800 }
1801
1802 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001803 /// @param source the source information
1804 /// @param lhs the left hand side expression initializer
1805 /// @param rhs the right hand side expression initializer
1806 /// @returns the assignment statement pointer
1807 template <typename LhsExpressionInit, typename RhsExpressionInit>
1808 ast::AssignmentStatement* Assign(const Source& source,
1809 LhsExpressionInit&& lhs,
1810 RhsExpressionInit&& rhs) {
1811 return create<ast::AssignmentStatement>(
1812 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1813 Expr(std::forward<RhsExpressionInit>(rhs)));
1814 }
1815
1816 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001817 /// @param lhs the left hand side expression initializer
1818 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001819 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001820 template <typename LhsExpressionInit, typename RhsExpressionInit>
1821 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1822 RhsExpressionInit&& rhs) {
1823 return create<ast::AssignmentStatement>(
1824 Expr(std::forward<LhsExpressionInit>(lhs)),
1825 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001826 }
1827
1828 /// Creates a ast::LoopStatement with input body and optional continuing
1829 /// @param body the loop body
1830 /// @param continuing the optional continuing block
1831 /// @returns the loop statement pointer
1832 ast::LoopStatement* Loop(ast::BlockStatement* body,
1833 ast::BlockStatement* continuing = nullptr) {
1834 return create<ast::LoopStatement>(body, continuing);
1835 }
1836
1837 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001838 /// @param source the source information
1839 /// @param var the variable to wrap in a decl statement
1840 /// @returns the variable decl statement pointer
1841 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1842 return create<ast::VariableDeclStatement>(source, var);
1843 }
1844
1845 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001846 /// @param var the variable to wrap in a decl statement
1847 /// @returns the variable decl statement pointer
1848 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1849 return create<ast::VariableDeclStatement>(var);
1850 }
1851
Antonio Maioranocea744d2021-03-25 12:55:27 +00001852 /// Creates a ast::SwitchStatement with input expression and cases
1853 /// @param condition the condition expression initializer
1854 /// @param cases case statements
1855 /// @returns the switch statement pointer
1856 template <typename ExpressionInit, typename... Cases>
1857 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1858 return create<ast::SwitchStatement>(
1859 Expr(std::forward<ExpressionInit>(condition)),
1860 ast::CaseStatementList{std::forward<Cases>(cases)...});
1861 }
1862
1863 /// Creates a ast::CaseStatement with input list of selectors, and body
1864 /// @param selectors list of selectors
1865 /// @param body the case body
1866 /// @returns the case statement pointer
1867 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1868 ast::BlockStatement* body = nullptr) {
1869 return create<ast::CaseStatement>(std::move(selectors),
1870 body ? body : Block());
1871 }
1872
1873 /// Convenient overload that takes a single selector
1874 /// @param selector a single case selector
1875 /// @param body the case body
1876 /// @returns the case statement pointer
1877 ast::CaseStatement* Case(ast::IntLiteral* selector,
1878 ast::BlockStatement* body = nullptr) {
1879 return Case(ast::CaseSelectorList{selector}, body);
1880 }
1881
1882 /// Convenience function that creates a 'default' ast::CaseStatement
1883 /// @param body the case body
1884 /// @returns the case statement pointer
1885 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1886 return Case(ast::CaseSelectorList{}, body);
1887 }
1888
James Price68f558f2021-04-06 15:51:47 +00001889 /// Creates an ast::BuiltinDecoration
1890 /// @param source the source information
1891 /// @param builtin the builtin value
1892 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001893 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001894 return create<ast::BuiltinDecoration>(source, builtin);
1895 }
1896
1897 /// Creates an ast::BuiltinDecoration
1898 /// @param builtin the builtin value
1899 /// @returns the builtin decoration pointer
1900 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1901 return create<ast::BuiltinDecoration>(source_, builtin);
1902 }
1903
1904 /// Creates an ast::LocationDecoration
1905 /// @param source the source information
1906 /// @param location the location value
1907 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001908 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001909 return create<ast::LocationDecoration>(source, location);
1910 }
1911
1912 /// Creates an ast::LocationDecoration
1913 /// @param location the location value
1914 /// @returns the location decoration pointer
1915 ast::LocationDecoration* Location(uint32_t location) {
1916 return create<ast::LocationDecoration>(source_, location);
1917 }
1918
James Pricef2f3bfc2021-05-13 20:32:32 +00001919 /// Creates an ast::OverrideDecoration with a specific constant ID
1920 /// @param source the source information
1921 /// @param id the id value
1922 /// @returns the override decoration pointer
1923 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1924 return create<ast::OverrideDecoration>(source, id);
1925 }
1926
1927 /// Creates an ast::OverrideDecoration with a specific constant ID
1928 /// @param id the optional id value
1929 /// @returns the override decoration pointer
1930 ast::OverrideDecoration* Override(uint32_t id) {
1931 return Override(source_, id);
1932 }
1933
1934 /// Creates an ast::OverrideDecoration without a constant ID
1935 /// @param source the source information
1936 /// @returns the override decoration pointer
1937 ast::OverrideDecoration* Override(const Source& source) {
1938 return create<ast::OverrideDecoration>(source);
1939 }
1940
1941 /// Creates an ast::OverrideDecoration without a constant ID
1942 /// @returns the override decoration pointer
1943 ast::OverrideDecoration* Override() { return Override(source_); }
1944
James Price68f558f2021-04-06 15:51:47 +00001945 /// Creates an ast::StageDecoration
1946 /// @param source the source information
1947 /// @param stage the pipeline stage
1948 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001949 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001950 return create<ast::StageDecoration>(source, stage);
1951 }
1952
1953 /// Creates an ast::StageDecoration
1954 /// @param stage the pipeline stage
1955 /// @returns the stage decoration pointer
1956 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1957 return create<ast::StageDecoration>(source_, stage);
1958 }
1959
James Price70f80bb2021-05-19 13:40:08 +00001960 /// Creates an ast::WorkgroupDecoration
1961 /// @param x the x dimension expression
1962 /// @returns the workgroup decoration pointer
1963 template <typename EXPR_X>
1964 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1965 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1966 }
1967
1968 /// Creates an ast::WorkgroupDecoration
1969 /// @param x the x dimension expression
1970 /// @param y the y dimension expression
1971 /// @returns the workgroup decoration pointer
1972 template <typename EXPR_X, typename EXPR_Y>
1973 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1974 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1975 nullptr);
1976 }
1977
1978 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00001979 /// @param source the source information
1980 /// @param x the x dimension expression
1981 /// @param y the y dimension expression
1982 /// @param z the z dimension expression
1983 /// @returns the workgroup decoration pointer
1984 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1985 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
1986 EXPR_X&& x,
1987 EXPR_Y&& y,
1988 EXPR_Z&& z) {
1989 return create<ast::WorkgroupDecoration>(
1990 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1991 Expr(std::forward<EXPR_Z>(z)));
1992 }
1993
1994 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00001995 /// @param x the x dimension expression
1996 /// @param y the y dimension expression
1997 /// @param z the z dimension expression
1998 /// @returns the workgroup decoration pointer
1999 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2000 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2001 return create<ast::WorkgroupDecoration>(
2002 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2003 Expr(std::forward<EXPR_Z>(z)));
2004 }
2005
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002006 /// Sets the current builder source to `src`
2007 /// @param src the Source used for future create() calls
2008 void SetSource(const Source& src) {
2009 AssertNotMoved();
2010 source_ = src;
2011 }
2012
2013 /// Sets the current builder source to `loc`
2014 /// @param loc the Source used for future create() calls
2015 void SetSource(const Source::Location& loc) {
2016 AssertNotMoved();
2017 source_ = Source(loc);
2018 }
2019
Ben Clayton33352542021-01-29 16:43:41 +00002020 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002021 /// @note As the Resolver is run when the Program is built, this will only be
2022 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002023 /// @param expr the AST expression
2024 /// @return the resolved semantic type for the expression, or nullptr if the
2025 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002026 sem::Type* TypeOf(const ast::Expression* expr) const;
2027
Ben Claytonfe0910f2021-05-17 15:51:47 +00002028 /// Helper for returning the resolved semantic type of the variable `var`.
2029 /// @note As the Resolver is run when the Program is built, this will only be
2030 /// useful for the Resolver itself and tests that use their own Resolver.
2031 /// @param var the AST variable
2032 /// @return the resolved semantic type for the variable, or nullptr if the
2033 /// variable has no resolved type.
2034 sem::Type* TypeOf(const ast::Variable* var) const;
2035
Ben Claytonfbec46f2021-04-30 20:20:19 +00002036 /// Helper for returning the resolved semantic type of the AST type `type`.
2037 /// @note As the Resolver is run when the Program is built, this will only be
2038 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002039 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002040 /// @return the resolved semantic type for the type, or nullptr if the type
2041 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002042 const sem::Type* TypeOf(const ast::Type* type) const;
2043
2044 /// Helper for returning the resolved semantic type of the AST type
2045 /// declaration `type_decl`.
2046 /// @note As the Resolver is run when the Program is built, this will only be
2047 /// useful for the Resolver itself and tests that use their own Resolver.
2048 /// @param type_decl the AST type declaration
2049 /// @return the resolved semantic type for the type declaration, or nullptr if
2050 /// the type declaration has no resolved type.
2051 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002052
Ben Clayton169512e2021-04-17 05:52:11 +00002053 /// Wraps the ast::Literal in a statement. This is used by tests that
2054 /// construct a partial AST and require the Resolver to reach these
2055 /// nodes.
2056 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2057 /// @return the ast::Statement that wraps the ast::Statement
2058 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002059 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002060 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002061 /// nodes.
2062 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2063 /// @return the ast::Statement that wraps the ast::Expression
2064 ast::Statement* WrapInStatement(ast::Expression* expr);
2065 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002066 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002067 /// these nodes.
2068 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2069 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2070 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2071 /// Returns the statement argument. Used as a passthrough-overload by
2072 /// WrapInFunction().
2073 /// @param stmt the ast::Statement
2074 /// @return `stmt`
2075 ast::Statement* WrapInStatement(ast::Statement* stmt);
2076 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002077 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002078 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002079 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002080 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002081 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002082 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002083 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002084 }
2085 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002086 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002087 /// @returns the function
2088 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002089
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002090 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002091 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002092
2093 protected:
2094 /// Asserts that the builder has not been moved.
2095 void AssertNotMoved() const;
2096
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002097 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002098 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002099 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002100 ASTNodeAllocator ast_nodes_;
2101 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002102 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002103 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002104 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002105 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002106
2107 /// The source to use when creating AST nodes without providing a Source as
2108 /// the first argument.
2109 Source source_;
2110
Ben Clayton5f0ea112021-03-09 10:54:37 +00002111 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002112 /// program when built.
2113 bool resolve_on_build_ = true;
2114
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002115 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2116 bool moved_ = false;
2117};
2118
2119//! @cond Doxygen_Suppress
2120// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2121template <>
2122struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002123 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002124 return t->i32();
2125 }
2126};
2127template <>
2128struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002129 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002130 return t->u32();
2131 }
2132};
2133template <>
2134struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002135 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002136 return t->f32();
2137 }
2138};
2139template <>
2140struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002141 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002142 return t->bool_();
2143 }
2144};
2145template <>
2146struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002147 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002148 return t->void_();
2149 }
2150};
2151//! @endcond
2152
Ben Clayton917b14b2021-04-19 16:50:23 +00002153/// @param builder the ProgramBuilder
2154/// @returns the ProgramID of the ProgramBuilder
2155inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2156 return builder->ID();
2157}
2158
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002159} // namespace tint
2160
2161#endif // SRC_PROGRAM_BUILDER_H_