blob: b04a5adde58922502b18f7dc05dc10e211effe3f [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 Clayton19b03192021-05-20 15:04:08 +0000437 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000438 }
439
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000440 /// @param source the Source of the node
441 /// @param type vector subtype
442 /// @param n vector width in elements
443 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000444 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000445 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000446 }
447
Antonio Maiorano25436862021-04-13 13:32:33 +0000448 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000449 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000450 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000451
452 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000453 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000454 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000455
456 /// @param type vector subtype
457 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000458 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000459
460 /// @param n vector width in elements
461 /// @return the tint AST type for a `n`-element vector of `type`.
462 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000463 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000464 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000465 }
466
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000467 /// @return the tint AST type for a 2-element vector of the C type `T`.
468 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000469 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000470 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000471 }
472
473 /// @return the tint AST type for a 3-element vector of the C type `T`.
474 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000475 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000476 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000477 }
478
479 /// @return the tint AST type for a 4-element vector of the C type `T`.
480 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000481 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000482 return vec4(Of<T>());
483 }
484
485 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000486 /// @param columns number of columns for the matrix
487 /// @param rows number of rows for the matrix
488 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000489 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000490 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000491 }
492
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000493 /// @param source the Source of the node
494 /// @param type matrix subtype
495 /// @param columns number of columns for the matrix
496 /// @param rows number of rows for the matrix
497 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000498 ast::Matrix* mat(const Source& source,
499 ast::Type* type,
500 uint32_t columns,
501 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000502 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000503 }
504
Ben Claytone204f272021-04-22 14:40:23 +0000505 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000506 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000507 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000508
509 /// @param type matrix subtype
510 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000511 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000512
513 /// @param type matrix subtype
514 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000515 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000516
517 /// @param type matrix subtype
518 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000519 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000520
521 /// @param type matrix subtype
522 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000523 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000524
525 /// @param type matrix subtype
526 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000527 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000528
529 /// @param type matrix subtype
530 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000531 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000532
533 /// @param type matrix subtype
534 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000535 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000536
537 /// @param type matrix subtype
538 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000539 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000540
Ben Claytone204f272021-04-22 14:40:23 +0000541 /// @param columns number of columns for the matrix
542 /// @param rows number of rows for the matrix
543 /// @return the tint AST type for a matrix of `type`
544 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000545 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000546 return mat(Of<T>(), columns, rows);
547 }
548
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000549 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
550 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000551 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000552 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000553 }
554
555 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
556 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000557 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000558 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559 }
560
561 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
562 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000563 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000564 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000565 }
566
567 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
568 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000569 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000570 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 }
572
573 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
574 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000575 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000576 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 }
578
579 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
580 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000581 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000582 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000583 }
584
585 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
586 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000587 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000588 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000589 }
590
591 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
592 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000593 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000594 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000595 }
596
597 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
598 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000599 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000600 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000601 }
602
603 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000604 /// @param n the array size. 0 represents a runtime-array
605 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000606 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000607 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000608 uint32_t n = 0,
609 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000610 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000611 }
612
613 /// @param source the Source of the node
614 /// @param subtype the array element type
615 /// @param n the array size. 0 represents a runtime-array
616 /// @param decos the optional decorations for the array
617 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000618 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000619 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000620 uint32_t n = 0,
621 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000622 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000623 }
624
Ben Claytonbab31972021-02-08 21:16:21 +0000625 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000626 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000627 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000628 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000629 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000630 ast::DecorationList decos;
631 if (stride) {
632 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
633 }
634 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000635 }
636
Ben Clayton0f88b312021-05-04 17:36:31 +0000637 /// @param source the Source of the node
638 /// @param subtype the array element type
639 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000640 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000641 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000642 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000643 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000644 uint32_t n,
645 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000646 ast::DecorationList decos;
647 if (stride) {
648 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
649 }
650 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000651 }
652
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000653 /// @return the tint AST type for an array of size `N` of type `T`
654 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000655 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000656 return array(Of<T>(), N);
657 }
658
Ben Claytonbab31972021-02-08 21:16:21 +0000659 /// @param stride the array stride
660 /// @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(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000663 return array(Of<T>(), N, stride);
664 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000665
Ben Clayton0f88b312021-05-04 17:36:31 +0000666 /// Creates a type name
667 /// @param name the name
668 /// @returns the type name
669 template <typename NAME>
670 ast::TypeName* type_name(NAME&& name) const {
671 return builder->create<ast::TypeName>(
672 builder->Sym(std::forward<NAME>(name)));
673 }
674
675 /// Creates a type name
676 /// @param source the Source of the node
677 /// @param name the name
678 /// @returns the type name
679 template <typename NAME>
680 ast::TypeName* type_name(const Source& source, NAME&& name) const {
681 return builder->create<ast::TypeName>(
682 source, builder->Sym(std::forward<NAME>(name)));
683 }
684
Ben Clayton42d1e092021-02-02 14:29:15 +0000685 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000686 /// @param name the alias name
687 /// @param type the alias type
688 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000689 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000690 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000691 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000692 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000693 }
694
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000695 /// Creates an alias type
696 /// @param source the Source of the node
697 /// @param name the alias name
698 /// @param type the alias type
699 /// @returns the alias pointer
700 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000701 ast::Alias* alias(const Source& source,
702 NAME&& name,
703 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000704 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000705 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000706 }
707
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000708 /// @param type the type of the pointer
709 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000710 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000711 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000712 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000713 ast::StorageClass storage_class,
714 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000715 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000716 }
717
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000718 /// @param source the Source of the node
719 /// @param type the type of the pointer
720 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000721 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000722 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000723 ast::Pointer* pointer(const Source& source,
724 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000725 ast::StorageClass storage_class,
726 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000727 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000728 }
729
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000730 /// @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
Ben Claytone204f272021-04-22 14:40:23 +0000732 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000733 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000734 ast::Pointer* pointer(ast::StorageClass storage_class,
735 ast::Access access = ast::Access::kUndefined) const {
736 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000737 }
738
Ben Claytone204f272021-04-22 14:40:23 +0000739 /// @param kind the kind of sampler
740 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000741 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000742 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000743 }
744
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000745 /// @param source the Source of the node
746 /// @param kind the kind of sampler
747 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000748 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000749 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000750 }
751
Ben Claytone204f272021-04-22 14:40:23 +0000752 /// @param dims the dimensionality of the texture
753 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000754 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000755 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000756 }
757
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000758 /// @param source the Source of the node
759 /// @param dims the dimensionality of the texture
760 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000761 ast::DepthTexture* depth_texture(const Source& source,
762 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000763 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000764 }
765
Ben Claytone204f272021-04-22 14:40:23 +0000766 /// @param dims the dimensionality of the texture
767 /// @param subtype the texture subtype.
768 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000769 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
770 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000771 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000772 }
773
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000774 /// @param source the Source of the node
775 /// @param dims the dimensionality of the texture
776 /// @param subtype the texture subtype.
777 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000778 ast::SampledTexture* sampled_texture(const Source& source,
779 ast::TextureDimension dims,
780 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000781 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000782 }
783
Ben Claytone204f272021-04-22 14:40:23 +0000784 /// @param dims the dimensionality of the texture
785 /// @param subtype the texture subtype.
786 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000787 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
788 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000789 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000790 }
791
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000792 /// @param source the Source of the node
793 /// @param dims the dimensionality of the texture
794 /// @param subtype the texture subtype.
795 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000796 ast::MultisampledTexture* multisampled_texture(const Source& source,
797 ast::TextureDimension dims,
798 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000799 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000800 }
801
Ben Claytone204f272021-04-22 14:40:23 +0000802 /// @param dims the dimensionality of the texture
803 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000804 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000805 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000806 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000807 ast::ImageFormat format,
808 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000809 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000810 return builder->create<ast::StorageTexture>(dims, format, subtype,
811 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000812 }
813
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000814 /// @param source the Source of the node
815 /// @param dims the dimensionality of the texture
816 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000817 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000818 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000819 ast::StorageTexture* storage_texture(const Source& source,
820 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000821 ast::ImageFormat format,
822 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000823 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000824 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
825 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000826 }
827
Brandon Jones7b257692021-05-17 17:40:17 +0000828 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000829 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000830 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000831 }
832
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000833 /// @param source the Source of the node
834 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000835 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000836 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000837 }
838
Ben Clayton19b03192021-05-20 15:04:08 +0000839 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000840 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000841 /// @param type the type
842 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000843 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000844
Ben Clayton8758f102021-06-09 14:32:14 +0000845 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000846 /// @param type the type
847 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000848 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000849
Ben Claytonf5f311e2021-04-28 14:31:23 +0000850 /// The ProgramBuilder
851 ProgramBuilder* const builder;
852
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000853 private:
854 /// CToAST<T> is specialized for various `T` types and each specialization
855 /// contains a single static `get()` method for obtaining the corresponding
856 /// AST type for the C type `T`.
857 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000858 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000859 template <typename T>
860 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000861 };
862
863 //////////////////////////////////////////////////////////////////////////////
864 // AST helper methods
865 //////////////////////////////////////////////////////////////////////////////
866
James Price65ae64d2021-04-29 12:59:14 +0000867 /// @return a new unnamed symbol
868 Symbol Sym() { return Symbols().New(); }
869
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000870 /// @param name the symbol string
871 /// @return a Symbol with the given name
872 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
873
874 /// @param sym the symbol
875 /// @return `sym`
876 Symbol Sym(Symbol sym) { return sym; }
877
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000878 /// @param expr the expression
879 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000880 template <typename T>
881 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
882 return expr;
883 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000884
Ben Claytonb8ea5912021-04-19 16:52:42 +0000885 /// Passthrough for nullptr
886 /// @return nullptr
887 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
888
Ben Claytonfe0910f2021-05-17 15:51:47 +0000889 /// @param source the source information
890 /// @param symbol the identifier symbol
891 /// @return an ast::IdentifierExpression with the given symbol
892 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
893 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000894 }
895
Ben Clayton46d78d72021-02-10 21:34:26 +0000896 /// @param symbol the identifier symbol
897 /// @return an ast::IdentifierExpression with the given symbol
898 ast::IdentifierExpression* Expr(Symbol symbol) {
899 return create<ast::IdentifierExpression>(symbol);
900 }
901
Ben Claytonfe0910f2021-05-17 15:51:47 +0000902 /// @param source the source information
903 /// @param variable the AST variable
904 /// @return an ast::IdentifierExpression with the variable's symbol
905 ast::IdentifierExpression* Expr(const Source& source,
906 ast::Variable* variable) {
907 return create<ast::IdentifierExpression>(source, variable->symbol());
908 }
909
Ben Claytonb8ea5912021-04-19 16:52:42 +0000910 /// @param variable the AST variable
911 /// @return an ast::IdentifierExpression with the variable's symbol
912 ast::IdentifierExpression* Expr(ast::Variable* variable) {
913 return create<ast::IdentifierExpression>(variable->symbol());
914 }
915
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000916 /// @param source the source information
917 /// @param name the identifier name
918 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000919 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
920 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
921 }
922
923 /// @param name the identifier name
924 /// @return an ast::IdentifierExpression with the given name
925 ast::IdentifierExpression* Expr(const char* name) {
926 return create<ast::IdentifierExpression>(Symbols().Register(name));
927 }
928
929 /// @param source the source information
930 /// @param name the identifier name
931 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 ast::IdentifierExpression* Expr(const Source& source,
933 const std::string& name) {
934 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
935 }
936
937 /// @param name the identifier name
938 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000939 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000940 return create<ast::IdentifierExpression>(Symbols().Register(name));
941 }
942
Ben Claytonfe0910f2021-05-17 15:51:47 +0000943 /// @param source the source information
944 /// @param value the boolean value
945 /// @return a Scalar constructor for the given value
946 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
947 return create<ast::ScalarConstructorExpression>(source, Literal(value));
948 }
949
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000950 /// @param value the boolean value
951 /// @return a Scalar constructor for the given value
952 ast::ScalarConstructorExpression* Expr(bool value) {
953 return create<ast::ScalarConstructorExpression>(Literal(value));
954 }
955
Ben Claytonfe0910f2021-05-17 15:51:47 +0000956 /// @param source the source information
957 /// @param value the float value
958 /// @return a Scalar constructor for the given value
959 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
960 return create<ast::ScalarConstructorExpression>(source, Literal(value));
961 }
962
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000963 /// @param value the float value
964 /// @return a Scalar constructor for the given value
965 ast::ScalarConstructorExpression* Expr(f32 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 integer value
971 /// @return a Scalar constructor for the given value
972 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
973 return create<ast::ScalarConstructorExpression>(source, Literal(value));
974 }
975
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000976 /// @param value the integer value
977 /// @return a Scalar constructor for the given value
978 ast::ScalarConstructorExpression* Expr(i32 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 unsigned int value
984 /// @return a Scalar constructor for the given value
985 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
986 return create<ast::ScalarConstructorExpression>(source, Literal(value));
987 }
988
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000989 /// @param value the unsigned int value
990 /// @return a Scalar constructor for the given value
991 ast::ScalarConstructorExpression* Expr(u32 value) {
992 return create<ast::ScalarConstructorExpression>(Literal(value));
993 }
994
995 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
996 /// `list`.
997 /// @param list the list to append too
998 /// @param arg the arg to create
999 template <typename ARG>
1000 void Append(ast::ExpressionList& list, ARG&& arg) {
1001 list.emplace_back(Expr(std::forward<ARG>(arg)));
1002 }
1003
1004 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1005 /// then appends them to `list`.
1006 /// @param list the list to append too
1007 /// @param arg0 the first argument
1008 /// @param args the rest of the arguments
1009 template <typename ARG0, typename... ARGS>
1010 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1011 Append(list, std::forward<ARG0>(arg0));
1012 Append(list, std::forward<ARGS>(args)...);
1013 }
1014
1015 /// @return an empty list of expressions
1016 ast::ExpressionList ExprList() { return {}; }
1017
1018 /// @param args the list of expressions
1019 /// @return the list of expressions converted to `ast::Expression`s using
1020 /// `Expr()`,
1021 template <typename... ARGS>
1022 ast::ExpressionList ExprList(ARGS&&... args) {
1023 ast::ExpressionList list;
1024 list.reserve(sizeof...(args));
1025 Append(list, std::forward<ARGS>(args)...);
1026 return list;
1027 }
1028
1029 /// @param list the list of expressions
1030 /// @return `list`
1031 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1032
1033 /// @param val the boolan value
1034 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001035 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001036
1037 /// @param val the float value
1038 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001039 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001040
1041 /// @param val the unsigned int value
1042 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001043 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001044
1045 /// @param val the integer value
1046 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001047 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001048
1049 /// @param args the arguments for the type constructor
1050 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1051 /// of `args` converted to `ast::Expression`s using `Expr()`
1052 template <typename T, typename... ARGS>
1053 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001054 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001055 }
1056
1057 /// @param type the type to construct
1058 /// @param args the arguments for the constructor
1059 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1060 /// values `args`.
1061 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001062 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001063 return create<ast::TypeConstructorExpression>(
1064 type, ExprList(std::forward<ARGS>(args)...));
1065 }
1066
1067 /// @param args the arguments for the vector constructor
1068 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1069 /// `T`, constructed with the values `args`.
1070 template <typename T, typename... ARGS>
1071 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001072 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001073 }
1074
1075 /// @param args the arguments for the vector constructor
1076 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1077 /// `T`, constructed with the values `args`.
1078 template <typename T, typename... ARGS>
1079 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001080 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001081 }
1082
1083 /// @param args the arguments for the vector constructor
1084 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1085 /// `T`, constructed with the values `args`.
1086 template <typename T, typename... ARGS>
1087 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001088 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 }
1090
1091 /// @param args the arguments for the matrix constructor
1092 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1093 /// `T`, constructed with the values `args`.
1094 template <typename T, typename... ARGS>
1095 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001096 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001097 }
1098
1099 /// @param args the arguments for the matrix constructor
1100 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1101 /// `T`, constructed with the values `args`.
1102 template <typename T, typename... ARGS>
1103 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001104 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001105 }
1106
1107 /// @param args the arguments for the matrix constructor
1108 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1109 /// `T`, constructed with the values `args`.
1110 template <typename T, typename... ARGS>
1111 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001112 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001113 }
1114
1115 /// @param args the arguments for the matrix constructor
1116 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1117 /// `T`, constructed with the values `args`.
1118 template <typename T, typename... ARGS>
1119 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001120 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 }
1122
1123 /// @param args the arguments for the matrix constructor
1124 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1125 /// `T`, constructed with the values `args`.
1126 template <typename T, typename... ARGS>
1127 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001128 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001129 }
1130
1131 /// @param args the arguments for the matrix constructor
1132 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1133 /// `T`, constructed with the values `args`.
1134 template <typename T, typename... ARGS>
1135 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001136 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 }
1138
1139 /// @param args the arguments for the matrix constructor
1140 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1141 /// `T`, constructed with the values `args`.
1142 template <typename T, typename... ARGS>
1143 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001144 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001145 }
1146
1147 /// @param args the arguments for the matrix constructor
1148 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1149 /// `T`, constructed with the values `args`.
1150 template <typename T, typename... ARGS>
1151 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001152 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001153 }
1154
1155 /// @param args the arguments for the matrix constructor
1156 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1157 /// `T`, constructed with the values `args`.
1158 template <typename T, typename... ARGS>
1159 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001160 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001161 }
1162
1163 /// @param args the arguments for the array constructor
1164 /// @return an `ast::TypeConstructorExpression` of an array with element type
1165 /// `T`, constructed with the values `args`.
1166 template <typename T, int N = 0, typename... ARGS>
1167 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001168 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001169 }
1170
1171 /// @param subtype the array element type
1172 /// @param n the array size. 0 represents a runtime-array.
1173 /// @param args the arguments for the array constructor
1174 /// @return an `ast::TypeConstructorExpression` of an array with element type
1175 /// `subtype`, constructed with the values `args`.
1176 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001177 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001178 uint32_t n,
1179 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001180 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181 }
1182
1183 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001184 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001185 /// @param optional the optional variable settings.
1186 /// Can be any of the following, in any order:
1187 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001188 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001189 /// * ast::Expression* - specifies the variable's initializer expression
1190 /// * ast::DecorationList - specifies the variable's decorations
1191 /// Note that repeated arguments of the same type will use the last argument's
1192 /// value.
1193 /// @returns a `ast::Variable` with the given name, type and additional
1194 /// options
1195 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001196 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001197 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001198 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001199 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1200 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001201 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001202 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001203 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001204
1205 /// @param source the variable source
1206 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001208 /// @param optional the optional variable settings.
1209 /// Can be any of the following, in any order:
1210 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001211 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001212 /// * ast::Expression* - specifies the variable's initializer expression
1213 /// * ast::DecorationList - specifies the variable's decorations
1214 /// Note that repeated arguments of the same type will use the last argument's
1215 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001217 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001219 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001220 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001221 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001222 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1223 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001224 opts.storage, opts.access, type, false,
1225 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001226 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001229 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001230 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001232 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001233 template <typename NAME>
1234 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001235 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001236 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001237 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001238 return create<ast::Variable>(
1239 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1240 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001241 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001242
1243 /// @param source the variable source
1244 /// @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>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001251 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001252 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001253 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001254 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001255 return create<ast::Variable>(
1256 source, 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
James Pricedaf1f1c2021-04-08 22:15:48 +00001260 /// @param name the parameter name
1261 /// @param type the parameter type
1262 /// @param decorations optional parameter decorations
1263 /// @returns a constant `ast::Variable` with the given name and type
1264 template <typename NAME>
1265 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001266 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001267 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001268 return create<ast::Variable>(
1269 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1270 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001271 }
1272
1273 /// @param source the parameter source
1274 /// @param name the parameter name
1275 /// @param type the parameter type
1276 /// @param decorations optional parameter decorations
1277 /// @returns a constant `ast::Variable` with the given name and type
1278 template <typename NAME>
1279 ast::Variable* Param(const Source& source,
1280 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001281 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001282 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001283 return create<ast::Variable>(
1284 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1285 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001286 }
1287
Ben Clayton42708342021-04-21 17:55:12 +00001288 /// @param name the variable name
1289 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001290 /// @param optional the optional variable settings.
1291 /// Can be any of the following, in any order:
1292 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001293 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001294 /// * ast::Expression* - specifies the variable's initializer expression
1295 /// * ast::DecorationList - specifies the variable's decorations
1296 /// Note that repeated arguments of the same type will use the last argument's
1297 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001298 /// @returns a new `ast::Variable`, which is automatically registered as a
1299 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001300 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001301 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001302 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001303 OPTIONAL&&... optional) {
1304 auto* var = Var(std::forward<NAME>(name), type,
1305 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001306 AST().AddGlobalVariable(var);
1307 return var;
1308 }
1309
1310 /// @param source the variable source
1311 /// @param name the variable name
1312 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001313 /// @param optional the optional variable settings.
1314 /// Can be any of the following, in any order:
1315 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001316 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001317 /// * ast::Expression* - specifies the variable's initializer expression
1318 /// * ast::DecorationList - specifies the variable's decorations
1319 /// Note that repeated arguments of the same type will use the last argument's
1320 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001321 /// @returns a new `ast::Variable`, which is automatically registered as a
1322 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001323 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001324 ast::Variable* Global(const Source& source,
1325 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001326 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001327 OPTIONAL&&... optional) {
1328 auto* var = Var(source, std::forward<NAME>(name), type,
1329 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001330 AST().AddGlobalVariable(var);
1331 return var;
1332 }
1333
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001334 /// @param name the variable name
1335 /// @param type the variable type
1336 /// @param constructor constructor expression
1337 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001338 /// @returns a const `ast::Variable` constructed by calling Var() with the
1339 /// arguments of `args`, which is automatically registered as a global
1340 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001341 template <typename NAME>
1342 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001343 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001344 ast::Expression* constructor,
1345 ast::DecorationList decorations = {}) {
1346 auto* var = Const(std::forward<NAME>(name), type, constructor,
1347 std::move(decorations));
1348 AST().AddGlobalVariable(var);
1349 return var;
1350 }
1351
1352 /// @param source the variable source
1353 /// @param name the variable name
1354 /// @param type the variable type
1355 /// @param constructor constructor expression
1356 /// @param decorations optional variable decorations
1357 /// @returns a const `ast::Variable` constructed by calling Var() with the
1358 /// arguments of `args`, which is automatically registered as a global
1359 /// variable with the ast::Module.
1360 template <typename NAME>
1361 ast::Variable* GlobalConst(const Source& source,
1362 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(source, std::forward<NAME>(name), type, constructor,
1367 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001368 AST().AddGlobalVariable(var);
1369 return var;
1370 }
1371
Ben Claytonfe0910f2021-05-17 15:51:47 +00001372 /// @param source the source information
1373 /// @param expr the expression to take the address of
1374 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1375 template <typename EXPR>
1376 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1377 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1378 Expr(std::forward<EXPR>(expr)));
1379 }
1380
1381 /// @param expr the expression to take the address of
1382 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1383 template <typename EXPR>
1384 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1385 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1386 Expr(std::forward<EXPR>(expr)));
1387 }
1388
1389 /// @param source the source information
1390 /// @param expr the expression to perform an indirection on
1391 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1392 template <typename EXPR>
1393 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1394 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1395 Expr(std::forward<EXPR>(expr)));
1396 }
1397
1398 /// @param expr the expression to perform an indirection on
1399 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1400 template <typename EXPR>
1401 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1402 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1403 Expr(std::forward<EXPR>(expr)));
1404 }
1405
Antonio Maiorano14b34032021-06-09 20:17:59 +00001406 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001407 /// @param func the function name
1408 /// @param args the function call arguments
1409 /// @returns a `ast::CallExpression` to the function `func`, with the
1410 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1411 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001412 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1413 return create<ast::CallExpression>(source, Expr(func),
1414 ExprList(std::forward<ARGS>(args)...));
1415 }
1416
1417 /// @param func the function name
1418 /// @param args the function call arguments
1419 /// @returns a `ast::CallExpression` to the function `func`, with the
1420 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1421 template <typename NAME,
1422 typename... ARGS,
1423 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001424 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1425 return create<ast::CallExpression>(Expr(func),
1426 ExprList(std::forward<ARGS>(args)...));
1427 }
1428
1429 /// @param lhs the left hand argument to the addition operation
1430 /// @param rhs the right hand argument to the addition operation
1431 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1432 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001433 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001434 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1435 Expr(std::forward<LHS>(lhs)),
1436 Expr(std::forward<RHS>(rhs)));
1437 }
1438
1439 /// @param lhs the left hand argument to the subtraction operation
1440 /// @param rhs the right hand argument to the subtraction operation
1441 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1442 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001443 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001444 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1445 Expr(std::forward<LHS>(lhs)),
1446 Expr(std::forward<RHS>(rhs)));
1447 }
1448
1449 /// @param lhs the left hand argument to the multiplication operation
1450 /// @param rhs the right hand argument to the multiplication operation
1451 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1452 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001453 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001454 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1455 Expr(std::forward<LHS>(lhs)),
1456 Expr(std::forward<RHS>(rhs)));
1457 }
1458
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001459 /// @param source the source information
1460 /// @param lhs the left hand argument to the multiplication operation
1461 /// @param rhs the right hand argument to the multiplication operation
1462 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1463 template <typename LHS, typename RHS>
1464 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1465 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1466 Expr(std::forward<LHS>(lhs)),
1467 Expr(std::forward<RHS>(rhs)));
1468 }
1469
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001470 /// @param lhs the left hand argument to the division operation
1471 /// @param rhs the right hand argument to the division operation
1472 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1473 template <typename LHS, typename RHS>
1474 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1475 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1476 Expr(std::forward<LHS>(lhs)),
1477 Expr(std::forward<RHS>(rhs)));
1478 }
1479
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001480 /// @param arr the array argument for the array accessor expression
1481 /// @param idx the index argument for the array accessor expression
1482 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1483 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001484 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001485 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1486 Expr(std::forward<IDX>(idx)));
1487 }
1488
1489 /// @param obj the object for the member accessor expression
1490 /// @param idx the index argument for the array accessor expression
1491 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1492 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001493 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001494 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1495 Expr(std::forward<IDX>(idx)));
1496 }
1497
Ben Clayton42d1e092021-02-02 14:29:15 +00001498 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001499 /// @param val the offset value
1500 /// @returns the offset decoration pointer
1501 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1502 return create<ast::StructMemberOffsetDecoration>(source_, val);
1503 }
1504
Ben Claytond614dd52021-03-15 10:43:11 +00001505 /// Creates a ast::StructMemberSizeDecoration
1506 /// @param source the source information
1507 /// @param val the size value
1508 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001509 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1510 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001511 return create<ast::StructMemberSizeDecoration>(source, val);
1512 }
1513
1514 /// Creates a ast::StructMemberSizeDecoration
1515 /// @param val the size value
1516 /// @returns the size decoration pointer
1517 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1518 return create<ast::StructMemberSizeDecoration>(source_, val);
1519 }
1520
1521 /// Creates a ast::StructMemberAlignDecoration
1522 /// @param source the source information
1523 /// @param val the align value
1524 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001525 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1526 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001527 return create<ast::StructMemberAlignDecoration>(source, val);
1528 }
1529
1530 /// Creates a ast::StructMemberAlignDecoration
1531 /// @param val the align value
1532 /// @returns the align decoration pointer
1533 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1534 return create<ast::StructMemberAlignDecoration>(source_, val);
1535 }
1536
Ben Clayton42d1e092021-02-02 14:29:15 +00001537 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001538 /// @param source the source information
1539 /// @param name the function name
1540 /// @param params the function parameters
1541 /// @param type the function return type
1542 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001543 /// @param decorations the optional function decorations
1544 /// @param return_type_decorations the optional function return type
1545 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001546 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001547 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001548 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001549 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001550 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001551 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001552 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001553 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001554 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001555 auto* func =
1556 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1557 type, create<ast::BlockStatement>(body),
1558 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001559 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001560 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001561 }
1562
Ben Clayton42d1e092021-02-02 14:29:15 +00001563 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001564 /// @param name the function name
1565 /// @param params the function parameters
1566 /// @param type the function return type
1567 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001568 /// @param decorations the optional function decorations
1569 /// @param return_type_decorations the optional function return type
1570 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001571 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001572 template <typename NAME>
1573 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001574 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001575 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001576 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001577 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001578 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001579 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1580 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001581 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001582 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001583 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001584 }
1585
Ben Clayton49668bb2021-04-17 05:32:01 +00001586 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001587 /// @param source the source information
1588 /// @returns the return statement pointer
1589 ast::ReturnStatement* Return(const Source& source) {
1590 return create<ast::ReturnStatement>(source);
1591 }
1592
1593 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001594 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001595 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1596
1597 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001598 /// @param source the source information
1599 /// @param val the return value
1600 /// @returns the return statement pointer
1601 template <typename EXPR>
1602 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1603 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1604 }
1605
1606 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001607 /// @param val the return value
1608 /// @returns the return statement pointer
1609 template <typename EXPR>
1610 ast::ReturnStatement* Return(EXPR&& val) {
1611 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001612 }
1613
Ben Clayton950809f2021-06-09 14:32:14 +00001614 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001615 /// @param source the source information
1616 /// @param name the alias name
1617 /// @param type the alias target type
1618 /// @returns the alias type
1619 template <typename NAME>
1620 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1621 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001622 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001623 return out;
1624 }
1625
Ben Clayton950809f2021-06-09 14:32:14 +00001626 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001627 /// @param name the alias name
1628 /// @param type the alias target type
1629 /// @returns the alias type
1630 template <typename NAME>
1631 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1632 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001633 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001634 return out;
1635 }
1636
Ben Clayton950809f2021-06-09 14:32:14 +00001637 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001638 /// @param source the source information
1639 /// @param name the struct name
1640 /// @param members the struct members
1641 /// @param decorations the optional struct decorations
1642 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001643 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001644 ast::Struct* Structure(const Source& source,
1645 NAME&& name,
1646 ast::StructMemberList members,
1647 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001648 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001649 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001650 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001651 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001652 return type;
1653 }
1654
Ben Clayton950809f2021-06-09 14:32:14 +00001655 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001656 /// @param name the struct name
1657 /// @param members the struct members
1658 /// @param decorations the optional struct decorations
1659 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001660 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001661 ast::Struct* Structure(NAME&& name,
1662 ast::StructMemberList members,
1663 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001664 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001665 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001666 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001667 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001668 return type;
1669 }
1670
Ben Clayton42d1e092021-02-02 14:29:15 +00001671 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001672 /// @param source the source information
1673 /// @param name the struct member name
1674 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001675 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001676 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001677 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001678 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001679 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001680 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001681 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001682 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1683 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001684 }
1685
Ben Clayton42d1e092021-02-02 14:29:15 +00001686 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001687 /// @param name the struct member name
1688 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001689 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001690 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001691 template <typename NAME>
1692 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001693 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001694 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001695 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1696 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001697 }
1698
Ben Claytonbab31972021-02-08 21:16:21 +00001699 /// Creates a ast::StructMember with the given byte offset
1700 /// @param offset the offset to use in the StructMemberOffsetDecoration
1701 /// @param name the struct member name
1702 /// @param type the struct member type
1703 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001704 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001705 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001706 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001707 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001708 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001709 create<ast::StructMemberOffsetDecoration>(offset),
1710 });
1711 }
1712
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001713 /// Creates a ast::BlockStatement with input statements
1714 /// @param statements statements of block
1715 /// @returns the block statement pointer
1716 template <typename... Statements>
1717 ast::BlockStatement* Block(Statements&&... statements) {
1718 return create<ast::BlockStatement>(
1719 ast::StatementList{std::forward<Statements>(statements)...});
1720 }
1721
1722 /// Creates a ast::ElseStatement with input condition and body
1723 /// @param condition the else condition expression
1724 /// @param body the else body
1725 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001726 template <typename CONDITION>
1727 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1728 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1729 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001730 }
1731
1732 /// Creates a ast::IfStatement with input condition, body, and optional
1733 /// variadic else statements
1734 /// @param condition the if statement condition expression
1735 /// @param body the if statement body
1736 /// @param elseStatements optional variadic else statements
1737 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001738 template <typename CONDITION, typename... ELSE_STATEMENTS>
1739 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001740 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001741 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001742 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001743 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001744 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001745 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001746 }
1747
1748 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001749 /// @param source the source information
1750 /// @param lhs the left hand side expression initializer
1751 /// @param rhs the right hand side expression initializer
1752 /// @returns the assignment statement pointer
1753 template <typename LhsExpressionInit, typename RhsExpressionInit>
1754 ast::AssignmentStatement* Assign(const Source& source,
1755 LhsExpressionInit&& lhs,
1756 RhsExpressionInit&& rhs) {
1757 return create<ast::AssignmentStatement>(
1758 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1759 Expr(std::forward<RhsExpressionInit>(rhs)));
1760 }
1761
1762 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001763 /// @param lhs the left hand side expression initializer
1764 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001765 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001766 template <typename LhsExpressionInit, typename RhsExpressionInit>
1767 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1768 RhsExpressionInit&& rhs) {
1769 return create<ast::AssignmentStatement>(
1770 Expr(std::forward<LhsExpressionInit>(lhs)),
1771 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001772 }
1773
1774 /// Creates a ast::LoopStatement with input body and optional continuing
1775 /// @param body the loop body
1776 /// @param continuing the optional continuing block
1777 /// @returns the loop statement pointer
1778 ast::LoopStatement* Loop(ast::BlockStatement* body,
1779 ast::BlockStatement* continuing = nullptr) {
1780 return create<ast::LoopStatement>(body, continuing);
1781 }
1782
1783 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001784 /// @param source the source information
1785 /// @param var the variable to wrap in a decl statement
1786 /// @returns the variable decl statement pointer
1787 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1788 return create<ast::VariableDeclStatement>(source, var);
1789 }
1790
1791 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001792 /// @param var the variable to wrap in a decl statement
1793 /// @returns the variable decl statement pointer
1794 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1795 return create<ast::VariableDeclStatement>(var);
1796 }
1797
Antonio Maioranocea744d2021-03-25 12:55:27 +00001798 /// Creates a ast::SwitchStatement with input expression and cases
1799 /// @param condition the condition expression initializer
1800 /// @param cases case statements
1801 /// @returns the switch statement pointer
1802 template <typename ExpressionInit, typename... Cases>
1803 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1804 return create<ast::SwitchStatement>(
1805 Expr(std::forward<ExpressionInit>(condition)),
1806 ast::CaseStatementList{std::forward<Cases>(cases)...});
1807 }
1808
1809 /// Creates a ast::CaseStatement with input list of selectors, and body
1810 /// @param selectors list of selectors
1811 /// @param body the case body
1812 /// @returns the case statement pointer
1813 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1814 ast::BlockStatement* body = nullptr) {
1815 return create<ast::CaseStatement>(std::move(selectors),
1816 body ? body : Block());
1817 }
1818
1819 /// Convenient overload that takes a single selector
1820 /// @param selector a single case selector
1821 /// @param body the case body
1822 /// @returns the case statement pointer
1823 ast::CaseStatement* Case(ast::IntLiteral* selector,
1824 ast::BlockStatement* body = nullptr) {
1825 return Case(ast::CaseSelectorList{selector}, body);
1826 }
1827
1828 /// Convenience function that creates a 'default' ast::CaseStatement
1829 /// @param body the case body
1830 /// @returns the case statement pointer
1831 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1832 return Case(ast::CaseSelectorList{}, body);
1833 }
1834
James Price68f558f2021-04-06 15:51:47 +00001835 /// Creates an ast::BuiltinDecoration
1836 /// @param source the source information
1837 /// @param builtin the builtin value
1838 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001839 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001840 return create<ast::BuiltinDecoration>(source, builtin);
1841 }
1842
1843 /// Creates an ast::BuiltinDecoration
1844 /// @param builtin the builtin value
1845 /// @returns the builtin decoration pointer
1846 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1847 return create<ast::BuiltinDecoration>(source_, builtin);
1848 }
1849
1850 /// Creates an ast::LocationDecoration
1851 /// @param source the source information
1852 /// @param location the location value
1853 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001854 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001855 return create<ast::LocationDecoration>(source, location);
1856 }
1857
1858 /// Creates an ast::LocationDecoration
1859 /// @param location the location value
1860 /// @returns the location decoration pointer
1861 ast::LocationDecoration* Location(uint32_t location) {
1862 return create<ast::LocationDecoration>(source_, location);
1863 }
1864
James Pricef2f3bfc2021-05-13 20:32:32 +00001865 /// Creates an ast::OverrideDecoration with a specific constant ID
1866 /// @param source the source information
1867 /// @param id the id value
1868 /// @returns the override decoration pointer
1869 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1870 return create<ast::OverrideDecoration>(source, id);
1871 }
1872
1873 /// Creates an ast::OverrideDecoration with a specific constant ID
1874 /// @param id the optional id value
1875 /// @returns the override decoration pointer
1876 ast::OverrideDecoration* Override(uint32_t id) {
1877 return Override(source_, id);
1878 }
1879
1880 /// Creates an ast::OverrideDecoration without a constant ID
1881 /// @param source the source information
1882 /// @returns the override decoration pointer
1883 ast::OverrideDecoration* Override(const Source& source) {
1884 return create<ast::OverrideDecoration>(source);
1885 }
1886
1887 /// Creates an ast::OverrideDecoration without a constant ID
1888 /// @returns the override decoration pointer
1889 ast::OverrideDecoration* Override() { return Override(source_); }
1890
James Price68f558f2021-04-06 15:51:47 +00001891 /// Creates an ast::StageDecoration
1892 /// @param source the source information
1893 /// @param stage the pipeline stage
1894 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001895 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001896 return create<ast::StageDecoration>(source, stage);
1897 }
1898
1899 /// Creates an ast::StageDecoration
1900 /// @param stage the pipeline stage
1901 /// @returns the stage decoration pointer
1902 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1903 return create<ast::StageDecoration>(source_, stage);
1904 }
1905
James Price70f80bb2021-05-19 13:40:08 +00001906 /// Creates an ast::WorkgroupDecoration
1907 /// @param x the x dimension expression
1908 /// @returns the workgroup decoration pointer
1909 template <typename EXPR_X>
1910 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1911 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1912 }
1913
1914 /// Creates an ast::WorkgroupDecoration
1915 /// @param x the x dimension expression
1916 /// @param y the y dimension expression
1917 /// @returns the workgroup decoration pointer
1918 template <typename EXPR_X, typename EXPR_Y>
1919 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1920 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1921 nullptr);
1922 }
1923
1924 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00001925 /// @param source the source information
1926 /// @param x the x dimension expression
1927 /// @param y the y dimension expression
1928 /// @param z the z dimension expression
1929 /// @returns the workgroup decoration pointer
1930 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1931 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
1932 EXPR_X&& x,
1933 EXPR_Y&& y,
1934 EXPR_Z&& z) {
1935 return create<ast::WorkgroupDecoration>(
1936 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1937 Expr(std::forward<EXPR_Z>(z)));
1938 }
1939
1940 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00001941 /// @param x the x dimension expression
1942 /// @param y the y dimension expression
1943 /// @param z the z dimension expression
1944 /// @returns the workgroup decoration pointer
1945 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1946 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1947 return create<ast::WorkgroupDecoration>(
1948 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1949 Expr(std::forward<EXPR_Z>(z)));
1950 }
1951
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001952 /// Sets the current builder source to `src`
1953 /// @param src the Source used for future create() calls
1954 void SetSource(const Source& src) {
1955 AssertNotMoved();
1956 source_ = src;
1957 }
1958
1959 /// Sets the current builder source to `loc`
1960 /// @param loc the Source used for future create() calls
1961 void SetSource(const Source::Location& loc) {
1962 AssertNotMoved();
1963 source_ = Source(loc);
1964 }
1965
Ben Clayton33352542021-01-29 16:43:41 +00001966 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001967 /// @note As the Resolver is run when the Program is built, this will only be
1968 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001969 /// @param expr the AST expression
1970 /// @return the resolved semantic type for the expression, or nullptr if the
1971 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001972 sem::Type* TypeOf(const ast::Expression* expr) const;
1973
Ben Claytonfe0910f2021-05-17 15:51:47 +00001974 /// Helper for returning the resolved semantic type of the variable `var`.
1975 /// @note As the Resolver is run when the Program is built, this will only be
1976 /// useful for the Resolver itself and tests that use their own Resolver.
1977 /// @param var the AST variable
1978 /// @return the resolved semantic type for the variable, or nullptr if the
1979 /// variable has no resolved type.
1980 sem::Type* TypeOf(const ast::Variable* var) const;
1981
Ben Claytonfbec46f2021-04-30 20:20:19 +00001982 /// Helper for returning the resolved semantic type of the AST type `type`.
1983 /// @note As the Resolver is run when the Program is built, this will only be
1984 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00001985 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00001986 /// @return the resolved semantic type for the type, or nullptr if the type
1987 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00001988 const sem::Type* TypeOf(const ast::Type* type) const;
1989
1990 /// Helper for returning the resolved semantic type of the AST type
1991 /// declaration `type_decl`.
1992 /// @note As the Resolver is run when the Program is built, this will only be
1993 /// useful for the Resolver itself and tests that use their own Resolver.
1994 /// @param type_decl the AST type declaration
1995 /// @return the resolved semantic type for the type declaration, or nullptr if
1996 /// the type declaration has no resolved type.
1997 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00001998
Ben Clayton169512e2021-04-17 05:52:11 +00001999 /// Wraps the ast::Literal in a statement. This is used by tests that
2000 /// construct a partial AST and require the Resolver to reach these
2001 /// nodes.
2002 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2003 /// @return the ast::Statement that wraps the ast::Statement
2004 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002005 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002006 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002007 /// nodes.
2008 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2009 /// @return the ast::Statement that wraps the ast::Expression
2010 ast::Statement* WrapInStatement(ast::Expression* expr);
2011 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002012 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002013 /// these nodes.
2014 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2015 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2016 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2017 /// Returns the statement argument. Used as a passthrough-overload by
2018 /// WrapInFunction().
2019 /// @param stmt the ast::Statement
2020 /// @return `stmt`
2021 ast::Statement* WrapInStatement(ast::Statement* stmt);
2022 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002023 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002024 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002025 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002026 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002027 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002028 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002029 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002030 }
2031 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002032 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002033 /// @returns the function
2034 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002035
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002036 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002037 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002038
2039 protected:
2040 /// Asserts that the builder has not been moved.
2041 void AssertNotMoved() const;
2042
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002043 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002044 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002045 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002046 ASTNodeAllocator ast_nodes_;
2047 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002048 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002049 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002050 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002051 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002052
2053 /// The source to use when creating AST nodes without providing a Source as
2054 /// the first argument.
2055 Source source_;
2056
Ben Clayton5f0ea112021-03-09 10:54:37 +00002057 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002058 /// program when built.
2059 bool resolve_on_build_ = true;
2060
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002061 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2062 bool moved_ = false;
2063};
2064
2065//! @cond Doxygen_Suppress
2066// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2067template <>
2068struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002069 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002070 return t->i32();
2071 }
2072};
2073template <>
2074struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002075 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002076 return t->u32();
2077 }
2078};
2079template <>
2080struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002081 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002082 return t->f32();
2083 }
2084};
2085template <>
2086struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002087 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002088 return t->bool_();
2089 }
2090};
2091template <>
2092struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002093 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002094 return t->void_();
2095 }
2096};
2097//! @endcond
2098
Ben Clayton917b14b2021-04-19 16:50:23 +00002099/// @param builder the ProgramBuilder
2100/// @returns the ProgramID of the ProgramBuilder
2101inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2102 return builder->ID();
2103}
2104
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002105} // namespace tint
2106
2107#endif // SRC_PROGRAM_BUILDER_H_