blob: 55995b54bac478eb4f5675e99fb9a3454626d416 [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
81namespace tint {
82
Ben Clayton401b96b2021-02-03 17:19:59 +000083// Forward declarations
84namespace ast {
85class VariableDeclStatement;
86} // namespace ast
87
Ben Claytona6b9a8e2021-01-26 16:57:10 +000088class CloneContext;
89
90/// ProgramBuilder is a mutable builder for a Program.
91/// To construct a Program, populate the builder and then `std::move` it to a
92/// Program.
93class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +000094 /// VarOptionals is a helper for accepting a number of optional, extra
95 /// arguments for Var() and Global().
96 struct VarOptionals {
97 template <typename... ARGS>
98 explicit VarOptionals(ARGS&&... args) {
99 Apply(std::forward<ARGS>(args)...);
100 }
101 ~VarOptionals();
102
103 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000104 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000105 ast::Expression* constructor = nullptr;
106 ast::DecorationList decorations = {};
107
108 private:
109 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000110 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000111 void Set(ast::Expression* c) { constructor = c; }
112 void Set(const ast::DecorationList& l) { decorations = l; }
113
114 template <typename FIRST, typename... ARGS>
115 void Apply(FIRST&& first, ARGS&&... args) {
116 Set(std::forward<FIRST>(first));
117 Apply(std::forward<ARGS>(args)...);
118 }
119 void Apply() {}
120 };
121
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000122 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000123 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
124 using ASTNodeAllocator = BlockAllocator<ast::Node>;
125
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000126 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
127 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000128
129 /// `i32` is a type alias to `int`.
130 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
131 /// WGSL syntax.
132 /// Note: this is intentionally not aliased to uint32_t as we want integer
133 /// literals passed to the builder to match WGSL's integer literal types.
134 using i32 = decltype(1);
135 /// `u32` is a type alias to `unsigned int`.
136 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
137 /// WGSL syntax.
138 /// Note: this is intentionally not aliased to uint32_t as we want integer
139 /// literals passed to the builder to match WGSL's integer literal types.
140 using u32 = decltype(1u);
141 /// `f32` is a type alias to `float`
142 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
143 /// WGSL syntax.
144 using f32 = float;
145
146 /// Constructor
147 ProgramBuilder();
148
149 /// Move constructor
150 /// @param rhs the builder to move
151 ProgramBuilder(ProgramBuilder&& rhs);
152
153 /// Destructor
154 virtual ~ProgramBuilder();
155
156 /// Move assignment operator
157 /// @param rhs the builder to move
158 /// @return this builder
159 ProgramBuilder& operator=(ProgramBuilder&& rhs);
160
Ben Claytone43c8302021-01-29 11:59:32 +0000161 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
162 /// making a deep clone of the Program contents.
163 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
164 /// existing immutable program.
165 /// As the returned ProgramBuilder wraps `program`, `program` must not be
166 /// destructed or assigned while using the returned ProgramBuilder.
167 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
168 /// function. See crbug.com/tint/460.
169 /// @param program the immutable Program to wrap
170 /// @return the ProgramBuilder that wraps `program`
171 static ProgramBuilder Wrap(const Program* program);
172
Ben Claytone6995de2021-04-13 23:27:27 +0000173 /// @returns the unique identifier for this program
174 ProgramID ID() const { return id_; }
175
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000176 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000177 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000178 AssertNotMoved();
179 return types_;
180 }
181
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000182 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000183 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000184 AssertNotMoved();
185 return types_;
186 }
187
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000188 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000189 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000190 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000191 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000192 }
193
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000194 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000195 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000196 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000197 return ast_nodes_;
198 }
199
200 /// @returns a reference to the program's semantic nodes storage
201 SemNodeAllocator& SemNodes() {
202 AssertNotMoved();
203 return sem_nodes_;
204 }
205
206 /// @returns a reference to the program's semantic nodes storage
207 const SemNodeAllocator& SemNodes() const {
208 AssertNotMoved();
209 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000210 }
211
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000212 /// @returns a reference to the program's AST root Module
213 ast::Module& AST() {
214 AssertNotMoved();
215 return *ast_;
216 }
217
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000218 /// @returns a reference to the program's AST root Module
219 const ast::Module& AST() const {
220 AssertNotMoved();
221 return *ast_;
222 }
223
224 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000225 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000226 AssertNotMoved();
227 return sem_;
228 }
229
230 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000231 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000232 AssertNotMoved();
233 return sem_;
234 }
235
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000236 /// @returns a reference to the program's SymbolTable
237 SymbolTable& Symbols() {
238 AssertNotMoved();
239 return symbols_;
240 }
241
Ben Clayton708dc2d2021-01-29 11:22:40 +0000242 /// @returns a reference to the program's SymbolTable
243 const SymbolTable& Symbols() const {
244 AssertNotMoved();
245 return symbols_;
246 }
247
Ben Clayton844217f2021-01-27 18:49:05 +0000248 /// @returns a reference to the program's diagnostics
249 diag::List& Diagnostics() {
250 AssertNotMoved();
251 return diagnostics_;
252 }
253
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000254 /// @returns a reference to the program's diagnostics
255 const diag::List& Diagnostics() const {
256 AssertNotMoved();
257 return diagnostics_;
258 }
259
Ben Clayton5f0ea112021-03-09 10:54:37 +0000260 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000261 /// @param enable the new flag value (defaults to true)
262 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
263
Ben Clayton5f0ea112021-03-09 10:54:37 +0000264 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000265 /// built.
266 bool ResolveOnBuild() const { return resolve_on_build_; }
267
Ben Clayton844217f2021-01-27 18:49:05 +0000268 /// @returns true if the program has no error diagnostics and is not missing
269 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000270 bool IsValid() const;
271
Ben Clayton708dc2d2021-01-29 11:22:40 +0000272 /// Writes a representation of the node to the output stream
273 /// @note unlike str(), to_str() does not automatically demangle the string.
274 /// @param node the AST node
275 /// @param out the stream to write to
276 /// @param indent number of spaces to indent the node when writing
277 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
278 node->to_str(Sem(), out, indent);
279 }
280
281 /// Returns a demangled, string representation of `node`.
282 /// @param node the AST node
283 /// @returns a string representation of the node
284 std::string str(const ast::Node* node) const;
285
Ben Clayton7fdfff12021-01-29 15:17:30 +0000286 /// Creates a new ast::Node owned by the ProgramBuilder. When the
287 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000288 /// @param source the Source of the node
289 /// @param args the arguments to pass to the type constructor
290 /// @returns the node pointer
291 template <typename T, typename... ARGS>
292 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
293 ARGS&&... args) {
294 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000295 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000296 }
297
Ben Clayton7fdfff12021-01-29 15:17:30 +0000298 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
299 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000300 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000301 /// When the ProgramBuilder is destructed, the ast::Node will also be
302 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000303 /// @returns the node pointer
304 template <typename T>
305 traits::EnableIfIsType<T, ast::Node>* create() {
306 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000307 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000308 }
309
Ben Clayton7fdfff12021-01-29 15:17:30 +0000310 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
311 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000312 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000313 /// When the ProgramBuilder is destructed, the ast::Node will also be
314 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000315 /// @param arg0 the first arguments to pass to the type constructor
316 /// @param args the remaining arguments to pass to the type constructor
317 /// @returns the node pointer
318 template <typename T, typename ARG0, typename... ARGS>
319 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
320 traits::IsTypeOrDerived<T, ast::Node>::value &&
321 !traits::IsTypeOrDerived<ARG0, Source>::value,
322 T>*
323 create(ARG0&& arg0, ARGS&&... args) {
324 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000325 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000326 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000327 }
328
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000329 /// Creates a new sem::Node owned by the ProgramBuilder.
330 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000331 /// destructed.
332 /// @param args the arguments to pass to the type constructor
333 /// @returns the node pointer
334 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000335 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
336 !traits::IsTypeOrDerived<T, sem::Type>::value,
337 T>*
338 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000339 AssertNotMoved();
340 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
341 }
342
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000343 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000344 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
345 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000346 /// Types are unique (de-aliased), and so calling create() for the same `T`
347 /// and arguments will return the same pointer.
348 /// @warning Use this method to acquire a type only if all of its type
349 /// information is provided in the constructor arguments `args`.<br>
350 /// If the type requires additional configuration after construction that
351 /// affect its fundamental type, build the type with `std::make_unique`, make
352 /// any necessary alterations and then call unique_type() instead.
353 /// @param args the arguments to pass to the type constructor
354 /// @returns the de-aliased type pointer
355 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000356 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
357 static_assert(std::is_base_of<sem::Type, T>::value,
358 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000359 AssertNotMoved();
360 return types_.Get<T>(std::forward<ARGS>(args)...);
361 }
362
363 /// Marks this builder as moved, preventing any further use of the builder.
364 void MarkAsMoved();
365
366 //////////////////////////////////////////////////////////////////////////////
367 // TypesBuilder
368 //////////////////////////////////////////////////////////////////////////////
369
370 /// TypesBuilder holds basic `tint` types and methods for constructing
371 /// complex types.
372 class TypesBuilder {
373 public:
374 /// Constructor
375 /// @param builder the program builder
376 explicit TypesBuilder(ProgramBuilder* builder);
377
378 /// @return the tint AST type for the C type `T`.
379 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000380 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000381 return CToAST<T>::get(this);
382 }
383
384 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000385 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000386
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000387 /// @param source the Source of the node
388 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000389 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000390 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000391 }
392
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000393 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000394 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000395
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000396 /// @param source the Source of the node
397 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000398 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000399 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000400 }
401
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000402 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000403 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000404
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000405 /// @param source the Source of the node
406 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000407 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000408 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000409 }
410
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000411 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000412 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000413
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000414 /// @param source the Source of the node
415 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000416 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000417 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000418 }
419
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000420 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000421 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000422
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000423 /// @param source the Source of the node
424 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000425 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000426 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000427 }
428
Antonio Maiorano25436862021-04-13 13:32:33 +0000429 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000430 /// @param n vector width in elements
431 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000432 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000433 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000434 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000435 }
436
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000437 /// @param source the Source of the node
438 /// @param type vector subtype
439 /// @param n vector width in elements
440 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000441 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000442 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000443 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000444 }
445
Antonio Maiorano25436862021-04-13 13:32:33 +0000446 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000447 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000448 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000449
450 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000451 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000452 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000453
454 /// @param type vector subtype
455 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000456 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000457
458 /// @param n vector width in elements
459 /// @return the tint AST type for a `n`-element vector of `type`.
460 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000461 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000462 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000463 }
464
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000465 /// @return the tint AST type for a 2-element vector of the C type `T`.
466 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000467 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000468 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000469 }
470
471 /// @return the tint AST type for a 3-element vector of the C type `T`.
472 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000473 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000474 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000475 }
476
477 /// @return the tint AST type for a 4-element vector of the C type `T`.
478 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000479 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000480 return vec4(Of<T>());
481 }
482
483 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000484 /// @param columns number of columns for the matrix
485 /// @param rows number of rows for the matrix
486 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000487 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000488 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000489 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000490 }
491
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000492 /// @param source the Source of the node
493 /// @param type matrix subtype
494 /// @param columns number of columns for the matrix
495 /// @param rows number of rows for the matrix
496 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000497 ast::Matrix* mat(const Source& source,
498 ast::Type* type,
499 uint32_t columns,
500 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000501 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000502 }
503
Ben Claytone204f272021-04-22 14:40:23 +0000504 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000505 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000506 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000507
508 /// @param type matrix subtype
509 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000510 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000511
512 /// @param type matrix subtype
513 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000514 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000515
516 /// @param type matrix subtype
517 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000518 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000519
520 /// @param type matrix subtype
521 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000522 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000523
524 /// @param type matrix subtype
525 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000526 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000527
528 /// @param type matrix subtype
529 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000530 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000531
532 /// @param type matrix subtype
533 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000534 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000535
536 /// @param type matrix subtype
537 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000538 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000539
Ben Claytone204f272021-04-22 14:40:23 +0000540 /// @param columns number of columns for the matrix
541 /// @param rows number of rows for the matrix
542 /// @return the tint AST type for a matrix of `type`
543 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000544 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000545 return mat(Of<T>(), columns, rows);
546 }
547
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000548 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
549 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000550 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000551 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000552 }
553
554 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
555 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000556 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000557 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000558 }
559
560 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
561 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000562 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000563 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000564 }
565
566 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
567 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000568 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000569 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000570 }
571
572 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
573 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000574 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000575 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000576 }
577
578 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
579 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000580 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000581 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000582 }
583
584 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
585 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000586 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000587 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000588 }
589
590 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
591 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000592 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000593 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000594 }
595
596 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
597 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000598 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000599 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000600 }
601
602 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000603 /// @param n the array size. 0 represents a runtime-array
604 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000605 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000606 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000607 uint32_t n = 0,
608 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000609 subtype = MaybeCreateTypename(subtype);
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 {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000622 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000623 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000624 }
625
Ben Claytonbab31972021-02-08 21:16:21 +0000626 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000627 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000628 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000629 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000630 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000631 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000632 ast::DecorationList decos;
633 if (stride) {
634 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
635 }
636 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000637 }
638
Ben Clayton0f88b312021-05-04 17:36:31 +0000639 /// @param source the Source of the node
640 /// @param subtype the array element type
641 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000642 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000643 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000644 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000645 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000646 uint32_t n,
647 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000648 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000649 ast::DecorationList decos;
650 if (stride) {
651 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
652 }
653 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000654 }
655
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000656 /// @return the tint AST type for an array of size `N` of type `T`
657 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000658 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000659 return array(Of<T>(), N);
660 }
661
Ben Claytonbab31972021-02-08 21:16:21 +0000662 /// @param stride the array stride
663 /// @return the tint AST type for an array of size `N` of type `T`
664 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000665 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000666 return array(Of<T>(), N, stride);
667 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000668
Ben Clayton0f88b312021-05-04 17:36:31 +0000669 /// Creates a type name
670 /// @param name the name
671 /// @returns the type name
672 template <typename NAME>
673 ast::TypeName* type_name(NAME&& name) const {
674 return builder->create<ast::TypeName>(
675 builder->Sym(std::forward<NAME>(name)));
676 }
677
678 /// Creates a type name
679 /// @param source the Source of the node
680 /// @param name the name
681 /// @returns the type name
682 template <typename NAME>
683 ast::TypeName* type_name(const Source& source, NAME&& name) const {
684 return builder->create<ast::TypeName>(
685 source, builder->Sym(std::forward<NAME>(name)));
686 }
687
Ben Clayton42d1e092021-02-02 14:29:15 +0000688 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000689 /// @param name the alias name
690 /// @param type the alias type
691 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000692 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000693 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000694 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000695 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000696 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000697 }
698
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000699 /// Creates an alias type
700 /// @param source the Source of the node
701 /// @param name the alias name
702 /// @param type the alias type
703 /// @returns the alias pointer
704 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000705 ast::Alias* alias(const Source& source,
706 NAME&& name,
707 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000708 type = MaybeCreateTypename(type);
709 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000710 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000711 }
712
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000713 /// @param type the type of the pointer
714 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000715 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000716 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000717 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000718 ast::StorageClass storage_class,
719 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000720 type = MaybeCreateTypename(type);
Ben Clayton18588542021-06-04 22:17:37 +0000721 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000722 }
723
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000724 /// @param source the Source of the node
725 /// @param type the type of the pointer
726 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000727 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000728 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000729 ast::Pointer* pointer(const Source& source,
730 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000731 ast::StorageClass storage_class,
732 ast::Access access = ast::Access::kUndefined) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000733 type = MaybeCreateTypename(type);
Ben Clayton18588542021-06-04 22:17:37 +0000734 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000735 }
736
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000737 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000738 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000739 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000740 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000741 ast::Pointer* pointer(ast::StorageClass storage_class,
742 ast::Access access = ast::Access::kUndefined) const {
743 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000744 }
745
Ben Claytone204f272021-04-22 14:40:23 +0000746 /// @param kind the kind of sampler
747 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000748 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000749 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000750 }
751
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000752 /// @param source the Source of the node
753 /// @param kind the kind of sampler
754 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000755 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000756 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000757 }
758
Ben Claytone204f272021-04-22 14:40:23 +0000759 /// @param dims the dimensionality of the texture
760 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000761 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000762 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000763 }
764
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000765 /// @param source the Source of the node
766 /// @param dims the dimensionality of the texture
767 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000768 ast::DepthTexture* depth_texture(const Source& source,
769 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000770 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000771 }
772
Ben Claytone204f272021-04-22 14:40:23 +0000773 /// @param dims the dimensionality of the texture
774 /// @param subtype the texture subtype.
775 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000776 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
777 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000778 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000779 }
780
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000781 /// @param source the Source of the node
782 /// @param dims the dimensionality of the texture
783 /// @param subtype the texture subtype.
784 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000785 ast::SampledTexture* sampled_texture(const Source& source,
786 ast::TextureDimension dims,
787 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000788 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000789 }
790
Ben Claytone204f272021-04-22 14:40:23 +0000791 /// @param dims the dimensionality of the texture
792 /// @param subtype the texture subtype.
793 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000794 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
795 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000796 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000797 }
798
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000799 /// @param source the Source of the node
800 /// @param dims the dimensionality of the texture
801 /// @param subtype the texture subtype.
802 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000803 ast::MultisampledTexture* multisampled_texture(const Source& source,
804 ast::TextureDimension dims,
805 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000806 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000807 }
808
Ben Claytone204f272021-04-22 14:40:23 +0000809 /// @param dims the dimensionality of the texture
810 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000811 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000812 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000813 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000814 ast::ImageFormat format,
815 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000816 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000817 return builder->create<ast::StorageTexture>(dims, format, subtype,
818 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000819 }
820
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000821 /// @param source the Source of the node
822 /// @param dims the dimensionality of the texture
823 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000824 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000825 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000826 ast::StorageTexture* storage_texture(const Source& source,
827 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000828 ast::ImageFormat format,
829 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000830 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000831 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
832 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000833 }
834
Brandon Jones7b257692021-05-17 17:40:17 +0000835 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000836 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000837 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000838 }
839
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000840 /// @param source the Source of the node
841 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000842 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000843 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000844 }
845
Ben Clayton19b03192021-05-20 15:04:08 +0000846 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
847 /// If ty is a ast::Struct or ast::Alias, the returned type is an
848 /// ast::TypeName of the given type's name, otherwise type is returned.
849 /// @param type the type
850 /// @return either type or a pointer to a new ast::TypeName
851 ast::Type* MaybeCreateTypename(ast::Type* type) const;
852
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000853 /// If ty is a ast::Struct or ast::Alias, the returned type is an
854 /// ast::TypeName of the given type's name, otherwise type is returned.
855 /// @param type the type
856 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton19b03192021-05-20 15:04:08 +0000857 const ast::Type* MaybeCreateTypename(const ast::Type* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000858
Ben Claytonf5f311e2021-04-28 14:31:23 +0000859 /// The ProgramBuilder
860 ProgramBuilder* const builder;
861
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000862 private:
863 /// CToAST<T> is specialized for various `T` types and each specialization
864 /// contains a single static `get()` method for obtaining the corresponding
865 /// AST type for the C type `T`.
866 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000867 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000868 template <typename T>
869 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000870 };
871
872 //////////////////////////////////////////////////////////////////////////////
873 // AST helper methods
874 //////////////////////////////////////////////////////////////////////////////
875
James Price65ae64d2021-04-29 12:59:14 +0000876 /// @return a new unnamed symbol
877 Symbol Sym() { return Symbols().New(); }
878
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000879 /// @param name the symbol string
880 /// @return a Symbol with the given name
881 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
882
883 /// @param sym the symbol
884 /// @return `sym`
885 Symbol Sym(Symbol sym) { return sym; }
886
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000887 /// @param expr the expression
888 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000889 template <typename T>
890 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
891 return expr;
892 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000893
Ben Claytonb8ea5912021-04-19 16:52:42 +0000894 /// Passthrough for nullptr
895 /// @return nullptr
896 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
897
Ben Claytonfe0910f2021-05-17 15:51:47 +0000898 /// @param source the source information
899 /// @param symbol the identifier symbol
900 /// @return an ast::IdentifierExpression with the given symbol
901 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
902 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000903 }
904
Ben Clayton46d78d72021-02-10 21:34:26 +0000905 /// @param symbol the identifier symbol
906 /// @return an ast::IdentifierExpression with the given symbol
907 ast::IdentifierExpression* Expr(Symbol symbol) {
908 return create<ast::IdentifierExpression>(symbol);
909 }
910
Ben Claytonfe0910f2021-05-17 15:51:47 +0000911 /// @param source the source information
912 /// @param variable the AST variable
913 /// @return an ast::IdentifierExpression with the variable's symbol
914 ast::IdentifierExpression* Expr(const Source& source,
915 ast::Variable* variable) {
916 return create<ast::IdentifierExpression>(source, variable->symbol());
917 }
918
Ben Claytonb8ea5912021-04-19 16:52:42 +0000919 /// @param variable the AST variable
920 /// @return an ast::IdentifierExpression with the variable's symbol
921 ast::IdentifierExpression* Expr(ast::Variable* variable) {
922 return create<ast::IdentifierExpression>(variable->symbol());
923 }
924
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000925 /// @param source the source information
926 /// @param name the identifier name
927 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000928 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
929 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
930 }
931
932 /// @param name the identifier name
933 /// @return an ast::IdentifierExpression with the given name
934 ast::IdentifierExpression* Expr(const char* name) {
935 return create<ast::IdentifierExpression>(Symbols().Register(name));
936 }
937
938 /// @param source the source information
939 /// @param name the identifier name
940 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000941 ast::IdentifierExpression* Expr(const Source& source,
942 const std::string& name) {
943 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
944 }
945
946 /// @param name the identifier name
947 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000948 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000949 return create<ast::IdentifierExpression>(Symbols().Register(name));
950 }
951
Ben Claytonfe0910f2021-05-17 15:51:47 +0000952 /// @param source the source information
953 /// @param value the boolean value
954 /// @return a Scalar constructor for the given value
955 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
956 return create<ast::ScalarConstructorExpression>(source, Literal(value));
957 }
958
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000959 /// @param value the boolean value
960 /// @return a Scalar constructor for the given value
961 ast::ScalarConstructorExpression* Expr(bool value) {
962 return create<ast::ScalarConstructorExpression>(Literal(value));
963 }
964
Ben Claytonfe0910f2021-05-17 15:51:47 +0000965 /// @param source the source information
966 /// @param value the float value
967 /// @return a Scalar constructor for the given value
968 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
969 return create<ast::ScalarConstructorExpression>(source, Literal(value));
970 }
971
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000972 /// @param value the float value
973 /// @return a Scalar constructor for the given value
974 ast::ScalarConstructorExpression* Expr(f32 value) {
975 return create<ast::ScalarConstructorExpression>(Literal(value));
976 }
977
Ben Claytonfe0910f2021-05-17 15:51:47 +0000978 /// @param source the source information
979 /// @param value the integer value
980 /// @return a Scalar constructor for the given value
981 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
982 return create<ast::ScalarConstructorExpression>(source, Literal(value));
983 }
984
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000985 /// @param value the integer value
986 /// @return a Scalar constructor for the given value
987 ast::ScalarConstructorExpression* Expr(i32 value) {
988 return create<ast::ScalarConstructorExpression>(Literal(value));
989 }
990
Ben Claytonfe0910f2021-05-17 15:51:47 +0000991 /// @param source the source information
992 /// @param value the unsigned int value
993 /// @return a Scalar constructor for the given value
994 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
995 return create<ast::ScalarConstructorExpression>(source, Literal(value));
996 }
997
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000998 /// @param value the unsigned int value
999 /// @return a Scalar constructor for the given value
1000 ast::ScalarConstructorExpression* Expr(u32 value) {
1001 return create<ast::ScalarConstructorExpression>(Literal(value));
1002 }
1003
1004 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1005 /// `list`.
1006 /// @param list the list to append too
1007 /// @param arg the arg to create
1008 template <typename ARG>
1009 void Append(ast::ExpressionList& list, ARG&& arg) {
1010 list.emplace_back(Expr(std::forward<ARG>(arg)));
1011 }
1012
1013 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1014 /// then appends them to `list`.
1015 /// @param list the list to append too
1016 /// @param arg0 the first argument
1017 /// @param args the rest of the arguments
1018 template <typename ARG0, typename... ARGS>
1019 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1020 Append(list, std::forward<ARG0>(arg0));
1021 Append(list, std::forward<ARGS>(args)...);
1022 }
1023
1024 /// @return an empty list of expressions
1025 ast::ExpressionList ExprList() { return {}; }
1026
1027 /// @param args the list of expressions
1028 /// @return the list of expressions converted to `ast::Expression`s using
1029 /// `Expr()`,
1030 template <typename... ARGS>
1031 ast::ExpressionList ExprList(ARGS&&... args) {
1032 ast::ExpressionList list;
1033 list.reserve(sizeof...(args));
1034 Append(list, std::forward<ARGS>(args)...);
1035 return list;
1036 }
1037
1038 /// @param list the list of expressions
1039 /// @return `list`
1040 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1041
1042 /// @param val the boolan value
1043 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001044 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001045
1046 /// @param val the float value
1047 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001048 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001049
1050 /// @param val the unsigned int value
1051 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001052 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001053
1054 /// @param val the integer value
1055 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001056 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001057
1058 /// @param args the arguments for the type constructor
1059 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1060 /// of `args` converted to `ast::Expression`s using `Expr()`
1061 template <typename T, typename... ARGS>
1062 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001063 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001064 }
1065
1066 /// @param type the type to construct
1067 /// @param args the arguments for the constructor
1068 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1069 /// values `args`.
1070 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001071 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001072 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001073 return create<ast::TypeConstructorExpression>(
1074 type, ExprList(std::forward<ARGS>(args)...));
1075 }
1076
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001077 /// Creates a constructor expression that constructs an object of
1078 /// `type` filled with `elem_value`. For example,
1079 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1080 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1081 /// @param type the type to construct
1082 /// @param elem_value the initial or element value (for vec and mat) to
1083 /// construct with
1084 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001085 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001086 int elem_value = 0);
1087
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001088 /// @param args the arguments for the vector constructor
1089 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1090 /// `T`, constructed with the values `args`.
1091 template <typename T, typename... ARGS>
1092 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001093 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001094 }
1095
1096 /// @param args the arguments for the vector constructor
1097 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1098 /// `T`, constructed with the values `args`.
1099 template <typename T, typename... ARGS>
1100 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001101 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001102 }
1103
1104 /// @param args the arguments for the vector constructor
1105 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1106 /// `T`, constructed with the values `args`.
1107 template <typename T, typename... ARGS>
1108 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001109 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001110 }
1111
1112 /// @param args the arguments for the matrix constructor
1113 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1114 /// `T`, constructed with the values `args`.
1115 template <typename T, typename... ARGS>
1116 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001117 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001118 }
1119
1120 /// @param args the arguments for the matrix constructor
1121 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1122 /// `T`, constructed with the values `args`.
1123 template <typename T, typename... ARGS>
1124 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001125 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001126 }
1127
1128 /// @param args the arguments for the matrix constructor
1129 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1130 /// `T`, constructed with the values `args`.
1131 template <typename T, typename... ARGS>
1132 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001133 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001134 }
1135
1136 /// @param args the arguments for the matrix constructor
1137 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1138 /// `T`, constructed with the values `args`.
1139 template <typename T, typename... ARGS>
1140 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001141 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001142 }
1143
1144 /// @param args the arguments for the matrix constructor
1145 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1146 /// `T`, constructed with the values `args`.
1147 template <typename T, typename... ARGS>
1148 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001149 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001150 }
1151
1152 /// @param args the arguments for the matrix constructor
1153 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1154 /// `T`, constructed with the values `args`.
1155 template <typename T, typename... ARGS>
1156 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001157 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001158 }
1159
1160 /// @param args the arguments for the matrix constructor
1161 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1162 /// `T`, constructed with the values `args`.
1163 template <typename T, typename... ARGS>
1164 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001165 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001166 }
1167
1168 /// @param args the arguments for the matrix constructor
1169 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1170 /// `T`, constructed with the values `args`.
1171 template <typename T, typename... ARGS>
1172 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001173 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001174 }
1175
1176 /// @param args the arguments for the matrix constructor
1177 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1178 /// `T`, constructed with the values `args`.
1179 template <typename T, typename... ARGS>
1180 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001181 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001182 }
1183
1184 /// @param args the arguments for the array constructor
1185 /// @return an `ast::TypeConstructorExpression` of an array with element type
1186 /// `T`, constructed with the values `args`.
1187 template <typename T, int N = 0, typename... ARGS>
1188 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001189 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001190 }
1191
1192 /// @param subtype the array element type
1193 /// @param n the array size. 0 represents a runtime-array.
1194 /// @param args the arguments for the array constructor
1195 /// @return an `ast::TypeConstructorExpression` of an array with element type
1196 /// `subtype`, constructed with the values `args`.
1197 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001198 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 uint32_t n,
1200 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001201 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001202 }
1203
1204 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001205 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001206 /// @param optional the optional variable settings.
1207 /// Can be any of the following, in any order:
1208 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001209 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001210 /// * ast::Expression* - specifies the variable's initializer expression
1211 /// * ast::DecorationList - specifies the variable's decorations
1212 /// Note that repeated arguments of the same type will use the last argument's
1213 /// value.
1214 /// @returns a `ast::Variable` with the given name, type and additional
1215 /// options
1216 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001217 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001218 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001219 OPTIONAL&&... optional) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001220 type = ty.MaybeCreateTypename(type);
Ben Clayton620d77e2021-06-04 19:55:08 +00001221 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1222 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001223 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001224 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001225 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001226
1227 /// @param source the variable source
1228 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001229 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001230 /// @param optional the optional variable settings.
1231 /// Can be any of the following, in any order:
1232 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001233 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001234 /// * ast::Expression* - specifies the variable's initializer expression
1235 /// * ast::DecorationList - specifies the variable's decorations
1236 /// Note that repeated arguments of the same type will use the last argument's
1237 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001239 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001240 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001241 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001242 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001243 OPTIONAL&&... optional) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001244 type = ty.MaybeCreateTypename(type);
Ben Clayton620d77e2021-06-04 19:55:08 +00001245 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1246 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001247 opts.storage, opts.access, type, false,
1248 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001249 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001251 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001252 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001253 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001254 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001255 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001256 template <typename NAME>
1257 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001258 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001259 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001260 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001261 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001262 return create<ast::Variable>(
1263 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1264 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001265 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001266
1267 /// @param source the variable source
1268 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001269 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001270 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001271 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001272 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001273 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001274 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001275 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001276 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001277 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001278 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001279 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001280 return create<ast::Variable>(
1281 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1282 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001283 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001284
James Pricedaf1f1c2021-04-08 22:15:48 +00001285 /// @param name the parameter name
1286 /// @param type the parameter type
1287 /// @param decorations optional parameter decorations
1288 /// @returns a constant `ast::Variable` with the given name and type
1289 template <typename NAME>
1290 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001291 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001292 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001293 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001294 return create<ast::Variable>(
1295 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1296 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001297 }
1298
1299 /// @param source the parameter source
1300 /// @param name the parameter name
1301 /// @param type the parameter type
1302 /// @param decorations optional parameter decorations
1303 /// @returns a constant `ast::Variable` with the given name and type
1304 template <typename NAME>
1305 ast::Variable* Param(const Source& source,
1306 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001307 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001308 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001309 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001310 return create<ast::Variable>(
1311 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1312 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001313 }
1314
Ben Clayton42708342021-04-21 17:55:12 +00001315 /// @param name the variable name
1316 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001317 /// @param optional the optional variable settings.
1318 /// Can be any of the following, in any order:
1319 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001320 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001321 /// * ast::Expression* - specifies the variable's initializer expression
1322 /// * ast::DecorationList - specifies the variable's decorations
1323 /// Note that repeated arguments of the same type will use the last argument's
1324 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001325 /// @returns a new `ast::Variable`, which is automatically registered as a
1326 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001327 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001328 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001329 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001330 OPTIONAL&&... optional) {
1331 auto* var = Var(std::forward<NAME>(name), type,
1332 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001333 AST().AddGlobalVariable(var);
1334 return var;
1335 }
1336
1337 /// @param source the variable source
1338 /// @param name the variable name
1339 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001340 /// @param optional the optional variable settings.
1341 /// Can be any of the following, in any order:
1342 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001343 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001344 /// * ast::Expression* - specifies the variable's initializer expression
1345 /// * ast::DecorationList - specifies the variable's decorations
1346 /// Note that repeated arguments of the same type will use the last argument's
1347 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001348 /// @returns a new `ast::Variable`, which is automatically registered as a
1349 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001350 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001351 ast::Variable* Global(const Source& source,
1352 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001353 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001354 OPTIONAL&&... optional) {
1355 auto* var = Var(source, std::forward<NAME>(name), type,
1356 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001357 AST().AddGlobalVariable(var);
1358 return var;
1359 }
1360
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001361 /// @param name the variable name
1362 /// @param type the variable type
1363 /// @param constructor constructor expression
1364 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001365 /// @returns a const `ast::Variable` constructed by calling Var() with the
1366 /// arguments of `args`, which is automatically registered as a global
1367 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001368 template <typename NAME>
1369 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001370 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001371 ast::Expression* constructor,
1372 ast::DecorationList decorations = {}) {
1373 auto* var = Const(std::forward<NAME>(name), type, constructor,
1374 std::move(decorations));
1375 AST().AddGlobalVariable(var);
1376 return var;
1377 }
1378
1379 /// @param source the variable source
1380 /// @param name the variable name
1381 /// @param type the variable type
1382 /// @param constructor constructor expression
1383 /// @param decorations optional variable decorations
1384 /// @returns a const `ast::Variable` constructed by calling Var() with the
1385 /// arguments of `args`, which is automatically registered as a global
1386 /// variable with the ast::Module.
1387 template <typename NAME>
1388 ast::Variable* GlobalConst(const Source& source,
1389 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001390 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001391 ast::Expression* constructor,
1392 ast::DecorationList decorations = {}) {
1393 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1394 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001395 AST().AddGlobalVariable(var);
1396 return var;
1397 }
1398
Ben Claytonfe0910f2021-05-17 15:51:47 +00001399 /// @param source the source information
1400 /// @param expr the expression to take the address of
1401 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1402 template <typename EXPR>
1403 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1404 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1405 Expr(std::forward<EXPR>(expr)));
1406 }
1407
1408 /// @param expr the expression to take the address of
1409 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1410 template <typename EXPR>
1411 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1412 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1413 Expr(std::forward<EXPR>(expr)));
1414 }
1415
1416 /// @param source the source information
1417 /// @param expr the expression to perform an indirection on
1418 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1419 template <typename EXPR>
1420 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1421 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1422 Expr(std::forward<EXPR>(expr)));
1423 }
1424
1425 /// @param expr the expression to perform an indirection on
1426 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1427 template <typename EXPR>
1428 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1429 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1430 Expr(std::forward<EXPR>(expr)));
1431 }
1432
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001433 /// @param func the function name
1434 /// @param args the function call arguments
1435 /// @returns a `ast::CallExpression` to the function `func`, with the
1436 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1437 template <typename NAME, typename... ARGS>
1438 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1439 return create<ast::CallExpression>(Expr(func),
1440 ExprList(std::forward<ARGS>(args)...));
1441 }
1442
1443 /// @param lhs the left hand argument to the addition operation
1444 /// @param rhs the right hand argument to the addition operation
1445 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1446 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001447 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001448 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1449 Expr(std::forward<LHS>(lhs)),
1450 Expr(std::forward<RHS>(rhs)));
1451 }
1452
1453 /// @param lhs the left hand argument to the subtraction operation
1454 /// @param rhs the right hand argument to the subtraction operation
1455 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1456 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001457 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001458 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1459 Expr(std::forward<LHS>(lhs)),
1460 Expr(std::forward<RHS>(rhs)));
1461 }
1462
1463 /// @param lhs the left hand argument to the multiplication operation
1464 /// @param rhs the right hand argument to the multiplication operation
1465 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1466 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001467 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001468 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1469 Expr(std::forward<LHS>(lhs)),
1470 Expr(std::forward<RHS>(rhs)));
1471 }
1472
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001473 /// @param source the source information
1474 /// @param lhs the left hand argument to the multiplication operation
1475 /// @param rhs the right hand argument to the multiplication operation
1476 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1477 template <typename LHS, typename RHS>
1478 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1479 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1480 Expr(std::forward<LHS>(lhs)),
1481 Expr(std::forward<RHS>(rhs)));
1482 }
1483
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001484 /// @param lhs the left hand argument to the division operation
1485 /// @param rhs the right hand argument to the division operation
1486 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1487 template <typename LHS, typename RHS>
1488 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1489 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1490 Expr(std::forward<LHS>(lhs)),
1491 Expr(std::forward<RHS>(rhs)));
1492 }
1493
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001494 /// @param arr the array argument for the array accessor expression
1495 /// @param idx the index argument for the array accessor expression
1496 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1497 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001498 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001499 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1500 Expr(std::forward<IDX>(idx)));
1501 }
1502
1503 /// @param obj the object for the member accessor expression
1504 /// @param idx the index argument for the array accessor expression
1505 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1506 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001507 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001508 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1509 Expr(std::forward<IDX>(idx)));
1510 }
1511
Ben Clayton42d1e092021-02-02 14:29:15 +00001512 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001513 /// @param val the offset value
1514 /// @returns the offset decoration pointer
1515 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1516 return create<ast::StructMemberOffsetDecoration>(source_, val);
1517 }
1518
Ben Claytond614dd52021-03-15 10:43:11 +00001519 /// Creates a ast::StructMemberSizeDecoration
1520 /// @param source the source information
1521 /// @param val the size value
1522 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001523 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1524 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001525 return create<ast::StructMemberSizeDecoration>(source, val);
1526 }
1527
1528 /// Creates a ast::StructMemberSizeDecoration
1529 /// @param val the size value
1530 /// @returns the size decoration pointer
1531 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1532 return create<ast::StructMemberSizeDecoration>(source_, val);
1533 }
1534
1535 /// Creates a ast::StructMemberAlignDecoration
1536 /// @param source the source information
1537 /// @param val the align value
1538 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001539 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1540 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001541 return create<ast::StructMemberAlignDecoration>(source, val);
1542 }
1543
1544 /// Creates a ast::StructMemberAlignDecoration
1545 /// @param val the align value
1546 /// @returns the align decoration pointer
1547 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1548 return create<ast::StructMemberAlignDecoration>(source_, val);
1549 }
1550
Ben Clayton42d1e092021-02-02 14:29:15 +00001551 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001552 /// @param source the source information
1553 /// @param name the function name
1554 /// @param params the function parameters
1555 /// @param type the function return type
1556 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001557 /// @param decorations the optional function decorations
1558 /// @param return_type_decorations the optional function return type
1559 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001560 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001561 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001562 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001563 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001564 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001565 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001566 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001567 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001568 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001569 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001570 auto* func =
1571 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1572 type, create<ast::BlockStatement>(body),
1573 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001574 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001575 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001576 }
1577
Ben Clayton42d1e092021-02-02 14:29:15 +00001578 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001579 /// @param name the function name
1580 /// @param params the function parameters
1581 /// @param type the function return type
1582 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001583 /// @param decorations the optional function decorations
1584 /// @param return_type_decorations the optional function return type
1585 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001586 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001587 template <typename NAME>
1588 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001589 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001590 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001591 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001592 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001593 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001594 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001595 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1596 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001597 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001598 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001599 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001600 }
1601
Ben Clayton49668bb2021-04-17 05:32:01 +00001602 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001603 /// @param source the source information
1604 /// @returns the return statement pointer
1605 ast::ReturnStatement* Return(const Source& source) {
1606 return create<ast::ReturnStatement>(source);
1607 }
1608
1609 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001610 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001611 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1612
1613 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001614 /// @param source the source information
1615 /// @param val the return value
1616 /// @returns the return statement pointer
1617 template <typename EXPR>
1618 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1619 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1620 }
1621
1622 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001623 /// @param val the return value
1624 /// @returns the return statement pointer
1625 template <typename EXPR>
1626 ast::ReturnStatement* Return(EXPR&& val) {
1627 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001628 }
1629
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001630 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001631 /// @param source the source information
1632 /// @param name the struct name
1633 /// @param members the struct members
1634 /// @param decorations the optional struct decorations
1635 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001636 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001637 ast::Struct* Structure(const Source& source,
1638 NAME&& name,
1639 ast::StructMemberList members,
1640 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001641 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001642 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001643 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001644 AST().AddConstructedType(type);
1645 return type;
1646 }
1647
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001648 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001649 /// @param name the struct name
1650 /// @param members the struct members
1651 /// @param decorations the optional struct decorations
1652 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001653 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001654 ast::Struct* Structure(NAME&& name,
1655 ast::StructMemberList members,
1656 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001657 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001658 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001659 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001660 AST().AddConstructedType(type);
1661 return type;
1662 }
1663
Ben Clayton42d1e092021-02-02 14:29:15 +00001664 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001665 /// @param source the source information
1666 /// @param name the struct member name
1667 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001668 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001669 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001670 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001671 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001672 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001673 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001674 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001675 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001676 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1677 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001678 }
1679
Ben Clayton42d1e092021-02-02 14:29:15 +00001680 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001681 /// @param name the struct member name
1682 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001683 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001684 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001685 template <typename NAME>
1686 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001687 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001688 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001689 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001690 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1691 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001692 }
1693
Ben Claytonbab31972021-02-08 21:16:21 +00001694 /// Creates a ast::StructMember with the given byte offset
1695 /// @param offset the offset to use in the StructMemberOffsetDecoration
1696 /// @param name the struct member name
1697 /// @param type the struct member type
1698 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001699 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001700 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001701 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001702 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001703 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001704 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001705 create<ast::StructMemberOffsetDecoration>(offset),
1706 });
1707 }
1708
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001709 /// Creates a ast::BlockStatement with input statements
1710 /// @param statements statements of block
1711 /// @returns the block statement pointer
1712 template <typename... Statements>
1713 ast::BlockStatement* Block(Statements&&... statements) {
1714 return create<ast::BlockStatement>(
1715 ast::StatementList{std::forward<Statements>(statements)...});
1716 }
1717
1718 /// Creates a ast::ElseStatement with input condition and body
1719 /// @param condition the else condition expression
1720 /// @param body the else body
1721 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001722 template <typename CONDITION>
1723 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1724 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1725 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001726 }
1727
1728 /// Creates a ast::IfStatement with input condition, body, and optional
1729 /// variadic else statements
1730 /// @param condition the if statement condition expression
1731 /// @param body the if statement body
1732 /// @param elseStatements optional variadic else statements
1733 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001734 template <typename CONDITION, typename... ELSE_STATEMENTS>
1735 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001736 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001737 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001738 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001739 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001740 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001741 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001742 }
1743
1744 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001745 /// @param source the source information
1746 /// @param lhs the left hand side expression initializer
1747 /// @param rhs the right hand side expression initializer
1748 /// @returns the assignment statement pointer
1749 template <typename LhsExpressionInit, typename RhsExpressionInit>
1750 ast::AssignmentStatement* Assign(const Source& source,
1751 LhsExpressionInit&& lhs,
1752 RhsExpressionInit&& rhs) {
1753 return create<ast::AssignmentStatement>(
1754 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1755 Expr(std::forward<RhsExpressionInit>(rhs)));
1756 }
1757
1758 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001759 /// @param lhs the left hand side expression initializer
1760 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001761 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001762 template <typename LhsExpressionInit, typename RhsExpressionInit>
1763 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1764 RhsExpressionInit&& rhs) {
1765 return create<ast::AssignmentStatement>(
1766 Expr(std::forward<LhsExpressionInit>(lhs)),
1767 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001768 }
1769
1770 /// Creates a ast::LoopStatement with input body and optional continuing
1771 /// @param body the loop body
1772 /// @param continuing the optional continuing block
1773 /// @returns the loop statement pointer
1774 ast::LoopStatement* Loop(ast::BlockStatement* body,
1775 ast::BlockStatement* continuing = nullptr) {
1776 return create<ast::LoopStatement>(body, continuing);
1777 }
1778
1779 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001780 /// @param source the source information
1781 /// @param var the variable to wrap in a decl statement
1782 /// @returns the variable decl statement pointer
1783 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1784 return create<ast::VariableDeclStatement>(source, var);
1785 }
1786
1787 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001788 /// @param var the variable to wrap in a decl statement
1789 /// @returns the variable decl statement pointer
1790 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1791 return create<ast::VariableDeclStatement>(var);
1792 }
1793
Antonio Maioranocea744d2021-03-25 12:55:27 +00001794 /// Creates a ast::SwitchStatement with input expression and cases
1795 /// @param condition the condition expression initializer
1796 /// @param cases case statements
1797 /// @returns the switch statement pointer
1798 template <typename ExpressionInit, typename... Cases>
1799 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1800 return create<ast::SwitchStatement>(
1801 Expr(std::forward<ExpressionInit>(condition)),
1802 ast::CaseStatementList{std::forward<Cases>(cases)...});
1803 }
1804
1805 /// Creates a ast::CaseStatement with input list of selectors, and body
1806 /// @param selectors list of selectors
1807 /// @param body the case body
1808 /// @returns the case statement pointer
1809 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1810 ast::BlockStatement* body = nullptr) {
1811 return create<ast::CaseStatement>(std::move(selectors),
1812 body ? body : Block());
1813 }
1814
1815 /// Convenient overload that takes a single selector
1816 /// @param selector a single case selector
1817 /// @param body the case body
1818 /// @returns the case statement pointer
1819 ast::CaseStatement* Case(ast::IntLiteral* selector,
1820 ast::BlockStatement* body = nullptr) {
1821 return Case(ast::CaseSelectorList{selector}, body);
1822 }
1823
1824 /// Convenience function that creates a 'default' ast::CaseStatement
1825 /// @param body the case body
1826 /// @returns the case statement pointer
1827 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1828 return Case(ast::CaseSelectorList{}, body);
1829 }
1830
James Price68f558f2021-04-06 15:51:47 +00001831 /// Creates an ast::BuiltinDecoration
1832 /// @param source the source information
1833 /// @param builtin the builtin value
1834 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001835 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001836 return create<ast::BuiltinDecoration>(source, builtin);
1837 }
1838
1839 /// Creates an ast::BuiltinDecoration
1840 /// @param builtin the builtin value
1841 /// @returns the builtin decoration pointer
1842 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1843 return create<ast::BuiltinDecoration>(source_, builtin);
1844 }
1845
1846 /// Creates an ast::LocationDecoration
1847 /// @param source the source information
1848 /// @param location the location value
1849 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001850 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001851 return create<ast::LocationDecoration>(source, location);
1852 }
1853
1854 /// Creates an ast::LocationDecoration
1855 /// @param location the location value
1856 /// @returns the location decoration pointer
1857 ast::LocationDecoration* Location(uint32_t location) {
1858 return create<ast::LocationDecoration>(source_, location);
1859 }
1860
James Pricef2f3bfc2021-05-13 20:32:32 +00001861 /// Creates an ast::OverrideDecoration with a specific constant ID
1862 /// @param source the source information
1863 /// @param id the id value
1864 /// @returns the override decoration pointer
1865 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1866 return create<ast::OverrideDecoration>(source, id);
1867 }
1868
1869 /// Creates an ast::OverrideDecoration with a specific constant ID
1870 /// @param id the optional id value
1871 /// @returns the override decoration pointer
1872 ast::OverrideDecoration* Override(uint32_t id) {
1873 return Override(source_, id);
1874 }
1875
1876 /// Creates an ast::OverrideDecoration without a constant ID
1877 /// @param source the source information
1878 /// @returns the override decoration pointer
1879 ast::OverrideDecoration* Override(const Source& source) {
1880 return create<ast::OverrideDecoration>(source);
1881 }
1882
1883 /// Creates an ast::OverrideDecoration without a constant ID
1884 /// @returns the override decoration pointer
1885 ast::OverrideDecoration* Override() { return Override(source_); }
1886
James Price68f558f2021-04-06 15:51:47 +00001887 /// Creates an ast::StageDecoration
1888 /// @param source the source information
1889 /// @param stage the pipeline stage
1890 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001891 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001892 return create<ast::StageDecoration>(source, stage);
1893 }
1894
1895 /// Creates an ast::StageDecoration
1896 /// @param stage the pipeline stage
1897 /// @returns the stage decoration pointer
1898 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1899 return create<ast::StageDecoration>(source_, stage);
1900 }
1901
James Price70f80bb2021-05-19 13:40:08 +00001902 /// Creates an ast::WorkgroupDecoration
1903 /// @param x the x dimension expression
1904 /// @returns the workgroup decoration pointer
1905 template <typename EXPR_X>
1906 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1907 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1908 }
1909
1910 /// Creates an ast::WorkgroupDecoration
1911 /// @param x the x dimension expression
1912 /// @param y the y dimension expression
1913 /// @returns the workgroup decoration pointer
1914 template <typename EXPR_X, typename EXPR_Y>
1915 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1916 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1917 nullptr);
1918 }
1919
1920 /// Creates an ast::WorkgroupDecoration
1921 /// @param x the x dimension expression
1922 /// @param y the y dimension expression
1923 /// @param z the z dimension expression
1924 /// @returns the workgroup decoration pointer
1925 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1926 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1927 return create<ast::WorkgroupDecoration>(
1928 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1929 Expr(std::forward<EXPR_Z>(z)));
1930 }
1931
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001932 /// Sets the current builder source to `src`
1933 /// @param src the Source used for future create() calls
1934 void SetSource(const Source& src) {
1935 AssertNotMoved();
1936 source_ = src;
1937 }
1938
1939 /// Sets the current builder source to `loc`
1940 /// @param loc the Source used for future create() calls
1941 void SetSource(const Source::Location& loc) {
1942 AssertNotMoved();
1943 source_ = Source(loc);
1944 }
1945
Ben Clayton33352542021-01-29 16:43:41 +00001946 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001947 /// @note As the Resolver is run when the Program is built, this will only be
1948 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001949 /// @param expr the AST expression
1950 /// @return the resolved semantic type for the expression, or nullptr if the
1951 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001952 sem::Type* TypeOf(const ast::Expression* expr) const;
1953
Ben Claytonfe0910f2021-05-17 15:51:47 +00001954 /// Helper for returning the resolved semantic type of the variable `var`.
1955 /// @note As the Resolver is run when the Program is built, this will only be
1956 /// useful for the Resolver itself and tests that use their own Resolver.
1957 /// @param var the AST variable
1958 /// @return the resolved semantic type for the variable, or nullptr if the
1959 /// variable has no resolved type.
1960 sem::Type* TypeOf(const ast::Variable* var) const;
1961
Ben Claytonfbec46f2021-04-30 20:20:19 +00001962 /// Helper for returning the resolved semantic type of the AST type `type`.
1963 /// @note As the Resolver is run when the Program is built, this will only be
1964 /// useful for the Resolver itself and tests that use their own Resolver.
1965 /// @param expr the AST type
1966 /// @return the resolved semantic type for the type, or nullptr if the type
1967 /// has no resolved type.
1968 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001969
Ben Clayton169512e2021-04-17 05:52:11 +00001970 /// Wraps the ast::Literal in a statement. This is used by tests that
1971 /// construct a partial AST and require the Resolver to reach these
1972 /// nodes.
1973 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1974 /// @return the ast::Statement that wraps the ast::Statement
1975 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001976 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001977 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001978 /// nodes.
1979 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1980 /// @return the ast::Statement that wraps the ast::Expression
1981 ast::Statement* WrapInStatement(ast::Expression* expr);
1982 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001983 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001984 /// these nodes.
1985 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1986 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1987 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1988 /// Returns the statement argument. Used as a passthrough-overload by
1989 /// WrapInFunction().
1990 /// @param stmt the ast::Statement
1991 /// @return `stmt`
1992 ast::Statement* WrapInStatement(ast::Statement* stmt);
1993 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001994 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001995 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001996 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001997 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001998 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001999 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002000 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002001 }
2002 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002003 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002004 /// @returns the function
2005 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002006
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002007 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002008 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002009
2010 protected:
2011 /// Asserts that the builder has not been moved.
2012 void AssertNotMoved() const;
2013
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002014 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002015 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002016 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002017 ASTNodeAllocator ast_nodes_;
2018 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002019 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002020 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002021 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002022 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002023
2024 /// The source to use when creating AST nodes without providing a Source as
2025 /// the first argument.
2026 Source source_;
2027
Ben Clayton5f0ea112021-03-09 10:54:37 +00002028 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002029 /// program when built.
2030 bool resolve_on_build_ = true;
2031
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002032 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2033 bool moved_ = false;
2034};
2035
2036//! @cond Doxygen_Suppress
2037// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2038template <>
2039struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002040 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002041 return t->i32();
2042 }
2043};
2044template <>
2045struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002046 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002047 return t->u32();
2048 }
2049};
2050template <>
2051struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002052 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002053 return t->f32();
2054 }
2055};
2056template <>
2057struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002058 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002059 return t->bool_();
2060 }
2061};
2062template <>
2063struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002064 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002065 return t->void_();
2066 }
2067};
2068//! @endcond
2069
Ben Clayton917b14b2021-04-19 16:50:23 +00002070/// @param builder the ProgramBuilder
2071/// @returns the ProgramID of the ProgramBuilder
2072inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2073 return builder->ID();
2074}
2075
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002076} // namespace tint
2077
2078#endif // SRC_PROGRAM_BUILDER_H_