blob: dcf311c910a356b41463b5006fb8970127f758d1 [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>
Ben Claytonb5cd10c2021-06-25 10:26:26 +000019#include <unordered_set>
Ben Claytona6b9a8e2021-01-26 16:57:10 +000020#include <utility>
21
Ben Claytone204f272021-04-22 14:40:23 +000022#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000023#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000024#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000025#include "src/ast/assignment_statement.h"
Ben Clayton8c7f0da2021-06-17 15:48:39 +000026#include "src/ast/atomic.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/binary_expression.h"
Ben Claytond9603282021-06-29 12:37:45 +000028#include "src/ast/bitcast_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000029#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030#include "src/ast/bool_literal.h"
31#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000032#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000033#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000034#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000035#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000036#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000037#include "src/ast/float_literal.h"
Ben Clayton65cd2592021-07-02 19:27:42 +000038#include "src/ast/for_loop_statement.h"
Ben Claytona922e652021-04-21 13:37:22 +000039#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000040#include "src/ast/if_statement.h"
James Price989a8e42021-06-28 23:04:43 +000041#include "src/ast/interpolate_decoration.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000042#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000043#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/ast/member_accessor_expression.h"
45#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000046#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000047#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000049#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000050#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/ast/scalar_constructor_expression.h"
52#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000053#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000054#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000055#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000056#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000058#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000059#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000060#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000061#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000062#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000064#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000065#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000066#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000067#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000068#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000069#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000070#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000071#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000072#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000073#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000074#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000075#include "src/sem/f32_type.h"
76#include "src/sem/i32_type.h"
77#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000078#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000079#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000080#include "src/sem/sampled_texture_type.h"
81#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000082#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000083#include "src/sem/u32_type.h"
84#include "src/sem/vector_type.h"
85#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000086
Ben Clayton03d10722021-06-05 12:45:50 +000087#ifdef INCLUDE_TINT_TINT_H_
88#error "internal tint header being #included from tint.h"
89#endif
90
Ben Clayton401b96b2021-02-03 17:19:59 +000091// Forward declarations
Ben Claytonb5cd10c2021-06-25 10:26:26 +000092namespace tint {
Ben Clayton401b96b2021-02-03 17:19:59 +000093namespace ast {
94class VariableDeclStatement;
95} // namespace ast
Ben Claytonb5cd10c2021-06-25 10:26:26 +000096} // namespace tint
Ben Clayton401b96b2021-02-03 17:19:59 +000097
Ben Claytonb5cd10c2021-06-25 10:26:26 +000098namespace tint {
Ben Claytona6b9a8e2021-01-26 16:57:10 +000099class CloneContext;
100
101/// ProgramBuilder is a mutable builder for a Program.
102/// To construct a Program, populate the builder and then `std::move` it to a
103/// Program.
104class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +0000105 /// VarOptionals is a helper for accepting a number of optional, extra
106 /// arguments for Var() and Global().
107 struct VarOptionals {
108 template <typename... ARGS>
109 explicit VarOptionals(ARGS&&... args) {
110 Apply(std::forward<ARGS>(args)...);
111 }
112 ~VarOptionals();
113
114 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000115 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000116 ast::Expression* constructor = nullptr;
117 ast::DecorationList decorations = {};
118
119 private:
120 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000121 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000122 void Set(ast::Expression* c) { constructor = c; }
123 void Set(const ast::DecorationList& l) { decorations = l; }
124
125 template <typename FIRST, typename... ARGS>
126 void Apply(FIRST&& first, ARGS&&... args) {
127 Set(std::forward<FIRST>(first));
128 Apply(std::forward<ARGS>(args)...);
129 }
130 void Apply() {}
131 };
132
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000133 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000134 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
135 using ASTNodeAllocator = BlockAllocator<ast::Node>;
136
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000137 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
138 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000139
140 /// `i32` is a type alias to `int`.
141 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
142 /// WGSL syntax.
143 /// Note: this is intentionally not aliased to uint32_t as we want integer
144 /// literals passed to the builder to match WGSL's integer literal types.
145 using i32 = decltype(1);
146 /// `u32` is a type alias to `unsigned int`.
147 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
148 /// WGSL syntax.
149 /// Note: this is intentionally not aliased to uint32_t as we want integer
150 /// literals passed to the builder to match WGSL's integer literal types.
151 using u32 = decltype(1u);
152 /// `f32` is a type alias to `float`
153 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
154 /// WGSL syntax.
155 using f32 = float;
156
157 /// Constructor
158 ProgramBuilder();
159
160 /// Move constructor
161 /// @param rhs the builder to move
162 ProgramBuilder(ProgramBuilder&& rhs);
163
164 /// Destructor
165 virtual ~ProgramBuilder();
166
167 /// Move assignment operator
168 /// @param rhs the builder to move
169 /// @return this builder
170 ProgramBuilder& operator=(ProgramBuilder&& rhs);
171
Ben Claytone43c8302021-01-29 11:59:32 +0000172 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
173 /// making a deep clone of the Program contents.
174 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
175 /// existing immutable program.
176 /// As the returned ProgramBuilder wraps `program`, `program` must not be
177 /// destructed or assigned while using the returned ProgramBuilder.
178 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
179 /// function. See crbug.com/tint/460.
180 /// @param program the immutable Program to wrap
181 /// @return the ProgramBuilder that wraps `program`
182 static ProgramBuilder Wrap(const Program* program);
183
Ben Claytone6995de2021-04-13 23:27:27 +0000184 /// @returns the unique identifier for this program
185 ProgramID ID() const { return id_; }
186
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000187 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000188 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000189 AssertNotMoved();
190 return types_;
191 }
192
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000193 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000194 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000195 AssertNotMoved();
196 return types_;
197 }
198
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000199 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000200 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000201 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000202 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000203 }
204
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000205 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000206 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000207 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000208 return ast_nodes_;
209 }
210
211 /// @returns a reference to the program's semantic nodes storage
212 SemNodeAllocator& SemNodes() {
213 AssertNotMoved();
214 return sem_nodes_;
215 }
216
217 /// @returns a reference to the program's semantic nodes storage
218 const SemNodeAllocator& SemNodes() const {
219 AssertNotMoved();
220 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000221 }
222
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000223 /// @returns a reference to the program's AST root Module
224 ast::Module& AST() {
225 AssertNotMoved();
226 return *ast_;
227 }
228
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000229 /// @returns a reference to the program's AST root Module
230 const ast::Module& AST() const {
231 AssertNotMoved();
232 return *ast_;
233 }
234
235 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000236 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000237 AssertNotMoved();
238 return sem_;
239 }
240
241 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000242 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000243 AssertNotMoved();
244 return sem_;
245 }
246
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000247 /// @returns a reference to the program's SymbolTable
248 SymbolTable& Symbols() {
249 AssertNotMoved();
250 return symbols_;
251 }
252
Ben Clayton708dc2d2021-01-29 11:22:40 +0000253 /// @returns a reference to the program's SymbolTable
254 const SymbolTable& Symbols() const {
255 AssertNotMoved();
256 return symbols_;
257 }
258
Ben Clayton844217f2021-01-27 18:49:05 +0000259 /// @returns a reference to the program's diagnostics
260 diag::List& Diagnostics() {
261 AssertNotMoved();
262 return diagnostics_;
263 }
264
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000265 /// @returns a reference to the program's diagnostics
266 const diag::List& Diagnostics() const {
267 AssertNotMoved();
268 return diagnostics_;
269 }
270
Ben Clayton5f0ea112021-03-09 10:54:37 +0000271 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000272 /// @param enable the new flag value (defaults to true)
273 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
274
Ben Clayton5f0ea112021-03-09 10:54:37 +0000275 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000276 /// built.
277 bool ResolveOnBuild() const { return resolve_on_build_; }
278
Ben Clayton844217f2021-01-27 18:49:05 +0000279 /// @returns true if the program has no error diagnostics and is not missing
280 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000281 bool IsValid() const;
282
Ben Clayton708dc2d2021-01-29 11:22:40 +0000283 /// Writes a representation of the node to the output stream
284 /// @note unlike str(), to_str() does not automatically demangle the string.
285 /// @param node the AST node
286 /// @param out the stream to write to
287 /// @param indent number of spaces to indent the node when writing
288 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
289 node->to_str(Sem(), out, indent);
290 }
291
292 /// Returns a demangled, string representation of `node`.
293 /// @param node the AST node
294 /// @returns a string representation of the node
295 std::string str(const ast::Node* node) const;
296
Ben Clayton7fdfff12021-01-29 15:17:30 +0000297 /// Creates a new ast::Node owned by the ProgramBuilder. When the
298 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000299 /// @param source the Source of the node
300 /// @param args the arguments to pass to the type constructor
301 /// @returns the node pointer
302 template <typename T, typename... ARGS>
303 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
304 ARGS&&... args) {
305 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000306 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000307 }
308
Ben Clayton7fdfff12021-01-29 15:17:30 +0000309 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
310 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000311 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000312 /// When the ProgramBuilder is destructed, the ast::Node will also be
313 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000314 /// @returns the node pointer
315 template <typename T>
316 traits::EnableIfIsType<T, ast::Node>* create() {
317 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000318 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000319 }
320
Ben Clayton7fdfff12021-01-29 15:17:30 +0000321 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
322 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000323 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000324 /// When the ProgramBuilder is destructed, the ast::Node will also be
325 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000326 /// @param arg0 the first arguments to pass to the type constructor
327 /// @param args the remaining arguments to pass to the type constructor
328 /// @returns the node pointer
329 template <typename T, typename ARG0, typename... ARGS>
330 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
331 traits::IsTypeOrDerived<T, ast::Node>::value &&
332 !traits::IsTypeOrDerived<ARG0, Source>::value,
333 T>*
334 create(ARG0&& arg0, ARGS&&... args) {
335 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000336 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000337 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000338 }
339
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000340 /// Creates a new sem::Node owned by the ProgramBuilder.
341 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000342 /// destructed.
343 /// @param args the arguments to pass to the type constructor
344 /// @returns the node pointer
345 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000346 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
347 !traits::IsTypeOrDerived<T, sem::Type>::value,
348 T>*
349 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000350 AssertNotMoved();
351 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
352 }
353
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000354 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000355 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
356 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000357 /// Types are unique (de-aliased), and so calling create() for the same `T`
358 /// and arguments will return the same pointer.
359 /// @warning Use this method to acquire a type only if all of its type
360 /// information is provided in the constructor arguments `args`.<br>
361 /// If the type requires additional configuration after construction that
362 /// affect its fundamental type, build the type with `std::make_unique`, make
363 /// any necessary alterations and then call unique_type() instead.
364 /// @param args the arguments to pass to the type constructor
365 /// @returns the de-aliased type pointer
366 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000367 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
368 static_assert(std::is_base_of<sem::Type, T>::value,
369 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000370 AssertNotMoved();
371 return types_.Get<T>(std::forward<ARGS>(args)...);
372 }
373
374 /// Marks this builder as moved, preventing any further use of the builder.
375 void MarkAsMoved();
376
377 //////////////////////////////////////////////////////////////////////////////
378 // TypesBuilder
379 //////////////////////////////////////////////////////////////////////////////
380
381 /// TypesBuilder holds basic `tint` types and methods for constructing
382 /// complex types.
383 class TypesBuilder {
384 public:
385 /// Constructor
386 /// @param builder the program builder
387 explicit TypesBuilder(ProgramBuilder* builder);
388
389 /// @return the tint AST type for the C type `T`.
390 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000391 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000392 return CToAST<T>::get(this);
393 }
394
395 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000396 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000397
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000398 /// @param source the Source of the node
399 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000400 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000401 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000402 }
403
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000404 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000405 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000406
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000407 /// @param source the Source of the node
408 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000409 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000410 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000411 }
412
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000413 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000414 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000415
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000416 /// @param source the Source of the node
417 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000418 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000419 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 }
421
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000422 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000423 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000424
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000425 /// @param source the Source of the node
426 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000427 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000428 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000429 }
430
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000431 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000432 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000433
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000434 /// @param source the Source of the node
435 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000436 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000437 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000438 }
439
Antonio Maiorano25436862021-04-13 13:32:33 +0000440 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000441 /// @param n vector width in elements
442 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000443 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000444 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000445 }
446
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000447 /// @param source the Source of the node
448 /// @param type vector subtype
449 /// @param n vector width in elements
450 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000451 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000452 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000453 }
454
Antonio Maiorano25436862021-04-13 13:32:33 +0000455 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000456 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000457 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000458
459 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000460 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000461 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000462
463 /// @param type vector subtype
464 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000465 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000466
467 /// @param n vector width in elements
468 /// @return the tint AST type for a `n`-element vector of `type`.
469 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000470 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000471 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000472 }
473
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000474 /// @return the tint AST type for a 2-element vector of the C type `T`.
475 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000476 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000477 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000478 }
479
480 /// @return the tint AST type for a 3-element vector of the C type `T`.
481 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000482 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000483 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000484 }
485
486 /// @return the tint AST type for a 4-element vector of the C type `T`.
487 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000488 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000489 return vec4(Of<T>());
490 }
491
492 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000493 /// @param columns number of columns for the matrix
494 /// @param rows number of rows for the matrix
495 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000496 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000497 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000498 }
499
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000500 /// @param source the Source of the node
501 /// @param type matrix subtype
502 /// @param columns number of columns for the matrix
503 /// @param rows number of rows for the matrix
504 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000505 ast::Matrix* mat(const Source& source,
506 ast::Type* type,
507 uint32_t columns,
508 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000509 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000510 }
511
Ben Claytone204f272021-04-22 14:40:23 +0000512 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000513 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000514 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000515
516 /// @param type matrix subtype
517 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000518 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000519
520 /// @param type matrix subtype
521 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000522 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000523
524 /// @param type matrix subtype
525 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000526 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000527
528 /// @param type matrix subtype
529 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000530 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000531
532 /// @param type matrix subtype
533 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000534 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000535
536 /// @param type matrix subtype
537 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000538 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000539
540 /// @param type matrix subtype
541 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000542 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000543
544 /// @param type matrix subtype
545 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000546 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000547
Ben Claytone204f272021-04-22 14:40:23 +0000548 /// @param columns number of columns for the matrix
549 /// @param rows number of rows for the matrix
550 /// @return the tint AST type for a matrix of `type`
551 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000552 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000553 return mat(Of<T>(), columns, rows);
554 }
555
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000556 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
557 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000558 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000559 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000560 }
561
562 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
563 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000564 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000565 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000566 }
567
568 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
569 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000570 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000571 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000572 }
573
574 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
575 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000576 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000577 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000578 }
579
580 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
581 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000582 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000583 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000584 }
585
586 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
587 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000588 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000589 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000590 }
591
592 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
593 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000594 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000595 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000596 }
597
598 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
599 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000600 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000601 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000602 }
603
604 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
605 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000606 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000607 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000608 }
609
610 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000611 /// @param n the array size. 0 represents a runtime-array
612 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000613 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000614 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000615 uint32_t n = 0,
616 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000617 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000618 }
619
620 /// @param source the Source of the node
621 /// @param subtype the array element type
622 /// @param n the array size. 0 represents a runtime-array
623 /// @param decos the optional decorations for the array
624 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000625 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000626 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000627 uint32_t n = 0,
628 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000629 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000630 }
631
Ben Claytonbab31972021-02-08 21:16:21 +0000632 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000633 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000634 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000635 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000636 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000637 ast::DecorationList decos;
638 if (stride) {
639 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
640 }
641 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000642 }
643
Ben Clayton0f88b312021-05-04 17:36:31 +0000644 /// @param source the Source of the node
645 /// @param subtype the array element type
646 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000647 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000648 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000649 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000650 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000651 uint32_t n,
652 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000653 ast::DecorationList decos;
654 if (stride) {
655 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
656 }
657 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000658 }
659
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000660 /// @return the tint AST type for an array of size `N` of type `T`
661 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000662 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000663 return array(Of<T>(), N);
664 }
665
Ben Claytonbab31972021-02-08 21:16:21 +0000666 /// @param stride the array stride
667 /// @return the tint AST type for an array of size `N` of type `T`
668 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000669 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000670 return array(Of<T>(), N, stride);
671 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000672
Ben Clayton0f88b312021-05-04 17:36:31 +0000673 /// Creates a type name
674 /// @param name the name
675 /// @returns the type name
676 template <typename NAME>
677 ast::TypeName* type_name(NAME&& name) const {
678 return builder->create<ast::TypeName>(
679 builder->Sym(std::forward<NAME>(name)));
680 }
681
682 /// Creates a type name
683 /// @param source the Source of the node
684 /// @param name the name
685 /// @returns the type name
686 template <typename NAME>
687 ast::TypeName* type_name(const Source& source, NAME&& name) const {
688 return builder->create<ast::TypeName>(
689 source, builder->Sym(std::forward<NAME>(name)));
690 }
691
Ben Clayton42d1e092021-02-02 14:29:15 +0000692 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000693 /// @param name the alias name
694 /// @param type the alias type
695 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000696 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000697 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000698 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000699 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000700 }
701
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000702 /// Creates an alias type
703 /// @param source the Source of the node
704 /// @param name the alias name
705 /// @param type the alias type
706 /// @returns the alias pointer
707 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000708 ast::Alias* alias(const Source& source,
709 NAME&& name,
710 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000711 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000712 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000713 }
714
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000715 /// @param type the type of the pointer
716 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000717 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000718 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000719 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000720 ast::StorageClass storage_class,
721 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000722 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000723 }
724
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000725 /// @param source the Source of the node
726 /// @param type the type of the pointer
727 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000728 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000729 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000730 ast::Pointer* pointer(const Source& source,
731 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000732 ast::StorageClass storage_class,
733 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000734 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000735 }
736
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000737 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000738 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000739 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000740 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000741 ast::Pointer* pointer(ast::StorageClass storage_class,
742 ast::Access access = ast::Access::kUndefined) const {
743 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000744 }
745
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000746 /// @param source the Source of the node
747 /// @param type the type of the atomic
748 /// @return the atomic to `type`
749 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
750 return builder->create<ast::Atomic>(source, type);
751 }
752
Ben Clayton313e6182021-06-17 19:56:14 +0000753 /// @param type the type of the atomic
754 /// @return the atomic to `type`
755 ast::Atomic* atomic(ast::Type* type) const {
756 return builder->create<ast::Atomic>(type);
757 }
758
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000759 /// @return the atomic to type `T`
760 template <typename T>
761 ast::Atomic* atomic() const {
762 return atomic(Of<T>());
763 }
764
Ben Claytone204f272021-04-22 14:40:23 +0000765 /// @param kind the kind of sampler
766 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000767 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000768 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000769 }
770
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000771 /// @param source the Source of the node
772 /// @param kind the kind of sampler
773 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000774 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000775 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000776 }
777
Ben Claytone204f272021-04-22 14:40:23 +0000778 /// @param dims the dimensionality of the texture
779 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000780 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000781 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000782 }
783
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000784 /// @param source the Source of the node
785 /// @param dims the dimensionality of the texture
786 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000787 ast::DepthTexture* depth_texture(const Source& source,
788 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000789 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000790 }
791
Ben Claytone204f272021-04-22 14:40:23 +0000792 /// @param dims the dimensionality of the texture
793 /// @param subtype the texture subtype.
794 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000795 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
796 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000797 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000798 }
799
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000800 /// @param source the Source of the node
801 /// @param dims the dimensionality of the texture
802 /// @param subtype the texture subtype.
803 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000804 ast::SampledTexture* sampled_texture(const Source& source,
805 ast::TextureDimension dims,
806 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000807 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000808 }
809
Ben Claytone204f272021-04-22 14:40:23 +0000810 /// @param dims the dimensionality of the texture
811 /// @param subtype the texture subtype.
812 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000813 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
814 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000815 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000816 }
817
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000818 /// @param source the Source of the node
819 /// @param dims the dimensionality of the texture
820 /// @param subtype the texture subtype.
821 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000822 ast::MultisampledTexture* multisampled_texture(const Source& source,
823 ast::TextureDimension dims,
824 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000825 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000826 }
827
Ben Claytone204f272021-04-22 14:40:23 +0000828 /// @param dims the dimensionality of the texture
829 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000830 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000831 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000832 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000833 ast::ImageFormat format,
834 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000835 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000836 return builder->create<ast::StorageTexture>(dims, format, subtype,
837 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000838 }
839
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000840 /// @param source the Source of the node
841 /// @param dims the dimensionality of the texture
842 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000843 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000844 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000845 ast::StorageTexture* storage_texture(const Source& source,
846 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000847 ast::ImageFormat format,
848 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000849 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000850 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
851 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000852 }
853
Brandon Jones7b257692021-05-17 17:40:17 +0000854 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000855 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000856 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000857 }
858
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000859 /// @param source the Source of the node
860 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000861 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000862 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000863 }
864
Ben Clayton19b03192021-05-20 15:04:08 +0000865 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000866 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +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 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000870
Ben Clayton8758f102021-06-09 14:32:14 +0000871 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000872 /// @param type the type
873 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000874 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000875
Ben Claytonf5f311e2021-04-28 14:31:23 +0000876 /// The ProgramBuilder
877 ProgramBuilder* const builder;
878
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000879 private:
880 /// CToAST<T> is specialized for various `T` types and each specialization
881 /// contains a single static `get()` method for obtaining the corresponding
882 /// AST type for the C type `T`.
883 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000884 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000885 template <typename T>
886 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000887 };
888
889 //////////////////////////////////////////////////////////////////////////////
890 // AST helper methods
891 //////////////////////////////////////////////////////////////////////////////
892
James Price65ae64d2021-04-29 12:59:14 +0000893 /// @return a new unnamed symbol
894 Symbol Sym() { return Symbols().New(); }
895
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000896 /// @param name the symbol string
897 /// @return a Symbol with the given name
898 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
899
900 /// @param sym the symbol
901 /// @return `sym`
902 Symbol Sym(Symbol sym) { return sym; }
903
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000904 /// @param expr the expression
905 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000906 template <typename T>
907 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
908 return expr;
909 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000910
Ben Claytonb8ea5912021-04-19 16:52:42 +0000911 /// Passthrough for nullptr
912 /// @return nullptr
913 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
914
Ben Claytonfe0910f2021-05-17 15:51:47 +0000915 /// @param source the source information
916 /// @param symbol the identifier symbol
917 /// @return an ast::IdentifierExpression with the given symbol
918 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
919 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000920 }
921
Ben Clayton46d78d72021-02-10 21:34:26 +0000922 /// @param symbol the identifier symbol
923 /// @return an ast::IdentifierExpression with the given symbol
924 ast::IdentifierExpression* Expr(Symbol symbol) {
925 return create<ast::IdentifierExpression>(symbol);
926 }
927
Ben Claytonfe0910f2021-05-17 15:51:47 +0000928 /// @param source the source information
929 /// @param variable the AST variable
930 /// @return an ast::IdentifierExpression with the variable's symbol
931 ast::IdentifierExpression* Expr(const Source& source,
932 ast::Variable* variable) {
933 return create<ast::IdentifierExpression>(source, variable->symbol());
934 }
935
Ben Claytonb8ea5912021-04-19 16:52:42 +0000936 /// @param variable the AST variable
937 /// @return an ast::IdentifierExpression with the variable's symbol
938 ast::IdentifierExpression* Expr(ast::Variable* variable) {
939 return create<ast::IdentifierExpression>(variable->symbol());
940 }
941
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000942 /// @param source the source information
943 /// @param name the identifier name
944 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000945 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
946 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
947 }
948
949 /// @param name the identifier name
950 /// @return an ast::IdentifierExpression with the given name
951 ast::IdentifierExpression* Expr(const char* name) {
952 return create<ast::IdentifierExpression>(Symbols().Register(name));
953 }
954
955 /// @param source the source information
956 /// @param name the identifier name
957 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000958 ast::IdentifierExpression* Expr(const Source& source,
959 const std::string& name) {
960 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
961 }
962
963 /// @param name the identifier name
964 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000965 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000966 return create<ast::IdentifierExpression>(Symbols().Register(name));
967 }
968
Ben Claytonfe0910f2021-05-17 15:51:47 +0000969 /// @param source the source information
970 /// @param value the boolean value
971 /// @return a Scalar constructor for the given value
972 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
973 return create<ast::ScalarConstructorExpression>(source, Literal(value));
974 }
975
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000976 /// @param value the boolean value
977 /// @return a Scalar constructor for the given value
978 ast::ScalarConstructorExpression* Expr(bool value) {
979 return create<ast::ScalarConstructorExpression>(Literal(value));
980 }
981
Ben Claytonfe0910f2021-05-17 15:51:47 +0000982 /// @param source the source information
983 /// @param value the float value
984 /// @return a Scalar constructor for the given value
985 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
986 return create<ast::ScalarConstructorExpression>(source, Literal(value));
987 }
988
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000989 /// @param value the float value
990 /// @return a Scalar constructor for the given value
991 ast::ScalarConstructorExpression* Expr(f32 value) {
992 return create<ast::ScalarConstructorExpression>(Literal(value));
993 }
994
Ben Claytonfe0910f2021-05-17 15:51:47 +0000995 /// @param source the source information
996 /// @param value the integer value
997 /// @return a Scalar constructor for the given value
998 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
999 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1000 }
1001
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001002 /// @param value the integer value
1003 /// @return a Scalar constructor for the given value
1004 ast::ScalarConstructorExpression* Expr(i32 value) {
1005 return create<ast::ScalarConstructorExpression>(Literal(value));
1006 }
1007
Ben Claytonfe0910f2021-05-17 15:51:47 +00001008 /// @param source the source information
1009 /// @param value the unsigned int value
1010 /// @return a Scalar constructor for the given value
1011 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1012 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1013 }
1014
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001015 /// @param value the unsigned int value
1016 /// @return a Scalar constructor for the given value
1017 ast::ScalarConstructorExpression* Expr(u32 value) {
1018 return create<ast::ScalarConstructorExpression>(Literal(value));
1019 }
1020
1021 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1022 /// `list`.
1023 /// @param list the list to append too
1024 /// @param arg the arg to create
1025 template <typename ARG>
1026 void Append(ast::ExpressionList& list, ARG&& arg) {
1027 list.emplace_back(Expr(std::forward<ARG>(arg)));
1028 }
1029
1030 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1031 /// then appends them to `list`.
1032 /// @param list the list to append too
1033 /// @param arg0 the first argument
1034 /// @param args the rest of the arguments
1035 template <typename ARG0, typename... ARGS>
1036 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1037 Append(list, std::forward<ARG0>(arg0));
1038 Append(list, std::forward<ARGS>(args)...);
1039 }
1040
1041 /// @return an empty list of expressions
1042 ast::ExpressionList ExprList() { return {}; }
1043
1044 /// @param args the list of expressions
1045 /// @return the list of expressions converted to `ast::Expression`s using
1046 /// `Expr()`,
1047 template <typename... ARGS>
1048 ast::ExpressionList ExprList(ARGS&&... args) {
1049 ast::ExpressionList list;
1050 list.reserve(sizeof...(args));
1051 Append(list, std::forward<ARGS>(args)...);
1052 return list;
1053 }
1054
1055 /// @param list the list of expressions
1056 /// @return `list`
1057 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1058
1059 /// @param val the boolan value
1060 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001061 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001062
1063 /// @param val the float value
1064 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001065 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001066
1067 /// @param val the unsigned int value
1068 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001069 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001070
1071 /// @param val the integer value
1072 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001073 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074
1075 /// @param args the arguments for the type constructor
1076 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1077 /// of `args` converted to `ast::Expression`s using `Expr()`
1078 template <typename T, typename... ARGS>
1079 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001080 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001081 }
1082
1083 /// @param type the type to construct
1084 /// @param args the arguments for the constructor
1085 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1086 /// values `args`.
1087 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001088 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 return create<ast::TypeConstructorExpression>(
1090 type, ExprList(std::forward<ARGS>(args)...));
1091 }
1092
Ben Clayton313e6182021-06-17 19:56:14 +00001093 /// @param source the source information
1094 /// @param type the type to construct
1095 /// @param args the arguments for the constructor
1096 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1097 /// values `args`.
1098 template <typename... ARGS>
1099 ast::TypeConstructorExpression* Construct(const Source& source,
1100 ast::Type* type,
1101 ARGS&&... args) {
1102 return create<ast::TypeConstructorExpression>(
1103 source, type, ExprList(std::forward<ARGS>(args)...));
1104 }
1105
Ben Claytond9603282021-06-29 12:37:45 +00001106 /// @param expr the expression for the bitcast
1107 /// @return an `ast::BitcastExpression` of type `ty`, with the values of
1108 /// `expr` converted to `ast::Expression`s using `Expr()`
1109 template <typename T, typename EXPR>
1110 ast::BitcastExpression* Bitcast(EXPR&& expr) {
1111 return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
1112 }
1113
1114 /// @param type the type to cast to
1115 /// @param expr the expression for the bitcast
1116 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1117 /// `expr`.
1118 template <typename EXPR>
1119 ast::BitcastExpression* Bitcast(ast::Type* type, EXPR&& expr) {
1120 return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
1121 }
1122
1123 /// @param source the source information
1124 /// @param type the type to cast to
1125 /// @param expr the expression for the bitcast
1126 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1127 /// `expr`.
1128 template <typename EXPR>
1129 ast::BitcastExpression* Bitcast(const Source& source,
1130 ast::Type* type,
1131 EXPR&& expr) {
1132 return create<ast::BitcastExpression>(source, type,
1133 Expr(std::forward<EXPR>(expr)));
1134 }
1135
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001136 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001137 /// @param type the vector type
1138 /// @param size the vector size
1139 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1140 /// type `type`, constructed with the values `args`.
1141 template <typename... ARGS>
1142 ast::TypeConstructorExpression* vec(ast::Type* type,
1143 uint32_t size,
1144 ARGS&&... args) {
1145 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1146 }
1147
1148 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001149 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1150 /// `T`, constructed with the values `args`.
1151 template <typename T, typename... ARGS>
1152 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001153 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001154 }
1155
1156 /// @param args the arguments for the vector constructor
1157 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1158 /// `T`, constructed with the values `args`.
1159 template <typename T, typename... ARGS>
1160 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001161 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001162 }
1163
1164 /// @param args the arguments for the vector constructor
1165 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1166 /// `T`, constructed with the values `args`.
1167 template <typename T, typename... ARGS>
1168 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001169 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001170 }
1171
1172 /// @param args the arguments for the matrix constructor
1173 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1174 /// `T`, constructed with the values `args`.
1175 template <typename T, typename... ARGS>
1176 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001177 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001178 }
1179
1180 /// @param args the arguments for the matrix constructor
1181 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1182 /// `T`, constructed with the values `args`.
1183 template <typename T, typename... ARGS>
1184 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001185 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001186 }
1187
1188 /// @param args the arguments for the matrix constructor
1189 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1190 /// `T`, constructed with the values `args`.
1191 template <typename T, typename... ARGS>
1192 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001193 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001194 }
1195
1196 /// @param args the arguments for the matrix constructor
1197 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1198 /// `T`, constructed with the values `args`.
1199 template <typename T, typename... ARGS>
1200 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001201 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001202 }
1203
1204 /// @param args the arguments for the matrix constructor
1205 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1206 /// `T`, constructed with the values `args`.
1207 template <typename T, typename... ARGS>
1208 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001209 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001210 }
1211
1212 /// @param args the arguments for the matrix constructor
1213 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1214 /// `T`, constructed with the values `args`.
1215 template <typename T, typename... ARGS>
1216 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001217 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 }
1219
1220 /// @param args the arguments for the matrix constructor
1221 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1222 /// `T`, constructed with the values `args`.
1223 template <typename T, typename... ARGS>
1224 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001225 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001226 }
1227
1228 /// @param args the arguments for the matrix constructor
1229 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1230 /// `T`, constructed with the values `args`.
1231 template <typename T, typename... ARGS>
1232 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001233 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001234 }
1235
1236 /// @param args the arguments for the matrix constructor
1237 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1238 /// `T`, constructed with the values `args`.
1239 template <typename T, typename... ARGS>
1240 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001241 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001242 }
1243
1244 /// @param args the arguments for the array constructor
1245 /// @return an `ast::TypeConstructorExpression` of an array with element type
1246 /// `T`, constructed with the values `args`.
1247 template <typename T, int N = 0, typename... ARGS>
1248 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001249 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250 }
1251
1252 /// @param subtype the array element type
1253 /// @param n the array size. 0 represents a runtime-array.
1254 /// @param args the arguments for the array constructor
1255 /// @return an `ast::TypeConstructorExpression` of an array with element type
1256 /// `subtype`, constructed with the values `args`.
1257 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001258 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001259 uint32_t n,
1260 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001261 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262 }
1263
1264 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001266 /// @param optional the optional variable settings.
1267 /// Can be any of the following, in any order:
1268 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001269 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001270 /// * ast::Expression* - specifies the variable's initializer expression
1271 /// * ast::DecorationList - specifies the variable's decorations
1272 /// Note that repeated arguments of the same type will use the last argument's
1273 /// value.
1274 /// @returns a `ast::Variable` with the given name, type and additional
1275 /// options
1276 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001277 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001278 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001279 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001280 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1281 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001282 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001283 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001284 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001285
1286 /// @param source the variable source
1287 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001288 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001289 /// @param optional the optional variable settings.
1290 /// Can be any of the following, in any order:
1291 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001292 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001293 /// * ast::Expression* - specifies the variable's initializer expression
1294 /// * ast::DecorationList - specifies the variable's decorations
1295 /// Note that repeated arguments of the same type will use the last argument's
1296 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001297 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001298 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001299 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001300 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001301 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001302 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001303 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1304 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001305 opts.storage, opts.access, type, false,
1306 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001307 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001308
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001309 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001310 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001311 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001312 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001313 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001314 template <typename NAME>
1315 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001316 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001317 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001318 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001319 return create<ast::Variable>(
1320 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1321 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001322 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001323
1324 /// @param source the variable source
1325 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001326 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001327 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001328 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001329 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001330 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001331 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001332 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001333 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001334 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001335 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001336 return create<ast::Variable>(
1337 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1338 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001339 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001340
James Pricedaf1f1c2021-04-08 22:15:48 +00001341 /// @param name the parameter name
1342 /// @param type the parameter type
1343 /// @param decorations optional parameter decorations
1344 /// @returns a constant `ast::Variable` with the given name and type
1345 template <typename NAME>
1346 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001347 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001348 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001349 return create<ast::Variable>(
1350 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1351 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001352 }
1353
1354 /// @param source the parameter source
1355 /// @param name the parameter name
1356 /// @param type the parameter type
1357 /// @param decorations optional parameter decorations
1358 /// @returns a constant `ast::Variable` with the given name and type
1359 template <typename NAME>
1360 ast::Variable* Param(const Source& source,
1361 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001362 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001363 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001364 return create<ast::Variable>(
1365 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1366 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001367 }
1368
Ben Clayton42708342021-04-21 17:55:12 +00001369 /// @param name the variable name
1370 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001371 /// @param optional the optional variable settings.
1372 /// Can be any of the following, in any order:
1373 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001374 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001375 /// * ast::Expression* - specifies the variable's initializer expression
1376 /// * ast::DecorationList - specifies the variable's decorations
1377 /// Note that repeated arguments of the same type will use the last argument's
1378 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001379 /// @returns a new `ast::Variable`, which is automatically registered as a
1380 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001381 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001382 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001383 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001384 OPTIONAL&&... optional) {
1385 auto* var = Var(std::forward<NAME>(name), type,
1386 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001387 AST().AddGlobalVariable(var);
1388 return var;
1389 }
1390
1391 /// @param source the variable source
1392 /// @param name the variable name
1393 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001394 /// @param optional the optional variable settings.
1395 /// Can be any of the following, in any order:
1396 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001397 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001398 /// * ast::Expression* - specifies the variable's initializer expression
1399 /// * ast::DecorationList - specifies the variable's decorations
1400 /// Note that repeated arguments of the same type will use the last argument's
1401 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001402 /// @returns a new `ast::Variable`, which is automatically registered as a
1403 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001404 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001405 ast::Variable* Global(const Source& source,
1406 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001407 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001408 OPTIONAL&&... optional) {
1409 auto* var = Var(source, std::forward<NAME>(name), type,
1410 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001411 AST().AddGlobalVariable(var);
1412 return var;
1413 }
1414
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001415 /// @param name the variable name
1416 /// @param type the variable type
1417 /// @param constructor constructor expression
1418 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001419 /// @returns a const `ast::Variable` constructed by calling Var() with the
1420 /// arguments of `args`, which is automatically registered as a global
1421 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001422 template <typename NAME>
1423 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001424 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001425 ast::Expression* constructor,
1426 ast::DecorationList decorations = {}) {
1427 auto* var = Const(std::forward<NAME>(name), type, constructor,
1428 std::move(decorations));
1429 AST().AddGlobalVariable(var);
1430 return var;
1431 }
1432
1433 /// @param source the variable source
1434 /// @param name the variable name
1435 /// @param type the variable type
1436 /// @param constructor constructor expression
1437 /// @param decorations optional variable decorations
1438 /// @returns a const `ast::Variable` constructed by calling Var() with the
1439 /// arguments of `args`, which is automatically registered as a global
1440 /// variable with the ast::Module.
1441 template <typename NAME>
1442 ast::Variable* GlobalConst(const Source& source,
1443 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001444 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001445 ast::Expression* constructor,
1446 ast::DecorationList decorations = {}) {
1447 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1448 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001449 AST().AddGlobalVariable(var);
1450 return var;
1451 }
1452
Ben Claytonfe0910f2021-05-17 15:51:47 +00001453 /// @param source the source information
1454 /// @param expr the expression to take the address of
1455 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1456 template <typename EXPR>
1457 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1458 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1459 Expr(std::forward<EXPR>(expr)));
1460 }
1461
1462 /// @param expr the expression to take the address of
1463 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1464 template <typename EXPR>
1465 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1466 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1467 Expr(std::forward<EXPR>(expr)));
1468 }
1469
1470 /// @param source the source information
1471 /// @param expr the expression to perform an indirection on
1472 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1473 template <typename EXPR>
1474 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1475 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1476 Expr(std::forward<EXPR>(expr)));
1477 }
1478
1479 /// @param expr the expression to perform an indirection on
1480 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1481 template <typename EXPR>
1482 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1483 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1484 Expr(std::forward<EXPR>(expr)));
1485 }
1486
Antonio Maiorano14b34032021-06-09 20:17:59 +00001487 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001488 /// @param func the function name
1489 /// @param args the function call arguments
1490 /// @returns a `ast::CallExpression` to the function `func`, with the
1491 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1492 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001493 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1494 return create<ast::CallExpression>(source, Expr(func),
1495 ExprList(std::forward<ARGS>(args)...));
1496 }
1497
1498 /// @param func the function name
1499 /// @param args the function call arguments
1500 /// @returns a `ast::CallExpression` to the function `func`, with the
1501 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1502 template <typename NAME,
1503 typename... ARGS,
1504 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001505 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1506 return create<ast::CallExpression>(Expr(func),
1507 ExprList(std::forward<ARGS>(args)...));
1508 }
1509
Ben Clayton7fe01062021-06-11 13:22:27 +00001510 /// @param expr the expression to ignore
1511 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1512 /// passed the single `expr` argument
1513 template <typename EXPR>
1514 ast::CallStatement* Ignore(EXPR&& expr) {
1515 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1516 }
1517
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001518 /// @param lhs the left hand argument to the addition operation
1519 /// @param rhs the right hand argument to the addition operation
1520 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1521 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001522 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001523 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1524 Expr(std::forward<LHS>(lhs)),
1525 Expr(std::forward<RHS>(rhs)));
1526 }
1527
James Pricec32e8f62021-06-22 20:08:29 +00001528 /// @param lhs the left hand argument to the and operation
1529 /// @param rhs the right hand argument to the and operation
1530 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1531 template <typename LHS, typename RHS>
1532 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1533 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1534 Expr(std::forward<LHS>(lhs)),
1535 Expr(std::forward<RHS>(rhs)));
1536 }
1537
Ben Claytond9603282021-06-29 12:37:45 +00001538 /// @param lhs the left hand argument to the or operation
1539 /// @param rhs the right hand argument to the or operation
1540 /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
1541 template <typename LHS, typename RHS>
1542 ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
1543 return create<ast::BinaryExpression>(ast::BinaryOp::kOr,
1544 Expr(std::forward<LHS>(lhs)),
1545 Expr(std::forward<RHS>(rhs)));
1546 }
1547
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001548 /// @param lhs the left hand argument to the subtraction operation
1549 /// @param rhs the right hand argument to the subtraction operation
1550 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1551 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001552 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001553 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1554 Expr(std::forward<LHS>(lhs)),
1555 Expr(std::forward<RHS>(rhs)));
1556 }
1557
1558 /// @param lhs the left hand argument to the multiplication operation
1559 /// @param rhs the right hand argument to the multiplication operation
1560 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1561 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001562 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001563 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1564 Expr(std::forward<LHS>(lhs)),
1565 Expr(std::forward<RHS>(rhs)));
1566 }
1567
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001568 /// @param source the source information
1569 /// @param lhs the left hand argument to the multiplication operation
1570 /// @param rhs the right hand argument to the multiplication operation
1571 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1572 template <typename LHS, typename RHS>
1573 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1574 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1575 Expr(std::forward<LHS>(lhs)),
1576 Expr(std::forward<RHS>(rhs)));
1577 }
1578
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001579 /// @param lhs the left hand argument to the division operation
1580 /// @param rhs the right hand argument to the division operation
1581 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1582 template <typename LHS, typename RHS>
1583 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1584 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1585 Expr(std::forward<LHS>(lhs)),
1586 Expr(std::forward<RHS>(rhs)));
1587 }
1588
Ben Claytond9603282021-06-29 12:37:45 +00001589 /// @param lhs the left hand argument to the bit shift right operation
1590 /// @param rhs the right hand argument to the bit shift right operation
1591 /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
1592 template <typename LHS, typename RHS>
1593 ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
1594 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftRight,
1595 Expr(std::forward<LHS>(lhs)),
1596 Expr(std::forward<RHS>(rhs)));
1597 }
1598
1599 /// @param lhs the left hand argument to the bit shift left operation
1600 /// @param rhs the right hand argument to the bit shift left operation
1601 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
1602 template <typename LHS, typename RHS>
1603 ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
1604 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftLeft,
1605 Expr(std::forward<LHS>(lhs)),
1606 Expr(std::forward<RHS>(rhs)));
1607 }
1608
Ben Clayton0597a2b2021-06-16 09:19:36 +00001609 /// @param source the source information
1610 /// @param arr the array argument for the array accessor expression
1611 /// @param idx the index argument for the array accessor expression
1612 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1613 template <typename ARR, typename IDX>
1614 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1615 ARR&& arr,
1616 IDX&& idx) {
1617 return create<ast::ArrayAccessorExpression>(
1618 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1619 }
1620
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001621 /// @param arr the array argument for the array accessor expression
1622 /// @param idx the index argument for the array accessor expression
1623 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1624 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001625 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001626 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1627 Expr(std::forward<IDX>(idx)));
1628 }
1629
1630 /// @param obj the object for the member accessor expression
1631 /// @param idx the index argument for the array accessor expression
1632 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1633 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001634 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001635 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1636 Expr(std::forward<IDX>(idx)));
1637 }
1638
Ben Clayton42d1e092021-02-02 14:29:15 +00001639 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001640 /// @param val the offset value
1641 /// @returns the offset decoration pointer
1642 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1643 return create<ast::StructMemberOffsetDecoration>(source_, val);
1644 }
1645
Ben Claytond614dd52021-03-15 10:43:11 +00001646 /// Creates a ast::StructMemberSizeDecoration
1647 /// @param source the source information
1648 /// @param val the size value
1649 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001650 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1651 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001652 return create<ast::StructMemberSizeDecoration>(source, val);
1653 }
1654
1655 /// Creates a ast::StructMemberSizeDecoration
1656 /// @param val the size value
1657 /// @returns the size decoration pointer
1658 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1659 return create<ast::StructMemberSizeDecoration>(source_, val);
1660 }
1661
1662 /// Creates a ast::StructMemberAlignDecoration
1663 /// @param source the source information
1664 /// @param val the align value
1665 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001666 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1667 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001668 return create<ast::StructMemberAlignDecoration>(source, val);
1669 }
1670
1671 /// Creates a ast::StructMemberAlignDecoration
1672 /// @param val the align value
1673 /// @returns the align decoration pointer
1674 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1675 return create<ast::StructMemberAlignDecoration>(source_, val);
1676 }
1677
Ben Clayton42d1e092021-02-02 14:29:15 +00001678 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001679 /// @param source the source information
1680 /// @param name the function name
1681 /// @param params the function parameters
1682 /// @param type the function return type
1683 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001684 /// @param decorations the optional function decorations
1685 /// @param return_type_decorations the optional function return type
1686 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001687 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001688 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001689 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001690 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001691 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001692 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001693 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001694 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001695 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001696 auto* func =
1697 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1698 type, create<ast::BlockStatement>(body),
1699 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001700 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001701 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001702 }
1703
Ben Clayton42d1e092021-02-02 14:29:15 +00001704 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001705 /// @param name the function name
1706 /// @param params the function parameters
1707 /// @param type the function return type
1708 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001709 /// @param decorations the optional function decorations
1710 /// @param return_type_decorations the optional function return type
1711 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001712 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001713 template <typename NAME>
1714 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001715 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001716 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001717 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001718 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001719 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001720 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1721 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001722 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001723 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001724 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001725 }
1726
Ben Clayton49668bb2021-04-17 05:32:01 +00001727 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001728 /// @param source the source information
1729 /// @returns the return statement pointer
1730 ast::ReturnStatement* Return(const Source& source) {
1731 return create<ast::ReturnStatement>(source);
1732 }
1733
1734 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001735 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001736 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1737
1738 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001739 /// @param source the source information
1740 /// @param val the return value
1741 /// @returns the return statement pointer
1742 template <typename EXPR>
1743 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1744 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1745 }
1746
1747 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001748 /// @param val the return value
1749 /// @returns the return statement pointer
1750 template <typename EXPR>
1751 ast::ReturnStatement* Return(EXPR&& val) {
1752 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001753 }
1754
Ben Clayton950809f2021-06-09 14:32:14 +00001755 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001756 /// @param source the source information
1757 /// @param name the alias name
1758 /// @param type the alias target type
1759 /// @returns the alias type
1760 template <typename NAME>
1761 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1762 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001763 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001764 return out;
1765 }
1766
Ben Clayton950809f2021-06-09 14:32:14 +00001767 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001768 /// @param name the alias name
1769 /// @param type the alias target type
1770 /// @returns the alias type
1771 template <typename NAME>
1772 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1773 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001774 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001775 return out;
1776 }
1777
Ben Clayton950809f2021-06-09 14:32:14 +00001778 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001779 /// @param source the source information
1780 /// @param name the struct name
1781 /// @param members the struct members
1782 /// @param decorations the optional struct decorations
1783 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001784 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001785 ast::Struct* Structure(const Source& source,
1786 NAME&& name,
1787 ast::StructMemberList members,
1788 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001789 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001790 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001791 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001792 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001793 return type;
1794 }
1795
Ben Clayton950809f2021-06-09 14:32:14 +00001796 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001797 /// @param name the struct name
1798 /// @param members the struct members
1799 /// @param decorations the optional struct decorations
1800 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001801 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001802 ast::Struct* Structure(NAME&& name,
1803 ast::StructMemberList members,
1804 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001805 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001806 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001807 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001808 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001809 return type;
1810 }
1811
Ben Clayton42d1e092021-02-02 14:29:15 +00001812 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001813 /// @param source the source information
1814 /// @param name the struct member name
1815 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001816 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001817 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001818 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001819 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001820 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001821 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001822 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001823 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1824 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001825 }
1826
Ben Clayton42d1e092021-02-02 14:29:15 +00001827 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001828 /// @param name the struct member name
1829 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001830 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001831 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001832 template <typename NAME>
1833 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001834 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001835 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001836 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1837 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001838 }
1839
Ben Claytonbab31972021-02-08 21:16:21 +00001840 /// Creates a ast::StructMember with the given byte offset
1841 /// @param offset the offset to use in the StructMemberOffsetDecoration
1842 /// @param name the struct member name
1843 /// @param type the struct member type
1844 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001845 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001846 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001847 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001848 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001849 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001850 create<ast::StructMemberOffsetDecoration>(offset),
1851 });
1852 }
1853
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001854 /// Creates a ast::BlockStatement with input statements
1855 /// @param statements statements of block
1856 /// @returns the block statement pointer
1857 template <typename... Statements>
1858 ast::BlockStatement* Block(Statements&&... statements) {
1859 return create<ast::BlockStatement>(
1860 ast::StatementList{std::forward<Statements>(statements)...});
1861 }
1862
1863 /// Creates a ast::ElseStatement with input condition and body
1864 /// @param condition the else condition expression
1865 /// @param body the else body
1866 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001867 template <typename CONDITION>
1868 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1869 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1870 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001871 }
1872
1873 /// Creates a ast::IfStatement with input condition, body, and optional
1874 /// variadic else statements
1875 /// @param condition the if statement condition expression
1876 /// @param body the if statement body
1877 /// @param elseStatements optional variadic else statements
1878 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001879 template <typename CONDITION, typename... ELSE_STATEMENTS>
1880 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001881 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001882 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001883 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001884 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001885 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001886 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001887 }
1888
1889 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001890 /// @param source the source information
1891 /// @param lhs the left hand side expression initializer
1892 /// @param rhs the right hand side expression initializer
1893 /// @returns the assignment statement pointer
1894 template <typename LhsExpressionInit, typename RhsExpressionInit>
1895 ast::AssignmentStatement* Assign(const Source& source,
1896 LhsExpressionInit&& lhs,
1897 RhsExpressionInit&& rhs) {
1898 return create<ast::AssignmentStatement>(
1899 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1900 Expr(std::forward<RhsExpressionInit>(rhs)));
1901 }
1902
1903 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001904 /// @param lhs the left hand side expression initializer
1905 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001906 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001907 template <typename LhsExpressionInit, typename RhsExpressionInit>
1908 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1909 RhsExpressionInit&& rhs) {
1910 return create<ast::AssignmentStatement>(
1911 Expr(std::forward<LhsExpressionInit>(lhs)),
1912 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001913 }
1914
1915 /// Creates a ast::LoopStatement with input body and optional continuing
1916 /// @param body the loop body
1917 /// @param continuing the optional continuing block
1918 /// @returns the loop statement pointer
1919 ast::LoopStatement* Loop(ast::BlockStatement* body,
1920 ast::BlockStatement* continuing = nullptr) {
1921 return create<ast::LoopStatement>(body, continuing);
1922 }
1923
Ben Clayton65cd2592021-07-02 19:27:42 +00001924 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1925 /// condition and continuing.
1926 /// @param source the source information
1927 /// @param init the optional loop initializer
1928 /// @param cond the optional loop condition
1929 /// @param cont the optional loop continuing
1930 /// @param body the loop body
1931 /// @returns the for loop statement pointer
1932 template <typename COND>
1933 ast::ForLoopStatement* For(const Source& source,
1934 ast::Statement* init,
1935 COND&& cond,
1936 ast::Statement* cont,
1937 ast::BlockStatement* body) {
1938 return create<ast::ForLoopStatement>(
1939 source, init, Expr(std::forward<COND>(cond)), cont, body);
1940 }
1941
1942 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1943 /// condition and continuing.
1944 /// @param init the optional loop initializer
1945 /// @param cond the optional loop condition
1946 /// @param cont the optional loop continuing
1947 /// @param body the loop body
1948 /// @returns the for loop statement pointer
1949 template <typename COND>
1950 ast::ForLoopStatement* For(ast::Statement* init,
1951 COND&& cond,
1952 ast::Statement* cont,
1953 ast::BlockStatement* body) {
1954 return create<ast::ForLoopStatement>(init, Expr(std::forward<COND>(cond)),
1955 cont, body);
1956 }
1957
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001958 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001959 /// @param source the source information
1960 /// @param var the variable to wrap in a decl statement
1961 /// @returns the variable decl statement pointer
1962 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1963 return create<ast::VariableDeclStatement>(source, var);
1964 }
1965
1966 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001967 /// @param var the variable to wrap in a decl statement
1968 /// @returns the variable decl statement pointer
1969 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1970 return create<ast::VariableDeclStatement>(var);
1971 }
1972
Antonio Maioranocea744d2021-03-25 12:55:27 +00001973 /// Creates a ast::SwitchStatement with input expression and cases
1974 /// @param condition the condition expression initializer
1975 /// @param cases case statements
1976 /// @returns the switch statement pointer
1977 template <typename ExpressionInit, typename... Cases>
1978 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1979 return create<ast::SwitchStatement>(
1980 Expr(std::forward<ExpressionInit>(condition)),
1981 ast::CaseStatementList{std::forward<Cases>(cases)...});
1982 }
1983
1984 /// Creates a ast::CaseStatement with input list of selectors, and body
1985 /// @param selectors list of selectors
1986 /// @param body the case body
1987 /// @returns the case statement pointer
1988 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1989 ast::BlockStatement* body = nullptr) {
1990 return create<ast::CaseStatement>(std::move(selectors),
1991 body ? body : Block());
1992 }
1993
1994 /// Convenient overload that takes a single selector
1995 /// @param selector a single case selector
1996 /// @param body the case body
1997 /// @returns the case statement pointer
1998 ast::CaseStatement* Case(ast::IntLiteral* selector,
1999 ast::BlockStatement* body = nullptr) {
2000 return Case(ast::CaseSelectorList{selector}, body);
2001 }
2002
2003 /// Convenience function that creates a 'default' ast::CaseStatement
2004 /// @param body the case body
2005 /// @returns the case statement pointer
2006 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
2007 return Case(ast::CaseSelectorList{}, body);
2008 }
2009
James Price68f558f2021-04-06 15:51:47 +00002010 /// Creates an ast::BuiltinDecoration
2011 /// @param source the source information
2012 /// @param builtin the builtin value
2013 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002014 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00002015 return create<ast::BuiltinDecoration>(source, builtin);
2016 }
2017
2018 /// Creates an ast::BuiltinDecoration
2019 /// @param builtin the builtin value
2020 /// @returns the builtin decoration pointer
2021 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
2022 return create<ast::BuiltinDecoration>(source_, builtin);
2023 }
2024
James Price989a8e42021-06-28 23:04:43 +00002025 /// Creates an ast::InterpolateDecoration
2026 /// @param source the source information
2027 /// @param type the interpolation type
2028 /// @param sampling the interpolation sampling
2029 /// @returns the interpolate decoration pointer
2030 ast::InterpolateDecoration* Interpolate(const Source& source,
2031 ast::InterpolationType type,
2032 ast::InterpolationSampling sampling) {
2033 return create<ast::InterpolateDecoration>(source, type, sampling);
2034 }
2035
2036 /// Creates an ast::InterpolateDecoration
2037 /// @param type the interpolation type
2038 /// @param sampling the interpolation sampling
2039 /// @returns the interpolate decoration pointer
2040 ast::InterpolateDecoration* Interpolate(ast::InterpolationType type,
2041 ast::InterpolationSampling sampling) {
2042 return create<ast::InterpolateDecoration>(source_, type, sampling);
2043 }
2044
James Price68f558f2021-04-06 15:51:47 +00002045 /// Creates an ast::LocationDecoration
2046 /// @param source the source information
2047 /// @param location the location value
2048 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002049 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00002050 return create<ast::LocationDecoration>(source, location);
2051 }
2052
2053 /// Creates an ast::LocationDecoration
2054 /// @param location the location value
2055 /// @returns the location decoration pointer
2056 ast::LocationDecoration* Location(uint32_t location) {
2057 return create<ast::LocationDecoration>(source_, location);
2058 }
2059
James Pricef2f3bfc2021-05-13 20:32:32 +00002060 /// Creates an ast::OverrideDecoration with a specific constant ID
2061 /// @param source the source information
2062 /// @param id the id value
2063 /// @returns the override decoration pointer
2064 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
2065 return create<ast::OverrideDecoration>(source, id);
2066 }
2067
2068 /// Creates an ast::OverrideDecoration with a specific constant ID
2069 /// @param id the optional id value
2070 /// @returns the override decoration pointer
2071 ast::OverrideDecoration* Override(uint32_t id) {
2072 return Override(source_, id);
2073 }
2074
2075 /// Creates an ast::OverrideDecoration without a constant ID
2076 /// @param source the source information
2077 /// @returns the override decoration pointer
2078 ast::OverrideDecoration* Override(const Source& source) {
2079 return create<ast::OverrideDecoration>(source);
2080 }
2081
2082 /// Creates an ast::OverrideDecoration without a constant ID
2083 /// @returns the override decoration pointer
2084 ast::OverrideDecoration* Override() { return Override(source_); }
2085
James Price68f558f2021-04-06 15:51:47 +00002086 /// Creates an ast::StageDecoration
2087 /// @param source the source information
2088 /// @param stage the pipeline stage
2089 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002090 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00002091 return create<ast::StageDecoration>(source, stage);
2092 }
2093
2094 /// Creates an ast::StageDecoration
2095 /// @param stage the pipeline stage
2096 /// @returns the stage decoration pointer
2097 ast::StageDecoration* Stage(ast::PipelineStage stage) {
2098 return create<ast::StageDecoration>(source_, stage);
2099 }
2100
James Price70f80bb2021-05-19 13:40:08 +00002101 /// Creates an ast::WorkgroupDecoration
2102 /// @param x the x dimension expression
2103 /// @returns the workgroup decoration pointer
2104 template <typename EXPR_X>
2105 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
2106 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
2107 }
2108
2109 /// Creates an ast::WorkgroupDecoration
2110 /// @param x the x dimension expression
2111 /// @param y the y dimension expression
2112 /// @returns the workgroup decoration pointer
2113 template <typename EXPR_X, typename EXPR_Y>
2114 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
2115 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
2116 nullptr);
2117 }
2118
2119 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002120 /// @param source the source information
2121 /// @param x the x dimension expression
2122 /// @param y the y dimension expression
2123 /// @param z the z dimension expression
2124 /// @returns the workgroup decoration pointer
2125 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2126 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2127 EXPR_X&& x,
2128 EXPR_Y&& y,
2129 EXPR_Z&& z) {
2130 return create<ast::WorkgroupDecoration>(
2131 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2132 Expr(std::forward<EXPR_Z>(z)));
2133 }
2134
2135 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002136 /// @param x the x dimension expression
2137 /// @param y the y dimension expression
2138 /// @param z the z dimension expression
2139 /// @returns the workgroup decoration pointer
2140 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2141 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2142 return create<ast::WorkgroupDecoration>(
2143 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2144 Expr(std::forward<EXPR_Z>(z)));
2145 }
2146
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002147 /// Sets the current builder source to `src`
2148 /// @param src the Source used for future create() calls
2149 void SetSource(const Source& src) {
2150 AssertNotMoved();
2151 source_ = src;
2152 }
2153
2154 /// Sets the current builder source to `loc`
2155 /// @param loc the Source used for future create() calls
2156 void SetSource(const Source::Location& loc) {
2157 AssertNotMoved();
2158 source_ = Source(loc);
2159 }
2160
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002161 /// Marks that the given transform has been applied to this program.
2162 /// @param transform the transform that has been applied
2163 void SetTransformApplied(const CastableBase* transform) {
2164 transforms_applied_.emplace(&transform->TypeInfo());
2165 }
2166
2167 /// Marks that the given transform `T` has been applied to this program.
2168 template <typename T>
2169 void SetTransformApplied() {
2170 transforms_applied_.emplace(&TypeInfo::Of<T>());
2171 }
2172
2173 /// Marks that the transforms with the given TypeInfos have been applied to
2174 /// this program.
2175 /// @param transforms the set of transform TypeInfos that has been applied
2176 void SetTransformApplied(
2177 const std::unordered_set<const TypeInfo*>& transforms) {
2178 for (auto* transform : transforms) {
2179 transforms_applied_.emplace(transform);
2180 }
2181 }
2182
2183 /// @returns true if the transform of type `T` was applied.
2184 template <typename T>
2185 bool HasTransformApplied() {
2186 return transforms_applied_.count(&TypeInfo::Of<T>());
2187 }
2188
2189 /// @return the TypeInfo pointers of all transforms that have been applied to
2190 /// this program.
2191 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2192 return transforms_applied_;
2193 }
2194
Ben Clayton33352542021-01-29 16:43:41 +00002195 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002196 /// @note As the Resolver is run when the Program is built, this will only be
2197 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002198 /// @param expr the AST expression
2199 /// @return the resolved semantic type for the expression, or nullptr if the
2200 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002201 sem::Type* TypeOf(const ast::Expression* expr) const;
2202
Ben Claytonfe0910f2021-05-17 15:51:47 +00002203 /// Helper for returning the resolved semantic type of the variable `var`.
2204 /// @note As the Resolver is run when the Program is built, this will only be
2205 /// useful for the Resolver itself and tests that use their own Resolver.
2206 /// @param var the AST variable
2207 /// @return the resolved semantic type for the variable, or nullptr if the
2208 /// variable has no resolved type.
2209 sem::Type* TypeOf(const ast::Variable* var) const;
2210
Ben Claytonfbec46f2021-04-30 20:20:19 +00002211 /// Helper for returning the resolved semantic type of the AST type `type`.
2212 /// @note As the Resolver is run when the Program is built, this will only be
2213 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002214 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002215 /// @return the resolved semantic type for the type, or nullptr if the type
2216 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002217 const sem::Type* TypeOf(const ast::Type* type) const;
2218
2219 /// Helper for returning the resolved semantic type of the AST type
2220 /// declaration `type_decl`.
2221 /// @note As the Resolver is run when the Program is built, this will only be
2222 /// useful for the Resolver itself and tests that use their own Resolver.
2223 /// @param type_decl the AST type declaration
2224 /// @return the resolved semantic type for the type declaration, or nullptr if
2225 /// the type declaration has no resolved type.
2226 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002227
Ben Clayton169512e2021-04-17 05:52:11 +00002228 /// Wraps the ast::Literal in a statement. This is used by tests that
2229 /// construct a partial AST and require the Resolver to reach these
2230 /// nodes.
2231 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2232 /// @return the ast::Statement that wraps the ast::Statement
2233 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002234 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002235 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002236 /// nodes.
2237 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2238 /// @return the ast::Statement that wraps the ast::Expression
2239 ast::Statement* WrapInStatement(ast::Expression* expr);
2240 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002241 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002242 /// these nodes.
2243 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2244 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2245 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2246 /// Returns the statement argument. Used as a passthrough-overload by
2247 /// WrapInFunction().
2248 /// @param stmt the ast::Statement
2249 /// @return `stmt`
2250 ast::Statement* WrapInStatement(ast::Statement* stmt);
2251 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002252 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002253 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002254 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002255 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002256 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002257 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002258 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002259 }
2260 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002261 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002262 /// @returns the function
2263 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002264
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002265 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002266 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002267
2268 protected:
2269 /// Asserts that the builder has not been moved.
2270 void AssertNotMoved() const;
2271
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002272 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002273 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002274 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002275 ASTNodeAllocator ast_nodes_;
2276 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002277 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002278 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002279 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002280 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002281 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002282
2283 /// The source to use when creating AST nodes without providing a Source as
2284 /// the first argument.
2285 Source source_;
2286
Ben Clayton5f0ea112021-03-09 10:54:37 +00002287 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002288 /// program when built.
2289 bool resolve_on_build_ = true;
2290
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002291 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2292 bool moved_ = false;
2293};
2294
2295//! @cond Doxygen_Suppress
2296// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2297template <>
2298struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002299 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002300 return t->i32();
2301 }
2302};
2303template <>
2304struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002305 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002306 return t->u32();
2307 }
2308};
2309template <>
2310struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002311 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002312 return t->f32();
2313 }
2314};
2315template <>
2316struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002317 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002318 return t->bool_();
2319 }
2320};
2321template <>
2322struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002323 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002324 return t->void_();
2325 }
2326};
2327//! @endcond
2328
Ben Clayton917b14b2021-04-19 16:50:23 +00002329/// @param builder the ProgramBuilder
2330/// @returns the ProgramID of the ProgramBuilder
2331inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2332 return builder->ID();
2333}
2334
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002335} // namespace tint
2336
2337#endif // SRC_PROGRAM_BUILDER_H_