blob: a1c7b46e75b11be889d4990eb45ceda376f6c4db [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Clayton8c7f0da2021-06-17 15:48:39 +000025#include "src/ast/atomic.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000026#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000027#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000028#include "src/ast/bool_literal.h"
29#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000030#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000031#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000032#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000033#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000035#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000036#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000037#include "src/ast/if_statement.h"
38#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000039#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000040#include "src/ast/member_accessor_expression.h"
41#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000043#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000045#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000046#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000047#include "src/ast/scalar_constructor_expression.h"
48#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000049#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000050#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000051#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000054#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000055#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000056#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000057#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000058#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000059#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000060#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000061#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000062#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000063#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000064#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000065#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000066#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000067#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000068#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000069#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000070#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000071#include "src/sem/f32_type.h"
72#include "src/sem/i32_type.h"
73#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000075#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000076#include "src/sem/sampled_texture_type.h"
77#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000078#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000079#include "src/sem/u32_type.h"
80#include "src/sem/vector_type.h"
81#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000082
Ben Clayton03d10722021-06-05 12:45:50 +000083#ifdef INCLUDE_TINT_TINT_H_
84#error "internal tint header being #included from tint.h"
85#endif
86
Ben Claytona6b9a8e2021-01-26 16:57:10 +000087namespace tint {
88
Ben Clayton401b96b2021-02-03 17:19:59 +000089// Forward declarations
90namespace ast {
91class VariableDeclStatement;
92} // namespace ast
93
Ben Claytona6b9a8e2021-01-26 16:57:10 +000094class CloneContext;
95
96/// ProgramBuilder is a mutable builder for a Program.
97/// To construct a Program, populate the builder and then `std::move` it to a
98/// Program.
99class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +0000100 /// VarOptionals is a helper for accepting a number of optional, extra
101 /// arguments for Var() and Global().
102 struct VarOptionals {
103 template <typename... ARGS>
104 explicit VarOptionals(ARGS&&... args) {
105 Apply(std::forward<ARGS>(args)...);
106 }
107 ~VarOptionals();
108
109 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000110 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000111 ast::Expression* constructor = nullptr;
112 ast::DecorationList decorations = {};
113
114 private:
115 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000116 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000117 void Set(ast::Expression* c) { constructor = c; }
118 void Set(const ast::DecorationList& l) { decorations = l; }
119
120 template <typename FIRST, typename... ARGS>
121 void Apply(FIRST&& first, ARGS&&... args) {
122 Set(std::forward<FIRST>(first));
123 Apply(std::forward<ARGS>(args)...);
124 }
125 void Apply() {}
126 };
127
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000128 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000129 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
130 using ASTNodeAllocator = BlockAllocator<ast::Node>;
131
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000132 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
133 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000134
135 /// `i32` is a type alias to `int`.
136 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
137 /// WGSL syntax.
138 /// Note: this is intentionally not aliased to uint32_t as we want integer
139 /// literals passed to the builder to match WGSL's integer literal types.
140 using i32 = decltype(1);
141 /// `u32` is a type alias to `unsigned int`.
142 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
143 /// WGSL syntax.
144 /// Note: this is intentionally not aliased to uint32_t as we want integer
145 /// literals passed to the builder to match WGSL's integer literal types.
146 using u32 = decltype(1u);
147 /// `f32` is a type alias to `float`
148 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
149 /// WGSL syntax.
150 using f32 = float;
151
152 /// Constructor
153 ProgramBuilder();
154
155 /// Move constructor
156 /// @param rhs the builder to move
157 ProgramBuilder(ProgramBuilder&& rhs);
158
159 /// Destructor
160 virtual ~ProgramBuilder();
161
162 /// Move assignment operator
163 /// @param rhs the builder to move
164 /// @return this builder
165 ProgramBuilder& operator=(ProgramBuilder&& rhs);
166
Ben Claytone43c8302021-01-29 11:59:32 +0000167 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
168 /// making a deep clone of the Program contents.
169 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
170 /// existing immutable program.
171 /// As the returned ProgramBuilder wraps `program`, `program` must not be
172 /// destructed or assigned while using the returned ProgramBuilder.
173 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
174 /// function. See crbug.com/tint/460.
175 /// @param program the immutable Program to wrap
176 /// @return the ProgramBuilder that wraps `program`
177 static ProgramBuilder Wrap(const Program* program);
178
Ben Claytone6995de2021-04-13 23:27:27 +0000179 /// @returns the unique identifier for this program
180 ProgramID ID() const { return id_; }
181
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000182 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000183 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000184 AssertNotMoved();
185 return types_;
186 }
187
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000188 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000189 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000190 AssertNotMoved();
191 return types_;
192 }
193
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000194 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000195 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000196 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000197 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000198 }
199
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000200 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000201 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000202 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000203 return ast_nodes_;
204 }
205
206 /// @returns a reference to the program's semantic nodes storage
207 SemNodeAllocator& SemNodes() {
208 AssertNotMoved();
209 return sem_nodes_;
210 }
211
212 /// @returns a reference to the program's semantic nodes storage
213 const SemNodeAllocator& SemNodes() const {
214 AssertNotMoved();
215 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000216 }
217
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000218 /// @returns a reference to the program's AST root Module
219 ast::Module& AST() {
220 AssertNotMoved();
221 return *ast_;
222 }
223
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000224 /// @returns a reference to the program's AST root Module
225 const ast::Module& AST() const {
226 AssertNotMoved();
227 return *ast_;
228 }
229
230 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000231 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000232 AssertNotMoved();
233 return sem_;
234 }
235
236 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000237 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000238 AssertNotMoved();
239 return sem_;
240 }
241
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000242 /// @returns a reference to the program's SymbolTable
243 SymbolTable& Symbols() {
244 AssertNotMoved();
245 return symbols_;
246 }
247
Ben Clayton708dc2d2021-01-29 11:22:40 +0000248 /// @returns a reference to the program's SymbolTable
249 const SymbolTable& Symbols() const {
250 AssertNotMoved();
251 return symbols_;
252 }
253
Ben Clayton844217f2021-01-27 18:49:05 +0000254 /// @returns a reference to the program's diagnostics
255 diag::List& Diagnostics() {
256 AssertNotMoved();
257 return diagnostics_;
258 }
259
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000260 /// @returns a reference to the program's diagnostics
261 const diag::List& Diagnostics() const {
262 AssertNotMoved();
263 return diagnostics_;
264 }
265
Ben Clayton5f0ea112021-03-09 10:54:37 +0000266 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000267 /// @param enable the new flag value (defaults to true)
268 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
269
Ben Clayton5f0ea112021-03-09 10:54:37 +0000270 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000271 /// built.
272 bool ResolveOnBuild() const { return resolve_on_build_; }
273
Ben Clayton844217f2021-01-27 18:49:05 +0000274 /// @returns true if the program has no error diagnostics and is not missing
275 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000276 bool IsValid() const;
277
Ben Clayton708dc2d2021-01-29 11:22:40 +0000278 /// Writes a representation of the node to the output stream
279 /// @note unlike str(), to_str() does not automatically demangle the string.
280 /// @param node the AST node
281 /// @param out the stream to write to
282 /// @param indent number of spaces to indent the node when writing
283 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
284 node->to_str(Sem(), out, indent);
285 }
286
287 /// Returns a demangled, string representation of `node`.
288 /// @param node the AST node
289 /// @returns a string representation of the node
290 std::string str(const ast::Node* node) const;
291
Ben Clayton7fdfff12021-01-29 15:17:30 +0000292 /// Creates a new ast::Node owned by the ProgramBuilder. When the
293 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000294 /// @param source the Source of the node
295 /// @param args the arguments to pass to the type constructor
296 /// @returns the node pointer
297 template <typename T, typename... ARGS>
298 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
299 ARGS&&... args) {
300 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000301 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000302 }
303
Ben Clayton7fdfff12021-01-29 15:17:30 +0000304 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
305 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000306 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000307 /// When the ProgramBuilder is destructed, the ast::Node will also be
308 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000309 /// @returns the node pointer
310 template <typename T>
311 traits::EnableIfIsType<T, ast::Node>* create() {
312 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000313 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000314 }
315
Ben Clayton7fdfff12021-01-29 15:17:30 +0000316 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
317 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000318 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000319 /// When the ProgramBuilder is destructed, the ast::Node will also be
320 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000321 /// @param arg0 the first arguments to pass to the type constructor
322 /// @param args the remaining arguments to pass to the type constructor
323 /// @returns the node pointer
324 template <typename T, typename ARG0, typename... ARGS>
325 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
326 traits::IsTypeOrDerived<T, ast::Node>::value &&
327 !traits::IsTypeOrDerived<ARG0, Source>::value,
328 T>*
329 create(ARG0&& arg0, ARGS&&... args) {
330 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000331 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000332 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000333 }
334
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000335 /// Creates a new sem::Node owned by the ProgramBuilder.
336 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000337 /// destructed.
338 /// @param args the arguments to pass to the type constructor
339 /// @returns the node pointer
340 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000341 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
342 !traits::IsTypeOrDerived<T, sem::Type>::value,
343 T>*
344 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000345 AssertNotMoved();
346 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
347 }
348
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000349 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000350 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
351 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000352 /// Types are unique (de-aliased), and so calling create() for the same `T`
353 /// and arguments will return the same pointer.
354 /// @warning Use this method to acquire a type only if all of its type
355 /// information is provided in the constructor arguments `args`.<br>
356 /// If the type requires additional configuration after construction that
357 /// affect its fundamental type, build the type with `std::make_unique`, make
358 /// any necessary alterations and then call unique_type() instead.
359 /// @param args the arguments to pass to the type constructor
360 /// @returns the de-aliased type pointer
361 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000362 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
363 static_assert(std::is_base_of<sem::Type, T>::value,
364 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000365 AssertNotMoved();
366 return types_.Get<T>(std::forward<ARGS>(args)...);
367 }
368
369 /// Marks this builder as moved, preventing any further use of the builder.
370 void MarkAsMoved();
371
372 //////////////////////////////////////////////////////////////////////////////
373 // TypesBuilder
374 //////////////////////////////////////////////////////////////////////////////
375
376 /// TypesBuilder holds basic `tint` types and methods for constructing
377 /// complex types.
378 class TypesBuilder {
379 public:
380 /// Constructor
381 /// @param builder the program builder
382 explicit TypesBuilder(ProgramBuilder* builder);
383
384 /// @return the tint AST type for the C type `T`.
385 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000386 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000387 return CToAST<T>::get(this);
388 }
389
390 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000391 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000392
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000393 /// @param source the Source of the node
394 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000395 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000396 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000397 }
398
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000399 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000400 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000401
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000402 /// @param source the Source of the node
403 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000404 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000405 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000406 }
407
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000408 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000409 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000410
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000411 /// @param source the Source of the node
412 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000413 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000414 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000415 }
416
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000417 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000418 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000419
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 /// @param source the Source of the node
421 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000422 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000423 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000424 }
425
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000426 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000427 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000428
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000429 /// @param source the Source of the node
430 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000431 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000432 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000433 }
434
Antonio Maiorano25436862021-04-13 13:32:33 +0000435 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000436 /// @param n vector width in elements
437 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000438 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000439 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000440 }
441
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000442 /// @param source the Source of the node
443 /// @param type vector subtype
444 /// @param n vector width in elements
445 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000446 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000447 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000448 }
449
Antonio Maiorano25436862021-04-13 13:32:33 +0000450 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000451 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000452 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000453
454 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000455 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000456 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000457
458 /// @param type vector subtype
459 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000460 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000461
462 /// @param n vector width in elements
463 /// @return the tint AST type for a `n`-element vector of `type`.
464 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000465 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000466 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000467 }
468
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000469 /// @return the tint AST type for a 2-element vector of the C type `T`.
470 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000471 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000472 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000473 }
474
475 /// @return the tint AST type for a 3-element vector of the C type `T`.
476 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000477 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000478 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000479 }
480
481 /// @return the tint AST type for a 4-element vector of the C type `T`.
482 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000483 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000484 return vec4(Of<T>());
485 }
486
487 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000488 /// @param columns number of columns for the matrix
489 /// @param rows number of rows for the matrix
490 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000491 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000492 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000493 }
494
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000495 /// @param source the Source of the node
496 /// @param type matrix subtype
497 /// @param columns number of columns for the matrix
498 /// @param rows number of rows for the matrix
499 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000500 ast::Matrix* mat(const Source& source,
501 ast::Type* type,
502 uint32_t columns,
503 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000504 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000505 }
506
Ben Claytone204f272021-04-22 14:40:23 +0000507 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000508 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000509 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000510
511 /// @param type matrix subtype
512 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000513 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000514
515 /// @param type matrix subtype
516 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000517 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000518
519 /// @param type matrix subtype
520 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000521 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000522
523 /// @param type matrix subtype
524 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000525 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000526
527 /// @param type matrix subtype
528 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000529 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000530
531 /// @param type matrix subtype
532 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000533 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000534
535 /// @param type matrix subtype
536 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000537 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000538
539 /// @param type matrix subtype
540 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000541 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000542
Ben Claytone204f272021-04-22 14:40:23 +0000543 /// @param columns number of columns for the matrix
544 /// @param rows number of rows for the matrix
545 /// @return the tint AST type for a matrix of `type`
546 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000547 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000548 return mat(Of<T>(), columns, rows);
549 }
550
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000551 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
552 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000553 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000554 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 }
556
557 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
558 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000559 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000560 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 }
562
563 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
564 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000565 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000566 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 }
568
569 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
570 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000571 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000572 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
575 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
576 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000577 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000578 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 }
580
581 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
582 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000583 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000584 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000585 }
586
587 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
588 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000589 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000590 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000591 }
592
593 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
594 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000595 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000596 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000597 }
598
599 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
600 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000601 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000602 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000603 }
604
605 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000606 /// @param n the array size. 0 represents a runtime-array
607 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000608 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000609 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000610 uint32_t n = 0,
611 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000612 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000613 }
614
615 /// @param source the Source of the node
616 /// @param subtype the array element type
617 /// @param n the array size. 0 represents a runtime-array
618 /// @param decos the optional decorations for the array
619 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000620 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000621 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000622 uint32_t n = 0,
623 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000624 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000625 }
626
Ben Claytonbab31972021-02-08 21:16:21 +0000627 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000628 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000629 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000630 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000631 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000632 ast::DecorationList decos;
633 if (stride) {
634 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
635 }
636 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000637 }
638
Ben Clayton0f88b312021-05-04 17:36:31 +0000639 /// @param source the Source of the node
640 /// @param subtype the array element type
641 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000642 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000643 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000644 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000645 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000646 uint32_t n,
647 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000648 ast::DecorationList decos;
649 if (stride) {
650 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
651 }
652 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000653 }
654
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000655 /// @return the tint AST type for an array of size `N` of type `T`
656 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000657 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000658 return array(Of<T>(), N);
659 }
660
Ben Claytonbab31972021-02-08 21:16:21 +0000661 /// @param stride the array stride
662 /// @return the tint AST type for an array of size `N` of type `T`
663 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000664 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000665 return array(Of<T>(), N, stride);
666 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000667
Ben Clayton0f88b312021-05-04 17:36:31 +0000668 /// Creates a type name
669 /// @param name the name
670 /// @returns the type name
671 template <typename NAME>
672 ast::TypeName* type_name(NAME&& name) const {
673 return builder->create<ast::TypeName>(
674 builder->Sym(std::forward<NAME>(name)));
675 }
676
677 /// Creates a type name
678 /// @param source the Source of the node
679 /// @param name the name
680 /// @returns the type name
681 template <typename NAME>
682 ast::TypeName* type_name(const Source& source, NAME&& name) const {
683 return builder->create<ast::TypeName>(
684 source, builder->Sym(std::forward<NAME>(name)));
685 }
686
Ben Clayton42d1e092021-02-02 14:29:15 +0000687 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000688 /// @param name the alias name
689 /// @param type the alias type
690 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000691 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000692 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000693 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000694 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000695 }
696
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000697 /// Creates an alias type
698 /// @param source the Source of the node
699 /// @param name the alias name
700 /// @param type the alias type
701 /// @returns the alias pointer
702 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000703 ast::Alias* alias(const Source& source,
704 NAME&& name,
705 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000706 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000707 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000708 }
709
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000710 /// @param type the type of the pointer
711 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000712 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000713 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000714 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000715 ast::StorageClass storage_class,
716 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000717 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000718 }
719
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000720 /// @param source the Source of the node
721 /// @param type the type of the pointer
722 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000723 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000724 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000725 ast::Pointer* pointer(const Source& source,
726 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000727 ast::StorageClass storage_class,
728 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000729 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000730 }
731
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000732 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000733 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000734 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000735 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000736 ast::Pointer* pointer(ast::StorageClass storage_class,
737 ast::Access access = ast::Access::kUndefined) const {
738 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000739 }
740
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000741 /// @param source the Source of the node
742 /// @param type the type of the atomic
743 /// @return the atomic to `type`
744 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
745 return builder->create<ast::Atomic>(source, type);
746 }
747
Ben Clayton313e6182021-06-17 19:56:14 +0000748 /// @param type the type of the atomic
749 /// @return the atomic to `type`
750 ast::Atomic* atomic(ast::Type* type) const {
751 return builder->create<ast::Atomic>(type);
752 }
753
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000754 /// @return the atomic to type `T`
755 template <typename T>
756 ast::Atomic* atomic() const {
757 return atomic(Of<T>());
758 }
759
Ben Claytone204f272021-04-22 14:40:23 +0000760 /// @param kind the kind of sampler
761 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000762 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000763 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000764 }
765
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000766 /// @param source the Source of the node
767 /// @param kind the kind of sampler
768 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000769 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000770 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000771 }
772
Ben Claytone204f272021-04-22 14:40:23 +0000773 /// @param dims the dimensionality of the texture
774 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000775 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000776 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000777 }
778
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000779 /// @param source the Source of the node
780 /// @param dims the dimensionality of the texture
781 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000782 ast::DepthTexture* depth_texture(const Source& source,
783 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000784 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000785 }
786
Ben Claytone204f272021-04-22 14:40:23 +0000787 /// @param dims the dimensionality of the texture
788 /// @param subtype the texture subtype.
789 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000790 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
791 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000792 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000793 }
794
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000795 /// @param source the Source of the node
796 /// @param dims the dimensionality of the texture
797 /// @param subtype the texture subtype.
798 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000799 ast::SampledTexture* sampled_texture(const Source& source,
800 ast::TextureDimension dims,
801 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000802 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000803 }
804
Ben Claytone204f272021-04-22 14:40:23 +0000805 /// @param dims the dimensionality of the texture
806 /// @param subtype the texture subtype.
807 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000808 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
809 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000810 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000811 }
812
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000813 /// @param source the Source of the node
814 /// @param dims the dimensionality of the texture
815 /// @param subtype the texture subtype.
816 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000817 ast::MultisampledTexture* multisampled_texture(const Source& source,
818 ast::TextureDimension dims,
819 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000820 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000821 }
822
Ben Claytone204f272021-04-22 14:40:23 +0000823 /// @param dims the dimensionality of the texture
824 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000825 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000826 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000827 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000828 ast::ImageFormat format,
829 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000830 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000831 return builder->create<ast::StorageTexture>(dims, format, subtype,
832 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000833 }
834
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000835 /// @param source the Source of the node
836 /// @param dims the dimensionality of the texture
837 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000838 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000839 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000840 ast::StorageTexture* storage_texture(const Source& source,
841 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000842 ast::ImageFormat format,
843 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000844 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000845 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
846 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000847 }
848
Brandon Jones7b257692021-05-17 17:40:17 +0000849 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000850 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000851 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000852 }
853
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000854 /// @param source the Source of the node
855 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000856 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000857 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000858 }
859
Ben Clayton19b03192021-05-20 15:04:08 +0000860 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000861 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000862 /// @param type the type
863 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000864 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000865
Ben Clayton8758f102021-06-09 14:32:14 +0000866 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000867 /// @param type the type
868 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000869 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000870
Ben Claytonf5f311e2021-04-28 14:31:23 +0000871 /// The ProgramBuilder
872 ProgramBuilder* const builder;
873
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000874 private:
875 /// CToAST<T> is specialized for various `T` types and each specialization
876 /// contains a single static `get()` method for obtaining the corresponding
877 /// AST type for the C type `T`.
878 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000879 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000880 template <typename T>
881 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000882 };
883
884 //////////////////////////////////////////////////////////////////////////////
885 // AST helper methods
886 //////////////////////////////////////////////////////////////////////////////
887
James Price65ae64d2021-04-29 12:59:14 +0000888 /// @return a new unnamed symbol
889 Symbol Sym() { return Symbols().New(); }
890
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000891 /// @param name the symbol string
892 /// @return a Symbol with the given name
893 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
894
895 /// @param sym the symbol
896 /// @return `sym`
897 Symbol Sym(Symbol sym) { return sym; }
898
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 /// @param expr the expression
900 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000901 template <typename T>
902 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
903 return expr;
904 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000905
Ben Claytonb8ea5912021-04-19 16:52:42 +0000906 /// Passthrough for nullptr
907 /// @return nullptr
908 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
909
Ben Claytonfe0910f2021-05-17 15:51:47 +0000910 /// @param source the source information
911 /// @param symbol the identifier symbol
912 /// @return an ast::IdentifierExpression with the given symbol
913 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
914 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000915 }
916
Ben Clayton46d78d72021-02-10 21:34:26 +0000917 /// @param symbol the identifier symbol
918 /// @return an ast::IdentifierExpression with the given symbol
919 ast::IdentifierExpression* Expr(Symbol symbol) {
920 return create<ast::IdentifierExpression>(symbol);
921 }
922
Ben Claytonfe0910f2021-05-17 15:51:47 +0000923 /// @param source the source information
924 /// @param variable the AST variable
925 /// @return an ast::IdentifierExpression with the variable's symbol
926 ast::IdentifierExpression* Expr(const Source& source,
927 ast::Variable* variable) {
928 return create<ast::IdentifierExpression>(source, variable->symbol());
929 }
930
Ben Claytonb8ea5912021-04-19 16:52:42 +0000931 /// @param variable the AST variable
932 /// @return an ast::IdentifierExpression with the variable's symbol
933 ast::IdentifierExpression* Expr(ast::Variable* variable) {
934 return create<ast::IdentifierExpression>(variable->symbol());
935 }
936
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000937 /// @param source the source information
938 /// @param name the identifier name
939 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000940 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
941 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
942 }
943
944 /// @param name the identifier name
945 /// @return an ast::IdentifierExpression with the given name
946 ast::IdentifierExpression* Expr(const char* name) {
947 return create<ast::IdentifierExpression>(Symbols().Register(name));
948 }
949
950 /// @param source the source information
951 /// @param name the identifier name
952 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000953 ast::IdentifierExpression* Expr(const Source& source,
954 const std::string& name) {
955 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
956 }
957
958 /// @param name the identifier name
959 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000960 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000961 return create<ast::IdentifierExpression>(Symbols().Register(name));
962 }
963
Ben Claytonfe0910f2021-05-17 15:51:47 +0000964 /// @param source the source information
965 /// @param value the boolean value
966 /// @return a Scalar constructor for the given value
967 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
968 return create<ast::ScalarConstructorExpression>(source, Literal(value));
969 }
970
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000971 /// @param value the boolean value
972 /// @return a Scalar constructor for the given value
973 ast::ScalarConstructorExpression* Expr(bool value) {
974 return create<ast::ScalarConstructorExpression>(Literal(value));
975 }
976
Ben Claytonfe0910f2021-05-17 15:51:47 +0000977 /// @param source the source information
978 /// @param value the float value
979 /// @return a Scalar constructor for the given value
980 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
981 return create<ast::ScalarConstructorExpression>(source, Literal(value));
982 }
983
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000984 /// @param value the float value
985 /// @return a Scalar constructor for the given value
986 ast::ScalarConstructorExpression* Expr(f32 value) {
987 return create<ast::ScalarConstructorExpression>(Literal(value));
988 }
989
Ben Claytonfe0910f2021-05-17 15:51:47 +0000990 /// @param source the source information
991 /// @param value the integer value
992 /// @return a Scalar constructor for the given value
993 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
994 return create<ast::ScalarConstructorExpression>(source, Literal(value));
995 }
996
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000997 /// @param value the integer value
998 /// @return a Scalar constructor for the given value
999 ast::ScalarConstructorExpression* Expr(i32 value) {
1000 return create<ast::ScalarConstructorExpression>(Literal(value));
1001 }
1002
Ben Claytonfe0910f2021-05-17 15:51:47 +00001003 /// @param source the source information
1004 /// @param value the unsigned int value
1005 /// @return a Scalar constructor for the given value
1006 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1007 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1008 }
1009
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001010 /// @param value the unsigned int value
1011 /// @return a Scalar constructor for the given value
1012 ast::ScalarConstructorExpression* Expr(u32 value) {
1013 return create<ast::ScalarConstructorExpression>(Literal(value));
1014 }
1015
1016 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1017 /// `list`.
1018 /// @param list the list to append too
1019 /// @param arg the arg to create
1020 template <typename ARG>
1021 void Append(ast::ExpressionList& list, ARG&& arg) {
1022 list.emplace_back(Expr(std::forward<ARG>(arg)));
1023 }
1024
1025 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1026 /// then appends them to `list`.
1027 /// @param list the list to append too
1028 /// @param arg0 the first argument
1029 /// @param args the rest of the arguments
1030 template <typename ARG0, typename... ARGS>
1031 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1032 Append(list, std::forward<ARG0>(arg0));
1033 Append(list, std::forward<ARGS>(args)...);
1034 }
1035
1036 /// @return an empty list of expressions
1037 ast::ExpressionList ExprList() { return {}; }
1038
1039 /// @param args the list of expressions
1040 /// @return the list of expressions converted to `ast::Expression`s using
1041 /// `Expr()`,
1042 template <typename... ARGS>
1043 ast::ExpressionList ExprList(ARGS&&... args) {
1044 ast::ExpressionList list;
1045 list.reserve(sizeof...(args));
1046 Append(list, std::forward<ARGS>(args)...);
1047 return list;
1048 }
1049
1050 /// @param list the list of expressions
1051 /// @return `list`
1052 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1053
1054 /// @param val the boolan value
1055 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001056 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001057
1058 /// @param val the float value
1059 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001060 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001061
1062 /// @param val the unsigned int value
1063 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001064 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001065
1066 /// @param val the integer value
1067 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001068 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001069
1070 /// @param args the arguments for the type constructor
1071 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1072 /// of `args` converted to `ast::Expression`s using `Expr()`
1073 template <typename T, typename... ARGS>
1074 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001075 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001076 }
1077
1078 /// @param type the type to construct
1079 /// @param args the arguments for the constructor
1080 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1081 /// values `args`.
1082 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001083 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001084 return create<ast::TypeConstructorExpression>(
1085 type, ExprList(std::forward<ARGS>(args)...));
1086 }
1087
Ben Clayton313e6182021-06-17 19:56:14 +00001088 /// @param source the source information
1089 /// @param type the type to construct
1090 /// @param args the arguments for the constructor
1091 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1092 /// values `args`.
1093 template <typename... ARGS>
1094 ast::TypeConstructorExpression* Construct(const Source& source,
1095 ast::Type* type,
1096 ARGS&&... args) {
1097 return create<ast::TypeConstructorExpression>(
1098 source, type, ExprList(std::forward<ARGS>(args)...));
1099 }
1100
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001101 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001102 /// @param type the vector type
1103 /// @param size the vector size
1104 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1105 /// type `type`, constructed with the values `args`.
1106 template <typename... ARGS>
1107 ast::TypeConstructorExpression* vec(ast::Type* type,
1108 uint32_t size,
1109 ARGS&&... args) {
1110 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1111 }
1112
1113 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1115 /// `T`, constructed with the values `args`.
1116 template <typename T, typename... ARGS>
1117 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001118 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 }
1120
1121 /// @param args the arguments for the vector constructor
1122 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1123 /// `T`, constructed with the values `args`.
1124 template <typename T, typename... ARGS>
1125 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001126 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 }
1128
1129 /// @param args the arguments for the vector constructor
1130 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1131 /// `T`, constructed with the values `args`.
1132 template <typename T, typename... ARGS>
1133 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001134 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001135 }
1136
1137 /// @param args the arguments for the matrix constructor
1138 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1139 /// `T`, constructed with the values `args`.
1140 template <typename T, typename... ARGS>
1141 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001142 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001143 }
1144
1145 /// @param args the arguments for the matrix constructor
1146 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1147 /// `T`, constructed with the values `args`.
1148 template <typename T, typename... ARGS>
1149 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001150 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001151 }
1152
1153 /// @param args the arguments for the matrix constructor
1154 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1155 /// `T`, constructed with the values `args`.
1156 template <typename T, typename... ARGS>
1157 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001158 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001159 }
1160
1161 /// @param args the arguments for the matrix constructor
1162 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1163 /// `T`, constructed with the values `args`.
1164 template <typename T, typename... ARGS>
1165 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001166 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001167 }
1168
1169 /// @param args the arguments for the matrix constructor
1170 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1171 /// `T`, constructed with the values `args`.
1172 template <typename T, typename... ARGS>
1173 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001174 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001175 }
1176
1177 /// @param args the arguments for the matrix constructor
1178 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1179 /// `T`, constructed with the values `args`.
1180 template <typename T, typename... ARGS>
1181 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001182 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001183 }
1184
1185 /// @param args the arguments for the matrix constructor
1186 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1187 /// `T`, constructed with the values `args`.
1188 template <typename T, typename... ARGS>
1189 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001190 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001191 }
1192
1193 /// @param args the arguments for the matrix constructor
1194 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1195 /// `T`, constructed with the values `args`.
1196 template <typename T, typename... ARGS>
1197 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001198 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 }
1200
1201 /// @param args the arguments for the matrix constructor
1202 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1203 /// `T`, constructed with the values `args`.
1204 template <typename T, typename... ARGS>
1205 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001206 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 }
1208
1209 /// @param args the arguments for the array constructor
1210 /// @return an `ast::TypeConstructorExpression` of an array with element type
1211 /// `T`, constructed with the values `args`.
1212 template <typename T, int N = 0, typename... ARGS>
1213 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001214 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 }
1216
1217 /// @param subtype the array element type
1218 /// @param n the array size. 0 represents a runtime-array.
1219 /// @param args the arguments for the array constructor
1220 /// @return an `ast::TypeConstructorExpression` of an array with element type
1221 /// `subtype`, constructed with the values `args`.
1222 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001223 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001224 uint32_t n,
1225 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001226 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227 }
1228
1229 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001231 /// @param optional the optional variable settings.
1232 /// Can be any of the following, in any order:
1233 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001234 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001235 /// * ast::Expression* - specifies the variable's initializer expression
1236 /// * ast::DecorationList - specifies the variable's decorations
1237 /// Note that repeated arguments of the same type will use the last argument's
1238 /// value.
1239 /// @returns a `ast::Variable` with the given name, type and additional
1240 /// options
1241 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001242 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001243 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001244 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001245 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1246 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001247 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001248 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001249 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250
1251 /// @param source the variable source
1252 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001254 /// @param optional the optional variable settings.
1255 /// Can be any of the following, in any order:
1256 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001257 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001258 /// * ast::Expression* - specifies the variable's initializer expression
1259 /// * ast::DecorationList - specifies the variable's decorations
1260 /// Note that repeated arguments of the same type will use the last argument's
1261 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001263 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001264 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001265 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001266 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001267 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001268 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1269 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001270 opts.storage, opts.access, type, false,
1271 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001272 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001273
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001274 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001275 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001276 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001277 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001278 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001279 template <typename NAME>
1280 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001281 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001282 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001283 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001284 return create<ast::Variable>(
1285 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1286 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001287 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001288
1289 /// @param source the variable source
1290 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001292 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001293 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001294 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001295 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001296 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001297 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001298 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001299 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001300 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001301 return create<ast::Variable>(
1302 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1303 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001304 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001305
James Pricedaf1f1c2021-04-08 22:15:48 +00001306 /// @param name the parameter name
1307 /// @param type the parameter type
1308 /// @param decorations optional parameter decorations
1309 /// @returns a constant `ast::Variable` with the given name and type
1310 template <typename NAME>
1311 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001312 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001313 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001314 return create<ast::Variable>(
1315 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1316 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001317 }
1318
1319 /// @param source the parameter source
1320 /// @param name the parameter name
1321 /// @param type the parameter type
1322 /// @param decorations optional parameter decorations
1323 /// @returns a constant `ast::Variable` with the given name and type
1324 template <typename NAME>
1325 ast::Variable* Param(const Source& source,
1326 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001327 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001328 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001329 return create<ast::Variable>(
1330 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1331 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001332 }
1333
Ben Clayton42708342021-04-21 17:55:12 +00001334 /// @param name the variable name
1335 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001336 /// @param optional the optional variable settings.
1337 /// Can be any of the following, in any order:
1338 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001339 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001340 /// * ast::Expression* - specifies the variable's initializer expression
1341 /// * ast::DecorationList - specifies the variable's decorations
1342 /// Note that repeated arguments of the same type will use the last argument's
1343 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001344 /// @returns a new `ast::Variable`, which is automatically registered as a
1345 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001346 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001347 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001348 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001349 OPTIONAL&&... optional) {
1350 auto* var = Var(std::forward<NAME>(name), type,
1351 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001352 AST().AddGlobalVariable(var);
1353 return var;
1354 }
1355
1356 /// @param source the variable source
1357 /// @param name the variable name
1358 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001359 /// @param optional the optional variable settings.
1360 /// Can be any of the following, in any order:
1361 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001362 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001363 /// * ast::Expression* - specifies the variable's initializer expression
1364 /// * ast::DecorationList - specifies the variable's decorations
1365 /// Note that repeated arguments of the same type will use the last argument's
1366 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001367 /// @returns a new `ast::Variable`, which is automatically registered as a
1368 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001369 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001370 ast::Variable* Global(const Source& source,
1371 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001372 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001373 OPTIONAL&&... optional) {
1374 auto* var = Var(source, std::forward<NAME>(name), type,
1375 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001376 AST().AddGlobalVariable(var);
1377 return var;
1378 }
1379
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001380 /// @param name the variable name
1381 /// @param type the variable type
1382 /// @param constructor constructor expression
1383 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001384 /// @returns a const `ast::Variable` constructed by calling Var() with the
1385 /// arguments of `args`, which is automatically registered as a global
1386 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001387 template <typename NAME>
1388 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001389 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001390 ast::Expression* constructor,
1391 ast::DecorationList decorations = {}) {
1392 auto* var = Const(std::forward<NAME>(name), type, constructor,
1393 std::move(decorations));
1394 AST().AddGlobalVariable(var);
1395 return var;
1396 }
1397
1398 /// @param source the variable source
1399 /// @param name the variable name
1400 /// @param type the variable type
1401 /// @param constructor constructor expression
1402 /// @param decorations optional variable decorations
1403 /// @returns a const `ast::Variable` constructed by calling Var() with the
1404 /// arguments of `args`, which is automatically registered as a global
1405 /// variable with the ast::Module.
1406 template <typename NAME>
1407 ast::Variable* GlobalConst(const Source& source,
1408 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001409 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001410 ast::Expression* constructor,
1411 ast::DecorationList decorations = {}) {
1412 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1413 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001414 AST().AddGlobalVariable(var);
1415 return var;
1416 }
1417
Ben Claytonfe0910f2021-05-17 15:51:47 +00001418 /// @param source the source information
1419 /// @param expr the expression to take the address of
1420 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1421 template <typename EXPR>
1422 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1423 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1424 Expr(std::forward<EXPR>(expr)));
1425 }
1426
1427 /// @param expr the expression to take the address of
1428 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1429 template <typename EXPR>
1430 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1431 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1432 Expr(std::forward<EXPR>(expr)));
1433 }
1434
1435 /// @param source the source information
1436 /// @param expr the expression to perform an indirection on
1437 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1438 template <typename EXPR>
1439 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1440 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1441 Expr(std::forward<EXPR>(expr)));
1442 }
1443
1444 /// @param expr the expression to perform an indirection on
1445 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1446 template <typename EXPR>
1447 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1448 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1449 Expr(std::forward<EXPR>(expr)));
1450 }
1451
Antonio Maiorano14b34032021-06-09 20:17:59 +00001452 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001453 /// @param func the function name
1454 /// @param args the function call arguments
1455 /// @returns a `ast::CallExpression` to the function `func`, with the
1456 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1457 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001458 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1459 return create<ast::CallExpression>(source, Expr(func),
1460 ExprList(std::forward<ARGS>(args)...));
1461 }
1462
1463 /// @param func the function name
1464 /// @param args the function call arguments
1465 /// @returns a `ast::CallExpression` to the function `func`, with the
1466 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1467 template <typename NAME,
1468 typename... ARGS,
1469 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001470 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1471 return create<ast::CallExpression>(Expr(func),
1472 ExprList(std::forward<ARGS>(args)...));
1473 }
1474
Ben Clayton7fe01062021-06-11 13:22:27 +00001475 /// @param expr the expression to ignore
1476 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1477 /// passed the single `expr` argument
1478 template <typename EXPR>
1479 ast::CallStatement* Ignore(EXPR&& expr) {
1480 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1481 }
1482
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001483 /// @param lhs the left hand argument to the addition operation
1484 /// @param rhs the right hand argument to the addition operation
1485 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1486 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001487 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001488 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1489 Expr(std::forward<LHS>(lhs)),
1490 Expr(std::forward<RHS>(rhs)));
1491 }
1492
1493 /// @param lhs the left hand argument to the subtraction operation
1494 /// @param rhs the right hand argument to the subtraction operation
1495 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1496 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001497 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001498 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1499 Expr(std::forward<LHS>(lhs)),
1500 Expr(std::forward<RHS>(rhs)));
1501 }
1502
1503 /// @param lhs the left hand argument to the multiplication operation
1504 /// @param rhs the right hand argument to the multiplication operation
1505 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1506 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001507 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001508 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1509 Expr(std::forward<LHS>(lhs)),
1510 Expr(std::forward<RHS>(rhs)));
1511 }
1512
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001513 /// @param source the source information
1514 /// @param lhs the left hand argument to the multiplication operation
1515 /// @param rhs the right hand argument to the multiplication operation
1516 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1517 template <typename LHS, typename RHS>
1518 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1519 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1520 Expr(std::forward<LHS>(lhs)),
1521 Expr(std::forward<RHS>(rhs)));
1522 }
1523
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001524 /// @param lhs the left hand argument to the division operation
1525 /// @param rhs the right hand argument to the division operation
1526 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1527 template <typename LHS, typename RHS>
1528 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1529 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1530 Expr(std::forward<LHS>(lhs)),
1531 Expr(std::forward<RHS>(rhs)));
1532 }
1533
Ben Clayton0597a2b2021-06-16 09:19:36 +00001534 /// @param source the source information
1535 /// @param arr the array argument for the array accessor expression
1536 /// @param idx the index argument for the array accessor expression
1537 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1538 template <typename ARR, typename IDX>
1539 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1540 ARR&& arr,
1541 IDX&& idx) {
1542 return create<ast::ArrayAccessorExpression>(
1543 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1544 }
1545
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001546 /// @param arr the array argument for the array accessor expression
1547 /// @param idx the index argument for the array accessor expression
1548 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1549 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001550 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001551 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1552 Expr(std::forward<IDX>(idx)));
1553 }
1554
1555 /// @param obj the object for the member accessor expression
1556 /// @param idx the index argument for the array accessor expression
1557 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1558 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001559 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001560 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1561 Expr(std::forward<IDX>(idx)));
1562 }
1563
Ben Clayton42d1e092021-02-02 14:29:15 +00001564 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001565 /// @param val the offset value
1566 /// @returns the offset decoration pointer
1567 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1568 return create<ast::StructMemberOffsetDecoration>(source_, val);
1569 }
1570
Ben Claytond614dd52021-03-15 10:43:11 +00001571 /// Creates a ast::StructMemberSizeDecoration
1572 /// @param source the source information
1573 /// @param val the size value
1574 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001575 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1576 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001577 return create<ast::StructMemberSizeDecoration>(source, val);
1578 }
1579
1580 /// Creates a ast::StructMemberSizeDecoration
1581 /// @param val the size value
1582 /// @returns the size decoration pointer
1583 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1584 return create<ast::StructMemberSizeDecoration>(source_, val);
1585 }
1586
1587 /// Creates a ast::StructMemberAlignDecoration
1588 /// @param source the source information
1589 /// @param val the align value
1590 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001591 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1592 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001593 return create<ast::StructMemberAlignDecoration>(source, val);
1594 }
1595
1596 /// Creates a ast::StructMemberAlignDecoration
1597 /// @param val the align value
1598 /// @returns the align decoration pointer
1599 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1600 return create<ast::StructMemberAlignDecoration>(source_, val);
1601 }
1602
Ben Clayton42d1e092021-02-02 14:29:15 +00001603 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001604 /// @param source the source information
1605 /// @param name the function name
1606 /// @param params the function parameters
1607 /// @param type the function return type
1608 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001609 /// @param decorations the optional function decorations
1610 /// @param return_type_decorations the optional function return type
1611 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001612 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001613 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001614 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001615 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001616 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001617 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001618 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001619 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001620 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001621 auto* func =
1622 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1623 type, create<ast::BlockStatement>(body),
1624 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001625 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001626 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001627 }
1628
Ben Clayton42d1e092021-02-02 14:29:15 +00001629 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001630 /// @param name the function name
1631 /// @param params the function parameters
1632 /// @param type the function return type
1633 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001634 /// @param decorations the optional function decorations
1635 /// @param return_type_decorations the optional function return type
1636 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001637 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001638 template <typename NAME>
1639 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001640 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001641 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001642 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001643 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001644 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001645 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1646 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001647 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001648 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001649 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001650 }
1651
Ben Clayton49668bb2021-04-17 05:32:01 +00001652 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001653 /// @param source the source information
1654 /// @returns the return statement pointer
1655 ast::ReturnStatement* Return(const Source& source) {
1656 return create<ast::ReturnStatement>(source);
1657 }
1658
1659 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001660 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001661 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1662
1663 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001664 /// @param source the source information
1665 /// @param val the return value
1666 /// @returns the return statement pointer
1667 template <typename EXPR>
1668 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1669 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1670 }
1671
1672 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001673 /// @param val the return value
1674 /// @returns the return statement pointer
1675 template <typename EXPR>
1676 ast::ReturnStatement* Return(EXPR&& val) {
1677 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001678 }
1679
Ben Clayton950809f2021-06-09 14:32:14 +00001680 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001681 /// @param source the source information
1682 /// @param name the alias name
1683 /// @param type the alias target type
1684 /// @returns the alias type
1685 template <typename NAME>
1686 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1687 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001688 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001689 return out;
1690 }
1691
Ben Clayton950809f2021-06-09 14:32:14 +00001692 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001693 /// @param name the alias name
1694 /// @param type the alias target type
1695 /// @returns the alias type
1696 template <typename NAME>
1697 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1698 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001699 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001700 return out;
1701 }
1702
Ben Clayton950809f2021-06-09 14:32:14 +00001703 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001704 /// @param source the source information
1705 /// @param name the struct name
1706 /// @param members the struct members
1707 /// @param decorations the optional struct decorations
1708 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001709 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001710 ast::Struct* Structure(const Source& source,
1711 NAME&& name,
1712 ast::StructMemberList members,
1713 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001714 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001715 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001716 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001717 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001718 return type;
1719 }
1720
Ben Clayton950809f2021-06-09 14:32:14 +00001721 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001722 /// @param name the struct name
1723 /// @param members the struct members
1724 /// @param decorations the optional struct decorations
1725 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001726 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001727 ast::Struct* Structure(NAME&& name,
1728 ast::StructMemberList members,
1729 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001730 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001731 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001732 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001733 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001734 return type;
1735 }
1736
Ben Clayton42d1e092021-02-02 14:29:15 +00001737 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001738 /// @param source the source information
1739 /// @param name the struct member name
1740 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001741 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001742 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001743 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001744 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001745 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001746 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001747 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001748 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1749 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001750 }
1751
Ben Clayton42d1e092021-02-02 14:29:15 +00001752 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001753 /// @param name the struct member name
1754 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001755 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001756 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001757 template <typename NAME>
1758 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001759 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001760 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001761 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1762 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001763 }
1764
Ben Claytonbab31972021-02-08 21:16:21 +00001765 /// Creates a ast::StructMember with the given byte offset
1766 /// @param offset the offset to use in the StructMemberOffsetDecoration
1767 /// @param name the struct member name
1768 /// @param type the struct member type
1769 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001770 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001771 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001772 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001773 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001774 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001775 create<ast::StructMemberOffsetDecoration>(offset),
1776 });
1777 }
1778
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001779 /// Creates a ast::BlockStatement with input statements
1780 /// @param statements statements of block
1781 /// @returns the block statement pointer
1782 template <typename... Statements>
1783 ast::BlockStatement* Block(Statements&&... statements) {
1784 return create<ast::BlockStatement>(
1785 ast::StatementList{std::forward<Statements>(statements)...});
1786 }
1787
1788 /// Creates a ast::ElseStatement with input condition and body
1789 /// @param condition the else condition expression
1790 /// @param body the else body
1791 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001792 template <typename CONDITION>
1793 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1794 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1795 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001796 }
1797
1798 /// Creates a ast::IfStatement with input condition, body, and optional
1799 /// variadic else statements
1800 /// @param condition the if statement condition expression
1801 /// @param body the if statement body
1802 /// @param elseStatements optional variadic else statements
1803 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001804 template <typename CONDITION, typename... ELSE_STATEMENTS>
1805 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001806 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001807 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001808 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001809 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001810 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001811 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001812 }
1813
1814 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001815 /// @param source the source information
1816 /// @param lhs the left hand side expression initializer
1817 /// @param rhs the right hand side expression initializer
1818 /// @returns the assignment statement pointer
1819 template <typename LhsExpressionInit, typename RhsExpressionInit>
1820 ast::AssignmentStatement* Assign(const Source& source,
1821 LhsExpressionInit&& lhs,
1822 RhsExpressionInit&& rhs) {
1823 return create<ast::AssignmentStatement>(
1824 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1825 Expr(std::forward<RhsExpressionInit>(rhs)));
1826 }
1827
1828 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001829 /// @param lhs the left hand side expression initializer
1830 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001831 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001832 template <typename LhsExpressionInit, typename RhsExpressionInit>
1833 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1834 RhsExpressionInit&& rhs) {
1835 return create<ast::AssignmentStatement>(
1836 Expr(std::forward<LhsExpressionInit>(lhs)),
1837 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001838 }
1839
1840 /// Creates a ast::LoopStatement with input body and optional continuing
1841 /// @param body the loop body
1842 /// @param continuing the optional continuing block
1843 /// @returns the loop statement pointer
1844 ast::LoopStatement* Loop(ast::BlockStatement* body,
1845 ast::BlockStatement* continuing = nullptr) {
1846 return create<ast::LoopStatement>(body, continuing);
1847 }
1848
1849 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001850 /// @param source the source information
1851 /// @param var the variable to wrap in a decl statement
1852 /// @returns the variable decl statement pointer
1853 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1854 return create<ast::VariableDeclStatement>(source, var);
1855 }
1856
1857 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001858 /// @param var the variable to wrap in a decl statement
1859 /// @returns the variable decl statement pointer
1860 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1861 return create<ast::VariableDeclStatement>(var);
1862 }
1863
Antonio Maioranocea744d2021-03-25 12:55:27 +00001864 /// Creates a ast::SwitchStatement with input expression and cases
1865 /// @param condition the condition expression initializer
1866 /// @param cases case statements
1867 /// @returns the switch statement pointer
1868 template <typename ExpressionInit, typename... Cases>
1869 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1870 return create<ast::SwitchStatement>(
1871 Expr(std::forward<ExpressionInit>(condition)),
1872 ast::CaseStatementList{std::forward<Cases>(cases)...});
1873 }
1874
1875 /// Creates a ast::CaseStatement with input list of selectors, and body
1876 /// @param selectors list of selectors
1877 /// @param body the case body
1878 /// @returns the case statement pointer
1879 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1880 ast::BlockStatement* body = nullptr) {
1881 return create<ast::CaseStatement>(std::move(selectors),
1882 body ? body : Block());
1883 }
1884
1885 /// Convenient overload that takes a single selector
1886 /// @param selector a single case selector
1887 /// @param body the case body
1888 /// @returns the case statement pointer
1889 ast::CaseStatement* Case(ast::IntLiteral* selector,
1890 ast::BlockStatement* body = nullptr) {
1891 return Case(ast::CaseSelectorList{selector}, body);
1892 }
1893
1894 /// Convenience function that creates a 'default' ast::CaseStatement
1895 /// @param body the case body
1896 /// @returns the case statement pointer
1897 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1898 return Case(ast::CaseSelectorList{}, body);
1899 }
1900
James Price68f558f2021-04-06 15:51:47 +00001901 /// Creates an ast::BuiltinDecoration
1902 /// @param source the source information
1903 /// @param builtin the builtin value
1904 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001905 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001906 return create<ast::BuiltinDecoration>(source, builtin);
1907 }
1908
1909 /// Creates an ast::BuiltinDecoration
1910 /// @param builtin the builtin value
1911 /// @returns the builtin decoration pointer
1912 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1913 return create<ast::BuiltinDecoration>(source_, builtin);
1914 }
1915
1916 /// Creates an ast::LocationDecoration
1917 /// @param source the source information
1918 /// @param location the location value
1919 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001920 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001921 return create<ast::LocationDecoration>(source, location);
1922 }
1923
1924 /// Creates an ast::LocationDecoration
1925 /// @param location the location value
1926 /// @returns the location decoration pointer
1927 ast::LocationDecoration* Location(uint32_t location) {
1928 return create<ast::LocationDecoration>(source_, location);
1929 }
1930
James Pricef2f3bfc2021-05-13 20:32:32 +00001931 /// Creates an ast::OverrideDecoration with a specific constant ID
1932 /// @param source the source information
1933 /// @param id the id value
1934 /// @returns the override decoration pointer
1935 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1936 return create<ast::OverrideDecoration>(source, id);
1937 }
1938
1939 /// Creates an ast::OverrideDecoration with a specific constant ID
1940 /// @param id the optional id value
1941 /// @returns the override decoration pointer
1942 ast::OverrideDecoration* Override(uint32_t id) {
1943 return Override(source_, id);
1944 }
1945
1946 /// Creates an ast::OverrideDecoration without a constant ID
1947 /// @param source the source information
1948 /// @returns the override decoration pointer
1949 ast::OverrideDecoration* Override(const Source& source) {
1950 return create<ast::OverrideDecoration>(source);
1951 }
1952
1953 /// Creates an ast::OverrideDecoration without a constant ID
1954 /// @returns the override decoration pointer
1955 ast::OverrideDecoration* Override() { return Override(source_); }
1956
James Price68f558f2021-04-06 15:51:47 +00001957 /// Creates an ast::StageDecoration
1958 /// @param source the source information
1959 /// @param stage the pipeline stage
1960 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001961 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001962 return create<ast::StageDecoration>(source, stage);
1963 }
1964
1965 /// Creates an ast::StageDecoration
1966 /// @param stage the pipeline stage
1967 /// @returns the stage decoration pointer
1968 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1969 return create<ast::StageDecoration>(source_, stage);
1970 }
1971
James Price70f80bb2021-05-19 13:40:08 +00001972 /// Creates an ast::WorkgroupDecoration
1973 /// @param x the x dimension expression
1974 /// @returns the workgroup decoration pointer
1975 template <typename EXPR_X>
1976 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1977 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1978 }
1979
1980 /// Creates an ast::WorkgroupDecoration
1981 /// @param x the x dimension expression
1982 /// @param y the y dimension expression
1983 /// @returns the workgroup decoration pointer
1984 template <typename EXPR_X, typename EXPR_Y>
1985 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1986 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1987 nullptr);
1988 }
1989
1990 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00001991 /// @param source the source information
1992 /// @param x the x dimension expression
1993 /// @param y the y dimension expression
1994 /// @param z the z dimension expression
1995 /// @returns the workgroup decoration pointer
1996 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1997 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
1998 EXPR_X&& x,
1999 EXPR_Y&& y,
2000 EXPR_Z&& z) {
2001 return create<ast::WorkgroupDecoration>(
2002 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2003 Expr(std::forward<EXPR_Z>(z)));
2004 }
2005
2006 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002007 /// @param x the x dimension expression
2008 /// @param y the y dimension expression
2009 /// @param z the z dimension expression
2010 /// @returns the workgroup decoration pointer
2011 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2012 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2013 return create<ast::WorkgroupDecoration>(
2014 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2015 Expr(std::forward<EXPR_Z>(z)));
2016 }
2017
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002018 /// Sets the current builder source to `src`
2019 /// @param src the Source used for future create() calls
2020 void SetSource(const Source& src) {
2021 AssertNotMoved();
2022 source_ = src;
2023 }
2024
2025 /// Sets the current builder source to `loc`
2026 /// @param loc the Source used for future create() calls
2027 void SetSource(const Source::Location& loc) {
2028 AssertNotMoved();
2029 source_ = Source(loc);
2030 }
2031
Ben Clayton33352542021-01-29 16:43:41 +00002032 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002033 /// @note As the Resolver is run when the Program is built, this will only be
2034 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002035 /// @param expr the AST expression
2036 /// @return the resolved semantic type for the expression, or nullptr if the
2037 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002038 sem::Type* TypeOf(const ast::Expression* expr) const;
2039
Ben Claytonfe0910f2021-05-17 15:51:47 +00002040 /// Helper for returning the resolved semantic type of the variable `var`.
2041 /// @note As the Resolver is run when the Program is built, this will only be
2042 /// useful for the Resolver itself and tests that use their own Resolver.
2043 /// @param var the AST variable
2044 /// @return the resolved semantic type for the variable, or nullptr if the
2045 /// variable has no resolved type.
2046 sem::Type* TypeOf(const ast::Variable* var) const;
2047
Ben Claytonfbec46f2021-04-30 20:20:19 +00002048 /// Helper for returning the resolved semantic type of the AST type `type`.
2049 /// @note As the Resolver is run when the Program is built, this will only be
2050 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002051 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002052 /// @return the resolved semantic type for the type, or nullptr if the type
2053 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002054 const sem::Type* TypeOf(const ast::Type* type) const;
2055
2056 /// Helper for returning the resolved semantic type of the AST type
2057 /// declaration `type_decl`.
2058 /// @note As the Resolver is run when the Program is built, this will only be
2059 /// useful for the Resolver itself and tests that use their own Resolver.
2060 /// @param type_decl the AST type declaration
2061 /// @return the resolved semantic type for the type declaration, or nullptr if
2062 /// the type declaration has no resolved type.
2063 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002064
Ben Clayton169512e2021-04-17 05:52:11 +00002065 /// Wraps the ast::Literal in a statement. This is used by tests that
2066 /// construct a partial AST and require the Resolver to reach these
2067 /// nodes.
2068 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2069 /// @return the ast::Statement that wraps the ast::Statement
2070 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002071 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002072 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002073 /// nodes.
2074 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2075 /// @return the ast::Statement that wraps the ast::Expression
2076 ast::Statement* WrapInStatement(ast::Expression* expr);
2077 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002078 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002079 /// these nodes.
2080 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2081 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2082 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2083 /// Returns the statement argument. Used as a passthrough-overload by
2084 /// WrapInFunction().
2085 /// @param stmt the ast::Statement
2086 /// @return `stmt`
2087 ast::Statement* WrapInStatement(ast::Statement* stmt);
2088 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002089 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002090 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002091 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002092 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002093 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002094 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002095 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002096 }
2097 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002098 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002099 /// @returns the function
2100 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002101
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002102 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002103 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002104
2105 protected:
2106 /// Asserts that the builder has not been moved.
2107 void AssertNotMoved() const;
2108
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002109 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002110 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002111 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002112 ASTNodeAllocator ast_nodes_;
2113 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002114 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002115 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002116 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002117 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002118
2119 /// The source to use when creating AST nodes without providing a Source as
2120 /// the first argument.
2121 Source source_;
2122
Ben Clayton5f0ea112021-03-09 10:54:37 +00002123 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002124 /// program when built.
2125 bool resolve_on_build_ = true;
2126
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002127 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2128 bool moved_ = false;
2129};
2130
2131//! @cond Doxygen_Suppress
2132// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2133template <>
2134struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002135 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002136 return t->i32();
2137 }
2138};
2139template <>
2140struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002141 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002142 return t->u32();
2143 }
2144};
2145template <>
2146struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002147 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002148 return t->f32();
2149 }
2150};
2151template <>
2152struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002153 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002154 return t->bool_();
2155 }
2156};
2157template <>
2158struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002159 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002160 return t->void_();
2161 }
2162};
2163//! @endcond
2164
Ben Clayton917b14b2021-04-19 16:50:23 +00002165/// @param builder the ProgramBuilder
2166/// @returns the ProgramID of the ProgramBuilder
2167inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2168 return builder->ID();
2169}
2170
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002171} // namespace tint
2172
2173#endif // SRC_PROGRAM_BUILDER_H_