blob: 4a9c3d0272c989c1bb2f20f56bf0f33bfdb60516 [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000026#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/bool_literal.h"
28#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000029#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000030#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000031#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000032#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000035#include "src/ast/if_statement.h"
36#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000037#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/member_accessor_expression.h"
39#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000040#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000041#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000043#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/scalar_constructor_expression.h"
46#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000047#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000049#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000050#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000053#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000055#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000056#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000058#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000059#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000060#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000061#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000062#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000064#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000065#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000066#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000067#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000068#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000069#include "src/sem/f32_type.h"
70#include "src/sem/i32_type.h"
71#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000073#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/sampled_texture_type.h"
75#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000076#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000077#include "src/sem/u32_type.h"
78#include "src/sem/vector_type.h"
79#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000080
Ben Clayton03d10722021-06-05 12:45:50 +000081#ifdef INCLUDE_TINT_TINT_H_
82#error "internal tint header being #included from tint.h"
83#endif
84
Ben Claytona6b9a8e2021-01-26 16:57:10 +000085namespace tint {
86
Ben Clayton401b96b2021-02-03 17:19:59 +000087// Forward declarations
88namespace ast {
89class VariableDeclStatement;
90} // namespace ast
91
Ben Claytona6b9a8e2021-01-26 16:57:10 +000092class CloneContext;
93
94/// ProgramBuilder is a mutable builder for a Program.
95/// To construct a Program, populate the builder and then `std::move` it to a
96/// Program.
97class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +000098 /// VarOptionals is a helper for accepting a number of optional, extra
99 /// arguments for Var() and Global().
100 struct VarOptionals {
101 template <typename... ARGS>
102 explicit VarOptionals(ARGS&&... args) {
103 Apply(std::forward<ARGS>(args)...);
104 }
105 ~VarOptionals();
106
107 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000108 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000109 ast::Expression* constructor = nullptr;
110 ast::DecorationList decorations = {};
111
112 private:
113 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000114 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000115 void Set(ast::Expression* c) { constructor = c; }
116 void Set(const ast::DecorationList& l) { decorations = l; }
117
118 template <typename FIRST, typename... ARGS>
119 void Apply(FIRST&& first, ARGS&&... args) {
120 Set(std::forward<FIRST>(first));
121 Apply(std::forward<ARGS>(args)...);
122 }
123 void Apply() {}
124 };
125
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000126 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000127 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
128 using ASTNodeAllocator = BlockAllocator<ast::Node>;
129
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000130 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
131 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000132
133 /// `i32` is a type alias to `int`.
134 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
135 /// WGSL syntax.
136 /// Note: this is intentionally not aliased to uint32_t as we want integer
137 /// literals passed to the builder to match WGSL's integer literal types.
138 using i32 = decltype(1);
139 /// `u32` is a type alias to `unsigned int`.
140 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
141 /// WGSL syntax.
142 /// Note: this is intentionally not aliased to uint32_t as we want integer
143 /// literals passed to the builder to match WGSL's integer literal types.
144 using u32 = decltype(1u);
145 /// `f32` is a type alias to `float`
146 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
147 /// WGSL syntax.
148 using f32 = float;
149
150 /// Constructor
151 ProgramBuilder();
152
153 /// Move constructor
154 /// @param rhs the builder to move
155 ProgramBuilder(ProgramBuilder&& rhs);
156
157 /// Destructor
158 virtual ~ProgramBuilder();
159
160 /// Move assignment operator
161 /// @param rhs the builder to move
162 /// @return this builder
163 ProgramBuilder& operator=(ProgramBuilder&& rhs);
164
Ben Claytone43c8302021-01-29 11:59:32 +0000165 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
166 /// making a deep clone of the Program contents.
167 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
168 /// existing immutable program.
169 /// As the returned ProgramBuilder wraps `program`, `program` must not be
170 /// destructed or assigned while using the returned ProgramBuilder.
171 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
172 /// function. See crbug.com/tint/460.
173 /// @param program the immutable Program to wrap
174 /// @return the ProgramBuilder that wraps `program`
175 static ProgramBuilder Wrap(const Program* program);
176
Ben Claytone6995de2021-04-13 23:27:27 +0000177 /// @returns the unique identifier for this program
178 ProgramID ID() const { return id_; }
179
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000180 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000181 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000182 AssertNotMoved();
183 return types_;
184 }
185
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000186 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000187 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000188 AssertNotMoved();
189 return types_;
190 }
191
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000192 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000193 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000194 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000195 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000196 }
197
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000198 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000199 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000200 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000201 return ast_nodes_;
202 }
203
204 /// @returns a reference to the program's semantic nodes storage
205 SemNodeAllocator& SemNodes() {
206 AssertNotMoved();
207 return sem_nodes_;
208 }
209
210 /// @returns a reference to the program's semantic nodes storage
211 const SemNodeAllocator& SemNodes() const {
212 AssertNotMoved();
213 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000214 }
215
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000216 /// @returns a reference to the program's AST root Module
217 ast::Module& AST() {
218 AssertNotMoved();
219 return *ast_;
220 }
221
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000222 /// @returns a reference to the program's AST root Module
223 const ast::Module& AST() const {
224 AssertNotMoved();
225 return *ast_;
226 }
227
228 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000229 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000230 AssertNotMoved();
231 return sem_;
232 }
233
234 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000235 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000236 AssertNotMoved();
237 return sem_;
238 }
239
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000240 /// @returns a reference to the program's SymbolTable
241 SymbolTable& Symbols() {
242 AssertNotMoved();
243 return symbols_;
244 }
245
Ben Clayton708dc2d2021-01-29 11:22:40 +0000246 /// @returns a reference to the program's SymbolTable
247 const SymbolTable& Symbols() const {
248 AssertNotMoved();
249 return symbols_;
250 }
251
Ben Clayton844217f2021-01-27 18:49:05 +0000252 /// @returns a reference to the program's diagnostics
253 diag::List& Diagnostics() {
254 AssertNotMoved();
255 return diagnostics_;
256 }
257
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000258 /// @returns a reference to the program's diagnostics
259 const diag::List& Diagnostics() const {
260 AssertNotMoved();
261 return diagnostics_;
262 }
263
Ben Clayton5f0ea112021-03-09 10:54:37 +0000264 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000265 /// @param enable the new flag value (defaults to true)
266 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
267
Ben Clayton5f0ea112021-03-09 10:54:37 +0000268 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000269 /// built.
270 bool ResolveOnBuild() const { return resolve_on_build_; }
271
Ben Clayton844217f2021-01-27 18:49:05 +0000272 /// @returns true if the program has no error diagnostics and is not missing
273 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000274 bool IsValid() const;
275
Ben Clayton708dc2d2021-01-29 11:22:40 +0000276 /// Writes a representation of the node to the output stream
277 /// @note unlike str(), to_str() does not automatically demangle the string.
278 /// @param node the AST node
279 /// @param out the stream to write to
280 /// @param indent number of spaces to indent the node when writing
281 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
282 node->to_str(Sem(), out, indent);
283 }
284
285 /// Returns a demangled, string representation of `node`.
286 /// @param node the AST node
287 /// @returns a string representation of the node
288 std::string str(const ast::Node* node) const;
289
Ben Clayton7fdfff12021-01-29 15:17:30 +0000290 /// Creates a new ast::Node owned by the ProgramBuilder. When the
291 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000292 /// @param source the Source of the node
293 /// @param args the arguments to pass to the type constructor
294 /// @returns the node pointer
295 template <typename T, typename... ARGS>
296 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
297 ARGS&&... args) {
298 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000299 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000300 }
301
Ben Clayton7fdfff12021-01-29 15:17:30 +0000302 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
303 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000304 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000305 /// When the ProgramBuilder is destructed, the ast::Node will also be
306 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000307 /// @returns the node pointer
308 template <typename T>
309 traits::EnableIfIsType<T, ast::Node>* create() {
310 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000311 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000312 }
313
Ben Clayton7fdfff12021-01-29 15:17:30 +0000314 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
315 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000316 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000317 /// When the ProgramBuilder is destructed, the ast::Node will also be
318 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000319 /// @param arg0 the first arguments to pass to the type constructor
320 /// @param args the remaining arguments to pass to the type constructor
321 /// @returns the node pointer
322 template <typename T, typename ARG0, typename... ARGS>
323 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
324 traits::IsTypeOrDerived<T, ast::Node>::value &&
325 !traits::IsTypeOrDerived<ARG0, Source>::value,
326 T>*
327 create(ARG0&& arg0, ARGS&&... args) {
328 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000329 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000330 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000331 }
332
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000333 /// Creates a new sem::Node owned by the ProgramBuilder.
334 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000335 /// destructed.
336 /// @param args the arguments to pass to the type constructor
337 /// @returns the node pointer
338 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000339 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
340 !traits::IsTypeOrDerived<T, sem::Type>::value,
341 T>*
342 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000343 AssertNotMoved();
344 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
345 }
346
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000347 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000348 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
349 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000350 /// Types are unique (de-aliased), and so calling create() for the same `T`
351 /// and arguments will return the same pointer.
352 /// @warning Use this method to acquire a type only if all of its type
353 /// information is provided in the constructor arguments `args`.<br>
354 /// If the type requires additional configuration after construction that
355 /// affect its fundamental type, build the type with `std::make_unique`, make
356 /// any necessary alterations and then call unique_type() instead.
357 /// @param args the arguments to pass to the type constructor
358 /// @returns the de-aliased type pointer
359 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000360 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
361 static_assert(std::is_base_of<sem::Type, T>::value,
362 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000363 AssertNotMoved();
364 return types_.Get<T>(std::forward<ARGS>(args)...);
365 }
366
367 /// Marks this builder as moved, preventing any further use of the builder.
368 void MarkAsMoved();
369
370 //////////////////////////////////////////////////////////////////////////////
371 // TypesBuilder
372 //////////////////////////////////////////////////////////////////////////////
373
374 /// TypesBuilder holds basic `tint` types and methods for constructing
375 /// complex types.
376 class TypesBuilder {
377 public:
378 /// Constructor
379 /// @param builder the program builder
380 explicit TypesBuilder(ProgramBuilder* builder);
381
382 /// @return the tint AST type for the C type `T`.
383 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000384 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000385 return CToAST<T>::get(this);
386 }
387
388 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000389 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000390
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000391 /// @param source the Source of the node
392 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000393 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000394 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000395 }
396
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000397 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000398 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000399
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000400 /// @param source the Source of the node
401 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000402 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000403 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000404 }
405
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000406 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000407 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000408
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000409 /// @param source the Source of the node
410 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000411 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000412 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000413 }
414
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000415 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000416 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000417
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000418 /// @param source the Source of the node
419 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000420 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000421 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000422 }
423
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000424 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000425 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000426
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000427 /// @param source the Source of the node
428 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000429 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000430 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000431 }
432
Antonio Maiorano25436862021-04-13 13:32:33 +0000433 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000434 /// @param n vector width in elements
435 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000436 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000437 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000438 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000439 }
440
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000441 /// @param source the Source of the node
442 /// @param type vector subtype
443 /// @param n vector width in elements
444 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000445 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000446 type = MaybeCreateTypename(type);
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 Clayton0f88b312021-05-04 17:36:31 +0000492 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000493 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000494 }
495
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000496 /// @param source the Source of the node
497 /// @param type matrix subtype
498 /// @param columns number of columns for the matrix
499 /// @param rows number of rows for the matrix
500 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000501 ast::Matrix* mat(const Source& source,
502 ast::Type* type,
503 uint32_t columns,
504 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000505 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000506 }
507
Ben Claytone204f272021-04-22 14:40:23 +0000508 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000509 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000510 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000511
512 /// @param type matrix subtype
513 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000514 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000515
516 /// @param type matrix subtype
517 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000518 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000519
520 /// @param type matrix subtype
521 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000522 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000523
524 /// @param type matrix subtype
525 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000526 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000527
528 /// @param type matrix subtype
529 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000530 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000531
532 /// @param type matrix subtype
533 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000534 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000535
536 /// @param type matrix subtype
537 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000538 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000539
540 /// @param type matrix subtype
541 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000542 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000543
Ben Claytone204f272021-04-22 14:40:23 +0000544 /// @param columns number of columns for the matrix
545 /// @param rows number of rows for the matrix
546 /// @return the tint AST type for a matrix of `type`
547 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000548 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000549 return mat(Of<T>(), columns, rows);
550 }
551
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000552 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
553 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000554 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000555 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000556 }
557
558 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
559 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000560 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000561 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000562 }
563
564 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
565 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000566 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000567 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000568 }
569
570 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
571 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000572 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000573 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000574 }
575
576 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
577 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000578 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000579 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000580 }
581
582 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
583 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000584 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000585 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000586 }
587
588 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
589 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000590 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000591 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000592 }
593
594 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
595 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000596 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000597 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000598 }
599
600 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
601 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000602 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000603 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000604 }
605
606 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000607 /// @param n the array size. 0 represents a runtime-array
608 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000609 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000610 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000611 uint32_t n = 0,
612 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000613 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000614 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000615 }
616
617 /// @param source the Source of the node
618 /// @param subtype the array element type
619 /// @param n the array size. 0 represents a runtime-array
620 /// @param decos the optional decorations for the array
621 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000622 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000623 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000624 uint32_t n = 0,
625 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000626 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000627 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000628 }
629
Ben Claytonbab31972021-02-08 21:16:21 +0000630 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000631 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000632 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000633 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000634 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000635 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000636 ast::DecorationList decos;
637 if (stride) {
638 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
639 }
640 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000641 }
642
Ben Clayton0f88b312021-05-04 17:36:31 +0000643 /// @param source the Source of the node
644 /// @param subtype the array element type
645 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000646 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000647 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000648 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000649 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000650 uint32_t n,
651 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000652 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000653 ast::DecorationList decos;
654 if (stride) {
655 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
656 }
657 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000658 }
659
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000660 /// @return the tint AST type for an array of size `N` of type `T`
661 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000662 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000663 return array(Of<T>(), N);
664 }
665
Ben Claytonbab31972021-02-08 21:16:21 +0000666 /// @param stride the array stride
667 /// @return the tint AST type for an array of size `N` of type `T`
668 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000669 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000670 return array(Of<T>(), N, stride);
671 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000672
Ben Clayton0f88b312021-05-04 17:36:31 +0000673 /// Creates a type name
674 /// @param name the name
675 /// @returns the type name
676 template <typename NAME>
677 ast::TypeName* type_name(NAME&& name) const {
678 return builder->create<ast::TypeName>(
679 builder->Sym(std::forward<NAME>(name)));
680 }
681
682 /// Creates a type name
683 /// @param source the Source of the node
684 /// @param name the name
685 /// @returns the type name
686 template <typename NAME>
687 ast::TypeName* type_name(const Source& source, NAME&& name) const {
688 return builder->create<ast::TypeName>(
689 source, builder->Sym(std::forward<NAME>(name)));
690 }
691
Ben Clayton42d1e092021-02-02 14:29:15 +0000692 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000693 /// @param name the alias name
694 /// @param type the alias type
695 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000696 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000697 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000698 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000699 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000700 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000701 }
702
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000703 /// Creates an alias type
704 /// @param source the Source of the node
705 /// @param name the alias name
706 /// @param type the alias type
707 /// @returns the alias pointer
708 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000709 ast::Alias* alias(const Source& source,
710 NAME&& name,
711 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000712 type = MaybeCreateTypename(type);
713 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000714 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000715 }
716
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000717 /// @param type the type of the pointer
718 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000719 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000720 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000721 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000722 ast::StorageClass storage_class,
723 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000724 type = MaybeCreateTypename(type);
Ben Clayton18588542021-06-04 22:17:37 +0000725 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000726 }
727
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000728 /// @param source the Source of the node
729 /// @param type the type of the pointer
730 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000731 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000732 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000733 ast::Pointer* pointer(const Source& source,
734 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000735 ast::StorageClass storage_class,
736 ast::Access access = ast::Access::kUndefined) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000737 type = MaybeCreateTypename(type);
Ben Clayton18588542021-06-04 22:17:37 +0000738 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000739 }
740
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000741 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000742 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000743 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000744 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000745 ast::Pointer* pointer(ast::StorageClass storage_class,
746 ast::Access access = ast::Access::kUndefined) const {
747 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000748 }
749
Ben Claytone204f272021-04-22 14:40:23 +0000750 /// @param kind the kind of sampler
751 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000752 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000753 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000754 }
755
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000756 /// @param source the Source of the node
757 /// @param kind the kind of sampler
758 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000759 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000760 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000761 }
762
Ben Claytone204f272021-04-22 14:40:23 +0000763 /// @param dims the dimensionality of the texture
764 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000765 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000766 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000767 }
768
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000769 /// @param source the Source of the node
770 /// @param dims the dimensionality of the texture
771 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000772 ast::DepthTexture* depth_texture(const Source& source,
773 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000774 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000775 }
776
Ben Claytone204f272021-04-22 14:40:23 +0000777 /// @param dims the dimensionality of the texture
778 /// @param subtype the texture subtype.
779 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000780 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
781 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000782 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000783 }
784
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000785 /// @param source the Source of the node
786 /// @param dims the dimensionality of the texture
787 /// @param subtype the texture subtype.
788 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000789 ast::SampledTexture* sampled_texture(const Source& source,
790 ast::TextureDimension dims,
791 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000792 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000793 }
794
Ben Claytone204f272021-04-22 14:40:23 +0000795 /// @param dims the dimensionality of the texture
796 /// @param subtype the texture subtype.
797 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000798 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
799 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000800 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000801 }
802
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000803 /// @param source the Source of the node
804 /// @param dims the dimensionality of the texture
805 /// @param subtype the texture subtype.
806 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000807 ast::MultisampledTexture* multisampled_texture(const Source& source,
808 ast::TextureDimension dims,
809 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000810 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000811 }
812
Ben Claytone204f272021-04-22 14:40:23 +0000813 /// @param dims the dimensionality of the texture
814 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000815 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000816 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000817 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000818 ast::ImageFormat format,
819 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000820 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000821 return builder->create<ast::StorageTexture>(dims, format, subtype,
822 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000823 }
824
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000825 /// @param source the Source of the node
826 /// @param dims the dimensionality of the texture
827 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000828 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000829 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000830 ast::StorageTexture* storage_texture(const Source& source,
831 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000832 ast::ImageFormat format,
833 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000834 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000835 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
836 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000837 }
838
Brandon Jones7b257692021-05-17 17:40:17 +0000839 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000840 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000841 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000842 }
843
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000844 /// @param source the Source of the node
845 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000846 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000847 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000848 }
849
Ben Clayton19b03192021-05-20 15:04:08 +0000850 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
851 /// If ty is a ast::Struct or ast::Alias, the returned type is an
852 /// ast::TypeName of the given type's name, otherwise type is returned.
853 /// @param type the type
854 /// @return either type or a pointer to a new ast::TypeName
855 ast::Type* MaybeCreateTypename(ast::Type* type) const;
856
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000857 /// If ty is a ast::Struct or ast::Alias, the returned type is an
858 /// ast::TypeName of the given type's name, otherwise type is returned.
859 /// @param type the type
860 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton19b03192021-05-20 15:04:08 +0000861 const ast::Type* MaybeCreateTypename(const ast::Type* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000862
Ben Claytonf5f311e2021-04-28 14:31:23 +0000863 /// The ProgramBuilder
864 ProgramBuilder* const builder;
865
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000866 private:
867 /// CToAST<T> is specialized for various `T` types and each specialization
868 /// contains a single static `get()` method for obtaining the corresponding
869 /// AST type for the C type `T`.
870 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000871 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000872 template <typename T>
873 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000874 };
875
876 //////////////////////////////////////////////////////////////////////////////
877 // AST helper methods
878 //////////////////////////////////////////////////////////////////////////////
879
James Price65ae64d2021-04-29 12:59:14 +0000880 /// @return a new unnamed symbol
881 Symbol Sym() { return Symbols().New(); }
882
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000883 /// @param name the symbol string
884 /// @return a Symbol with the given name
885 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
886
887 /// @param sym the symbol
888 /// @return `sym`
889 Symbol Sym(Symbol sym) { return sym; }
890
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000891 /// @param expr the expression
892 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000893 template <typename T>
894 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
895 return expr;
896 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000897
Ben Claytonb8ea5912021-04-19 16:52:42 +0000898 /// Passthrough for nullptr
899 /// @return nullptr
900 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
901
Ben Claytonfe0910f2021-05-17 15:51:47 +0000902 /// @param source the source information
903 /// @param symbol the identifier symbol
904 /// @return an ast::IdentifierExpression with the given symbol
905 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
906 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000907 }
908
Ben Clayton46d78d72021-02-10 21:34:26 +0000909 /// @param symbol the identifier symbol
910 /// @return an ast::IdentifierExpression with the given symbol
911 ast::IdentifierExpression* Expr(Symbol symbol) {
912 return create<ast::IdentifierExpression>(symbol);
913 }
914
Ben Claytonfe0910f2021-05-17 15:51:47 +0000915 /// @param source the source information
916 /// @param variable the AST variable
917 /// @return an ast::IdentifierExpression with the variable's symbol
918 ast::IdentifierExpression* Expr(const Source& source,
919 ast::Variable* variable) {
920 return create<ast::IdentifierExpression>(source, variable->symbol());
921 }
922
Ben Claytonb8ea5912021-04-19 16:52:42 +0000923 /// @param variable the AST variable
924 /// @return an ast::IdentifierExpression with the variable's symbol
925 ast::IdentifierExpression* Expr(ast::Variable* variable) {
926 return create<ast::IdentifierExpression>(variable->symbol());
927 }
928
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000929 /// @param source the source information
930 /// @param name the identifier name
931 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000932 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
933 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
934 }
935
936 /// @param name the identifier name
937 /// @return an ast::IdentifierExpression with the given name
938 ast::IdentifierExpression* Expr(const char* name) {
939 return create<ast::IdentifierExpression>(Symbols().Register(name));
940 }
941
942 /// @param source the source information
943 /// @param name the identifier name
944 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000945 ast::IdentifierExpression* Expr(const Source& source,
946 const std::string& name) {
947 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
948 }
949
950 /// @param name the identifier name
951 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000952 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000953 return create<ast::IdentifierExpression>(Symbols().Register(name));
954 }
955
Ben Claytonfe0910f2021-05-17 15:51:47 +0000956 /// @param source the source information
957 /// @param value the boolean value
958 /// @return a Scalar constructor for the given value
959 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
960 return create<ast::ScalarConstructorExpression>(source, Literal(value));
961 }
962
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000963 /// @param value the boolean value
964 /// @return a Scalar constructor for the given value
965 ast::ScalarConstructorExpression* Expr(bool value) {
966 return create<ast::ScalarConstructorExpression>(Literal(value));
967 }
968
Ben Claytonfe0910f2021-05-17 15:51:47 +0000969 /// @param source the source information
970 /// @param value the float value
971 /// @return a Scalar constructor for the given value
972 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
973 return create<ast::ScalarConstructorExpression>(source, Literal(value));
974 }
975
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000976 /// @param value the float value
977 /// @return a Scalar constructor for the given value
978 ast::ScalarConstructorExpression* Expr(f32 value) {
979 return create<ast::ScalarConstructorExpression>(Literal(value));
980 }
981
Ben Claytonfe0910f2021-05-17 15:51:47 +0000982 /// @param source the source information
983 /// @param value the integer value
984 /// @return a Scalar constructor for the given value
985 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
986 return create<ast::ScalarConstructorExpression>(source, Literal(value));
987 }
988
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000989 /// @param value the integer value
990 /// @return a Scalar constructor for the given value
991 ast::ScalarConstructorExpression* Expr(i32 value) {
992 return create<ast::ScalarConstructorExpression>(Literal(value));
993 }
994
Ben Claytonfe0910f2021-05-17 15:51:47 +0000995 /// @param source the source information
996 /// @param value the unsigned int value
997 /// @return a Scalar constructor for the given value
998 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
999 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1000 }
1001
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001002 /// @param value the unsigned int value
1003 /// @return a Scalar constructor for the given value
1004 ast::ScalarConstructorExpression* Expr(u32 value) {
1005 return create<ast::ScalarConstructorExpression>(Literal(value));
1006 }
1007
1008 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1009 /// `list`.
1010 /// @param list the list to append too
1011 /// @param arg the arg to create
1012 template <typename ARG>
1013 void Append(ast::ExpressionList& list, ARG&& arg) {
1014 list.emplace_back(Expr(std::forward<ARG>(arg)));
1015 }
1016
1017 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1018 /// then appends them to `list`.
1019 /// @param list the list to append too
1020 /// @param arg0 the first argument
1021 /// @param args the rest of the arguments
1022 template <typename ARG0, typename... ARGS>
1023 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1024 Append(list, std::forward<ARG0>(arg0));
1025 Append(list, std::forward<ARGS>(args)...);
1026 }
1027
1028 /// @return an empty list of expressions
1029 ast::ExpressionList ExprList() { return {}; }
1030
1031 /// @param args the list of expressions
1032 /// @return the list of expressions converted to `ast::Expression`s using
1033 /// `Expr()`,
1034 template <typename... ARGS>
1035 ast::ExpressionList ExprList(ARGS&&... args) {
1036 ast::ExpressionList list;
1037 list.reserve(sizeof...(args));
1038 Append(list, std::forward<ARGS>(args)...);
1039 return list;
1040 }
1041
1042 /// @param list the list of expressions
1043 /// @return `list`
1044 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1045
1046 /// @param val the boolan value
1047 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001048 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001049
1050 /// @param val the float value
1051 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001052 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001053
1054 /// @param val the unsigned int value
1055 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001056 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001057
1058 /// @param val the integer value
1059 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001060 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001061
1062 /// @param args the arguments for the type constructor
1063 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1064 /// of `args` converted to `ast::Expression`s using `Expr()`
1065 template <typename T, typename... ARGS>
1066 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001067 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001068 }
1069
1070 /// @param type the type to construct
1071 /// @param args the arguments for the constructor
1072 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1073 /// values `args`.
1074 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001075 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001076 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001077 return create<ast::TypeConstructorExpression>(
1078 type, ExprList(std::forward<ARGS>(args)...));
1079 }
1080
1081 /// @param args the arguments for the vector constructor
1082 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1083 /// `T`, constructed with the values `args`.
1084 template <typename T, typename... ARGS>
1085 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001086 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001087 }
1088
1089 /// @param args the arguments for the vector constructor
1090 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1091 /// `T`, constructed with the values `args`.
1092 template <typename T, typename... ARGS>
1093 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001094 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001095 }
1096
1097 /// @param args the arguments for the vector constructor
1098 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1099 /// `T`, constructed with the values `args`.
1100 template <typename T, typename... ARGS>
1101 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001102 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001103 }
1104
1105 /// @param args the arguments for the matrix constructor
1106 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1107 /// `T`, constructed with the values `args`.
1108 template <typename T, typename... ARGS>
1109 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001110 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001111 }
1112
1113 /// @param args the arguments for the matrix constructor
1114 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1115 /// `T`, constructed with the values `args`.
1116 template <typename T, typename... ARGS>
1117 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001118 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 }
1120
1121 /// @param args the arguments for the matrix constructor
1122 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1123 /// `T`, constructed with the values `args`.
1124 template <typename T, typename... ARGS>
1125 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001126 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 }
1128
1129 /// @param args the arguments for the matrix constructor
1130 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1131 /// `T`, constructed with the values `args`.
1132 template <typename T, typename... ARGS>
1133 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001134 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001135 }
1136
1137 /// @param args the arguments for the matrix constructor
1138 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1139 /// `T`, constructed with the values `args`.
1140 template <typename T, typename... ARGS>
1141 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001142 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001143 }
1144
1145 /// @param args the arguments for the matrix constructor
1146 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1147 /// `T`, constructed with the values `args`.
1148 template <typename T, typename... ARGS>
1149 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001150 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001151 }
1152
1153 /// @param args the arguments for the matrix constructor
1154 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1155 /// `T`, constructed with the values `args`.
1156 template <typename T, typename... ARGS>
1157 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001158 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001159 }
1160
1161 /// @param args the arguments for the matrix constructor
1162 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1163 /// `T`, constructed with the values `args`.
1164 template <typename T, typename... ARGS>
1165 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001166 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001167 }
1168
1169 /// @param args the arguments for the matrix constructor
1170 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1171 /// `T`, constructed with the values `args`.
1172 template <typename T, typename... ARGS>
1173 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001174 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001175 }
1176
1177 /// @param args the arguments for the array constructor
1178 /// @return an `ast::TypeConstructorExpression` of an array with element type
1179 /// `T`, constructed with the values `args`.
1180 template <typename T, int N = 0, typename... ARGS>
1181 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001182 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001183 }
1184
1185 /// @param subtype the array element type
1186 /// @param n the array size. 0 represents a runtime-array.
1187 /// @param args the arguments for the array constructor
1188 /// @return an `ast::TypeConstructorExpression` of an array with element type
1189 /// `subtype`, constructed with the values `args`.
1190 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001191 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001192 uint32_t n,
1193 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001194 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001195 }
1196
1197 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001198 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001199 /// @param optional the optional variable settings.
1200 /// Can be any of the following, in any order:
1201 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001202 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001203 /// * ast::Expression* - specifies the variable's initializer expression
1204 /// * ast::DecorationList - specifies the variable's decorations
1205 /// Note that repeated arguments of the same type will use the last argument's
1206 /// value.
1207 /// @returns a `ast::Variable` with the given name, type and additional
1208 /// options
1209 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001210 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001211 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001212 OPTIONAL&&... optional) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001213 type = ty.MaybeCreateTypename(type);
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 Clayton3b3cf5f2021-04-23 15:41:34 +00001237 type = ty.MaybeCreateTypename(type);
Ben Clayton620d77e2021-06-04 19:55:08 +00001238 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1239 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001240 opts.storage, opts.access, type, false,
1241 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001242 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001243
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001244 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001245 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001246 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001247 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001248 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001249 template <typename NAME>
1250 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001251 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001252 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001253 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001254 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001255 return create<ast::Variable>(
1256 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1257 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001258 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001259
1260 /// @param source the variable source
1261 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001263 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001264 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001265 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001266 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001267 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001268 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001269 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001270 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001271 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001272 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001273 return create<ast::Variable>(
1274 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1275 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001276 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001277
James Pricedaf1f1c2021-04-08 22:15:48 +00001278 /// @param name the parameter name
1279 /// @param type the parameter type
1280 /// @param decorations optional parameter decorations
1281 /// @returns a constant `ast::Variable` with the given name and type
1282 template <typename NAME>
1283 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001284 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001285 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001286 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001287 return create<ast::Variable>(
1288 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1289 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001290 }
1291
1292 /// @param source the parameter source
1293 /// @param name the parameter name
1294 /// @param type the parameter type
1295 /// @param decorations optional parameter decorations
1296 /// @returns a constant `ast::Variable` with the given name and type
1297 template <typename NAME>
1298 ast::Variable* Param(const Source& source,
1299 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001300 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001301 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001302 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001303 return create<ast::Variable>(
1304 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1305 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001306 }
1307
Ben Clayton42708342021-04-21 17:55:12 +00001308 /// @param name the variable name
1309 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001310 /// @param optional the optional variable settings.
1311 /// Can be any of the following, in any order:
1312 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001313 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001314 /// * ast::Expression* - specifies the variable's initializer expression
1315 /// * ast::DecorationList - specifies the variable's decorations
1316 /// Note that repeated arguments of the same type will use the last argument's
1317 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001318 /// @returns a new `ast::Variable`, which is automatically registered as a
1319 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001320 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001321 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001322 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001323 OPTIONAL&&... optional) {
1324 auto* var = Var(std::forward<NAME>(name), type,
1325 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001326 AST().AddGlobalVariable(var);
1327 return var;
1328 }
1329
1330 /// @param source the variable source
1331 /// @param name the variable name
1332 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001333 /// @param optional the optional variable settings.
1334 /// Can be any of the following, in any order:
1335 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001336 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001337 /// * ast::Expression* - specifies the variable's initializer expression
1338 /// * ast::DecorationList - specifies the variable's decorations
1339 /// Note that repeated arguments of the same type will use the last argument's
1340 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001341 /// @returns a new `ast::Variable`, which is automatically registered as a
1342 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001343 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001344 ast::Variable* Global(const Source& source,
1345 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001346 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001347 OPTIONAL&&... optional) {
1348 auto* var = Var(source, std::forward<NAME>(name), type,
1349 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001350 AST().AddGlobalVariable(var);
1351 return var;
1352 }
1353
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001354 /// @param name the variable name
1355 /// @param type the variable type
1356 /// @param constructor constructor expression
1357 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001358 /// @returns a const `ast::Variable` constructed by calling Var() with the
1359 /// arguments of `args`, which is automatically registered as a global
1360 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001361 template <typename NAME>
1362 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001363 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001364 ast::Expression* constructor,
1365 ast::DecorationList decorations = {}) {
1366 auto* var = Const(std::forward<NAME>(name), type, constructor,
1367 std::move(decorations));
1368 AST().AddGlobalVariable(var);
1369 return var;
1370 }
1371
1372 /// @param source the variable source
1373 /// @param name the variable name
1374 /// @param type the variable type
1375 /// @param constructor constructor expression
1376 /// @param decorations optional variable decorations
1377 /// @returns a const `ast::Variable` constructed by calling Var() with the
1378 /// arguments of `args`, which is automatically registered as a global
1379 /// variable with the ast::Module.
1380 template <typename NAME>
1381 ast::Variable* GlobalConst(const Source& source,
1382 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001383 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001384 ast::Expression* constructor,
1385 ast::DecorationList decorations = {}) {
1386 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1387 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001388 AST().AddGlobalVariable(var);
1389 return var;
1390 }
1391
Ben Claytonfe0910f2021-05-17 15:51:47 +00001392 /// @param source the source information
1393 /// @param expr the expression to take the address of
1394 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1395 template <typename EXPR>
1396 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1397 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1398 Expr(std::forward<EXPR>(expr)));
1399 }
1400
1401 /// @param expr the expression to take the address of
1402 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1403 template <typename EXPR>
1404 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1405 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1406 Expr(std::forward<EXPR>(expr)));
1407 }
1408
1409 /// @param source the source information
1410 /// @param expr the expression to perform an indirection on
1411 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1412 template <typename EXPR>
1413 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1414 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1415 Expr(std::forward<EXPR>(expr)));
1416 }
1417
1418 /// @param expr the expression to perform an indirection on
1419 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1420 template <typename EXPR>
1421 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1422 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1423 Expr(std::forward<EXPR>(expr)));
1424 }
1425
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001426 /// @param func the function name
1427 /// @param args the function call arguments
1428 /// @returns a `ast::CallExpression` to the function `func`, with the
1429 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1430 template <typename NAME, typename... ARGS>
1431 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1432 return create<ast::CallExpression>(Expr(func),
1433 ExprList(std::forward<ARGS>(args)...));
1434 }
1435
1436 /// @param lhs the left hand argument to the addition operation
1437 /// @param rhs the right hand argument to the addition operation
1438 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1439 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001440 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001441 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1442 Expr(std::forward<LHS>(lhs)),
1443 Expr(std::forward<RHS>(rhs)));
1444 }
1445
1446 /// @param lhs the left hand argument to the subtraction operation
1447 /// @param rhs the right hand argument to the subtraction operation
1448 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1449 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001450 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001451 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1452 Expr(std::forward<LHS>(lhs)),
1453 Expr(std::forward<RHS>(rhs)));
1454 }
1455
1456 /// @param lhs the left hand argument to the multiplication operation
1457 /// @param rhs the right hand argument to the multiplication operation
1458 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1459 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001460 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001461 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1462 Expr(std::forward<LHS>(lhs)),
1463 Expr(std::forward<RHS>(rhs)));
1464 }
1465
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001466 /// @param source the source information
1467 /// @param lhs the left hand argument to the multiplication operation
1468 /// @param rhs the right hand argument to the multiplication operation
1469 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1470 template <typename LHS, typename RHS>
1471 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1472 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1473 Expr(std::forward<LHS>(lhs)),
1474 Expr(std::forward<RHS>(rhs)));
1475 }
1476
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001477 /// @param lhs the left hand argument to the division operation
1478 /// @param rhs the right hand argument to the division operation
1479 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1480 template <typename LHS, typename RHS>
1481 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1482 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1483 Expr(std::forward<LHS>(lhs)),
1484 Expr(std::forward<RHS>(rhs)));
1485 }
1486
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001487 /// @param arr the array argument for the array accessor expression
1488 /// @param idx the index argument for the array accessor expression
1489 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1490 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001491 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001492 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1493 Expr(std::forward<IDX>(idx)));
1494 }
1495
1496 /// @param obj the object for the member accessor expression
1497 /// @param idx the index argument for the array accessor expression
1498 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1499 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001500 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001501 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1502 Expr(std::forward<IDX>(idx)));
1503 }
1504
Ben Clayton42d1e092021-02-02 14:29:15 +00001505 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001506 /// @param val the offset value
1507 /// @returns the offset decoration pointer
1508 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1509 return create<ast::StructMemberOffsetDecoration>(source_, val);
1510 }
1511
Ben Claytond614dd52021-03-15 10:43:11 +00001512 /// Creates a ast::StructMemberSizeDecoration
1513 /// @param source the source information
1514 /// @param val the size value
1515 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001516 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1517 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001518 return create<ast::StructMemberSizeDecoration>(source, val);
1519 }
1520
1521 /// Creates a ast::StructMemberSizeDecoration
1522 /// @param val the size value
1523 /// @returns the size decoration pointer
1524 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1525 return create<ast::StructMemberSizeDecoration>(source_, val);
1526 }
1527
1528 /// Creates a ast::StructMemberAlignDecoration
1529 /// @param source the source information
1530 /// @param val the align value
1531 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001532 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1533 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001534 return create<ast::StructMemberAlignDecoration>(source, val);
1535 }
1536
1537 /// Creates a ast::StructMemberAlignDecoration
1538 /// @param val the align value
1539 /// @returns the align decoration pointer
1540 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1541 return create<ast::StructMemberAlignDecoration>(source_, val);
1542 }
1543
Ben Clayton42d1e092021-02-02 14:29:15 +00001544 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001545 /// @param source the source information
1546 /// @param name the function name
1547 /// @param params the function parameters
1548 /// @param type the function return type
1549 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001550 /// @param decorations the optional function decorations
1551 /// @param return_type_decorations the optional function return type
1552 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001553 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001554 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001555 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001556 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001557 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001558 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001559 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001560 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001561 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001562 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001563 auto* func =
1564 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1565 type, create<ast::BlockStatement>(body),
1566 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001567 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001568 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001569 }
1570
Ben Clayton42d1e092021-02-02 14:29:15 +00001571 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001572 /// @param name the function name
1573 /// @param params the function parameters
1574 /// @param type the function return type
1575 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001576 /// @param decorations the optional function decorations
1577 /// @param return_type_decorations the optional function return type
1578 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001579 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001580 template <typename NAME>
1581 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001582 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001583 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001584 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001585 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001586 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001587 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001588 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1589 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001590 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001591 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001592 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001593 }
1594
Ben Clayton49668bb2021-04-17 05:32:01 +00001595 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001596 /// @param source the source information
1597 /// @returns the return statement pointer
1598 ast::ReturnStatement* Return(const Source& source) {
1599 return create<ast::ReturnStatement>(source);
1600 }
1601
1602 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001603 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001604 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1605
1606 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001607 /// @param source the source information
1608 /// @param val the return value
1609 /// @returns the return statement pointer
1610 template <typename EXPR>
1611 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1612 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1613 }
1614
1615 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001616 /// @param val the return value
1617 /// @returns the return statement pointer
1618 template <typename EXPR>
1619 ast::ReturnStatement* Return(EXPR&& val) {
1620 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001621 }
1622
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001623 /// Creates a ast::Alias registering it with the AST().ConstructedTypes().
1624 /// @param source the source information
1625 /// @param name the alias name
1626 /// @param type the alias target type
1627 /// @returns the alias type
1628 template <typename NAME>
1629 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1630 auto* out = ty.alias(source, std::forward<NAME>(name), type);
1631 AST().AddConstructedType(out);
1632 return out;
1633 }
1634
1635 /// Creates a ast::Alias registering it with the AST().ConstructedTypes().
1636 /// @param name the alias name
1637 /// @param type the alias target type
1638 /// @returns the alias type
1639 template <typename NAME>
1640 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1641 auto* out = ty.alias(std::forward<NAME>(name), type);
1642 AST().AddConstructedType(out);
1643 return out;
1644 }
1645
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001646 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001647 /// @param source the source information
1648 /// @param name the struct name
1649 /// @param members the struct members
1650 /// @param decorations the optional struct decorations
1651 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001652 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001653 ast::Struct* Structure(const Source& source,
1654 NAME&& name,
1655 ast::StructMemberList members,
1656 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001657 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001658 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001659 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001660 AST().AddConstructedType(type);
1661 return type;
1662 }
1663
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001664 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001665 /// @param name the struct name
1666 /// @param members the struct members
1667 /// @param decorations the optional struct decorations
1668 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001669 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001670 ast::Struct* Structure(NAME&& name,
1671 ast::StructMemberList members,
1672 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001673 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001674 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001675 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001676 AST().AddConstructedType(type);
1677 return type;
1678 }
1679
Ben Clayton42d1e092021-02-02 14:29:15 +00001680 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001681 /// @param source the source information
1682 /// @param name the struct member name
1683 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001684 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001685 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001686 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001687 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001688 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001689 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001690 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001691 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001692 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1693 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001694 }
1695
Ben Clayton42d1e092021-02-02 14:29:15 +00001696 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001697 /// @param name the struct member name
1698 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001699 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001700 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001701 template <typename NAME>
1702 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001703 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001704 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001705 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001706 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1707 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001708 }
1709
Ben Claytonbab31972021-02-08 21:16:21 +00001710 /// Creates a ast::StructMember with the given byte offset
1711 /// @param offset the offset to use in the StructMemberOffsetDecoration
1712 /// @param name the struct member name
1713 /// @param type the struct member type
1714 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001715 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001716 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001717 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001718 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001719 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001720 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001721 create<ast::StructMemberOffsetDecoration>(offset),
1722 });
1723 }
1724
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001725 /// Creates a ast::BlockStatement with input statements
1726 /// @param statements statements of block
1727 /// @returns the block statement pointer
1728 template <typename... Statements>
1729 ast::BlockStatement* Block(Statements&&... statements) {
1730 return create<ast::BlockStatement>(
1731 ast::StatementList{std::forward<Statements>(statements)...});
1732 }
1733
1734 /// Creates a ast::ElseStatement with input condition and body
1735 /// @param condition the else condition expression
1736 /// @param body the else body
1737 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001738 template <typename CONDITION>
1739 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1740 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1741 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001742 }
1743
1744 /// Creates a ast::IfStatement with input condition, body, and optional
1745 /// variadic else statements
1746 /// @param condition the if statement condition expression
1747 /// @param body the if statement body
1748 /// @param elseStatements optional variadic else statements
1749 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001750 template <typename CONDITION, typename... ELSE_STATEMENTS>
1751 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001752 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001753 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001754 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001755 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001756 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001757 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001758 }
1759
1760 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001761 /// @param source the source information
1762 /// @param lhs the left hand side expression initializer
1763 /// @param rhs the right hand side expression initializer
1764 /// @returns the assignment statement pointer
1765 template <typename LhsExpressionInit, typename RhsExpressionInit>
1766 ast::AssignmentStatement* Assign(const Source& source,
1767 LhsExpressionInit&& lhs,
1768 RhsExpressionInit&& rhs) {
1769 return create<ast::AssignmentStatement>(
1770 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1771 Expr(std::forward<RhsExpressionInit>(rhs)));
1772 }
1773
1774 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001775 /// @param lhs the left hand side expression initializer
1776 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001777 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001778 template <typename LhsExpressionInit, typename RhsExpressionInit>
1779 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1780 RhsExpressionInit&& rhs) {
1781 return create<ast::AssignmentStatement>(
1782 Expr(std::forward<LhsExpressionInit>(lhs)),
1783 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001784 }
1785
1786 /// Creates a ast::LoopStatement with input body and optional continuing
1787 /// @param body the loop body
1788 /// @param continuing the optional continuing block
1789 /// @returns the loop statement pointer
1790 ast::LoopStatement* Loop(ast::BlockStatement* body,
1791 ast::BlockStatement* continuing = nullptr) {
1792 return create<ast::LoopStatement>(body, continuing);
1793 }
1794
1795 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001796 /// @param source the source information
1797 /// @param var the variable to wrap in a decl statement
1798 /// @returns the variable decl statement pointer
1799 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1800 return create<ast::VariableDeclStatement>(source, var);
1801 }
1802
1803 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001804 /// @param var the variable to wrap in a decl statement
1805 /// @returns the variable decl statement pointer
1806 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1807 return create<ast::VariableDeclStatement>(var);
1808 }
1809
Antonio Maioranocea744d2021-03-25 12:55:27 +00001810 /// Creates a ast::SwitchStatement with input expression and cases
1811 /// @param condition the condition expression initializer
1812 /// @param cases case statements
1813 /// @returns the switch statement pointer
1814 template <typename ExpressionInit, typename... Cases>
1815 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1816 return create<ast::SwitchStatement>(
1817 Expr(std::forward<ExpressionInit>(condition)),
1818 ast::CaseStatementList{std::forward<Cases>(cases)...});
1819 }
1820
1821 /// Creates a ast::CaseStatement with input list of selectors, and body
1822 /// @param selectors list of selectors
1823 /// @param body the case body
1824 /// @returns the case statement pointer
1825 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1826 ast::BlockStatement* body = nullptr) {
1827 return create<ast::CaseStatement>(std::move(selectors),
1828 body ? body : Block());
1829 }
1830
1831 /// Convenient overload that takes a single selector
1832 /// @param selector a single case selector
1833 /// @param body the case body
1834 /// @returns the case statement pointer
1835 ast::CaseStatement* Case(ast::IntLiteral* selector,
1836 ast::BlockStatement* body = nullptr) {
1837 return Case(ast::CaseSelectorList{selector}, body);
1838 }
1839
1840 /// Convenience function that creates a 'default' ast::CaseStatement
1841 /// @param body the case body
1842 /// @returns the case statement pointer
1843 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1844 return Case(ast::CaseSelectorList{}, body);
1845 }
1846
James Price68f558f2021-04-06 15:51:47 +00001847 /// Creates an ast::BuiltinDecoration
1848 /// @param source the source information
1849 /// @param builtin the builtin value
1850 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001851 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001852 return create<ast::BuiltinDecoration>(source, builtin);
1853 }
1854
1855 /// Creates an ast::BuiltinDecoration
1856 /// @param builtin the builtin value
1857 /// @returns the builtin decoration pointer
1858 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1859 return create<ast::BuiltinDecoration>(source_, builtin);
1860 }
1861
1862 /// Creates an ast::LocationDecoration
1863 /// @param source the source information
1864 /// @param location the location value
1865 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001866 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001867 return create<ast::LocationDecoration>(source, location);
1868 }
1869
1870 /// Creates an ast::LocationDecoration
1871 /// @param location the location value
1872 /// @returns the location decoration pointer
1873 ast::LocationDecoration* Location(uint32_t location) {
1874 return create<ast::LocationDecoration>(source_, location);
1875 }
1876
James Pricef2f3bfc2021-05-13 20:32:32 +00001877 /// Creates an ast::OverrideDecoration with a specific constant ID
1878 /// @param source the source information
1879 /// @param id the id value
1880 /// @returns the override decoration pointer
1881 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1882 return create<ast::OverrideDecoration>(source, id);
1883 }
1884
1885 /// Creates an ast::OverrideDecoration with a specific constant ID
1886 /// @param id the optional id value
1887 /// @returns the override decoration pointer
1888 ast::OverrideDecoration* Override(uint32_t id) {
1889 return Override(source_, id);
1890 }
1891
1892 /// Creates an ast::OverrideDecoration without a constant ID
1893 /// @param source the source information
1894 /// @returns the override decoration pointer
1895 ast::OverrideDecoration* Override(const Source& source) {
1896 return create<ast::OverrideDecoration>(source);
1897 }
1898
1899 /// Creates an ast::OverrideDecoration without a constant ID
1900 /// @returns the override decoration pointer
1901 ast::OverrideDecoration* Override() { return Override(source_); }
1902
James Price68f558f2021-04-06 15:51:47 +00001903 /// Creates an ast::StageDecoration
1904 /// @param source the source information
1905 /// @param stage the pipeline stage
1906 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001907 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001908 return create<ast::StageDecoration>(source, stage);
1909 }
1910
1911 /// Creates an ast::StageDecoration
1912 /// @param stage the pipeline stage
1913 /// @returns the stage decoration pointer
1914 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1915 return create<ast::StageDecoration>(source_, stage);
1916 }
1917
James Price70f80bb2021-05-19 13:40:08 +00001918 /// Creates an ast::WorkgroupDecoration
1919 /// @param x the x dimension expression
1920 /// @returns the workgroup decoration pointer
1921 template <typename EXPR_X>
1922 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1923 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1924 }
1925
1926 /// Creates an ast::WorkgroupDecoration
1927 /// @param x the x dimension expression
1928 /// @param y the y dimension expression
1929 /// @returns the workgroup decoration pointer
1930 template <typename EXPR_X, typename EXPR_Y>
1931 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1932 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1933 nullptr);
1934 }
1935
1936 /// Creates an ast::WorkgroupDecoration
1937 /// @param x the x dimension expression
1938 /// @param y the y dimension expression
1939 /// @param z the z dimension expression
1940 /// @returns the workgroup decoration pointer
1941 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1942 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1943 return create<ast::WorkgroupDecoration>(
1944 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1945 Expr(std::forward<EXPR_Z>(z)));
1946 }
1947
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001948 /// Sets the current builder source to `src`
1949 /// @param src the Source used for future create() calls
1950 void SetSource(const Source& src) {
1951 AssertNotMoved();
1952 source_ = src;
1953 }
1954
1955 /// Sets the current builder source to `loc`
1956 /// @param loc the Source used for future create() calls
1957 void SetSource(const Source::Location& loc) {
1958 AssertNotMoved();
1959 source_ = Source(loc);
1960 }
1961
Ben Clayton33352542021-01-29 16:43:41 +00001962 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001963 /// @note As the Resolver is run when the Program is built, this will only be
1964 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001965 /// @param expr the AST expression
1966 /// @return the resolved semantic type for the expression, or nullptr if the
1967 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001968 sem::Type* TypeOf(const ast::Expression* expr) const;
1969
Ben Claytonfe0910f2021-05-17 15:51:47 +00001970 /// Helper for returning the resolved semantic type of the variable `var`.
1971 /// @note As the Resolver is run when the Program is built, this will only be
1972 /// useful for the Resolver itself and tests that use their own Resolver.
1973 /// @param var the AST variable
1974 /// @return the resolved semantic type for the variable, or nullptr if the
1975 /// variable has no resolved type.
1976 sem::Type* TypeOf(const ast::Variable* var) const;
1977
Ben Claytonfbec46f2021-04-30 20:20:19 +00001978 /// Helper for returning the resolved semantic type of the AST type `type`.
1979 /// @note As the Resolver is run when the Program is built, this will only be
1980 /// useful for the Resolver itself and tests that use their own Resolver.
1981 /// @param expr the AST type
1982 /// @return the resolved semantic type for the type, or nullptr if the type
1983 /// has no resolved type.
1984 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001985
Ben Clayton169512e2021-04-17 05:52:11 +00001986 /// Wraps the ast::Literal in a statement. This is used by tests that
1987 /// construct a partial AST and require the Resolver to reach these
1988 /// nodes.
1989 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1990 /// @return the ast::Statement that wraps the ast::Statement
1991 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001992 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001993 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001994 /// nodes.
1995 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1996 /// @return the ast::Statement that wraps the ast::Expression
1997 ast::Statement* WrapInStatement(ast::Expression* expr);
1998 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001999 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002000 /// these nodes.
2001 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2002 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2003 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2004 /// Returns the statement argument. Used as a passthrough-overload by
2005 /// WrapInFunction().
2006 /// @param stmt the ast::Statement
2007 /// @return `stmt`
2008 ast::Statement* WrapInStatement(ast::Statement* stmt);
2009 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002010 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002011 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002012 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002013 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002014 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002015 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002016 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002017 }
2018 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002019 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002020 /// @returns the function
2021 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002022
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002023 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002024 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002025
2026 protected:
2027 /// Asserts that the builder has not been moved.
2028 void AssertNotMoved() const;
2029
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002030 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002031 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002032 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002033 ASTNodeAllocator ast_nodes_;
2034 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002035 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002036 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002037 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002038 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002039
2040 /// The source to use when creating AST nodes without providing a Source as
2041 /// the first argument.
2042 Source source_;
2043
Ben Clayton5f0ea112021-03-09 10:54:37 +00002044 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002045 /// program when built.
2046 bool resolve_on_build_ = true;
2047
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002048 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2049 bool moved_ = false;
2050};
2051
2052//! @cond Doxygen_Suppress
2053// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2054template <>
2055struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002056 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002057 return t->i32();
2058 }
2059};
2060template <>
2061struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002062 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002063 return t->u32();
2064 }
2065};
2066template <>
2067struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002068 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002069 return t->f32();
2070 }
2071};
2072template <>
2073struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002074 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002075 return t->bool_();
2076 }
2077};
2078template <>
2079struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002080 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002081 return t->void_();
2082 }
2083};
2084//! @endcond
2085
Ben Clayton917b14b2021-04-19 16:50:23 +00002086/// @param builder the ProgramBuilder
2087/// @returns the ProgramID of the ProgramBuilder
2088inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2089 return builder->ID();
2090}
2091
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002092} // namespace tint
2093
2094#endif // SRC_PROGRAM_BUILDER_H_