blob: ace90f08a7347da78b4b3cb81ccb04d41f1cbfa8 [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 Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000026#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/bool_literal.h"
28#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000029#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000030#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000031#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000032#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000033#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000034#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000035#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000036#include "src/ast/if_statement.h"
37#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000038#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000039#include "src/ast/member_accessor_expression.h"
40#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000041#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000042#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000043#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000044#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000045#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000046#include "src/ast/scalar_constructor_expression.h"
47#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000048#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000049#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000050#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000051#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000052#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000053#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000054#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000055#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000056#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000057#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000058#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000059#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000060#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000061#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000062#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000063#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000064#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000065#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000066#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000067#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000068#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000069#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000070#include "src/sem/f32_type.h"
71#include "src/sem/i32_type.h"
72#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000073#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000074#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000075#include "src/sem/sampled_texture_type.h"
76#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000077#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000078#include "src/sem/u32_type.h"
79#include "src/sem/vector_type.h"
80#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000081
Ben Clayton03d10722021-06-05 12:45:50 +000082#ifdef INCLUDE_TINT_TINT_H_
83#error "internal tint header being #included from tint.h"
84#endif
85
Ben Claytona6b9a8e2021-01-26 16:57:10 +000086namespace tint {
87
Ben Clayton401b96b2021-02-03 17:19:59 +000088// Forward declarations
89namespace ast {
90class VariableDeclStatement;
91} // namespace ast
92
Ben Claytona6b9a8e2021-01-26 16:57:10 +000093class CloneContext;
94
95/// ProgramBuilder is a mutable builder for a Program.
96/// To construct a Program, populate the builder and then `std::move` it to a
97/// Program.
98class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +000099 /// VarOptionals is a helper for accepting a number of optional, extra
100 /// arguments for Var() and Global().
101 struct VarOptionals {
102 template <typename... ARGS>
103 explicit VarOptionals(ARGS&&... args) {
104 Apply(std::forward<ARGS>(args)...);
105 }
106 ~VarOptionals();
107
108 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000109 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000110 ast::Expression* constructor = nullptr;
111 ast::DecorationList decorations = {};
112
113 private:
114 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000115 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000116 void Set(ast::Expression* c) { constructor = c; }
117 void Set(const ast::DecorationList& l) { decorations = l; }
118
119 template <typename FIRST, typename... ARGS>
120 void Apply(FIRST&& first, ARGS&&... args) {
121 Set(std::forward<FIRST>(first));
122 Apply(std::forward<ARGS>(args)...);
123 }
124 void Apply() {}
125 };
126
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000127 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000128 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
129 using ASTNodeAllocator = BlockAllocator<ast::Node>;
130
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000131 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
132 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000133
134 /// `i32` is a type alias to `int`.
135 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
136 /// WGSL syntax.
137 /// Note: this is intentionally not aliased to uint32_t as we want integer
138 /// literals passed to the builder to match WGSL's integer literal types.
139 using i32 = decltype(1);
140 /// `u32` is a type alias to `unsigned int`.
141 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
142 /// WGSL syntax.
143 /// Note: this is intentionally not aliased to uint32_t as we want integer
144 /// literals passed to the builder to match WGSL's integer literal types.
145 using u32 = decltype(1u);
146 /// `f32` is a type alias to `float`
147 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
148 /// WGSL syntax.
149 using f32 = float;
150
151 /// Constructor
152 ProgramBuilder();
153
154 /// Move constructor
155 /// @param rhs the builder to move
156 ProgramBuilder(ProgramBuilder&& rhs);
157
158 /// Destructor
159 virtual ~ProgramBuilder();
160
161 /// Move assignment operator
162 /// @param rhs the builder to move
163 /// @return this builder
164 ProgramBuilder& operator=(ProgramBuilder&& rhs);
165
Ben Claytone43c8302021-01-29 11:59:32 +0000166 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
167 /// making a deep clone of the Program contents.
168 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
169 /// existing immutable program.
170 /// As the returned ProgramBuilder wraps `program`, `program` must not be
171 /// destructed or assigned while using the returned ProgramBuilder.
172 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
173 /// function. See crbug.com/tint/460.
174 /// @param program the immutable Program to wrap
175 /// @return the ProgramBuilder that wraps `program`
176 static ProgramBuilder Wrap(const Program* program);
177
Ben Claytone6995de2021-04-13 23:27:27 +0000178 /// @returns the unique identifier for this program
179 ProgramID ID() const { return id_; }
180
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000181 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000182 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000183 AssertNotMoved();
184 return types_;
185 }
186
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000187 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000188 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000189 AssertNotMoved();
190 return types_;
191 }
192
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000193 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000194 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000195 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000196 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000197 }
198
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000199 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000200 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000201 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000202 return ast_nodes_;
203 }
204
205 /// @returns a reference to the program's semantic nodes storage
206 SemNodeAllocator& SemNodes() {
207 AssertNotMoved();
208 return sem_nodes_;
209 }
210
211 /// @returns a reference to the program's semantic nodes storage
212 const SemNodeAllocator& SemNodes() const {
213 AssertNotMoved();
214 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000215 }
216
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000217 /// @returns a reference to the program's AST root Module
218 ast::Module& AST() {
219 AssertNotMoved();
220 return *ast_;
221 }
222
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000223 /// @returns a reference to the program's AST root Module
224 const ast::Module& AST() const {
225 AssertNotMoved();
226 return *ast_;
227 }
228
229 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000230 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000231 AssertNotMoved();
232 return sem_;
233 }
234
235 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000236 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000237 AssertNotMoved();
238 return sem_;
239 }
240
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000241 /// @returns a reference to the program's SymbolTable
242 SymbolTable& Symbols() {
243 AssertNotMoved();
244 return symbols_;
245 }
246
Ben Clayton708dc2d2021-01-29 11:22:40 +0000247 /// @returns a reference to the program's SymbolTable
248 const SymbolTable& Symbols() const {
249 AssertNotMoved();
250 return symbols_;
251 }
252
Ben Clayton844217f2021-01-27 18:49:05 +0000253 /// @returns a reference to the program's diagnostics
254 diag::List& Diagnostics() {
255 AssertNotMoved();
256 return diagnostics_;
257 }
258
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000259 /// @returns a reference to the program's diagnostics
260 const diag::List& Diagnostics() const {
261 AssertNotMoved();
262 return diagnostics_;
263 }
264
Ben Clayton5f0ea112021-03-09 10:54:37 +0000265 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000266 /// @param enable the new flag value (defaults to true)
267 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
268
Ben Clayton5f0ea112021-03-09 10:54:37 +0000269 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000270 /// built.
271 bool ResolveOnBuild() const { return resolve_on_build_; }
272
Ben Clayton844217f2021-01-27 18:49:05 +0000273 /// @returns true if the program has no error diagnostics and is not missing
274 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000275 bool IsValid() const;
276
Ben Clayton708dc2d2021-01-29 11:22:40 +0000277 /// Writes a representation of the node to the output stream
278 /// @note unlike str(), to_str() does not automatically demangle the string.
279 /// @param node the AST node
280 /// @param out the stream to write to
281 /// @param indent number of spaces to indent the node when writing
282 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
283 node->to_str(Sem(), out, indent);
284 }
285
286 /// Returns a demangled, string representation of `node`.
287 /// @param node the AST node
288 /// @returns a string representation of the node
289 std::string str(const ast::Node* node) const;
290
Ben Clayton7fdfff12021-01-29 15:17:30 +0000291 /// Creates a new ast::Node owned by the ProgramBuilder. When the
292 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000293 /// @param source the Source of the node
294 /// @param args the arguments to pass to the type constructor
295 /// @returns the node pointer
296 template <typename T, typename... ARGS>
297 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
298 ARGS&&... args) {
299 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000300 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000301 }
302
Ben Clayton7fdfff12021-01-29 15:17:30 +0000303 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
304 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000305 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000306 /// When the ProgramBuilder is destructed, the ast::Node will also be
307 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000308 /// @returns the node pointer
309 template <typename T>
310 traits::EnableIfIsType<T, ast::Node>* create() {
311 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000312 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000313 }
314
Ben Clayton7fdfff12021-01-29 15:17:30 +0000315 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
316 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000317 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000318 /// When the ProgramBuilder is destructed, the ast::Node will also be
319 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000320 /// @param arg0 the first arguments to pass to the type constructor
321 /// @param args the remaining arguments to pass to the type constructor
322 /// @returns the node pointer
323 template <typename T, typename ARG0, typename... ARGS>
324 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
325 traits::IsTypeOrDerived<T, ast::Node>::value &&
326 !traits::IsTypeOrDerived<ARG0, Source>::value,
327 T>*
328 create(ARG0&& arg0, ARGS&&... args) {
329 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000330 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000331 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000332 }
333
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000334 /// Creates a new sem::Node owned by the ProgramBuilder.
335 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000336 /// destructed.
337 /// @param args the arguments to pass to the type constructor
338 /// @returns the node pointer
339 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000340 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
341 !traits::IsTypeOrDerived<T, sem::Type>::value,
342 T>*
343 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000344 AssertNotMoved();
345 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
346 }
347
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000348 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000349 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
350 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000351 /// Types are unique (de-aliased), and so calling create() for the same `T`
352 /// and arguments will return the same pointer.
353 /// @warning Use this method to acquire a type only if all of its type
354 /// information is provided in the constructor arguments `args`.<br>
355 /// If the type requires additional configuration after construction that
356 /// affect its fundamental type, build the type with `std::make_unique`, make
357 /// any necessary alterations and then call unique_type() instead.
358 /// @param args the arguments to pass to the type constructor
359 /// @returns the de-aliased type pointer
360 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000361 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
362 static_assert(std::is_base_of<sem::Type, T>::value,
363 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000364 AssertNotMoved();
365 return types_.Get<T>(std::forward<ARGS>(args)...);
366 }
367
368 /// Marks this builder as moved, preventing any further use of the builder.
369 void MarkAsMoved();
370
371 //////////////////////////////////////////////////////////////////////////////
372 // TypesBuilder
373 //////////////////////////////////////////////////////////////////////////////
374
375 /// TypesBuilder holds basic `tint` types and methods for constructing
376 /// complex types.
377 class TypesBuilder {
378 public:
379 /// Constructor
380 /// @param builder the program builder
381 explicit TypesBuilder(ProgramBuilder* builder);
382
383 /// @return the tint AST type for the C type `T`.
384 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000385 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000386 return CToAST<T>::get(this);
387 }
388
389 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000390 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000391
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000392 /// @param source the Source of the node
393 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000394 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000395 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000396 }
397
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000398 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000399 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000400
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000401 /// @param source the Source of the node
402 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000403 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000404 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000405 }
406
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000407 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000408 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000409
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000410 /// @param source the Source of the node
411 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000412 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000413 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000414 }
415
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000416 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000417 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000418
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000419 /// @param source the Source of the node
420 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000421 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000422 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000423 }
424
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000425 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000426 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000427
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000428 /// @param source the Source of the node
429 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000430 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000431 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000432 }
433
Antonio Maiorano25436862021-04-13 13:32:33 +0000434 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000435 /// @param n vector width in elements
436 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000437 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000438 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000439 }
440
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000441 /// @param source the Source of the node
442 /// @param type vector subtype
443 /// @param n vector width in elements
444 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000445 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000446 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000447 }
448
Antonio Maiorano25436862021-04-13 13:32:33 +0000449 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000450 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000451 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000452
453 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000454 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000455 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000456
457 /// @param type vector subtype
458 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000459 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000460
461 /// @param n vector width in elements
462 /// @return the tint AST type for a `n`-element vector of `type`.
463 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000464 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000465 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000466 }
467
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000468 /// @return the tint AST type for a 2-element vector of the C type `T`.
469 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000470 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000471 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000472 }
473
474 /// @return the tint AST type for a 3-element vector of the C type `T`.
475 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000476 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000477 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000478 }
479
480 /// @return the tint AST type for a 4-element vector of the C type `T`.
481 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000482 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000483 return vec4(Of<T>());
484 }
485
486 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000487 /// @param columns number of columns for the matrix
488 /// @param rows number of rows for the matrix
489 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000490 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000491 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000492 }
493
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000494 /// @param source the Source of the node
495 /// @param type matrix subtype
496 /// @param columns number of columns for the matrix
497 /// @param rows number of rows for the matrix
498 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000499 ast::Matrix* mat(const Source& source,
500 ast::Type* type,
501 uint32_t columns,
502 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000503 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000504 }
505
Ben Claytone204f272021-04-22 14:40:23 +0000506 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000507 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000508 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000509
510 /// @param type matrix subtype
511 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000512 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000513
514 /// @param type matrix subtype
515 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000516 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000517
518 /// @param type matrix subtype
519 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000520 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000521
522 /// @param type matrix subtype
523 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000524 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000525
526 /// @param type matrix subtype
527 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000528 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000529
530 /// @param type matrix subtype
531 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000532 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000533
534 /// @param type matrix subtype
535 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000536 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000537
538 /// @param type matrix subtype
539 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000540 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000541
Ben Claytone204f272021-04-22 14:40:23 +0000542 /// @param columns number of columns for the matrix
543 /// @param rows number of rows for the matrix
544 /// @return the tint AST type for a matrix of `type`
545 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000546 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000547 return mat(Of<T>(), columns, rows);
548 }
549
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000550 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
551 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000552 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000553 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000554 }
555
556 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
557 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000558 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000559 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000560 }
561
562 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
563 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000564 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000565 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000566 }
567
568 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
569 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000570 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000571 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000572 }
573
574 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
575 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000576 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000577 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000578 }
579
580 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
581 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000582 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000583 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000584 }
585
586 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
587 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000588 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000589 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000590 }
591
592 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
593 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000594 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000595 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000596 }
597
598 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
599 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000600 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000601 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000602 }
603
604 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000605 /// @param n the array size. 0 represents a runtime-array
606 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000607 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000608 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000609 uint32_t n = 0,
610 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000611 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000612 }
613
614 /// @param source the Source of the node
615 /// @param subtype the array element type
616 /// @param n the array size. 0 represents a runtime-array
617 /// @param decos the optional decorations for the array
618 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000619 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000620 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000621 uint32_t n = 0,
622 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000623 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000624 }
625
Ben Claytonbab31972021-02-08 21:16:21 +0000626 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000627 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000628 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000629 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000630 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000631 ast::DecorationList decos;
632 if (stride) {
633 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
634 }
635 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000636 }
637
Ben Clayton0f88b312021-05-04 17:36:31 +0000638 /// @param source the Source of the node
639 /// @param subtype the array element type
640 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000641 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000642 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000643 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000644 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000645 uint32_t n,
646 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000647 ast::DecorationList decos;
648 if (stride) {
649 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
650 }
651 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000652 }
653
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000654 /// @return the tint AST type for an array of size `N` of type `T`
655 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000656 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000657 return array(Of<T>(), N);
658 }
659
Ben Claytonbab31972021-02-08 21:16:21 +0000660 /// @param stride the array stride
661 /// @return the tint AST type for an array of size `N` of type `T`
662 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000663 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000664 return array(Of<T>(), N, stride);
665 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000666
Ben Clayton0f88b312021-05-04 17:36:31 +0000667 /// Creates a type name
668 /// @param name the name
669 /// @returns the type name
670 template <typename NAME>
671 ast::TypeName* type_name(NAME&& name) const {
672 return builder->create<ast::TypeName>(
673 builder->Sym(std::forward<NAME>(name)));
674 }
675
676 /// Creates a type name
677 /// @param source the Source of the node
678 /// @param name the name
679 /// @returns the type name
680 template <typename NAME>
681 ast::TypeName* type_name(const Source& source, NAME&& name) const {
682 return builder->create<ast::TypeName>(
683 source, builder->Sym(std::forward<NAME>(name)));
684 }
685
Ben Clayton42d1e092021-02-02 14:29:15 +0000686 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000687 /// @param name the alias name
688 /// @param type the alias type
689 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000690 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000691 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000692 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000693 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000694 }
695
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000696 /// Creates an alias type
697 /// @param source the Source of the node
698 /// @param name the alias name
699 /// @param type the alias type
700 /// @returns the alias pointer
701 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000702 ast::Alias* alias(const Source& source,
703 NAME&& name,
704 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000705 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000706 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000707 }
708
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000709 /// @param type the type of the pointer
710 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000711 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000712 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000713 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000714 ast::StorageClass storage_class,
715 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000716 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000717 }
718
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000719 /// @param source the Source of the node
720 /// @param type the type of the pointer
721 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000722 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000723 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000724 ast::Pointer* pointer(const Source& source,
725 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000726 ast::StorageClass storage_class,
727 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000728 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000729 }
730
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000731 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000732 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000733 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000734 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000735 ast::Pointer* pointer(ast::StorageClass storage_class,
736 ast::Access access = ast::Access::kUndefined) const {
737 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000738 }
739
Ben Claytone204f272021-04-22 14:40:23 +0000740 /// @param kind the kind of sampler
741 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000742 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000743 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000744 }
745
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000746 /// @param source the Source of the node
747 /// @param kind the kind of sampler
748 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000749 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000750 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000751 }
752
Ben Claytone204f272021-04-22 14:40:23 +0000753 /// @param dims the dimensionality of the texture
754 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000755 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000756 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000757 }
758
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000759 /// @param source the Source of the node
760 /// @param dims the dimensionality of the texture
761 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000762 ast::DepthTexture* depth_texture(const Source& source,
763 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000764 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000765 }
766
Ben Claytone204f272021-04-22 14:40:23 +0000767 /// @param dims the dimensionality of the texture
768 /// @param subtype the texture subtype.
769 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000770 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
771 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000772 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000773 }
774
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000775 /// @param source the Source of the node
776 /// @param dims the dimensionality of the texture
777 /// @param subtype the texture subtype.
778 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000779 ast::SampledTexture* sampled_texture(const Source& source,
780 ast::TextureDimension dims,
781 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000782 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000783 }
784
Ben Claytone204f272021-04-22 14:40:23 +0000785 /// @param dims the dimensionality of the texture
786 /// @param subtype the texture subtype.
787 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000788 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
789 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000790 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000791 }
792
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000793 /// @param source the Source of the node
794 /// @param dims the dimensionality of the texture
795 /// @param subtype the texture subtype.
796 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000797 ast::MultisampledTexture* multisampled_texture(const Source& source,
798 ast::TextureDimension dims,
799 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000800 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000801 }
802
Ben Claytone204f272021-04-22 14:40:23 +0000803 /// @param dims the dimensionality of the texture
804 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000805 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000806 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000807 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000808 ast::ImageFormat format,
809 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000810 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000811 return builder->create<ast::StorageTexture>(dims, format, subtype,
812 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000813 }
814
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000815 /// @param source the Source of the node
816 /// @param dims the dimensionality of the texture
817 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000818 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000819 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000820 ast::StorageTexture* storage_texture(const Source& source,
821 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000822 ast::ImageFormat format,
823 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000824 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000825 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
826 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000827 }
828
Brandon Jones7b257692021-05-17 17:40:17 +0000829 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000830 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000831 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000832 }
833
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000834 /// @param source the Source of the node
835 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000836 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000837 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000838 }
839
Ben Clayton19b03192021-05-20 15:04:08 +0000840 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000841 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000842 /// @param type the type
843 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000844 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000845
Ben Clayton8758f102021-06-09 14:32:14 +0000846 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000847 /// @param type the type
848 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000849 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000850
Ben Claytonf5f311e2021-04-28 14:31:23 +0000851 /// The ProgramBuilder
852 ProgramBuilder* const builder;
853
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000854 private:
855 /// CToAST<T> is specialized for various `T` types and each specialization
856 /// contains a single static `get()` method for obtaining the corresponding
857 /// AST type for the C type `T`.
858 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000859 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000860 template <typename T>
861 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000862 };
863
864 //////////////////////////////////////////////////////////////////////////////
865 // AST helper methods
866 //////////////////////////////////////////////////////////////////////////////
867
James Price65ae64d2021-04-29 12:59:14 +0000868 /// @return a new unnamed symbol
869 Symbol Sym() { return Symbols().New(); }
870
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000871 /// @param name the symbol string
872 /// @return a Symbol with the given name
873 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
874
875 /// @param sym the symbol
876 /// @return `sym`
877 Symbol Sym(Symbol sym) { return sym; }
878
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000879 /// @param expr the expression
880 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000881 template <typename T>
882 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
883 return expr;
884 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000885
Ben Claytonb8ea5912021-04-19 16:52:42 +0000886 /// Passthrough for nullptr
887 /// @return nullptr
888 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
889
Ben Claytonfe0910f2021-05-17 15:51:47 +0000890 /// @param source the source information
891 /// @param symbol the identifier symbol
892 /// @return an ast::IdentifierExpression with the given symbol
893 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
894 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000895 }
896
Ben Clayton46d78d72021-02-10 21:34:26 +0000897 /// @param symbol the identifier symbol
898 /// @return an ast::IdentifierExpression with the given symbol
899 ast::IdentifierExpression* Expr(Symbol symbol) {
900 return create<ast::IdentifierExpression>(symbol);
901 }
902
Ben Claytonfe0910f2021-05-17 15:51:47 +0000903 /// @param source the source information
904 /// @param variable the AST variable
905 /// @return an ast::IdentifierExpression with the variable's symbol
906 ast::IdentifierExpression* Expr(const Source& source,
907 ast::Variable* variable) {
908 return create<ast::IdentifierExpression>(source, variable->symbol());
909 }
910
Ben Claytonb8ea5912021-04-19 16:52:42 +0000911 /// @param variable the AST variable
912 /// @return an ast::IdentifierExpression with the variable's symbol
913 ast::IdentifierExpression* Expr(ast::Variable* variable) {
914 return create<ast::IdentifierExpression>(variable->symbol());
915 }
916
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000917 /// @param source the source information
918 /// @param name the identifier name
919 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000920 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
921 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
922 }
923
924 /// @param name the identifier name
925 /// @return an ast::IdentifierExpression with the given name
926 ast::IdentifierExpression* Expr(const char* name) {
927 return create<ast::IdentifierExpression>(Symbols().Register(name));
928 }
929
930 /// @param source the source information
931 /// @param name the identifier name
932 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000933 ast::IdentifierExpression* Expr(const Source& source,
934 const std::string& name) {
935 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
936 }
937
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 std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000941 return create<ast::IdentifierExpression>(Symbols().Register(name));
942 }
943
Ben Claytonfe0910f2021-05-17 15:51:47 +0000944 /// @param source the source information
945 /// @param value the boolean value
946 /// @return a Scalar constructor for the given value
947 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
948 return create<ast::ScalarConstructorExpression>(source, Literal(value));
949 }
950
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000951 /// @param value the boolean value
952 /// @return a Scalar constructor for the given value
953 ast::ScalarConstructorExpression* Expr(bool value) {
954 return create<ast::ScalarConstructorExpression>(Literal(value));
955 }
956
Ben Claytonfe0910f2021-05-17 15:51:47 +0000957 /// @param source the source information
958 /// @param value the float value
959 /// @return a Scalar constructor for the given value
960 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
961 return create<ast::ScalarConstructorExpression>(source, Literal(value));
962 }
963
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000964 /// @param value the float value
965 /// @return a Scalar constructor for the given value
966 ast::ScalarConstructorExpression* Expr(f32 value) {
967 return create<ast::ScalarConstructorExpression>(Literal(value));
968 }
969
Ben Claytonfe0910f2021-05-17 15:51:47 +0000970 /// @param source the source information
971 /// @param value the integer value
972 /// @return a Scalar constructor for the given value
973 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
974 return create<ast::ScalarConstructorExpression>(source, Literal(value));
975 }
976
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000977 /// @param value the integer value
978 /// @return a Scalar constructor for the given value
979 ast::ScalarConstructorExpression* Expr(i32 value) {
980 return create<ast::ScalarConstructorExpression>(Literal(value));
981 }
982
Ben Claytonfe0910f2021-05-17 15:51:47 +0000983 /// @param source the source information
984 /// @param value the unsigned int value
985 /// @return a Scalar constructor for the given value
986 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
987 return create<ast::ScalarConstructorExpression>(source, Literal(value));
988 }
989
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000990 /// @param value the unsigned int value
991 /// @return a Scalar constructor for the given value
992 ast::ScalarConstructorExpression* Expr(u32 value) {
993 return create<ast::ScalarConstructorExpression>(Literal(value));
994 }
995
996 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
997 /// `list`.
998 /// @param list the list to append too
999 /// @param arg the arg to create
1000 template <typename ARG>
1001 void Append(ast::ExpressionList& list, ARG&& arg) {
1002 list.emplace_back(Expr(std::forward<ARG>(arg)));
1003 }
1004
1005 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1006 /// then appends them to `list`.
1007 /// @param list the list to append too
1008 /// @param arg0 the first argument
1009 /// @param args the rest of the arguments
1010 template <typename ARG0, typename... ARGS>
1011 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1012 Append(list, std::forward<ARG0>(arg0));
1013 Append(list, std::forward<ARGS>(args)...);
1014 }
1015
1016 /// @return an empty list of expressions
1017 ast::ExpressionList ExprList() { return {}; }
1018
1019 /// @param args the list of expressions
1020 /// @return the list of expressions converted to `ast::Expression`s using
1021 /// `Expr()`,
1022 template <typename... ARGS>
1023 ast::ExpressionList ExprList(ARGS&&... args) {
1024 ast::ExpressionList list;
1025 list.reserve(sizeof...(args));
1026 Append(list, std::forward<ARGS>(args)...);
1027 return list;
1028 }
1029
1030 /// @param list the list of expressions
1031 /// @return `list`
1032 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1033
1034 /// @param val the boolan value
1035 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001036 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001037
1038 /// @param val the float value
1039 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001040 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001041
1042 /// @param val the unsigned int value
1043 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001044 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001045
1046 /// @param val the integer value
1047 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001048 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001049
1050 /// @param args the arguments for the type constructor
1051 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1052 /// of `args` converted to `ast::Expression`s using `Expr()`
1053 template <typename T, typename... ARGS>
1054 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001055 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001056 }
1057
1058 /// @param type the type to construct
1059 /// @param args the arguments for the constructor
1060 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1061 /// values `args`.
1062 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001063 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001064 return create<ast::TypeConstructorExpression>(
1065 type, ExprList(std::forward<ARGS>(args)...));
1066 }
1067
1068 /// @param args the arguments for the vector constructor
1069 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1070 /// `T`, constructed with the values `args`.
1071 template <typename T, typename... ARGS>
1072 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001073 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074 }
1075
1076 /// @param args the arguments for the vector constructor
1077 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1078 /// `T`, constructed with the values `args`.
1079 template <typename T, typename... ARGS>
1080 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001081 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001082 }
1083
1084 /// @param args the arguments for the vector constructor
1085 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1086 /// `T`, constructed with the values `args`.
1087 template <typename T, typename... ARGS>
1088 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001089 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001090 }
1091
1092 /// @param args the arguments for the matrix constructor
1093 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1094 /// `T`, constructed with the values `args`.
1095 template <typename T, typename... ARGS>
1096 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001097 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001098 }
1099
1100 /// @param args the arguments for the matrix constructor
1101 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1102 /// `T`, constructed with the values `args`.
1103 template <typename T, typename... ARGS>
1104 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001105 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001106 }
1107
1108 /// @param args the arguments for the matrix constructor
1109 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1110 /// `T`, constructed with the values `args`.
1111 template <typename T, typename... ARGS>
1112 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001113 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 }
1115
1116 /// @param args the arguments for the matrix constructor
1117 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1118 /// `T`, constructed with the values `args`.
1119 template <typename T, typename... ARGS>
1120 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001121 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001122 }
1123
1124 /// @param args the arguments for the matrix constructor
1125 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1126 /// `T`, constructed with the values `args`.
1127 template <typename T, typename... ARGS>
1128 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001129 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001130 }
1131
1132 /// @param args the arguments for the matrix constructor
1133 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1134 /// `T`, constructed with the values `args`.
1135 template <typename T, typename... ARGS>
1136 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001137 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001138 }
1139
1140 /// @param args the arguments for the matrix constructor
1141 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1142 /// `T`, constructed with the values `args`.
1143 template <typename T, typename... ARGS>
1144 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001145 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001146 }
1147
1148 /// @param args the arguments for the matrix constructor
1149 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1150 /// `T`, constructed with the values `args`.
1151 template <typename T, typename... ARGS>
1152 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001153 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001154 }
1155
1156 /// @param args the arguments for the matrix constructor
1157 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1158 /// `T`, constructed with the values `args`.
1159 template <typename T, typename... ARGS>
1160 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001161 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001162 }
1163
1164 /// @param args the arguments for the array constructor
1165 /// @return an `ast::TypeConstructorExpression` of an array with element type
1166 /// `T`, constructed with the values `args`.
1167 template <typename T, int N = 0, typename... ARGS>
1168 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001169 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001170 }
1171
1172 /// @param subtype the array element type
1173 /// @param n the array size. 0 represents a runtime-array.
1174 /// @param args the arguments for the array constructor
1175 /// @return an `ast::TypeConstructorExpression` of an array with element type
1176 /// `subtype`, constructed with the values `args`.
1177 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001178 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001179 uint32_t n,
1180 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001181 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001182 }
1183
1184 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001185 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001186 /// @param optional the optional variable settings.
1187 /// Can be any of the following, in any order:
1188 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001189 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001190 /// * ast::Expression* - specifies the variable's initializer expression
1191 /// * ast::DecorationList - specifies the variable's decorations
1192 /// Note that repeated arguments of the same type will use the last argument's
1193 /// value.
1194 /// @returns a `ast::Variable` with the given name, type and additional
1195 /// options
1196 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001197 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001198 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001199 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001200 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1201 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001202 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001203 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001204 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001205
1206 /// @param source the variable source
1207 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001208 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001209 /// @param optional the optional variable settings.
1210 /// Can be any of the following, in any order:
1211 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001212 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001213 /// * ast::Expression* - specifies the variable's initializer expression
1214 /// * ast::DecorationList - specifies the variable's decorations
1215 /// Note that repeated arguments of the same type will use the last argument's
1216 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001217 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001218 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001219 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001220 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001221 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001222 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001223 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1224 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001225 opts.storage, opts.access, type, false,
1226 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001227 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001229 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001231 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001232 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001233 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001234 template <typename NAME>
1235 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001236 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001237 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001238 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001239 return create<ast::Variable>(
1240 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1241 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001242 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001243
1244 /// @param source the variable source
1245 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001246 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001247 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001248 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001249 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001250 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001251 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001252 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001253 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001254 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001255 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001256 return create<ast::Variable>(
1257 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1258 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001259 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001260
James Pricedaf1f1c2021-04-08 22:15:48 +00001261 /// @param name the parameter name
1262 /// @param type the parameter type
1263 /// @param decorations optional parameter decorations
1264 /// @returns a constant `ast::Variable` with the given name and type
1265 template <typename NAME>
1266 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001267 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001268 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001269 return create<ast::Variable>(
1270 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1271 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001272 }
1273
1274 /// @param source the parameter source
1275 /// @param name the parameter name
1276 /// @param type the parameter type
1277 /// @param decorations optional parameter decorations
1278 /// @returns a constant `ast::Variable` with the given name and type
1279 template <typename NAME>
1280 ast::Variable* Param(const Source& source,
1281 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001282 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001283 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001284 return create<ast::Variable>(
1285 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1286 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001287 }
1288
Ben Clayton42708342021-04-21 17:55:12 +00001289 /// @param name the variable name
1290 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001291 /// @param optional the optional variable settings.
1292 /// Can be any of the following, in any order:
1293 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001294 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001295 /// * ast::Expression* - specifies the variable's initializer expression
1296 /// * ast::DecorationList - specifies the variable's decorations
1297 /// Note that repeated arguments of the same type will use the last argument's
1298 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001299 /// @returns a new `ast::Variable`, which is automatically registered as a
1300 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001301 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001302 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001303 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001304 OPTIONAL&&... optional) {
1305 auto* var = Var(std::forward<NAME>(name), type,
1306 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001307 AST().AddGlobalVariable(var);
1308 return var;
1309 }
1310
1311 /// @param source the variable source
1312 /// @param name the variable name
1313 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001314 /// @param optional the optional variable settings.
1315 /// Can be any of the following, in any order:
1316 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001317 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001318 /// * ast::Expression* - specifies the variable's initializer expression
1319 /// * ast::DecorationList - specifies the variable's decorations
1320 /// Note that repeated arguments of the same type will use the last argument's
1321 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001322 /// @returns a new `ast::Variable`, which is automatically registered as a
1323 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001324 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001325 ast::Variable* Global(const Source& source,
1326 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001327 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001328 OPTIONAL&&... optional) {
1329 auto* var = Var(source, std::forward<NAME>(name), type,
1330 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001331 AST().AddGlobalVariable(var);
1332 return var;
1333 }
1334
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001335 /// @param name the variable name
1336 /// @param type the variable type
1337 /// @param constructor constructor expression
1338 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001339 /// @returns a const `ast::Variable` constructed by calling Var() with the
1340 /// arguments of `args`, which is automatically registered as a global
1341 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001342 template <typename NAME>
1343 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001344 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001345 ast::Expression* constructor,
1346 ast::DecorationList decorations = {}) {
1347 auto* var = Const(std::forward<NAME>(name), type, constructor,
1348 std::move(decorations));
1349 AST().AddGlobalVariable(var);
1350 return var;
1351 }
1352
1353 /// @param source the variable source
1354 /// @param name the variable name
1355 /// @param type the variable type
1356 /// @param constructor constructor expression
1357 /// @param decorations optional variable decorations
1358 /// @returns a const `ast::Variable` constructed by calling Var() with the
1359 /// arguments of `args`, which is automatically registered as a global
1360 /// variable with the ast::Module.
1361 template <typename NAME>
1362 ast::Variable* GlobalConst(const Source& source,
1363 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001364 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001365 ast::Expression* constructor,
1366 ast::DecorationList decorations = {}) {
1367 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1368 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001369 AST().AddGlobalVariable(var);
1370 return var;
1371 }
1372
Ben Claytonfe0910f2021-05-17 15:51:47 +00001373 /// @param source the source information
1374 /// @param expr the expression to take the address of
1375 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1376 template <typename EXPR>
1377 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1378 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1379 Expr(std::forward<EXPR>(expr)));
1380 }
1381
1382 /// @param expr the expression to take the address of
1383 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1384 template <typename EXPR>
1385 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1386 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1387 Expr(std::forward<EXPR>(expr)));
1388 }
1389
1390 /// @param source the source information
1391 /// @param expr the expression to perform an indirection on
1392 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1393 template <typename EXPR>
1394 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1395 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1396 Expr(std::forward<EXPR>(expr)));
1397 }
1398
1399 /// @param expr the expression to perform an indirection on
1400 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1401 template <typename EXPR>
1402 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1403 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1404 Expr(std::forward<EXPR>(expr)));
1405 }
1406
Antonio Maiorano14b34032021-06-09 20:17:59 +00001407 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001408 /// @param func the function name
1409 /// @param args the function call arguments
1410 /// @returns a `ast::CallExpression` to the function `func`, with the
1411 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1412 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001413 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1414 return create<ast::CallExpression>(source, Expr(func),
1415 ExprList(std::forward<ARGS>(args)...));
1416 }
1417
1418 /// @param func the function name
1419 /// @param args the function call arguments
1420 /// @returns a `ast::CallExpression` to the function `func`, with the
1421 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1422 template <typename NAME,
1423 typename... ARGS,
1424 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001425 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1426 return create<ast::CallExpression>(Expr(func),
1427 ExprList(std::forward<ARGS>(args)...));
1428 }
1429
Ben Clayton7fe01062021-06-11 13:22:27 +00001430 /// @param expr the expression to ignore
1431 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1432 /// passed the single `expr` argument
1433 template <typename EXPR>
1434 ast::CallStatement* Ignore(EXPR&& expr) {
1435 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1436 }
1437
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001438 /// @param lhs the left hand argument to the addition operation
1439 /// @param rhs the right hand argument to the addition operation
1440 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1441 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001442 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001443 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1444 Expr(std::forward<LHS>(lhs)),
1445 Expr(std::forward<RHS>(rhs)));
1446 }
1447
1448 /// @param lhs the left hand argument to the subtraction operation
1449 /// @param rhs the right hand argument to the subtraction operation
1450 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1451 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001452 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001453 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1454 Expr(std::forward<LHS>(lhs)),
1455 Expr(std::forward<RHS>(rhs)));
1456 }
1457
1458 /// @param lhs the left hand argument to the multiplication operation
1459 /// @param rhs the right hand argument to the multiplication operation
1460 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1461 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001462 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001463 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1464 Expr(std::forward<LHS>(lhs)),
1465 Expr(std::forward<RHS>(rhs)));
1466 }
1467
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001468 /// @param source the source information
1469 /// @param lhs the left hand argument to the multiplication operation
1470 /// @param rhs the right hand argument to the multiplication operation
1471 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1472 template <typename LHS, typename RHS>
1473 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1474 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1475 Expr(std::forward<LHS>(lhs)),
1476 Expr(std::forward<RHS>(rhs)));
1477 }
1478
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001479 /// @param lhs the left hand argument to the division operation
1480 /// @param rhs the right hand argument to the division operation
1481 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1482 template <typename LHS, typename RHS>
1483 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1484 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1485 Expr(std::forward<LHS>(lhs)),
1486 Expr(std::forward<RHS>(rhs)));
1487 }
1488
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001489 /// @param arr the array argument for the array accessor expression
1490 /// @param idx the index argument for the array accessor expression
1491 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1492 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001493 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001494 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1495 Expr(std::forward<IDX>(idx)));
1496 }
1497
1498 /// @param obj the object for the member accessor expression
1499 /// @param idx the index argument for the array accessor expression
1500 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1501 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001502 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001503 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1504 Expr(std::forward<IDX>(idx)));
1505 }
1506
Ben Clayton42d1e092021-02-02 14:29:15 +00001507 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001508 /// @param val the offset value
1509 /// @returns the offset decoration pointer
1510 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1511 return create<ast::StructMemberOffsetDecoration>(source_, val);
1512 }
1513
Ben Claytond614dd52021-03-15 10:43:11 +00001514 /// Creates a ast::StructMemberSizeDecoration
1515 /// @param source the source information
1516 /// @param val the size value
1517 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001518 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1519 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001520 return create<ast::StructMemberSizeDecoration>(source, val);
1521 }
1522
1523 /// Creates a ast::StructMemberSizeDecoration
1524 /// @param val the size value
1525 /// @returns the size decoration pointer
1526 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1527 return create<ast::StructMemberSizeDecoration>(source_, val);
1528 }
1529
1530 /// Creates a ast::StructMemberAlignDecoration
1531 /// @param source the source information
1532 /// @param val the align value
1533 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001534 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1535 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001536 return create<ast::StructMemberAlignDecoration>(source, val);
1537 }
1538
1539 /// Creates a ast::StructMemberAlignDecoration
1540 /// @param val the align value
1541 /// @returns the align decoration pointer
1542 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1543 return create<ast::StructMemberAlignDecoration>(source_, val);
1544 }
1545
Ben Clayton42d1e092021-02-02 14:29:15 +00001546 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001547 /// @param source the source information
1548 /// @param name the function name
1549 /// @param params the function parameters
1550 /// @param type the function return type
1551 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001552 /// @param decorations the optional function decorations
1553 /// @param return_type_decorations the optional function return type
1554 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001555 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001556 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001557 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001558 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001559 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001560 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001561 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001562 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001563 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001564 auto* func =
1565 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1566 type, create<ast::BlockStatement>(body),
1567 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001568 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001569 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001570 }
1571
Ben Clayton42d1e092021-02-02 14:29:15 +00001572 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001573 /// @param name the function name
1574 /// @param params the function parameters
1575 /// @param type the function return type
1576 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001577 /// @param decorations the optional function decorations
1578 /// @param return_type_decorations the optional function return type
1579 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001580 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001581 template <typename NAME>
1582 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001583 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001584 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001585 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001586 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001587 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001588 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1589 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001590 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001591 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001592 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001593 }
1594
Ben Clayton49668bb2021-04-17 05:32:01 +00001595 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001596 /// @param source the source information
1597 /// @returns the return statement pointer
1598 ast::ReturnStatement* Return(const Source& source) {
1599 return create<ast::ReturnStatement>(source);
1600 }
1601
1602 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001603 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001604 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1605
1606 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001607 /// @param source the source information
1608 /// @param val the return value
1609 /// @returns the return statement pointer
1610 template <typename EXPR>
1611 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1612 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1613 }
1614
1615 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001616 /// @param val the return value
1617 /// @returns the return statement pointer
1618 template <typename EXPR>
1619 ast::ReturnStatement* Return(EXPR&& val) {
1620 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001621 }
1622
Ben Clayton950809f2021-06-09 14:32:14 +00001623 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001624 /// @param source the source information
1625 /// @param name the alias name
1626 /// @param type the alias target type
1627 /// @returns the alias type
1628 template <typename NAME>
1629 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1630 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001631 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001632 return out;
1633 }
1634
Ben Clayton950809f2021-06-09 14:32:14 +00001635 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001636 /// @param name the alias name
1637 /// @param type the alias target type
1638 /// @returns the alias type
1639 template <typename NAME>
1640 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1641 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001642 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001643 return out;
1644 }
1645
Ben Clayton950809f2021-06-09 14:32:14 +00001646 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001647 /// @param source the source information
1648 /// @param name the struct name
1649 /// @param members the struct members
1650 /// @param decorations the optional struct decorations
1651 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001652 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001653 ast::Struct* Structure(const Source& source,
1654 NAME&& name,
1655 ast::StructMemberList members,
1656 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001657 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001658 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001659 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001660 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001661 return type;
1662 }
1663
Ben Clayton950809f2021-06-09 14:32:14 +00001664 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001665 /// @param name the struct name
1666 /// @param members the struct members
1667 /// @param decorations the optional struct decorations
1668 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001669 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001670 ast::Struct* Structure(NAME&& name,
1671 ast::StructMemberList members,
1672 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001673 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001674 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001675 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001676 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001677 return type;
1678 }
1679
Ben Clayton42d1e092021-02-02 14:29:15 +00001680 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001681 /// @param source the source information
1682 /// @param name the struct member name
1683 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001684 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001685 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001686 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001687 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001688 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001689 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001690 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001691 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1692 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001693 }
1694
Ben Clayton42d1e092021-02-02 14:29:15 +00001695 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001696 /// @param name the struct member name
1697 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001698 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001699 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001700 template <typename NAME>
1701 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001702 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001703 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001704 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1705 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001706 }
1707
Ben Claytonbab31972021-02-08 21:16:21 +00001708 /// Creates a ast::StructMember with the given byte offset
1709 /// @param offset the offset to use in the StructMemberOffsetDecoration
1710 /// @param name the struct member name
1711 /// @param type the struct member type
1712 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001713 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001714 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001715 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001716 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001717 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001718 create<ast::StructMemberOffsetDecoration>(offset),
1719 });
1720 }
1721
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001722 /// Creates a ast::BlockStatement with input statements
1723 /// @param statements statements of block
1724 /// @returns the block statement pointer
1725 template <typename... Statements>
1726 ast::BlockStatement* Block(Statements&&... statements) {
1727 return create<ast::BlockStatement>(
1728 ast::StatementList{std::forward<Statements>(statements)...});
1729 }
1730
1731 /// Creates a ast::ElseStatement with input condition and body
1732 /// @param condition the else condition expression
1733 /// @param body the else body
1734 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001735 template <typename CONDITION>
1736 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1737 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1738 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001739 }
1740
1741 /// Creates a ast::IfStatement with input condition, body, and optional
1742 /// variadic else statements
1743 /// @param condition the if statement condition expression
1744 /// @param body the if statement body
1745 /// @param elseStatements optional variadic else statements
1746 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001747 template <typename CONDITION, typename... ELSE_STATEMENTS>
1748 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001749 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001750 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001751 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001752 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001753 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001754 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001755 }
1756
1757 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001758 /// @param source the source information
1759 /// @param lhs the left hand side expression initializer
1760 /// @param rhs the right hand side expression initializer
1761 /// @returns the assignment statement pointer
1762 template <typename LhsExpressionInit, typename RhsExpressionInit>
1763 ast::AssignmentStatement* Assign(const Source& source,
1764 LhsExpressionInit&& lhs,
1765 RhsExpressionInit&& rhs) {
1766 return create<ast::AssignmentStatement>(
1767 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1768 Expr(std::forward<RhsExpressionInit>(rhs)));
1769 }
1770
1771 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001772 /// @param lhs the left hand side expression initializer
1773 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001774 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001775 template <typename LhsExpressionInit, typename RhsExpressionInit>
1776 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1777 RhsExpressionInit&& rhs) {
1778 return create<ast::AssignmentStatement>(
1779 Expr(std::forward<LhsExpressionInit>(lhs)),
1780 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001781 }
1782
1783 /// Creates a ast::LoopStatement with input body and optional continuing
1784 /// @param body the loop body
1785 /// @param continuing the optional continuing block
1786 /// @returns the loop statement pointer
1787 ast::LoopStatement* Loop(ast::BlockStatement* body,
1788 ast::BlockStatement* continuing = nullptr) {
1789 return create<ast::LoopStatement>(body, continuing);
1790 }
1791
1792 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001793 /// @param source the source information
1794 /// @param var the variable to wrap in a decl statement
1795 /// @returns the variable decl statement pointer
1796 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1797 return create<ast::VariableDeclStatement>(source, var);
1798 }
1799
1800 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001801 /// @param var the variable to wrap in a decl statement
1802 /// @returns the variable decl statement pointer
1803 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1804 return create<ast::VariableDeclStatement>(var);
1805 }
1806
Antonio Maioranocea744d2021-03-25 12:55:27 +00001807 /// Creates a ast::SwitchStatement with input expression and cases
1808 /// @param condition the condition expression initializer
1809 /// @param cases case statements
1810 /// @returns the switch statement pointer
1811 template <typename ExpressionInit, typename... Cases>
1812 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1813 return create<ast::SwitchStatement>(
1814 Expr(std::forward<ExpressionInit>(condition)),
1815 ast::CaseStatementList{std::forward<Cases>(cases)...});
1816 }
1817
1818 /// Creates a ast::CaseStatement with input list of selectors, and body
1819 /// @param selectors list of selectors
1820 /// @param body the case body
1821 /// @returns the case statement pointer
1822 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1823 ast::BlockStatement* body = nullptr) {
1824 return create<ast::CaseStatement>(std::move(selectors),
1825 body ? body : Block());
1826 }
1827
1828 /// Convenient overload that takes a single selector
1829 /// @param selector a single case selector
1830 /// @param body the case body
1831 /// @returns the case statement pointer
1832 ast::CaseStatement* Case(ast::IntLiteral* selector,
1833 ast::BlockStatement* body = nullptr) {
1834 return Case(ast::CaseSelectorList{selector}, body);
1835 }
1836
1837 /// Convenience function that creates a 'default' ast::CaseStatement
1838 /// @param body the case body
1839 /// @returns the case statement pointer
1840 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1841 return Case(ast::CaseSelectorList{}, body);
1842 }
1843
James Price68f558f2021-04-06 15:51:47 +00001844 /// Creates an ast::BuiltinDecoration
1845 /// @param source the source information
1846 /// @param builtin the builtin value
1847 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001848 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001849 return create<ast::BuiltinDecoration>(source, builtin);
1850 }
1851
1852 /// Creates an ast::BuiltinDecoration
1853 /// @param builtin the builtin value
1854 /// @returns the builtin decoration pointer
1855 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1856 return create<ast::BuiltinDecoration>(source_, builtin);
1857 }
1858
1859 /// Creates an ast::LocationDecoration
1860 /// @param source the source information
1861 /// @param location the location value
1862 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001863 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001864 return create<ast::LocationDecoration>(source, location);
1865 }
1866
1867 /// Creates an ast::LocationDecoration
1868 /// @param location the location value
1869 /// @returns the location decoration pointer
1870 ast::LocationDecoration* Location(uint32_t location) {
1871 return create<ast::LocationDecoration>(source_, location);
1872 }
1873
James Pricef2f3bfc2021-05-13 20:32:32 +00001874 /// Creates an ast::OverrideDecoration with a specific constant ID
1875 /// @param source the source information
1876 /// @param id the id value
1877 /// @returns the override decoration pointer
1878 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1879 return create<ast::OverrideDecoration>(source, id);
1880 }
1881
1882 /// Creates an ast::OverrideDecoration with a specific constant ID
1883 /// @param id the optional id value
1884 /// @returns the override decoration pointer
1885 ast::OverrideDecoration* Override(uint32_t id) {
1886 return Override(source_, id);
1887 }
1888
1889 /// Creates an ast::OverrideDecoration without a constant ID
1890 /// @param source the source information
1891 /// @returns the override decoration pointer
1892 ast::OverrideDecoration* Override(const Source& source) {
1893 return create<ast::OverrideDecoration>(source);
1894 }
1895
1896 /// Creates an ast::OverrideDecoration without a constant ID
1897 /// @returns the override decoration pointer
1898 ast::OverrideDecoration* Override() { return Override(source_); }
1899
James Price68f558f2021-04-06 15:51:47 +00001900 /// Creates an ast::StageDecoration
1901 /// @param source the source information
1902 /// @param stage the pipeline stage
1903 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001904 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001905 return create<ast::StageDecoration>(source, stage);
1906 }
1907
1908 /// Creates an ast::StageDecoration
1909 /// @param stage the pipeline stage
1910 /// @returns the stage decoration pointer
1911 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1912 return create<ast::StageDecoration>(source_, stage);
1913 }
1914
James Price70f80bb2021-05-19 13:40:08 +00001915 /// Creates an ast::WorkgroupDecoration
1916 /// @param x the x dimension expression
1917 /// @returns the workgroup decoration pointer
1918 template <typename EXPR_X>
1919 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1920 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1921 }
1922
1923 /// Creates an ast::WorkgroupDecoration
1924 /// @param x the x dimension expression
1925 /// @param y the y dimension expression
1926 /// @returns the workgroup decoration pointer
1927 template <typename EXPR_X, typename EXPR_Y>
1928 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1929 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1930 nullptr);
1931 }
1932
1933 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00001934 /// @param source the source information
1935 /// @param x the x dimension expression
1936 /// @param y the y dimension expression
1937 /// @param z the z dimension expression
1938 /// @returns the workgroup decoration pointer
1939 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1940 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
1941 EXPR_X&& x,
1942 EXPR_Y&& y,
1943 EXPR_Z&& z) {
1944 return create<ast::WorkgroupDecoration>(
1945 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1946 Expr(std::forward<EXPR_Z>(z)));
1947 }
1948
1949 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00001950 /// @param x the x dimension expression
1951 /// @param y the y dimension expression
1952 /// @param z the z dimension expression
1953 /// @returns the workgroup decoration pointer
1954 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1955 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1956 return create<ast::WorkgroupDecoration>(
1957 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1958 Expr(std::forward<EXPR_Z>(z)));
1959 }
1960
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001961 /// Sets the current builder source to `src`
1962 /// @param src the Source used for future create() calls
1963 void SetSource(const Source& src) {
1964 AssertNotMoved();
1965 source_ = src;
1966 }
1967
1968 /// Sets the current builder source to `loc`
1969 /// @param loc the Source used for future create() calls
1970 void SetSource(const Source::Location& loc) {
1971 AssertNotMoved();
1972 source_ = Source(loc);
1973 }
1974
Ben Clayton33352542021-01-29 16:43:41 +00001975 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001976 /// @note As the Resolver is run when the Program is built, this will only be
1977 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001978 /// @param expr the AST expression
1979 /// @return the resolved semantic type for the expression, or nullptr if the
1980 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001981 sem::Type* TypeOf(const ast::Expression* expr) const;
1982
Ben Claytonfe0910f2021-05-17 15:51:47 +00001983 /// Helper for returning the resolved semantic type of the variable `var`.
1984 /// @note As the Resolver is run when the Program is built, this will only be
1985 /// useful for the Resolver itself and tests that use their own Resolver.
1986 /// @param var the AST variable
1987 /// @return the resolved semantic type for the variable, or nullptr if the
1988 /// variable has no resolved type.
1989 sem::Type* TypeOf(const ast::Variable* var) const;
1990
Ben Claytonfbec46f2021-04-30 20:20:19 +00001991 /// Helper for returning the resolved semantic type of the AST type `type`.
1992 /// @note As the Resolver is run when the Program is built, this will only be
1993 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00001994 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00001995 /// @return the resolved semantic type for the type, or nullptr if the type
1996 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00001997 const sem::Type* TypeOf(const ast::Type* type) const;
1998
1999 /// Helper for returning the resolved semantic type of the AST type
2000 /// declaration `type_decl`.
2001 /// @note As the Resolver is run when the Program is built, this will only be
2002 /// useful for the Resolver itself and tests that use their own Resolver.
2003 /// @param type_decl the AST type declaration
2004 /// @return the resolved semantic type for the type declaration, or nullptr if
2005 /// the type declaration has no resolved type.
2006 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002007
Ben Clayton169512e2021-04-17 05:52:11 +00002008 /// Wraps the ast::Literal in a statement. This is used by tests that
2009 /// construct a partial AST and require the Resolver to reach these
2010 /// nodes.
2011 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2012 /// @return the ast::Statement that wraps the ast::Statement
2013 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002014 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002015 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002016 /// nodes.
2017 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2018 /// @return the ast::Statement that wraps the ast::Expression
2019 ast::Statement* WrapInStatement(ast::Expression* expr);
2020 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002021 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002022 /// these nodes.
2023 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2024 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2025 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2026 /// Returns the statement argument. Used as a passthrough-overload by
2027 /// WrapInFunction().
2028 /// @param stmt the ast::Statement
2029 /// @return `stmt`
2030 ast::Statement* WrapInStatement(ast::Statement* stmt);
2031 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002032 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002033 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002034 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002035 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002036 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002037 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002038 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002039 }
2040 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002041 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002042 /// @returns the function
2043 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002044
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002045 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002046 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002047
2048 protected:
2049 /// Asserts that the builder has not been moved.
2050 void AssertNotMoved() const;
2051
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002052 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002053 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002054 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002055 ASTNodeAllocator ast_nodes_;
2056 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002057 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002058 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002059 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002060 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002061
2062 /// The source to use when creating AST nodes without providing a Source as
2063 /// the first argument.
2064 Source source_;
2065
Ben Clayton5f0ea112021-03-09 10:54:37 +00002066 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002067 /// program when built.
2068 bool resolve_on_build_ = true;
2069
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002070 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2071 bool moved_ = false;
2072};
2073
2074//! @cond Doxygen_Suppress
2075// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2076template <>
2077struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002078 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002079 return t->i32();
2080 }
2081};
2082template <>
2083struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002084 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002085 return t->u32();
2086 }
2087};
2088template <>
2089struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002090 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002091 return t->f32();
2092 }
2093};
2094template <>
2095struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002096 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002097 return t->bool_();
2098 }
2099};
2100template <>
2101struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002102 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002103 return t->void_();
2104 }
2105};
2106//! @endcond
2107
Ben Clayton917b14b2021-04-19 16:50:23 +00002108/// @param builder the ProgramBuilder
2109/// @returns the ProgramID of the ProgramBuilder
2110inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2111 return builder->ID();
2112}
2113
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002114} // namespace tint
2115
2116#endif // SRC_PROGRAM_BUILDER_H_