blob: 371ab4d040de709a07e5d38baa6360e173520676 [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 Claytone204f272021-04-22 14:40:23 +0000715 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000716 ast::Pointer* pointer(ast::Type* type,
717 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000718 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000719 return builder->create<ast::Pointer>(type, storage_class);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000720 }
721
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000722 /// @param source the Source of the node
723 /// @param type the type of the pointer
724 /// @param storage_class the storage class of the pointer
725 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000726 ast::Pointer* pointer(const Source& source,
727 ast::Type* type,
728 ast::StorageClass storage_class) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000729 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000730 return builder->create<ast::Pointer>(source, type, storage_class);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000731 }
732
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000733 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000734 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000735 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000736 ast::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000737 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000738 }
739
Ben Claytone204f272021-04-22 14:40:23 +0000740 /// @param kind the kind of sampler
741 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000742 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000743 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000744 }
745
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000746 /// @param source the Source of the node
747 /// @param kind the kind of sampler
748 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000749 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000750 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000751 }
752
Ben Claytone204f272021-04-22 14:40:23 +0000753 /// @param dims the dimensionality of the texture
754 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000755 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000756 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000757 }
758
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000759 /// @param source the Source of the node
760 /// @param dims the dimensionality of the texture
761 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000762 ast::DepthTexture* depth_texture(const Source& source,
763 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000764 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000765 }
766
Ben Claytone204f272021-04-22 14:40:23 +0000767 /// @param dims the dimensionality of the texture
768 /// @param subtype the texture subtype.
769 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000770 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
771 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000772 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000773 }
774
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000775 /// @param source the Source of the node
776 /// @param dims the dimensionality of the texture
777 /// @param subtype the texture subtype.
778 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000779 ast::SampledTexture* sampled_texture(const Source& source,
780 ast::TextureDimension dims,
781 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000782 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000783 }
784
Ben Claytone204f272021-04-22 14:40:23 +0000785 /// @param dims the dimensionality of the texture
786 /// @param subtype the texture subtype.
787 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000788 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
789 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000790 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000791 }
792
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000793 /// @param source the Source of the node
794 /// @param dims the dimensionality of the texture
795 /// @param subtype the texture subtype.
796 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000797 ast::MultisampledTexture* multisampled_texture(const Source& source,
798 ast::TextureDimension dims,
799 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000800 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000801 }
802
Ben Claytone204f272021-04-22 14:40:23 +0000803 /// @param dims the dimensionality of the texture
804 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000805 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000806 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000807 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000808 ast::ImageFormat format,
809 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000810 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000811 return builder->create<ast::StorageTexture>(dims, format, subtype,
812 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000813 }
814
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000815 /// @param source the Source of the node
816 /// @param dims the dimensionality of the texture
817 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000818 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000819 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000820 ast::StorageTexture* storage_texture(const Source& source,
821 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000822 ast::ImageFormat format,
823 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000824 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000825 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
826 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000827 }
828
Brandon Jones7b257692021-05-17 17:40:17 +0000829 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000830 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000831 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000832 }
833
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000834 /// @param source the Source of the node
835 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000836 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000837 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000838 }
839
Ben Clayton19b03192021-05-20 15:04:08 +0000840 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
841 /// If ty is a ast::Struct or ast::Alias, the returned type is an
842 /// ast::TypeName of the given type's name, otherwise type is returned.
843 /// @param type the type
844 /// @return either type or a pointer to a new ast::TypeName
845 ast::Type* MaybeCreateTypename(ast::Type* type) const;
846
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000847 /// 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
Ben Clayton19b03192021-05-20 15:04:08 +0000851 const ast::Type* MaybeCreateTypename(const ast::Type* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000852
Ben Claytonf5f311e2021-04-28 14:31:23 +0000853 /// The ProgramBuilder
854 ProgramBuilder* const builder;
855
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000856 private:
857 /// CToAST<T> is specialized for various `T` types and each specialization
858 /// contains a single static `get()` method for obtaining the corresponding
859 /// AST type for the C type `T`.
860 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000861 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000862 template <typename T>
863 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000864 };
865
866 //////////////////////////////////////////////////////////////////////////////
867 // AST helper methods
868 //////////////////////////////////////////////////////////////////////////////
869
James Price65ae64d2021-04-29 12:59:14 +0000870 /// @return a new unnamed symbol
871 Symbol Sym() { return Symbols().New(); }
872
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000873 /// @param name the symbol string
874 /// @return a Symbol with the given name
875 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
876
877 /// @param sym the symbol
878 /// @return `sym`
879 Symbol Sym(Symbol sym) { return sym; }
880
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000881 /// @param expr the expression
882 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000883 template <typename T>
884 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
885 return expr;
886 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000887
Ben Claytonb8ea5912021-04-19 16:52:42 +0000888 /// Passthrough for nullptr
889 /// @return nullptr
890 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
891
Ben Claytonfe0910f2021-05-17 15:51:47 +0000892 /// @param source the source information
893 /// @param symbol the identifier symbol
894 /// @return an ast::IdentifierExpression with the given symbol
895 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
896 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000897 }
898
Ben Clayton46d78d72021-02-10 21:34:26 +0000899 /// @param symbol the identifier symbol
900 /// @return an ast::IdentifierExpression with the given symbol
901 ast::IdentifierExpression* Expr(Symbol symbol) {
902 return create<ast::IdentifierExpression>(symbol);
903 }
904
Ben Claytonfe0910f2021-05-17 15:51:47 +0000905 /// @param source the source information
906 /// @param variable the AST variable
907 /// @return an ast::IdentifierExpression with the variable's symbol
908 ast::IdentifierExpression* Expr(const Source& source,
909 ast::Variable* variable) {
910 return create<ast::IdentifierExpression>(source, variable->symbol());
911 }
912
Ben Claytonb8ea5912021-04-19 16:52:42 +0000913 /// @param variable the AST variable
914 /// @return an ast::IdentifierExpression with the variable's symbol
915 ast::IdentifierExpression* Expr(ast::Variable* variable) {
916 return create<ast::IdentifierExpression>(variable->symbol());
917 }
918
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000919 /// @param source the source information
920 /// @param name the identifier name
921 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000922 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
923 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
924 }
925
926 /// @param name the identifier name
927 /// @return an ast::IdentifierExpression with the given name
928 ast::IdentifierExpression* Expr(const char* name) {
929 return create<ast::IdentifierExpression>(Symbols().Register(name));
930 }
931
932 /// @param source the source information
933 /// @param name the identifier name
934 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000935 ast::IdentifierExpression* Expr(const Source& source,
936 const std::string& name) {
937 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
938 }
939
940 /// @param name the identifier name
941 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000942 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000943 return create<ast::IdentifierExpression>(Symbols().Register(name));
944 }
945
Ben Claytonfe0910f2021-05-17 15:51:47 +0000946 /// @param source the source information
947 /// @param value the boolean value
948 /// @return a Scalar constructor for the given value
949 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
950 return create<ast::ScalarConstructorExpression>(source, Literal(value));
951 }
952
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000953 /// @param value the boolean value
954 /// @return a Scalar constructor for the given value
955 ast::ScalarConstructorExpression* Expr(bool value) {
956 return create<ast::ScalarConstructorExpression>(Literal(value));
957 }
958
Ben Claytonfe0910f2021-05-17 15:51:47 +0000959 /// @param source the source information
960 /// @param value the float value
961 /// @return a Scalar constructor for the given value
962 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
963 return create<ast::ScalarConstructorExpression>(source, Literal(value));
964 }
965
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000966 /// @param value the float value
967 /// @return a Scalar constructor for the given value
968 ast::ScalarConstructorExpression* Expr(f32 value) {
969 return create<ast::ScalarConstructorExpression>(Literal(value));
970 }
971
Ben Claytonfe0910f2021-05-17 15:51:47 +0000972 /// @param source the source information
973 /// @param value the integer value
974 /// @return a Scalar constructor for the given value
975 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
976 return create<ast::ScalarConstructorExpression>(source, Literal(value));
977 }
978
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000979 /// @param value the integer value
980 /// @return a Scalar constructor for the given value
981 ast::ScalarConstructorExpression* Expr(i32 value) {
982 return create<ast::ScalarConstructorExpression>(Literal(value));
983 }
984
Ben Claytonfe0910f2021-05-17 15:51:47 +0000985 /// @param source the source information
986 /// @param value the unsigned int value
987 /// @return a Scalar constructor for the given value
988 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
989 return create<ast::ScalarConstructorExpression>(source, Literal(value));
990 }
991
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000992 /// @param value the unsigned int value
993 /// @return a Scalar constructor for the given value
994 ast::ScalarConstructorExpression* Expr(u32 value) {
995 return create<ast::ScalarConstructorExpression>(Literal(value));
996 }
997
998 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
999 /// `list`.
1000 /// @param list the list to append too
1001 /// @param arg the arg to create
1002 template <typename ARG>
1003 void Append(ast::ExpressionList& list, ARG&& arg) {
1004 list.emplace_back(Expr(std::forward<ARG>(arg)));
1005 }
1006
1007 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1008 /// then appends them to `list`.
1009 /// @param list the list to append too
1010 /// @param arg0 the first argument
1011 /// @param args the rest of the arguments
1012 template <typename ARG0, typename... ARGS>
1013 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1014 Append(list, std::forward<ARG0>(arg0));
1015 Append(list, std::forward<ARGS>(args)...);
1016 }
1017
1018 /// @return an empty list of expressions
1019 ast::ExpressionList ExprList() { return {}; }
1020
1021 /// @param args the list of expressions
1022 /// @return the list of expressions converted to `ast::Expression`s using
1023 /// `Expr()`,
1024 template <typename... ARGS>
1025 ast::ExpressionList ExprList(ARGS&&... args) {
1026 ast::ExpressionList list;
1027 list.reserve(sizeof...(args));
1028 Append(list, std::forward<ARGS>(args)...);
1029 return list;
1030 }
1031
1032 /// @param list the list of expressions
1033 /// @return `list`
1034 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1035
1036 /// @param val the boolan value
1037 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001038 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001039
1040 /// @param val the float value
1041 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001042 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001043
1044 /// @param val the unsigned int value
1045 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001046 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001047
1048 /// @param val the integer value
1049 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001050 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001051
1052 /// @param args the arguments for the type constructor
1053 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1054 /// of `args` converted to `ast::Expression`s using `Expr()`
1055 template <typename T, typename... ARGS>
1056 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001057 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001058 }
1059
1060 /// @param type the type to construct
1061 /// @param args the arguments for the constructor
1062 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1063 /// values `args`.
1064 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001065 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001066 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001067 return create<ast::TypeConstructorExpression>(
1068 type, ExprList(std::forward<ARGS>(args)...));
1069 }
1070
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001071 /// Creates a constructor expression that constructs an object of
1072 /// `type` filled with `elem_value`. For example,
1073 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1074 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1075 /// @param type the type to construct
1076 /// @param elem_value the initial or element value (for vec and mat) to
1077 /// construct with
1078 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001079 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001080 int elem_value = 0);
1081
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001082 /// @param args the arguments for the vector constructor
1083 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1084 /// `T`, constructed with the values `args`.
1085 template <typename T, typename... ARGS>
1086 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001087 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001088 }
1089
1090 /// @param args the arguments for the vector constructor
1091 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1092 /// `T`, constructed with the values `args`.
1093 template <typename T, typename... ARGS>
1094 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001095 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001096 }
1097
1098 /// @param args the arguments for the vector constructor
1099 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1100 /// `T`, constructed with the values `args`.
1101 template <typename T, typename... ARGS>
1102 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001103 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001104 }
1105
1106 /// @param args the arguments for the matrix constructor
1107 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1108 /// `T`, constructed with the values `args`.
1109 template <typename T, typename... ARGS>
1110 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001111 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001112 }
1113
1114 /// @param args the arguments for the matrix constructor
1115 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1116 /// `T`, constructed with the values `args`.
1117 template <typename T, typename... ARGS>
1118 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001119 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001120 }
1121
1122 /// @param args the arguments for the matrix constructor
1123 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1124 /// `T`, constructed with the values `args`.
1125 template <typename T, typename... ARGS>
1126 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001127 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001128 }
1129
1130 /// @param args the arguments for the matrix constructor
1131 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1132 /// `T`, constructed with the values `args`.
1133 template <typename T, typename... ARGS>
1134 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001135 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001136 }
1137
1138 /// @param args the arguments for the matrix constructor
1139 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1140 /// `T`, constructed with the values `args`.
1141 template <typename T, typename... ARGS>
1142 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001143 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001144 }
1145
1146 /// @param args the arguments for the matrix constructor
1147 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1148 /// `T`, constructed with the values `args`.
1149 template <typename T, typename... ARGS>
1150 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001151 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001152 }
1153
1154 /// @param args the arguments for the matrix constructor
1155 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1156 /// `T`, constructed with the values `args`.
1157 template <typename T, typename... ARGS>
1158 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001159 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001160 }
1161
1162 /// @param args the arguments for the matrix constructor
1163 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1164 /// `T`, constructed with the values `args`.
1165 template <typename T, typename... ARGS>
1166 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001167 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001168 }
1169
1170 /// @param args the arguments for the matrix constructor
1171 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1172 /// `T`, constructed with the values `args`.
1173 template <typename T, typename... ARGS>
1174 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001175 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001176 }
1177
1178 /// @param args the arguments for the array constructor
1179 /// @return an `ast::TypeConstructorExpression` of an array with element type
1180 /// `T`, constructed with the values `args`.
1181 template <typename T, int N = 0, typename... ARGS>
1182 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001183 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001184 }
1185
1186 /// @param subtype the array element type
1187 /// @param n the array size. 0 represents a runtime-array.
1188 /// @param args the arguments for the array constructor
1189 /// @return an `ast::TypeConstructorExpression` of an array with element type
1190 /// `subtype`, constructed with the values `args`.
1191 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001192 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001193 uint32_t n,
1194 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001195 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001196 }
1197
1198 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001200 /// @param optional the optional variable settings.
1201 /// Can be any of the following, in any order:
1202 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001203 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001204 /// * ast::Expression* - specifies the variable's initializer expression
1205 /// * ast::DecorationList - specifies the variable's decorations
1206 /// Note that repeated arguments of the same type will use the last argument's
1207 /// value.
1208 /// @returns a `ast::Variable` with the given name, type and additional
1209 /// options
1210 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001211 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001212 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001213 OPTIONAL&&... optional) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001214 type = ty.MaybeCreateTypename(type);
Ben Clayton620d77e2021-06-04 19:55:08 +00001215 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1216 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001217 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001218 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001219 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001220
1221 /// @param source the variable source
1222 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001223 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001224 /// @param optional the optional variable settings.
1225 /// Can be any of the following, in any order:
1226 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001227 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001228 /// * ast::Expression* - specifies the variable's initializer expression
1229 /// * ast::DecorationList - specifies the variable's decorations
1230 /// Note that repeated arguments of the same type will use the last argument's
1231 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001232 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001233 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001234 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001235 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001236 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001237 OPTIONAL&&... optional) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001238 type = ty.MaybeCreateTypename(type);
Ben Clayton620d77e2021-06-04 19:55:08 +00001239 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1240 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001241 opts.storage, opts.access, type, false,
1242 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001243 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001244
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001245 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001246 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001247 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001248 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001249 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001250 template <typename NAME>
1251 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001252 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001253 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001254 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001255 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001256 return create<ast::Variable>(
1257 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1258 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001259 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001260
1261 /// @param source the variable source
1262 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001263 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001264 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001266 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001267 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001268 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001269 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001270 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001271 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001272 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001273 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001274 return create<ast::Variable>(
1275 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1276 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001277 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001278
James Pricedaf1f1c2021-04-08 22:15:48 +00001279 /// @param name the parameter name
1280 /// @param type the parameter type
1281 /// @param decorations optional parameter decorations
1282 /// @returns a constant `ast::Variable` with the given name and type
1283 template <typename NAME>
1284 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001285 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001286 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001287 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001288 return create<ast::Variable>(
1289 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1290 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001291 }
1292
1293 /// @param source the parameter source
1294 /// @param name the parameter name
1295 /// @param type the parameter type
1296 /// @param decorations optional parameter decorations
1297 /// @returns a constant `ast::Variable` with the given name and type
1298 template <typename NAME>
1299 ast::Variable* Param(const Source& source,
1300 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001301 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001302 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001303 type = ty.MaybeCreateTypename(type);
Ben Clayton93e8f522021-06-04 20:41:47 +00001304 return create<ast::Variable>(
1305 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1306 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001307 }
1308
Ben Clayton42708342021-04-21 17:55:12 +00001309 /// @param name the variable name
1310 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001311 /// @param optional the optional variable settings.
1312 /// Can be any of the following, in any order:
1313 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001314 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001315 /// * ast::Expression* - specifies the variable's initializer expression
1316 /// * ast::DecorationList - specifies the variable's decorations
1317 /// Note that repeated arguments of the same type will use the last argument's
1318 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001319 /// @returns a new `ast::Variable`, which is automatically registered as a
1320 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001321 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001322 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001323 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001324 OPTIONAL&&... optional) {
1325 auto* var = Var(std::forward<NAME>(name), type,
1326 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001327 AST().AddGlobalVariable(var);
1328 return var;
1329 }
1330
1331 /// @param source the variable source
1332 /// @param name the variable name
1333 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001334 /// @param optional the optional variable settings.
1335 /// Can be any of the following, in any order:
1336 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001337 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001338 /// * ast::Expression* - specifies the variable's initializer expression
1339 /// * ast::DecorationList - specifies the variable's decorations
1340 /// Note that repeated arguments of the same type will use the last argument's
1341 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001342 /// @returns a new `ast::Variable`, which is automatically registered as a
1343 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001344 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001345 ast::Variable* Global(const Source& source,
1346 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001347 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001348 OPTIONAL&&... optional) {
1349 auto* var = Var(source, std::forward<NAME>(name), type,
1350 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001351 AST().AddGlobalVariable(var);
1352 return var;
1353 }
1354
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001355 /// @param name the variable name
1356 /// @param type the variable type
1357 /// @param constructor constructor expression
1358 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001359 /// @returns a const `ast::Variable` constructed by calling Var() with the
1360 /// arguments of `args`, which is automatically registered as a global
1361 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001362 template <typename NAME>
1363 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001364 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001365 ast::Expression* constructor,
1366 ast::DecorationList decorations = {}) {
1367 auto* var = Const(std::forward<NAME>(name), type, constructor,
1368 std::move(decorations));
1369 AST().AddGlobalVariable(var);
1370 return var;
1371 }
1372
1373 /// @param source the variable source
1374 /// @param name the variable name
1375 /// @param type the variable type
1376 /// @param constructor constructor expression
1377 /// @param decorations optional variable decorations
1378 /// @returns a const `ast::Variable` constructed by calling Var() with the
1379 /// arguments of `args`, which is automatically registered as a global
1380 /// variable with the ast::Module.
1381 template <typename NAME>
1382 ast::Variable* GlobalConst(const Source& source,
1383 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001384 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001385 ast::Expression* constructor,
1386 ast::DecorationList decorations = {}) {
1387 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1388 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001389 AST().AddGlobalVariable(var);
1390 return var;
1391 }
1392
Ben Claytonfe0910f2021-05-17 15:51:47 +00001393 /// @param source the source information
1394 /// @param expr the expression to take the address of
1395 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1396 template <typename EXPR>
1397 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1398 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1399 Expr(std::forward<EXPR>(expr)));
1400 }
1401
1402 /// @param expr the expression to take the address of
1403 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1404 template <typename EXPR>
1405 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1406 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1407 Expr(std::forward<EXPR>(expr)));
1408 }
1409
1410 /// @param source the source information
1411 /// @param expr the expression to perform an indirection on
1412 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1413 template <typename EXPR>
1414 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1415 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1416 Expr(std::forward<EXPR>(expr)));
1417 }
1418
1419 /// @param expr the expression to perform an indirection on
1420 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1421 template <typename EXPR>
1422 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1423 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1424 Expr(std::forward<EXPR>(expr)));
1425 }
1426
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001427 /// @param func the function name
1428 /// @param args the function call arguments
1429 /// @returns a `ast::CallExpression` to the function `func`, with the
1430 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1431 template <typename NAME, typename... ARGS>
1432 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1433 return create<ast::CallExpression>(Expr(func),
1434 ExprList(std::forward<ARGS>(args)...));
1435 }
1436
1437 /// @param lhs the left hand argument to the addition operation
1438 /// @param rhs the right hand argument to the addition operation
1439 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1440 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001441 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001442 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1443 Expr(std::forward<LHS>(lhs)),
1444 Expr(std::forward<RHS>(rhs)));
1445 }
1446
1447 /// @param lhs the left hand argument to the subtraction operation
1448 /// @param rhs the right hand argument to the subtraction operation
1449 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1450 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001451 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001452 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1453 Expr(std::forward<LHS>(lhs)),
1454 Expr(std::forward<RHS>(rhs)));
1455 }
1456
1457 /// @param lhs the left hand argument to the multiplication operation
1458 /// @param rhs the right hand argument to the multiplication operation
1459 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1460 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001461 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001462 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1463 Expr(std::forward<LHS>(lhs)),
1464 Expr(std::forward<RHS>(rhs)));
1465 }
1466
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001467 /// @param source the source information
1468 /// @param lhs the left hand argument to the multiplication operation
1469 /// @param rhs the right hand argument to the multiplication operation
1470 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1471 template <typename LHS, typename RHS>
1472 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1473 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1474 Expr(std::forward<LHS>(lhs)),
1475 Expr(std::forward<RHS>(rhs)));
1476 }
1477
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001478 /// @param lhs the left hand argument to the division operation
1479 /// @param rhs the right hand argument to the division operation
1480 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1481 template <typename LHS, typename RHS>
1482 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1483 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1484 Expr(std::forward<LHS>(lhs)),
1485 Expr(std::forward<RHS>(rhs)));
1486 }
1487
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001488 /// @param arr the array argument for the array accessor expression
1489 /// @param idx the index argument for the array accessor expression
1490 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1491 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001492 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001493 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1494 Expr(std::forward<IDX>(idx)));
1495 }
1496
1497 /// @param obj the object for the member accessor expression
1498 /// @param idx the index argument for the array accessor expression
1499 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1500 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001501 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001502 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1503 Expr(std::forward<IDX>(idx)));
1504 }
1505
Ben Clayton42d1e092021-02-02 14:29:15 +00001506 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001507 /// @param val the offset value
1508 /// @returns the offset decoration pointer
1509 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1510 return create<ast::StructMemberOffsetDecoration>(source_, val);
1511 }
1512
Ben Claytond614dd52021-03-15 10:43:11 +00001513 /// Creates a ast::StructMemberSizeDecoration
1514 /// @param source the source information
1515 /// @param val the size value
1516 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001517 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1518 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001519 return create<ast::StructMemberSizeDecoration>(source, val);
1520 }
1521
1522 /// Creates a ast::StructMemberSizeDecoration
1523 /// @param val the size value
1524 /// @returns the size decoration pointer
1525 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1526 return create<ast::StructMemberSizeDecoration>(source_, val);
1527 }
1528
1529 /// Creates a ast::StructMemberAlignDecoration
1530 /// @param source the source information
1531 /// @param val the align value
1532 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001533 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1534 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001535 return create<ast::StructMemberAlignDecoration>(source, val);
1536 }
1537
1538 /// Creates a ast::StructMemberAlignDecoration
1539 /// @param val the align value
1540 /// @returns the align decoration pointer
1541 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1542 return create<ast::StructMemberAlignDecoration>(source_, val);
1543 }
1544
Ben Clayton42d1e092021-02-02 14:29:15 +00001545 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001546 /// @param source the source information
1547 /// @param name the function name
1548 /// @param params the function parameters
1549 /// @param type the function return type
1550 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001551 /// @param decorations the optional function decorations
1552 /// @param return_type_decorations the optional function return type
1553 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001554 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001555 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001556 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001557 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001558 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001559 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001560 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001561 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001562 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001563 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001564 auto* func =
1565 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1566 type, create<ast::BlockStatement>(body),
1567 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001568 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001569 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001570 }
1571
Ben Clayton42d1e092021-02-02 14:29:15 +00001572 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001573 /// @param name the function name
1574 /// @param params the function parameters
1575 /// @param type the function return type
1576 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001577 /// @param decorations the optional function decorations
1578 /// @param return_type_decorations the optional function return type
1579 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001580 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001581 template <typename NAME>
1582 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001583 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001584 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001585 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001586 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001587 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001588 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001589 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1590 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001591 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001592 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001593 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001594 }
1595
Ben Clayton49668bb2021-04-17 05:32:01 +00001596 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001597 /// @param source the source information
1598 /// @returns the return statement pointer
1599 ast::ReturnStatement* Return(const Source& source) {
1600 return create<ast::ReturnStatement>(source);
1601 }
1602
1603 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001604 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001605 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1606
1607 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001608 /// @param source the source information
1609 /// @param val the return value
1610 /// @returns the return statement pointer
1611 template <typename EXPR>
1612 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1613 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1614 }
1615
1616 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001617 /// @param val the return value
1618 /// @returns the return statement pointer
1619 template <typename EXPR>
1620 ast::ReturnStatement* Return(EXPR&& val) {
1621 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001622 }
1623
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001624 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001625 /// @param source the source information
1626 /// @param name the struct name
1627 /// @param members the struct members
1628 /// @param decorations the optional struct decorations
1629 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001630 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001631 ast::Struct* Structure(const Source& source,
1632 NAME&& name,
1633 ast::StructMemberList members,
1634 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001635 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001636 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001637 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001638 AST().AddConstructedType(type);
1639 return type;
1640 }
1641
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001642 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001643 /// @param name the struct name
1644 /// @param members the struct members
1645 /// @param decorations the optional struct decorations
1646 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001647 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001648 ast::Struct* Structure(NAME&& name,
1649 ast::StructMemberList members,
1650 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001651 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001652 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001653 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001654 AST().AddConstructedType(type);
1655 return type;
1656 }
1657
Ben Clayton42d1e092021-02-02 14:29:15 +00001658 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001659 /// @param source the source information
1660 /// @param name the struct member name
1661 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001662 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001663 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001664 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001665 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001666 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001667 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001668 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001669 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001670 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1671 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001672 }
1673
Ben Clayton42d1e092021-02-02 14:29:15 +00001674 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001675 /// @param name the struct member name
1676 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001677 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001678 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001679 template <typename NAME>
1680 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001681 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001682 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001683 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001684 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1685 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001686 }
1687
Ben Claytonbab31972021-02-08 21:16:21 +00001688 /// Creates a ast::StructMember with the given byte offset
1689 /// @param offset the offset to use in the StructMemberOffsetDecoration
1690 /// @param name the struct member name
1691 /// @param type the struct member type
1692 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001693 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001694 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001695 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001696 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001697 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001698 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001699 create<ast::StructMemberOffsetDecoration>(offset),
1700 });
1701 }
1702
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001703 /// Creates a ast::BlockStatement with input statements
1704 /// @param statements statements of block
1705 /// @returns the block statement pointer
1706 template <typename... Statements>
1707 ast::BlockStatement* Block(Statements&&... statements) {
1708 return create<ast::BlockStatement>(
1709 ast::StatementList{std::forward<Statements>(statements)...});
1710 }
1711
1712 /// Creates a ast::ElseStatement with input condition and body
1713 /// @param condition the else condition expression
1714 /// @param body the else body
1715 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001716 template <typename CONDITION>
1717 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1718 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1719 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001720 }
1721
1722 /// Creates a ast::IfStatement with input condition, body, and optional
1723 /// variadic else statements
1724 /// @param condition the if statement condition expression
1725 /// @param body the if statement body
1726 /// @param elseStatements optional variadic else statements
1727 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001728 template <typename CONDITION, typename... ELSE_STATEMENTS>
1729 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001730 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001731 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001732 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001733 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001734 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001735 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001736 }
1737
1738 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001739 /// @param source the source information
1740 /// @param lhs the left hand side expression initializer
1741 /// @param rhs the right hand side expression initializer
1742 /// @returns the assignment statement pointer
1743 template <typename LhsExpressionInit, typename RhsExpressionInit>
1744 ast::AssignmentStatement* Assign(const Source& source,
1745 LhsExpressionInit&& lhs,
1746 RhsExpressionInit&& rhs) {
1747 return create<ast::AssignmentStatement>(
1748 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1749 Expr(std::forward<RhsExpressionInit>(rhs)));
1750 }
1751
1752 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001753 /// @param lhs the left hand side expression initializer
1754 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001755 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001756 template <typename LhsExpressionInit, typename RhsExpressionInit>
1757 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1758 RhsExpressionInit&& rhs) {
1759 return create<ast::AssignmentStatement>(
1760 Expr(std::forward<LhsExpressionInit>(lhs)),
1761 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001762 }
1763
1764 /// Creates a ast::LoopStatement with input body and optional continuing
1765 /// @param body the loop body
1766 /// @param continuing the optional continuing block
1767 /// @returns the loop statement pointer
1768 ast::LoopStatement* Loop(ast::BlockStatement* body,
1769 ast::BlockStatement* continuing = nullptr) {
1770 return create<ast::LoopStatement>(body, continuing);
1771 }
1772
1773 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001774 /// @param source the source information
1775 /// @param var the variable to wrap in a decl statement
1776 /// @returns the variable decl statement pointer
1777 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1778 return create<ast::VariableDeclStatement>(source, var);
1779 }
1780
1781 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001782 /// @param var the variable to wrap in a decl statement
1783 /// @returns the variable decl statement pointer
1784 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1785 return create<ast::VariableDeclStatement>(var);
1786 }
1787
Antonio Maioranocea744d2021-03-25 12:55:27 +00001788 /// Creates a ast::SwitchStatement with input expression and cases
1789 /// @param condition the condition expression initializer
1790 /// @param cases case statements
1791 /// @returns the switch statement pointer
1792 template <typename ExpressionInit, typename... Cases>
1793 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1794 return create<ast::SwitchStatement>(
1795 Expr(std::forward<ExpressionInit>(condition)),
1796 ast::CaseStatementList{std::forward<Cases>(cases)...});
1797 }
1798
1799 /// Creates a ast::CaseStatement with input list of selectors, and body
1800 /// @param selectors list of selectors
1801 /// @param body the case body
1802 /// @returns the case statement pointer
1803 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1804 ast::BlockStatement* body = nullptr) {
1805 return create<ast::CaseStatement>(std::move(selectors),
1806 body ? body : Block());
1807 }
1808
1809 /// Convenient overload that takes a single selector
1810 /// @param selector a single case selector
1811 /// @param body the case body
1812 /// @returns the case statement pointer
1813 ast::CaseStatement* Case(ast::IntLiteral* selector,
1814 ast::BlockStatement* body = nullptr) {
1815 return Case(ast::CaseSelectorList{selector}, body);
1816 }
1817
1818 /// Convenience function that creates a 'default' ast::CaseStatement
1819 /// @param body the case body
1820 /// @returns the case statement pointer
1821 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1822 return Case(ast::CaseSelectorList{}, body);
1823 }
1824
James Price68f558f2021-04-06 15:51:47 +00001825 /// Creates an ast::BuiltinDecoration
1826 /// @param source the source information
1827 /// @param builtin the builtin value
1828 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001829 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001830 return create<ast::BuiltinDecoration>(source, builtin);
1831 }
1832
1833 /// Creates an ast::BuiltinDecoration
1834 /// @param builtin the builtin value
1835 /// @returns the builtin decoration pointer
1836 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1837 return create<ast::BuiltinDecoration>(source_, builtin);
1838 }
1839
1840 /// Creates an ast::LocationDecoration
1841 /// @param source the source information
1842 /// @param location the location value
1843 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001844 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001845 return create<ast::LocationDecoration>(source, location);
1846 }
1847
1848 /// Creates an ast::LocationDecoration
1849 /// @param location the location value
1850 /// @returns the location decoration pointer
1851 ast::LocationDecoration* Location(uint32_t location) {
1852 return create<ast::LocationDecoration>(source_, location);
1853 }
1854
James Pricef2f3bfc2021-05-13 20:32:32 +00001855 /// Creates an ast::OverrideDecoration with a specific constant ID
1856 /// @param source the source information
1857 /// @param id the id value
1858 /// @returns the override decoration pointer
1859 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1860 return create<ast::OverrideDecoration>(source, id);
1861 }
1862
1863 /// Creates an ast::OverrideDecoration with a specific constant ID
1864 /// @param id the optional id value
1865 /// @returns the override decoration pointer
1866 ast::OverrideDecoration* Override(uint32_t id) {
1867 return Override(source_, id);
1868 }
1869
1870 /// Creates an ast::OverrideDecoration without a constant ID
1871 /// @param source the source information
1872 /// @returns the override decoration pointer
1873 ast::OverrideDecoration* Override(const Source& source) {
1874 return create<ast::OverrideDecoration>(source);
1875 }
1876
1877 /// Creates an ast::OverrideDecoration without a constant ID
1878 /// @returns the override decoration pointer
1879 ast::OverrideDecoration* Override() { return Override(source_); }
1880
James Price68f558f2021-04-06 15:51:47 +00001881 /// Creates an ast::StageDecoration
1882 /// @param source the source information
1883 /// @param stage the pipeline stage
1884 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001885 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001886 return create<ast::StageDecoration>(source, stage);
1887 }
1888
1889 /// Creates an ast::StageDecoration
1890 /// @param stage the pipeline stage
1891 /// @returns the stage decoration pointer
1892 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1893 return create<ast::StageDecoration>(source_, stage);
1894 }
1895
James Price70f80bb2021-05-19 13:40:08 +00001896 /// Creates an ast::WorkgroupDecoration
1897 /// @param x the x dimension expression
1898 /// @returns the workgroup decoration pointer
1899 template <typename EXPR_X>
1900 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1901 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1902 }
1903
1904 /// Creates an ast::WorkgroupDecoration
1905 /// @param x the x dimension expression
1906 /// @param y the y dimension expression
1907 /// @returns the workgroup decoration pointer
1908 template <typename EXPR_X, typename EXPR_Y>
1909 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1910 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1911 nullptr);
1912 }
1913
1914 /// Creates an ast::WorkgroupDecoration
1915 /// @param x the x dimension expression
1916 /// @param y the y dimension expression
1917 /// @param z the z dimension expression
1918 /// @returns the workgroup decoration pointer
1919 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1920 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1921 return create<ast::WorkgroupDecoration>(
1922 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1923 Expr(std::forward<EXPR_Z>(z)));
1924 }
1925
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001926 /// Sets the current builder source to `src`
1927 /// @param src the Source used for future create() calls
1928 void SetSource(const Source& src) {
1929 AssertNotMoved();
1930 source_ = src;
1931 }
1932
1933 /// Sets the current builder source to `loc`
1934 /// @param loc the Source used for future create() calls
1935 void SetSource(const Source::Location& loc) {
1936 AssertNotMoved();
1937 source_ = Source(loc);
1938 }
1939
Ben Clayton33352542021-01-29 16:43:41 +00001940 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001941 /// @note As the Resolver is run when the Program is built, this will only be
1942 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001943 /// @param expr the AST expression
1944 /// @return the resolved semantic type for the expression, or nullptr if the
1945 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001946 sem::Type* TypeOf(const ast::Expression* expr) const;
1947
Ben Claytonfe0910f2021-05-17 15:51:47 +00001948 /// Helper for returning the resolved semantic type of the variable `var`.
1949 /// @note As the Resolver is run when the Program is built, this will only be
1950 /// useful for the Resolver itself and tests that use their own Resolver.
1951 /// @param var the AST variable
1952 /// @return the resolved semantic type for the variable, or nullptr if the
1953 /// variable has no resolved type.
1954 sem::Type* TypeOf(const ast::Variable* var) const;
1955
Ben Claytonfbec46f2021-04-30 20:20:19 +00001956 /// Helper for returning the resolved semantic type of the AST type `type`.
1957 /// @note As the Resolver is run when the Program is built, this will only be
1958 /// useful for the Resolver itself and tests that use their own Resolver.
1959 /// @param expr the AST type
1960 /// @return the resolved semantic type for the type, or nullptr if the type
1961 /// has no resolved type.
1962 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001963
Ben Clayton169512e2021-04-17 05:52:11 +00001964 /// Wraps the ast::Literal in a statement. This is used by tests that
1965 /// construct a partial AST and require the Resolver to reach these
1966 /// nodes.
1967 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1968 /// @return the ast::Statement that wraps the ast::Statement
1969 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001970 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001971 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001972 /// nodes.
1973 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1974 /// @return the ast::Statement that wraps the ast::Expression
1975 ast::Statement* WrapInStatement(ast::Expression* expr);
1976 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001977 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001978 /// these nodes.
1979 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1980 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1981 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1982 /// Returns the statement argument. Used as a passthrough-overload by
1983 /// WrapInFunction().
1984 /// @param stmt the ast::Statement
1985 /// @return `stmt`
1986 ast::Statement* WrapInStatement(ast::Statement* stmt);
1987 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001988 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001989 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001990 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001991 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001992 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001993 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001994 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001995 }
1996 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001997 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001998 /// @returns the function
1999 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002000
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002001 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002002 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002003
2004 protected:
2005 /// Asserts that the builder has not been moved.
2006 void AssertNotMoved() const;
2007
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002008 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002009 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002010 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002011 ASTNodeAllocator ast_nodes_;
2012 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002013 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002014 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002015 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002016 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002017
2018 /// The source to use when creating AST nodes without providing a Source as
2019 /// the first argument.
2020 Source source_;
2021
Ben Clayton5f0ea112021-03-09 10:54:37 +00002022 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002023 /// program when built.
2024 bool resolve_on_build_ = true;
2025
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002026 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2027 bool moved_ = false;
2028};
2029
2030//! @cond Doxygen_Suppress
2031// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2032template <>
2033struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002034 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002035 return t->i32();
2036 }
2037};
2038template <>
2039struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
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->u32();
2042 }
2043};
2044template <>
2045struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
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->f32();
2048 }
2049};
2050template <>
2051struct ProgramBuilder::TypesBuilder::CToAST<bool> {
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->bool_();
2054 }
2055};
2056template <>
2057struct ProgramBuilder::TypesBuilder::CToAST<void> {
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->void_();
2060 }
2061};
2062//! @endcond
2063
Ben Clayton917b14b2021-04-19 16:50:23 +00002064/// @param builder the ProgramBuilder
2065/// @returns the ProgramID of the ProgramBuilder
2066inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2067 return builder->ID();
2068}
2069
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002070} // namespace tint
2071
2072#endif // SRC_PROGRAM_BUILDER_H_