blob: c6c58394d799b99a84f0e632a0dc0967568ca3ae [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Clayton8c7f0da2021-06-17 15:48:39 +000025#include "src/ast/atomic.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000026#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000027#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000028#include "src/ast/bool_literal.h"
29#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000030#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000031#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000032#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000033#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000035#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000036#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000037#include "src/ast/if_statement.h"
38#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000039#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000040#include "src/ast/member_accessor_expression.h"
41#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000043#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000045#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000046#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000047#include "src/ast/scalar_constructor_expression.h"
48#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000049#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000050#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000051#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000054#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000055#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000056#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000057#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000058#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000059#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000060#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000061#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000062#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000063#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000064#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000065#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000066#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000067#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000068#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000069#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000070#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000071#include "src/sem/f32_type.h"
72#include "src/sem/i32_type.h"
73#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000075#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000076#include "src/sem/sampled_texture_type.h"
77#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000078#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000079#include "src/sem/u32_type.h"
80#include "src/sem/vector_type.h"
81#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000082
Ben Clayton03d10722021-06-05 12:45:50 +000083#ifdef INCLUDE_TINT_TINT_H_
84#error "internal tint header being #included from tint.h"
85#endif
86
Ben Claytona6b9a8e2021-01-26 16:57:10 +000087namespace tint {
88
Ben Clayton401b96b2021-02-03 17:19:59 +000089// Forward declarations
90namespace ast {
91class VariableDeclStatement;
92} // namespace ast
93
Ben Claytona6b9a8e2021-01-26 16:57:10 +000094class CloneContext;
95
96/// ProgramBuilder is a mutable builder for a Program.
97/// To construct a Program, populate the builder and then `std::move` it to a
98/// Program.
99class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +0000100 /// VarOptionals is a helper for accepting a number of optional, extra
101 /// arguments for Var() and Global().
102 struct VarOptionals {
103 template <typename... ARGS>
104 explicit VarOptionals(ARGS&&... args) {
105 Apply(std::forward<ARGS>(args)...);
106 }
107 ~VarOptionals();
108
109 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000110 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000111 ast::Expression* constructor = nullptr;
112 ast::DecorationList decorations = {};
113
114 private:
115 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000116 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000117 void Set(ast::Expression* c) { constructor = c; }
118 void Set(const ast::DecorationList& l) { decorations = l; }
119
120 template <typename FIRST, typename... ARGS>
121 void Apply(FIRST&& first, ARGS&&... args) {
122 Set(std::forward<FIRST>(first));
123 Apply(std::forward<ARGS>(args)...);
124 }
125 void Apply() {}
126 };
127
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000128 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000129 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
130 using ASTNodeAllocator = BlockAllocator<ast::Node>;
131
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000132 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
133 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000134
135 /// `i32` is a type alias to `int`.
136 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
137 /// WGSL syntax.
138 /// Note: this is intentionally not aliased to uint32_t as we want integer
139 /// literals passed to the builder to match WGSL's integer literal types.
140 using i32 = decltype(1);
141 /// `u32` is a type alias to `unsigned int`.
142 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
143 /// WGSL syntax.
144 /// Note: this is intentionally not aliased to uint32_t as we want integer
145 /// literals passed to the builder to match WGSL's integer literal types.
146 using u32 = decltype(1u);
147 /// `f32` is a type alias to `float`
148 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
149 /// WGSL syntax.
150 using f32 = float;
151
152 /// Constructor
153 ProgramBuilder();
154
155 /// Move constructor
156 /// @param rhs the builder to move
157 ProgramBuilder(ProgramBuilder&& rhs);
158
159 /// Destructor
160 virtual ~ProgramBuilder();
161
162 /// Move assignment operator
163 /// @param rhs the builder to move
164 /// @return this builder
165 ProgramBuilder& operator=(ProgramBuilder&& rhs);
166
Ben Claytone43c8302021-01-29 11:59:32 +0000167 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
168 /// making a deep clone of the Program contents.
169 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
170 /// existing immutable program.
171 /// As the returned ProgramBuilder wraps `program`, `program` must not be
172 /// destructed or assigned while using the returned ProgramBuilder.
173 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
174 /// function. See crbug.com/tint/460.
175 /// @param program the immutable Program to wrap
176 /// @return the ProgramBuilder that wraps `program`
177 static ProgramBuilder Wrap(const Program* program);
178
Ben Claytone6995de2021-04-13 23:27:27 +0000179 /// @returns the unique identifier for this program
180 ProgramID ID() const { return id_; }
181
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000182 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000183 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000184 AssertNotMoved();
185 return types_;
186 }
187
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000188 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000189 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000190 AssertNotMoved();
191 return types_;
192 }
193
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000194 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000195 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000196 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000197 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000198 }
199
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000200 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000201 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000202 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000203 return ast_nodes_;
204 }
205
206 /// @returns a reference to the program's semantic nodes storage
207 SemNodeAllocator& SemNodes() {
208 AssertNotMoved();
209 return sem_nodes_;
210 }
211
212 /// @returns a reference to the program's semantic nodes storage
213 const SemNodeAllocator& SemNodes() const {
214 AssertNotMoved();
215 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000216 }
217
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000218 /// @returns a reference to the program's AST root Module
219 ast::Module& AST() {
220 AssertNotMoved();
221 return *ast_;
222 }
223
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000224 /// @returns a reference to the program's AST root Module
225 const ast::Module& AST() const {
226 AssertNotMoved();
227 return *ast_;
228 }
229
230 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000231 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000232 AssertNotMoved();
233 return sem_;
234 }
235
236 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000237 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000238 AssertNotMoved();
239 return sem_;
240 }
241
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000242 /// @returns a reference to the program's SymbolTable
243 SymbolTable& Symbols() {
244 AssertNotMoved();
245 return symbols_;
246 }
247
Ben Clayton708dc2d2021-01-29 11:22:40 +0000248 /// @returns a reference to the program's SymbolTable
249 const SymbolTable& Symbols() const {
250 AssertNotMoved();
251 return symbols_;
252 }
253
Ben Clayton844217f2021-01-27 18:49:05 +0000254 /// @returns a reference to the program's diagnostics
255 diag::List& Diagnostics() {
256 AssertNotMoved();
257 return diagnostics_;
258 }
259
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000260 /// @returns a reference to the program's diagnostics
261 const diag::List& Diagnostics() const {
262 AssertNotMoved();
263 return diagnostics_;
264 }
265
Ben Clayton5f0ea112021-03-09 10:54:37 +0000266 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000267 /// @param enable the new flag value (defaults to true)
268 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
269
Ben Clayton5f0ea112021-03-09 10:54:37 +0000270 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000271 /// built.
272 bool ResolveOnBuild() const { return resolve_on_build_; }
273
Ben Clayton844217f2021-01-27 18:49:05 +0000274 /// @returns true if the program has no error diagnostics and is not missing
275 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000276 bool IsValid() const;
277
Ben Clayton708dc2d2021-01-29 11:22:40 +0000278 /// Writes a representation of the node to the output stream
279 /// @note unlike str(), to_str() does not automatically demangle the string.
280 /// @param node the AST node
281 /// @param out the stream to write to
282 /// @param indent number of spaces to indent the node when writing
283 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
284 node->to_str(Sem(), out, indent);
285 }
286
287 /// Returns a demangled, string representation of `node`.
288 /// @param node the AST node
289 /// @returns a string representation of the node
290 std::string str(const ast::Node* node) const;
291
Ben Clayton7fdfff12021-01-29 15:17:30 +0000292 /// Creates a new ast::Node owned by the ProgramBuilder. When the
293 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000294 /// @param source the Source of the node
295 /// @param args the arguments to pass to the type constructor
296 /// @returns the node pointer
297 template <typename T, typename... ARGS>
298 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
299 ARGS&&... args) {
300 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000301 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000302 }
303
Ben Clayton7fdfff12021-01-29 15:17:30 +0000304 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
305 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000306 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000307 /// When the ProgramBuilder is destructed, the ast::Node will also be
308 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000309 /// @returns the node pointer
310 template <typename T>
311 traits::EnableIfIsType<T, ast::Node>* create() {
312 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000313 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000314 }
315
Ben Clayton7fdfff12021-01-29 15:17:30 +0000316 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
317 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000318 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000319 /// When the ProgramBuilder is destructed, the ast::Node will also be
320 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000321 /// @param arg0 the first arguments to pass to the type constructor
322 /// @param args the remaining arguments to pass to the type constructor
323 /// @returns the node pointer
324 template <typename T, typename ARG0, typename... ARGS>
325 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
326 traits::IsTypeOrDerived<T, ast::Node>::value &&
327 !traits::IsTypeOrDerived<ARG0, Source>::value,
328 T>*
329 create(ARG0&& arg0, ARGS&&... args) {
330 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000331 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000332 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000333 }
334
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000335 /// Creates a new sem::Node owned by the ProgramBuilder.
336 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000337 /// destructed.
338 /// @param args the arguments to pass to the type constructor
339 /// @returns the node pointer
340 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000341 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
342 !traits::IsTypeOrDerived<T, sem::Type>::value,
343 T>*
344 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000345 AssertNotMoved();
346 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
347 }
348
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000349 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000350 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
351 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000352 /// Types are unique (de-aliased), and so calling create() for the same `T`
353 /// and arguments will return the same pointer.
354 /// @warning Use this method to acquire a type only if all of its type
355 /// information is provided in the constructor arguments `args`.<br>
356 /// If the type requires additional configuration after construction that
357 /// affect its fundamental type, build the type with `std::make_unique`, make
358 /// any necessary alterations and then call unique_type() instead.
359 /// @param args the arguments to pass to the type constructor
360 /// @returns the de-aliased type pointer
361 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000362 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
363 static_assert(std::is_base_of<sem::Type, T>::value,
364 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000365 AssertNotMoved();
366 return types_.Get<T>(std::forward<ARGS>(args)...);
367 }
368
369 /// Marks this builder as moved, preventing any further use of the builder.
370 void MarkAsMoved();
371
372 //////////////////////////////////////////////////////////////////////////////
373 // TypesBuilder
374 //////////////////////////////////////////////////////////////////////////////
375
376 /// TypesBuilder holds basic `tint` types and methods for constructing
377 /// complex types.
378 class TypesBuilder {
379 public:
380 /// Constructor
381 /// @param builder the program builder
382 explicit TypesBuilder(ProgramBuilder* builder);
383
384 /// @return the tint AST type for the C type `T`.
385 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000386 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000387 return CToAST<T>::get(this);
388 }
389
390 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000391 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000392
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000393 /// @param source the Source of the node
394 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000395 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000396 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000397 }
398
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000399 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000400 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000401
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000402 /// @param source the Source of the node
403 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000404 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000405 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000406 }
407
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000408 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000409 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000410
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000411 /// @param source the Source of the node
412 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000413 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000414 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000415 }
416
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000417 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000418 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000419
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 /// @param source the Source of the node
421 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000422 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000423 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000424 }
425
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000426 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000427 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000428
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000429 /// @param source the Source of the node
430 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000431 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000432 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000433 }
434
Antonio Maiorano25436862021-04-13 13:32:33 +0000435 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000436 /// @param n vector width in elements
437 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000438 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000439 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000440 }
441
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000442 /// @param source the Source of the node
443 /// @param type vector subtype
444 /// @param n vector width in elements
445 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000446 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000447 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000448 }
449
Antonio Maiorano25436862021-04-13 13:32:33 +0000450 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000451 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000452 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000453
454 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000455 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000456 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000457
458 /// @param type vector subtype
459 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000460 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000461
462 /// @param n vector width in elements
463 /// @return the tint AST type for a `n`-element vector of `type`.
464 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000465 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000466 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000467 }
468
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000469 /// @return the tint AST type for a 2-element vector of the C type `T`.
470 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000471 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000472 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000473 }
474
475 /// @return the tint AST type for a 3-element vector of the C type `T`.
476 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000477 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000478 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000479 }
480
481 /// @return the tint AST type for a 4-element vector of the C type `T`.
482 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000483 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000484 return vec4(Of<T>());
485 }
486
487 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000488 /// @param columns number of columns for the matrix
489 /// @param rows number of rows for the matrix
490 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000491 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000492 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000493 }
494
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000495 /// @param source the Source of the node
496 /// @param type matrix subtype
497 /// @param columns number of columns for the matrix
498 /// @param rows number of rows for the matrix
499 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000500 ast::Matrix* mat(const Source& source,
501 ast::Type* type,
502 uint32_t columns,
503 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000504 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000505 }
506
Ben Claytone204f272021-04-22 14:40:23 +0000507 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000508 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000509 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000510
511 /// @param type matrix subtype
512 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000513 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000514
515 /// @param type matrix subtype
516 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000517 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000518
519 /// @param type matrix subtype
520 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000521 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000522
523 /// @param type matrix subtype
524 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000525 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000526
527 /// @param type matrix subtype
528 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000529 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000530
531 /// @param type matrix subtype
532 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000533 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000534
535 /// @param type matrix subtype
536 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000537 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000538
539 /// @param type matrix subtype
540 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000541 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000542
Ben Claytone204f272021-04-22 14:40:23 +0000543 /// @param columns number of columns for the matrix
544 /// @param rows number of rows for the matrix
545 /// @return the tint AST type for a matrix of `type`
546 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000547 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000548 return mat(Of<T>(), columns, rows);
549 }
550
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000551 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
552 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000553 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000554 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 }
556
557 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
558 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000559 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000560 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 }
562
563 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
564 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000565 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000566 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 }
568
569 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
570 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000571 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000572 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
575 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
576 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000577 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000578 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 }
580
581 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
582 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000583 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000584 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000585 }
586
587 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
588 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000589 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000590 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000591 }
592
593 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
594 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000595 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000596 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000597 }
598
599 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
600 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000601 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000602 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000603 }
604
605 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000606 /// @param n the array size. 0 represents a runtime-array
607 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000608 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000609 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000610 uint32_t n = 0,
611 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000612 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000613 }
614
615 /// @param source the Source of the node
616 /// @param subtype the array element type
617 /// @param n the array size. 0 represents a runtime-array
618 /// @param decos the optional decorations for the array
619 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000620 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000621 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000622 uint32_t n = 0,
623 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000624 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000625 }
626
Ben Claytonbab31972021-02-08 21:16:21 +0000627 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000628 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000629 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000630 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000631 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000632 ast::DecorationList decos;
633 if (stride) {
634 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
635 }
636 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000637 }
638
Ben Clayton0f88b312021-05-04 17:36:31 +0000639 /// @param source the Source of the node
640 /// @param subtype the array element type
641 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000642 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000643 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000644 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000645 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000646 uint32_t n,
647 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000648 ast::DecorationList decos;
649 if (stride) {
650 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
651 }
652 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000653 }
654
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000655 /// @return the tint AST type for an array of size `N` of type `T`
656 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000657 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000658 return array(Of<T>(), N);
659 }
660
Ben Claytonbab31972021-02-08 21:16:21 +0000661 /// @param stride the array stride
662 /// @return the tint AST type for an array of size `N` of type `T`
663 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000664 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000665 return array(Of<T>(), N, stride);
666 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000667
Ben Clayton0f88b312021-05-04 17:36:31 +0000668 /// Creates a type name
669 /// @param name the name
670 /// @returns the type name
671 template <typename NAME>
672 ast::TypeName* type_name(NAME&& name) const {
673 return builder->create<ast::TypeName>(
674 builder->Sym(std::forward<NAME>(name)));
675 }
676
677 /// Creates a type name
678 /// @param source the Source of the node
679 /// @param name the name
680 /// @returns the type name
681 template <typename NAME>
682 ast::TypeName* type_name(const Source& source, NAME&& name) const {
683 return builder->create<ast::TypeName>(
684 source, builder->Sym(std::forward<NAME>(name)));
685 }
686
Ben Clayton42d1e092021-02-02 14:29:15 +0000687 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000688 /// @param name the alias name
689 /// @param type the alias type
690 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000691 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000692 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000693 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000694 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000695 }
696
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000697 /// Creates an alias type
698 /// @param source the Source of the node
699 /// @param name the alias name
700 /// @param type the alias type
701 /// @returns the alias pointer
702 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000703 ast::Alias* alias(const Source& source,
704 NAME&& name,
705 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000706 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000707 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000708 }
709
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000710 /// @param type the type of the pointer
711 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000712 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000713 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000714 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000715 ast::StorageClass storage_class,
716 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000717 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000718 }
719
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000720 /// @param source the Source of the node
721 /// @param type the type of the pointer
722 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000723 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000724 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000725 ast::Pointer* pointer(const Source& source,
726 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000727 ast::StorageClass storage_class,
728 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000729 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000730 }
731
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000732 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000733 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000734 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000735 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000736 ast::Pointer* pointer(ast::StorageClass storage_class,
737 ast::Access access = ast::Access::kUndefined) const {
738 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000739 }
740
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000741 /// @param source the Source of the node
742 /// @param type the type of the atomic
743 /// @return the atomic to `type`
744 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
745 return builder->create<ast::Atomic>(source, type);
746 }
747
748 /// @return the atomic to type `T`
749 template <typename T>
750 ast::Atomic* atomic() const {
751 return atomic(Of<T>());
752 }
753
Ben Claytone204f272021-04-22 14:40:23 +0000754 /// @param kind the kind of sampler
755 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000756 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000757 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000758 }
759
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000760 /// @param source the Source of the node
761 /// @param kind the kind of sampler
762 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000763 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000764 return builder->create<ast::Sampler>(source, kind);
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 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000769 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000770 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000771 }
772
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000773 /// @param source the Source of the node
774 /// @param dims the dimensionality of the texture
775 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000776 ast::DepthTexture* depth_texture(const Source& source,
777 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000778 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000779 }
780
Ben Claytone204f272021-04-22 14:40:23 +0000781 /// @param dims the dimensionality of the texture
782 /// @param subtype the texture subtype.
783 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000784 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
785 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000786 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000787 }
788
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000789 /// @param source the Source of the node
790 /// @param dims the dimensionality of the texture
791 /// @param subtype the texture subtype.
792 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000793 ast::SampledTexture* sampled_texture(const Source& source,
794 ast::TextureDimension dims,
795 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000796 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000797 }
798
Ben Claytone204f272021-04-22 14:40:23 +0000799 /// @param dims the dimensionality of the texture
800 /// @param subtype the texture subtype.
801 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000802 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
803 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000804 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000805 }
806
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000807 /// @param source the Source of the node
808 /// @param dims the dimensionality of the texture
809 /// @param subtype the texture subtype.
810 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000811 ast::MultisampledTexture* multisampled_texture(const Source& source,
812 ast::TextureDimension dims,
813 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000814 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000815 }
816
Ben Claytone204f272021-04-22 14:40:23 +0000817 /// @param dims the dimensionality of the texture
818 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000819 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000820 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000821 ast::StorageTexture* storage_texture(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>(dims, format, subtype,
826 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000827 }
828
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000829 /// @param source the Source of the node
830 /// @param dims the dimensionality of the texture
831 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000832 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000833 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000834 ast::StorageTexture* storage_texture(const Source& source,
835 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000836 ast::ImageFormat format,
837 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000838 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000839 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
840 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000841 }
842
Brandon Jones7b257692021-05-17 17:40:17 +0000843 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000844 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000845 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000846 }
847
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000848 /// @param source the Source of the node
849 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000850 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000851 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000852 }
853
Ben Clayton19b03192021-05-20 15:04:08 +0000854 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000855 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000856 /// @param type the type
857 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000858 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000859
Ben Clayton8758f102021-06-09 14:32:14 +0000860 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000861 /// @param type the type
862 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000863 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000864
Ben Claytonf5f311e2021-04-28 14:31:23 +0000865 /// The ProgramBuilder
866 ProgramBuilder* const builder;
867
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000868 private:
869 /// CToAST<T> is specialized for various `T` types and each specialization
870 /// contains a single static `get()` method for obtaining the corresponding
871 /// AST type for the C type `T`.
872 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000873 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000874 template <typename T>
875 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000876 };
877
878 //////////////////////////////////////////////////////////////////////////////
879 // AST helper methods
880 //////////////////////////////////////////////////////////////////////////////
881
James Price65ae64d2021-04-29 12:59:14 +0000882 /// @return a new unnamed symbol
883 Symbol Sym() { return Symbols().New(); }
884
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000885 /// @param name the symbol string
886 /// @return a Symbol with the given name
887 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
888
889 /// @param sym the symbol
890 /// @return `sym`
891 Symbol Sym(Symbol sym) { return sym; }
892
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000893 /// @param expr the expression
894 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000895 template <typename T>
896 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
897 return expr;
898 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899
Ben Claytonb8ea5912021-04-19 16:52:42 +0000900 /// Passthrough for nullptr
901 /// @return nullptr
902 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
903
Ben Claytonfe0910f2021-05-17 15:51:47 +0000904 /// @param source the source information
905 /// @param symbol the identifier symbol
906 /// @return an ast::IdentifierExpression with the given symbol
907 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
908 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000909 }
910
Ben Clayton46d78d72021-02-10 21:34:26 +0000911 /// @param symbol the identifier symbol
912 /// @return an ast::IdentifierExpression with the given symbol
913 ast::IdentifierExpression* Expr(Symbol symbol) {
914 return create<ast::IdentifierExpression>(symbol);
915 }
916
Ben Claytonfe0910f2021-05-17 15:51:47 +0000917 /// @param source the source information
918 /// @param variable the AST variable
919 /// @return an ast::IdentifierExpression with the variable's symbol
920 ast::IdentifierExpression* Expr(const Source& source,
921 ast::Variable* variable) {
922 return create<ast::IdentifierExpression>(source, variable->symbol());
923 }
924
Ben Claytonb8ea5912021-04-19 16:52:42 +0000925 /// @param variable the AST variable
926 /// @return an ast::IdentifierExpression with the variable's symbol
927 ast::IdentifierExpression* Expr(ast::Variable* variable) {
928 return create<ast::IdentifierExpression>(variable->symbol());
929 }
930
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000931 /// @param source the source information
932 /// @param name the identifier name
933 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000934 ast::IdentifierExpression* Expr(const Source& source, const char* 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
940 ast::IdentifierExpression* Expr(const char* name) {
941 return create<ast::IdentifierExpression>(Symbols().Register(name));
942 }
943
944 /// @param source the source information
945 /// @param name the identifier name
946 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000947 ast::IdentifierExpression* Expr(const Source& source,
948 const std::string& name) {
949 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
950 }
951
952 /// @param name the identifier name
953 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000954 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000955 return create<ast::IdentifierExpression>(Symbols().Register(name));
956 }
957
Ben Claytonfe0910f2021-05-17 15:51:47 +0000958 /// @param source the source information
959 /// @param value the boolean value
960 /// @return a Scalar constructor for the given value
961 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
962 return create<ast::ScalarConstructorExpression>(source, Literal(value));
963 }
964
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000965 /// @param value the boolean value
966 /// @return a Scalar constructor for the given value
967 ast::ScalarConstructorExpression* Expr(bool value) {
968 return create<ast::ScalarConstructorExpression>(Literal(value));
969 }
970
Ben Claytonfe0910f2021-05-17 15:51:47 +0000971 /// @param source the source information
972 /// @param value the float value
973 /// @return a Scalar constructor for the given value
974 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
975 return create<ast::ScalarConstructorExpression>(source, Literal(value));
976 }
977
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000978 /// @param value the float value
979 /// @return a Scalar constructor for the given value
980 ast::ScalarConstructorExpression* Expr(f32 value) {
981 return create<ast::ScalarConstructorExpression>(Literal(value));
982 }
983
Ben Claytonfe0910f2021-05-17 15:51:47 +0000984 /// @param source the source information
985 /// @param value the integer value
986 /// @return a Scalar constructor for the given value
987 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
988 return create<ast::ScalarConstructorExpression>(source, Literal(value));
989 }
990
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000991 /// @param value the integer value
992 /// @return a Scalar constructor for the given value
993 ast::ScalarConstructorExpression* Expr(i32 value) {
994 return create<ast::ScalarConstructorExpression>(Literal(value));
995 }
996
Ben Claytonfe0910f2021-05-17 15:51:47 +0000997 /// @param source the source information
998 /// @param value the unsigned int value
999 /// @return a Scalar constructor for the given value
1000 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1001 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1002 }
1003
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001004 /// @param value the unsigned int value
1005 /// @return a Scalar constructor for the given value
1006 ast::ScalarConstructorExpression* Expr(u32 value) {
1007 return create<ast::ScalarConstructorExpression>(Literal(value));
1008 }
1009
1010 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1011 /// `list`.
1012 /// @param list the list to append too
1013 /// @param arg the arg to create
1014 template <typename ARG>
1015 void Append(ast::ExpressionList& list, ARG&& arg) {
1016 list.emplace_back(Expr(std::forward<ARG>(arg)));
1017 }
1018
1019 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1020 /// then appends them to `list`.
1021 /// @param list the list to append too
1022 /// @param arg0 the first argument
1023 /// @param args the rest of the arguments
1024 template <typename ARG0, typename... ARGS>
1025 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1026 Append(list, std::forward<ARG0>(arg0));
1027 Append(list, std::forward<ARGS>(args)...);
1028 }
1029
1030 /// @return an empty list of expressions
1031 ast::ExpressionList ExprList() { return {}; }
1032
1033 /// @param args the list of expressions
1034 /// @return the list of expressions converted to `ast::Expression`s using
1035 /// `Expr()`,
1036 template <typename... ARGS>
1037 ast::ExpressionList ExprList(ARGS&&... args) {
1038 ast::ExpressionList list;
1039 list.reserve(sizeof...(args));
1040 Append(list, std::forward<ARGS>(args)...);
1041 return list;
1042 }
1043
1044 /// @param list the list of expressions
1045 /// @return `list`
1046 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1047
1048 /// @param val the boolan value
1049 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001050 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001051
1052 /// @param val the float value
1053 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001054 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001055
1056 /// @param val the unsigned int value
1057 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001058 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001059
1060 /// @param val the integer value
1061 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001062 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001063
1064 /// @param args the arguments for the type constructor
1065 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1066 /// of `args` converted to `ast::Expression`s using `Expr()`
1067 template <typename T, typename... ARGS>
1068 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001069 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001070 }
1071
1072 /// @param type the type to construct
1073 /// @param args the arguments for the constructor
1074 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1075 /// values `args`.
1076 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001077 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078 return create<ast::TypeConstructorExpression>(
1079 type, ExprList(std::forward<ARGS>(args)...));
1080 }
1081
1082 /// @param args the arguments for the vector constructor
1083 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1084 /// `T`, constructed with the values `args`.
1085 template <typename T, typename... ARGS>
1086 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001087 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001088 }
1089
1090 /// @param args the arguments for the vector constructor
1091 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1092 /// `T`, constructed with the values `args`.
1093 template <typename T, typename... ARGS>
1094 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001095 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001096 }
1097
1098 /// @param args the arguments for the vector constructor
1099 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1100 /// `T`, constructed with the values `args`.
1101 template <typename T, typename... ARGS>
1102 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001103 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001104 }
1105
1106 /// @param args the arguments for the matrix constructor
1107 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1108 /// `T`, constructed with the values `args`.
1109 template <typename T, typename... ARGS>
1110 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001111 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001112 }
1113
1114 /// @param args the arguments for the matrix constructor
1115 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1116 /// `T`, constructed with the values `args`.
1117 template <typename T, typename... ARGS>
1118 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001119 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001120 }
1121
1122 /// @param args the arguments for the matrix constructor
1123 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1124 /// `T`, constructed with the values `args`.
1125 template <typename T, typename... ARGS>
1126 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001127 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001128 }
1129
1130 /// @param args the arguments for the matrix constructor
1131 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1132 /// `T`, constructed with the values `args`.
1133 template <typename T, typename... ARGS>
1134 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001135 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001136 }
1137
1138 /// @param args the arguments for the matrix constructor
1139 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1140 /// `T`, constructed with the values `args`.
1141 template <typename T, typename... ARGS>
1142 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001143 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001144 }
1145
1146 /// @param args the arguments for the matrix constructor
1147 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1148 /// `T`, constructed with the values `args`.
1149 template <typename T, typename... ARGS>
1150 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001151 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001152 }
1153
1154 /// @param args the arguments for the matrix constructor
1155 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1156 /// `T`, constructed with the values `args`.
1157 template <typename T, typename... ARGS>
1158 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001159 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001160 }
1161
1162 /// @param args the arguments for the matrix constructor
1163 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1164 /// `T`, constructed with the values `args`.
1165 template <typename T, typename... ARGS>
1166 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001167 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001168 }
1169
1170 /// @param args the arguments for the matrix constructor
1171 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1172 /// `T`, constructed with the values `args`.
1173 template <typename T, typename... ARGS>
1174 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001175 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001176 }
1177
1178 /// @param args the arguments for the array constructor
1179 /// @return an `ast::TypeConstructorExpression` of an array with element type
1180 /// `T`, constructed with the values `args`.
1181 template <typename T, int N = 0, typename... ARGS>
1182 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001183 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001184 }
1185
1186 /// @param subtype the array element type
1187 /// @param n the array size. 0 represents a runtime-array.
1188 /// @param args the arguments for the array constructor
1189 /// @return an `ast::TypeConstructorExpression` of an array with element type
1190 /// `subtype`, constructed with the values `args`.
1191 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001192 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001193 uint32_t n,
1194 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001195 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001196 }
1197
1198 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001200 /// @param optional the optional variable settings.
1201 /// Can be any of the following, in any order:
1202 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001203 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001204 /// * ast::Expression* - specifies the variable's initializer expression
1205 /// * ast::DecorationList - specifies the variable's decorations
1206 /// Note that repeated arguments of the same type will use the last argument's
1207 /// value.
1208 /// @returns a `ast::Variable` with the given name, type and additional
1209 /// options
1210 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001211 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001212 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001213 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001214 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1215 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001216 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001217 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001218 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001219
1220 /// @param source the variable source
1221 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001222 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001223 /// @param optional the optional variable settings.
1224 /// Can be any of the following, in any order:
1225 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001226 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001227 /// * ast::Expression* - specifies the variable's initializer expression
1228 /// * ast::DecorationList - specifies the variable's decorations
1229 /// Note that repeated arguments of the same type will use the last argument's
1230 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001232 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001233 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001234 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001235 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001236 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001237 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1238 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001239 opts.storage, opts.access, type, false,
1240 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001241 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001242
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001243 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001244 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001245 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001246 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001247 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001248 template <typename NAME>
1249 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001250 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001251 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001252 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001253 return create<ast::Variable>(
1254 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1255 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001256 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001257
1258 /// @param source the variable source
1259 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001260 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001261 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001263 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001264 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001266 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001267 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001268 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001269 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001270 return create<ast::Variable>(
1271 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1272 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001273 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001274
James Pricedaf1f1c2021-04-08 22:15:48 +00001275 /// @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(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001281 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001282 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001283 return create<ast::Variable>(
1284 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1285 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001286 }
1287
1288 /// @param source the parameter source
1289 /// @param name the parameter name
1290 /// @param type the parameter type
1291 /// @param decorations optional parameter decorations
1292 /// @returns a constant `ast::Variable` with the given name and type
1293 template <typename NAME>
1294 ast::Variable* Param(const Source& source,
1295 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001296 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001297 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001298 return create<ast::Variable>(
1299 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1300 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001301 }
1302
Ben Clayton42708342021-04-21 17:55:12 +00001303 /// @param name the variable name
1304 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001305 /// @param optional the optional variable settings.
1306 /// Can be any of the following, in any order:
1307 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001308 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001309 /// * ast::Expression* - specifies the variable's initializer expression
1310 /// * ast::DecorationList - specifies the variable's decorations
1311 /// Note that repeated arguments of the same type will use the last argument's
1312 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001313 /// @returns a new `ast::Variable`, which is automatically registered as a
1314 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001315 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001316 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001317 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001318 OPTIONAL&&... optional) {
1319 auto* var = Var(std::forward<NAME>(name), type,
1320 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001321 AST().AddGlobalVariable(var);
1322 return var;
1323 }
1324
1325 /// @param source the variable source
1326 /// @param name the variable name
1327 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001328 /// @param optional the optional variable settings.
1329 /// Can be any of the following, in any order:
1330 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001331 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001332 /// * ast::Expression* - specifies the variable's initializer expression
1333 /// * ast::DecorationList - specifies the variable's decorations
1334 /// Note that repeated arguments of the same type will use the last argument's
1335 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001336 /// @returns a new `ast::Variable`, which is automatically registered as a
1337 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001338 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001339 ast::Variable* Global(const Source& source,
1340 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001341 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001342 OPTIONAL&&... optional) {
1343 auto* var = Var(source, std::forward<NAME>(name), type,
1344 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001345 AST().AddGlobalVariable(var);
1346 return var;
1347 }
1348
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001349 /// @param name the variable name
1350 /// @param type the variable type
1351 /// @param constructor constructor expression
1352 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001353 /// @returns a const `ast::Variable` constructed by calling Var() with the
1354 /// arguments of `args`, which is automatically registered as a global
1355 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001356 template <typename NAME>
1357 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001358 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001359 ast::Expression* constructor,
1360 ast::DecorationList decorations = {}) {
1361 auto* var = Const(std::forward<NAME>(name), type, constructor,
1362 std::move(decorations));
1363 AST().AddGlobalVariable(var);
1364 return var;
1365 }
1366
1367 /// @param source the variable source
1368 /// @param name the variable name
1369 /// @param type the variable type
1370 /// @param constructor constructor expression
1371 /// @param decorations optional variable decorations
1372 /// @returns a const `ast::Variable` constructed by calling Var() with the
1373 /// arguments of `args`, which is automatically registered as a global
1374 /// variable with the ast::Module.
1375 template <typename NAME>
1376 ast::Variable* GlobalConst(const Source& source,
1377 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001378 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001379 ast::Expression* constructor,
1380 ast::DecorationList decorations = {}) {
1381 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1382 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001383 AST().AddGlobalVariable(var);
1384 return var;
1385 }
1386
Ben Claytonfe0910f2021-05-17 15:51:47 +00001387 /// @param source the source information
1388 /// @param expr the expression to take the address of
1389 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1390 template <typename EXPR>
1391 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1392 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1393 Expr(std::forward<EXPR>(expr)));
1394 }
1395
1396 /// @param expr the expression to take the address of
1397 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1398 template <typename EXPR>
1399 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1400 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1401 Expr(std::forward<EXPR>(expr)));
1402 }
1403
1404 /// @param source the source information
1405 /// @param expr the expression to perform an indirection on
1406 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1407 template <typename EXPR>
1408 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1409 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1410 Expr(std::forward<EXPR>(expr)));
1411 }
1412
1413 /// @param expr the expression to perform an indirection on
1414 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1415 template <typename EXPR>
1416 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1417 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1418 Expr(std::forward<EXPR>(expr)));
1419 }
1420
Antonio Maiorano14b34032021-06-09 20:17:59 +00001421 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001422 /// @param func the function name
1423 /// @param args the function call arguments
1424 /// @returns a `ast::CallExpression` to the function `func`, with the
1425 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1426 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001427 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1428 return create<ast::CallExpression>(source, Expr(func),
1429 ExprList(std::forward<ARGS>(args)...));
1430 }
1431
1432 /// @param func the function name
1433 /// @param args the function call arguments
1434 /// @returns a `ast::CallExpression` to the function `func`, with the
1435 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1436 template <typename NAME,
1437 typename... ARGS,
1438 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001439 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1440 return create<ast::CallExpression>(Expr(func),
1441 ExprList(std::forward<ARGS>(args)...));
1442 }
1443
Ben Clayton7fe01062021-06-11 13:22:27 +00001444 /// @param expr the expression to ignore
1445 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1446 /// passed the single `expr` argument
1447 template <typename EXPR>
1448 ast::CallStatement* Ignore(EXPR&& expr) {
1449 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1450 }
1451
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001452 /// @param lhs the left hand argument to the addition operation
1453 /// @param rhs the right hand argument to the addition operation
1454 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1455 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001456 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001457 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1458 Expr(std::forward<LHS>(lhs)),
1459 Expr(std::forward<RHS>(rhs)));
1460 }
1461
1462 /// @param lhs the left hand argument to the subtraction operation
1463 /// @param rhs the right hand argument to the subtraction operation
1464 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1465 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001466 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001467 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1468 Expr(std::forward<LHS>(lhs)),
1469 Expr(std::forward<RHS>(rhs)));
1470 }
1471
1472 /// @param lhs the left hand argument to the multiplication operation
1473 /// @param rhs the right hand argument to the multiplication operation
1474 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1475 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001476 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001477 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1478 Expr(std::forward<LHS>(lhs)),
1479 Expr(std::forward<RHS>(rhs)));
1480 }
1481
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001482 /// @param source the source information
1483 /// @param lhs the left hand argument to the multiplication operation
1484 /// @param rhs the right hand argument to the multiplication operation
1485 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1486 template <typename LHS, typename RHS>
1487 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1488 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1489 Expr(std::forward<LHS>(lhs)),
1490 Expr(std::forward<RHS>(rhs)));
1491 }
1492
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001493 /// @param lhs the left hand argument to the division operation
1494 /// @param rhs the right hand argument to the division operation
1495 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1496 template <typename LHS, typename RHS>
1497 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1498 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1499 Expr(std::forward<LHS>(lhs)),
1500 Expr(std::forward<RHS>(rhs)));
1501 }
1502
Ben Clayton0597a2b2021-06-16 09:19:36 +00001503 /// @param source the source information
1504 /// @param arr the array argument for the array accessor expression
1505 /// @param idx the index argument for the array accessor expression
1506 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1507 template <typename ARR, typename IDX>
1508 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1509 ARR&& arr,
1510 IDX&& idx) {
1511 return create<ast::ArrayAccessorExpression>(
1512 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1513 }
1514
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001515 /// @param arr the array argument for the array accessor expression
1516 /// @param idx the index argument for the array accessor expression
1517 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1518 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001519 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001520 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1521 Expr(std::forward<IDX>(idx)));
1522 }
1523
1524 /// @param obj the object for the member accessor expression
1525 /// @param idx the index argument for the array accessor expression
1526 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1527 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001528 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001529 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1530 Expr(std::forward<IDX>(idx)));
1531 }
1532
Ben Clayton42d1e092021-02-02 14:29:15 +00001533 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001534 /// @param val the offset value
1535 /// @returns the offset decoration pointer
1536 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1537 return create<ast::StructMemberOffsetDecoration>(source_, val);
1538 }
1539
Ben Claytond614dd52021-03-15 10:43:11 +00001540 /// Creates a ast::StructMemberSizeDecoration
1541 /// @param source the source information
1542 /// @param val the size value
1543 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001544 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1545 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001546 return create<ast::StructMemberSizeDecoration>(source, val);
1547 }
1548
1549 /// Creates a ast::StructMemberSizeDecoration
1550 /// @param val the size value
1551 /// @returns the size decoration pointer
1552 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1553 return create<ast::StructMemberSizeDecoration>(source_, val);
1554 }
1555
1556 /// Creates a ast::StructMemberAlignDecoration
1557 /// @param source the source information
1558 /// @param val the align value
1559 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001560 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1561 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001562 return create<ast::StructMemberAlignDecoration>(source, val);
1563 }
1564
1565 /// Creates a ast::StructMemberAlignDecoration
1566 /// @param val the align value
1567 /// @returns the align decoration pointer
1568 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1569 return create<ast::StructMemberAlignDecoration>(source_, val);
1570 }
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 source the source information
1574 /// @param name the function name
1575 /// @param params the function parameters
1576 /// @param type the function return type
1577 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001578 /// @param decorations the optional function decorations
1579 /// @param return_type_decorations the optional function return type
1580 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001581 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001582 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001583 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001584 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001585 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001586 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001587 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001588 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001589 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001590 auto* func =
1591 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1592 type, create<ast::BlockStatement>(body),
1593 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001594 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001595 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001596 }
1597
Ben Clayton42d1e092021-02-02 14:29:15 +00001598 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001599 /// @param name the function name
1600 /// @param params the function parameters
1601 /// @param type the function return type
1602 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001603 /// @param decorations the optional function decorations
1604 /// @param return_type_decorations the optional function return type
1605 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001606 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001607 template <typename NAME>
1608 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001609 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001610 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001611 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001612 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001613 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001614 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1615 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001616 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001617 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001618 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001619 }
1620
Ben Clayton49668bb2021-04-17 05:32:01 +00001621 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001622 /// @param source the source information
1623 /// @returns the return statement pointer
1624 ast::ReturnStatement* Return(const Source& source) {
1625 return create<ast::ReturnStatement>(source);
1626 }
1627
1628 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001629 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001630 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1631
1632 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001633 /// @param source the source information
1634 /// @param val the return value
1635 /// @returns the return statement pointer
1636 template <typename EXPR>
1637 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1638 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1639 }
1640
1641 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001642 /// @param val the return value
1643 /// @returns the return statement pointer
1644 template <typename EXPR>
1645 ast::ReturnStatement* Return(EXPR&& val) {
1646 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001647 }
1648
Ben Clayton950809f2021-06-09 14:32:14 +00001649 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001650 /// @param source the source information
1651 /// @param name the alias name
1652 /// @param type the alias target type
1653 /// @returns the alias type
1654 template <typename NAME>
1655 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1656 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001657 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001658 return out;
1659 }
1660
Ben Clayton950809f2021-06-09 14:32:14 +00001661 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001662 /// @param name the alias name
1663 /// @param type the alias target type
1664 /// @returns the alias type
1665 template <typename NAME>
1666 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1667 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001668 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001669 return out;
1670 }
1671
Ben Clayton950809f2021-06-09 14:32:14 +00001672 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001673 /// @param source the source information
1674 /// @param name the struct name
1675 /// @param members the struct members
1676 /// @param decorations the optional struct decorations
1677 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001678 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001679 ast::Struct* Structure(const Source& source,
1680 NAME&& name,
1681 ast::StructMemberList members,
1682 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001683 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001684 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001685 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001686 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001687 return type;
1688 }
1689
Ben Clayton950809f2021-06-09 14:32:14 +00001690 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001691 /// @param name the struct name
1692 /// @param members the struct members
1693 /// @param decorations the optional struct decorations
1694 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001695 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001696 ast::Struct* Structure(NAME&& name,
1697 ast::StructMemberList members,
1698 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001699 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001700 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001701 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001702 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001703 return type;
1704 }
1705
Ben Clayton42d1e092021-02-02 14:29:15 +00001706 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001707 /// @param source the source information
1708 /// @param name the struct member name
1709 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001710 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001711 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001712 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001713 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001714 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001715 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001716 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001717 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1718 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001719 }
1720
Ben Clayton42d1e092021-02-02 14:29:15 +00001721 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001722 /// @param name the struct member name
1723 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001724 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001725 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001726 template <typename NAME>
1727 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001728 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001729 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001730 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1731 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001732 }
1733
Ben Claytonbab31972021-02-08 21:16:21 +00001734 /// Creates a ast::StructMember with the given byte offset
1735 /// @param offset the offset to use in the StructMemberOffsetDecoration
1736 /// @param name the struct member name
1737 /// @param type the struct member type
1738 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001739 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001740 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001741 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001742 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001743 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001744 create<ast::StructMemberOffsetDecoration>(offset),
1745 });
1746 }
1747
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001748 /// Creates a ast::BlockStatement with input statements
1749 /// @param statements statements of block
1750 /// @returns the block statement pointer
1751 template <typename... Statements>
1752 ast::BlockStatement* Block(Statements&&... statements) {
1753 return create<ast::BlockStatement>(
1754 ast::StatementList{std::forward<Statements>(statements)...});
1755 }
1756
1757 /// Creates a ast::ElseStatement with input condition and body
1758 /// @param condition the else condition expression
1759 /// @param body the else body
1760 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001761 template <typename CONDITION>
1762 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1763 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1764 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001765 }
1766
1767 /// Creates a ast::IfStatement with input condition, body, and optional
1768 /// variadic else statements
1769 /// @param condition the if statement condition expression
1770 /// @param body the if statement body
1771 /// @param elseStatements optional variadic else statements
1772 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001773 template <typename CONDITION, typename... ELSE_STATEMENTS>
1774 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001775 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001776 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001777 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001778 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001779 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001780 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001781 }
1782
1783 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001784 /// @param source the source information
1785 /// @param lhs the left hand side expression initializer
1786 /// @param rhs the right hand side expression initializer
1787 /// @returns the assignment statement pointer
1788 template <typename LhsExpressionInit, typename RhsExpressionInit>
1789 ast::AssignmentStatement* Assign(const Source& source,
1790 LhsExpressionInit&& lhs,
1791 RhsExpressionInit&& rhs) {
1792 return create<ast::AssignmentStatement>(
1793 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1794 Expr(std::forward<RhsExpressionInit>(rhs)));
1795 }
1796
1797 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001798 /// @param lhs the left hand side expression initializer
1799 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001800 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001801 template <typename LhsExpressionInit, typename RhsExpressionInit>
1802 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1803 RhsExpressionInit&& rhs) {
1804 return create<ast::AssignmentStatement>(
1805 Expr(std::forward<LhsExpressionInit>(lhs)),
1806 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001807 }
1808
1809 /// Creates a ast::LoopStatement with input body and optional continuing
1810 /// @param body the loop body
1811 /// @param continuing the optional continuing block
1812 /// @returns the loop statement pointer
1813 ast::LoopStatement* Loop(ast::BlockStatement* body,
1814 ast::BlockStatement* continuing = nullptr) {
1815 return create<ast::LoopStatement>(body, continuing);
1816 }
1817
1818 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001819 /// @param source the source information
1820 /// @param var the variable to wrap in a decl statement
1821 /// @returns the variable decl statement pointer
1822 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1823 return create<ast::VariableDeclStatement>(source, var);
1824 }
1825
1826 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001827 /// @param var the variable to wrap in a decl statement
1828 /// @returns the variable decl statement pointer
1829 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1830 return create<ast::VariableDeclStatement>(var);
1831 }
1832
Antonio Maioranocea744d2021-03-25 12:55:27 +00001833 /// Creates a ast::SwitchStatement with input expression and cases
1834 /// @param condition the condition expression initializer
1835 /// @param cases case statements
1836 /// @returns the switch statement pointer
1837 template <typename ExpressionInit, typename... Cases>
1838 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1839 return create<ast::SwitchStatement>(
1840 Expr(std::forward<ExpressionInit>(condition)),
1841 ast::CaseStatementList{std::forward<Cases>(cases)...});
1842 }
1843
1844 /// Creates a ast::CaseStatement with input list of selectors, and body
1845 /// @param selectors list of selectors
1846 /// @param body the case body
1847 /// @returns the case statement pointer
1848 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1849 ast::BlockStatement* body = nullptr) {
1850 return create<ast::CaseStatement>(std::move(selectors),
1851 body ? body : Block());
1852 }
1853
1854 /// Convenient overload that takes a single selector
1855 /// @param selector a single case selector
1856 /// @param body the case body
1857 /// @returns the case statement pointer
1858 ast::CaseStatement* Case(ast::IntLiteral* selector,
1859 ast::BlockStatement* body = nullptr) {
1860 return Case(ast::CaseSelectorList{selector}, body);
1861 }
1862
1863 /// Convenience function that creates a 'default' ast::CaseStatement
1864 /// @param body the case body
1865 /// @returns the case statement pointer
1866 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1867 return Case(ast::CaseSelectorList{}, body);
1868 }
1869
James Price68f558f2021-04-06 15:51:47 +00001870 /// Creates an ast::BuiltinDecoration
1871 /// @param source the source information
1872 /// @param builtin the builtin value
1873 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001874 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001875 return create<ast::BuiltinDecoration>(source, builtin);
1876 }
1877
1878 /// Creates an ast::BuiltinDecoration
1879 /// @param builtin the builtin value
1880 /// @returns the builtin decoration pointer
1881 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1882 return create<ast::BuiltinDecoration>(source_, builtin);
1883 }
1884
1885 /// Creates an ast::LocationDecoration
1886 /// @param source the source information
1887 /// @param location the location value
1888 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001889 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001890 return create<ast::LocationDecoration>(source, location);
1891 }
1892
1893 /// Creates an ast::LocationDecoration
1894 /// @param location the location value
1895 /// @returns the location decoration pointer
1896 ast::LocationDecoration* Location(uint32_t location) {
1897 return create<ast::LocationDecoration>(source_, location);
1898 }
1899
James Pricef2f3bfc2021-05-13 20:32:32 +00001900 /// Creates an ast::OverrideDecoration with a specific constant ID
1901 /// @param source the source information
1902 /// @param id the id value
1903 /// @returns the override decoration pointer
1904 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1905 return create<ast::OverrideDecoration>(source, id);
1906 }
1907
1908 /// Creates an ast::OverrideDecoration with a specific constant ID
1909 /// @param id the optional id value
1910 /// @returns the override decoration pointer
1911 ast::OverrideDecoration* Override(uint32_t id) {
1912 return Override(source_, id);
1913 }
1914
1915 /// Creates an ast::OverrideDecoration without a constant ID
1916 /// @param source the source information
1917 /// @returns the override decoration pointer
1918 ast::OverrideDecoration* Override(const Source& source) {
1919 return create<ast::OverrideDecoration>(source);
1920 }
1921
1922 /// Creates an ast::OverrideDecoration without a constant ID
1923 /// @returns the override decoration pointer
1924 ast::OverrideDecoration* Override() { return Override(source_); }
1925
James Price68f558f2021-04-06 15:51:47 +00001926 /// Creates an ast::StageDecoration
1927 /// @param source the source information
1928 /// @param stage the pipeline stage
1929 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001930 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001931 return create<ast::StageDecoration>(source, stage);
1932 }
1933
1934 /// Creates an ast::StageDecoration
1935 /// @param stage the pipeline stage
1936 /// @returns the stage decoration pointer
1937 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1938 return create<ast::StageDecoration>(source_, stage);
1939 }
1940
James Price70f80bb2021-05-19 13:40:08 +00001941 /// Creates an ast::WorkgroupDecoration
1942 /// @param x the x dimension expression
1943 /// @returns the workgroup decoration pointer
1944 template <typename EXPR_X>
1945 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1946 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1947 }
1948
1949 /// Creates an ast::WorkgroupDecoration
1950 /// @param x the x dimension expression
1951 /// @param y the y dimension expression
1952 /// @returns the workgroup decoration pointer
1953 template <typename EXPR_X, typename EXPR_Y>
1954 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1955 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1956 nullptr);
1957 }
1958
1959 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00001960 /// @param source the source information
1961 /// @param x the x dimension expression
1962 /// @param y the y dimension expression
1963 /// @param z the z dimension expression
1964 /// @returns the workgroup decoration pointer
1965 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1966 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
1967 EXPR_X&& x,
1968 EXPR_Y&& y,
1969 EXPR_Z&& z) {
1970 return create<ast::WorkgroupDecoration>(
1971 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1972 Expr(std::forward<EXPR_Z>(z)));
1973 }
1974
1975 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00001976 /// @param x the x dimension expression
1977 /// @param y the y dimension expression
1978 /// @param z the z dimension expression
1979 /// @returns the workgroup decoration pointer
1980 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1981 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1982 return create<ast::WorkgroupDecoration>(
1983 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1984 Expr(std::forward<EXPR_Z>(z)));
1985 }
1986
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001987 /// Sets the current builder source to `src`
1988 /// @param src the Source used for future create() calls
1989 void SetSource(const Source& src) {
1990 AssertNotMoved();
1991 source_ = src;
1992 }
1993
1994 /// Sets the current builder source to `loc`
1995 /// @param loc the Source used for future create() calls
1996 void SetSource(const Source::Location& loc) {
1997 AssertNotMoved();
1998 source_ = Source(loc);
1999 }
2000
Ben Clayton33352542021-01-29 16:43:41 +00002001 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002002 /// @note As the Resolver is run when the Program is built, this will only be
2003 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002004 /// @param expr the AST expression
2005 /// @return the resolved semantic type for the expression, or nullptr if the
2006 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002007 sem::Type* TypeOf(const ast::Expression* expr) const;
2008
Ben Claytonfe0910f2021-05-17 15:51:47 +00002009 /// Helper for returning the resolved semantic type of the variable `var`.
2010 /// @note As the Resolver is run when the Program is built, this will only be
2011 /// useful for the Resolver itself and tests that use their own Resolver.
2012 /// @param var the AST variable
2013 /// @return the resolved semantic type for the variable, or nullptr if the
2014 /// variable has no resolved type.
2015 sem::Type* TypeOf(const ast::Variable* var) const;
2016
Ben Claytonfbec46f2021-04-30 20:20:19 +00002017 /// Helper for returning the resolved semantic type of the AST type `type`.
2018 /// @note As the Resolver is run when the Program is built, this will only be
2019 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002020 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002021 /// @return the resolved semantic type for the type, or nullptr if the type
2022 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002023 const sem::Type* TypeOf(const ast::Type* type) const;
2024
2025 /// Helper for returning the resolved semantic type of the AST type
2026 /// declaration `type_decl`.
2027 /// @note As the Resolver is run when the Program is built, this will only be
2028 /// useful for the Resolver itself and tests that use their own Resolver.
2029 /// @param type_decl the AST type declaration
2030 /// @return the resolved semantic type for the type declaration, or nullptr if
2031 /// the type declaration has no resolved type.
2032 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002033
Ben Clayton169512e2021-04-17 05:52:11 +00002034 /// Wraps the ast::Literal in a statement. This is used by tests that
2035 /// construct a partial AST and require the Resolver to reach these
2036 /// nodes.
2037 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2038 /// @return the ast::Statement that wraps the ast::Statement
2039 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002040 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002041 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002042 /// nodes.
2043 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2044 /// @return the ast::Statement that wraps the ast::Expression
2045 ast::Statement* WrapInStatement(ast::Expression* expr);
2046 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002047 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002048 /// these nodes.
2049 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2050 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2051 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2052 /// Returns the statement argument. Used as a passthrough-overload by
2053 /// WrapInFunction().
2054 /// @param stmt the ast::Statement
2055 /// @return `stmt`
2056 ast::Statement* WrapInStatement(ast::Statement* stmt);
2057 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002058 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002059 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002060 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002061 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002062 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002063 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002064 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002065 }
2066 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002067 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002068 /// @returns the function
2069 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002070
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002071 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002072 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002073
2074 protected:
2075 /// Asserts that the builder has not been moved.
2076 void AssertNotMoved() const;
2077
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002078 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002079 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002080 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002081 ASTNodeAllocator ast_nodes_;
2082 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002083 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002084 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002085 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002086 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002087
2088 /// The source to use when creating AST nodes without providing a Source as
2089 /// the first argument.
2090 Source source_;
2091
Ben Clayton5f0ea112021-03-09 10:54:37 +00002092 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002093 /// program when built.
2094 bool resolve_on_build_ = true;
2095
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002096 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2097 bool moved_ = false;
2098};
2099
2100//! @cond Doxygen_Suppress
2101// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2102template <>
2103struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002104 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002105 return t->i32();
2106 }
2107};
2108template <>
2109struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002110 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002111 return t->u32();
2112 }
2113};
2114template <>
2115struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002116 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002117 return t->f32();
2118 }
2119};
2120template <>
2121struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002122 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002123 return t->bool_();
2124 }
2125};
2126template <>
2127struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002128 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002129 return t->void_();
2130 }
2131};
2132//! @endcond
2133
Ben Clayton917b14b2021-04-19 16:50:23 +00002134/// @param builder the ProgramBuilder
2135/// @returns the ProgramID of the ProgramBuilder
2136inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2137 return builder->ID();
2138}
2139
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002140} // namespace tint
2141
2142#endif // SRC_PROGRAM_BUILDER_H_