blob: 8627c9afddaeeeb0df099256f7a46fb5727148b0 [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 Claytona922e652021-04-21 13:37:22 +000028#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000029#include "src/ast/bool_literal.h"
30#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000031#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000032#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000033#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000034#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000035#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000036#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000037#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000038#include "src/ast/if_statement.h"
James Price989a8e42021-06-28 23:04:43 +000039#include "src/ast/interpolate_decoration.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000040#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000041#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000042#include "src/ast/member_accessor_expression.h"
43#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000045#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000046#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000047#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000049#include "src/ast/scalar_constructor_expression.h"
50#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000051#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000052#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000053#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000054#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000055#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000056#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000057#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000058#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000059#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000060#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000061#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000062#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000063#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000064#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000065#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000066#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000067#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000068#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000069#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000070#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000071#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000072#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000073#include "src/sem/f32_type.h"
74#include "src/sem/i32_type.h"
75#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000076#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000077#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000078#include "src/sem/sampled_texture_type.h"
79#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000080#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000081#include "src/sem/u32_type.h"
82#include "src/sem/vector_type.h"
83#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000084
Ben Clayton03d10722021-06-05 12:45:50 +000085#ifdef INCLUDE_TINT_TINT_H_
86#error "internal tint header being #included from tint.h"
87#endif
88
Ben Clayton401b96b2021-02-03 17:19:59 +000089// Forward declarations
Ben Claytonb5cd10c2021-06-25 10:26:26 +000090namespace tint {
Ben Clayton401b96b2021-02-03 17:19:59 +000091namespace ast {
92class VariableDeclStatement;
93} // namespace ast
Ben Claytonb5cd10c2021-06-25 10:26:26 +000094} // namespace tint
Ben Clayton401b96b2021-02-03 17:19:59 +000095
Ben Claytonb5cd10c2021-06-25 10:26:26 +000096namespace tint {
Ben Claytona6b9a8e2021-01-26 16:57:10 +000097class CloneContext;
98
99/// ProgramBuilder is a mutable builder for a Program.
100/// To construct a Program, populate the builder and then `std::move` it to a
101/// Program.
102class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +0000103 /// VarOptionals is a helper for accepting a number of optional, extra
104 /// arguments for Var() and Global().
105 struct VarOptionals {
106 template <typename... ARGS>
107 explicit VarOptionals(ARGS&&... args) {
108 Apply(std::forward<ARGS>(args)...);
109 }
110 ~VarOptionals();
111
112 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000113 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000114 ast::Expression* constructor = nullptr;
115 ast::DecorationList decorations = {};
116
117 private:
118 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000119 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000120 void Set(ast::Expression* c) { constructor = c; }
121 void Set(const ast::DecorationList& l) { decorations = l; }
122
123 template <typename FIRST, typename... ARGS>
124 void Apply(FIRST&& first, ARGS&&... args) {
125 Set(std::forward<FIRST>(first));
126 Apply(std::forward<ARGS>(args)...);
127 }
128 void Apply() {}
129 };
130
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000131 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000132 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
133 using ASTNodeAllocator = BlockAllocator<ast::Node>;
134
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000135 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
136 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000137
138 /// `i32` is a type alias to `int`.
139 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
140 /// WGSL syntax.
141 /// Note: this is intentionally not aliased to uint32_t as we want integer
142 /// literals passed to the builder to match WGSL's integer literal types.
143 using i32 = decltype(1);
144 /// `u32` is a type alias to `unsigned int`.
145 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
146 /// WGSL syntax.
147 /// Note: this is intentionally not aliased to uint32_t as we want integer
148 /// literals passed to the builder to match WGSL's integer literal types.
149 using u32 = decltype(1u);
150 /// `f32` is a type alias to `float`
151 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
152 /// WGSL syntax.
153 using f32 = float;
154
155 /// Constructor
156 ProgramBuilder();
157
158 /// Move constructor
159 /// @param rhs the builder to move
160 ProgramBuilder(ProgramBuilder&& rhs);
161
162 /// Destructor
163 virtual ~ProgramBuilder();
164
165 /// Move assignment operator
166 /// @param rhs the builder to move
167 /// @return this builder
168 ProgramBuilder& operator=(ProgramBuilder&& rhs);
169
Ben Claytone43c8302021-01-29 11:59:32 +0000170 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
171 /// making a deep clone of the Program contents.
172 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
173 /// existing immutable program.
174 /// As the returned ProgramBuilder wraps `program`, `program` must not be
175 /// destructed or assigned while using the returned ProgramBuilder.
176 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
177 /// function. See crbug.com/tint/460.
178 /// @param program the immutable Program to wrap
179 /// @return the ProgramBuilder that wraps `program`
180 static ProgramBuilder Wrap(const Program* program);
181
Ben Claytone6995de2021-04-13 23:27:27 +0000182 /// @returns the unique identifier for this program
183 ProgramID ID() const { return id_; }
184
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000185 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000186 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000187 AssertNotMoved();
188 return types_;
189 }
190
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000191 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000192 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000193 AssertNotMoved();
194 return types_;
195 }
196
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000197 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000198 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000199 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000200 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000201 }
202
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000203 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000204 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000205 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000206 return ast_nodes_;
207 }
208
209 /// @returns a reference to the program's semantic nodes storage
210 SemNodeAllocator& SemNodes() {
211 AssertNotMoved();
212 return sem_nodes_;
213 }
214
215 /// @returns a reference to the program's semantic nodes storage
216 const SemNodeAllocator& SemNodes() const {
217 AssertNotMoved();
218 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000219 }
220
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000221 /// @returns a reference to the program's AST root Module
222 ast::Module& AST() {
223 AssertNotMoved();
224 return *ast_;
225 }
226
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000227 /// @returns a reference to the program's AST root Module
228 const ast::Module& AST() const {
229 AssertNotMoved();
230 return *ast_;
231 }
232
233 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000234 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000235 AssertNotMoved();
236 return sem_;
237 }
238
239 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000240 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000241 AssertNotMoved();
242 return sem_;
243 }
244
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000245 /// @returns a reference to the program's SymbolTable
246 SymbolTable& Symbols() {
247 AssertNotMoved();
248 return symbols_;
249 }
250
Ben Clayton708dc2d2021-01-29 11:22:40 +0000251 /// @returns a reference to the program's SymbolTable
252 const SymbolTable& Symbols() const {
253 AssertNotMoved();
254 return symbols_;
255 }
256
Ben Clayton844217f2021-01-27 18:49:05 +0000257 /// @returns a reference to the program's diagnostics
258 diag::List& Diagnostics() {
259 AssertNotMoved();
260 return diagnostics_;
261 }
262
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000263 /// @returns a reference to the program's diagnostics
264 const diag::List& Diagnostics() const {
265 AssertNotMoved();
266 return diagnostics_;
267 }
268
Ben Clayton5f0ea112021-03-09 10:54:37 +0000269 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000270 /// @param enable the new flag value (defaults to true)
271 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
272
Ben Clayton5f0ea112021-03-09 10:54:37 +0000273 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000274 /// built.
275 bool ResolveOnBuild() const { return resolve_on_build_; }
276
Ben Clayton844217f2021-01-27 18:49:05 +0000277 /// @returns true if the program has no error diagnostics and is not missing
278 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000279 bool IsValid() const;
280
Ben Clayton708dc2d2021-01-29 11:22:40 +0000281 /// Writes a representation of the node to the output stream
282 /// @note unlike str(), to_str() does not automatically demangle the string.
283 /// @param node the AST node
284 /// @param out the stream to write to
285 /// @param indent number of spaces to indent the node when writing
286 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
287 node->to_str(Sem(), out, indent);
288 }
289
290 /// Returns a demangled, string representation of `node`.
291 /// @param node the AST node
292 /// @returns a string representation of the node
293 std::string str(const ast::Node* node) const;
294
Ben Clayton7fdfff12021-01-29 15:17:30 +0000295 /// Creates a new ast::Node owned by the ProgramBuilder. When the
296 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000297 /// @param source the Source of the node
298 /// @param args the arguments to pass to the type constructor
299 /// @returns the node pointer
300 template <typename T, typename... ARGS>
301 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
302 ARGS&&... args) {
303 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000304 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000305 }
306
Ben Clayton7fdfff12021-01-29 15:17:30 +0000307 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
308 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000309 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000310 /// When the ProgramBuilder is destructed, the ast::Node will also be
311 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000312 /// @returns the node pointer
313 template <typename T>
314 traits::EnableIfIsType<T, ast::Node>* create() {
315 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000316 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000317 }
318
Ben Clayton7fdfff12021-01-29 15:17:30 +0000319 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
320 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000321 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000322 /// When the ProgramBuilder is destructed, the ast::Node will also be
323 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000324 /// @param arg0 the first arguments to pass to the type constructor
325 /// @param args the remaining arguments to pass to the type constructor
326 /// @returns the node pointer
327 template <typename T, typename ARG0, typename... ARGS>
328 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
329 traits::IsTypeOrDerived<T, ast::Node>::value &&
330 !traits::IsTypeOrDerived<ARG0, Source>::value,
331 T>*
332 create(ARG0&& arg0, ARGS&&... args) {
333 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000334 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000335 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000336 }
337
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000338 /// Creates a new sem::Node owned by the ProgramBuilder.
339 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000340 /// destructed.
341 /// @param args the arguments to pass to the type constructor
342 /// @returns the node pointer
343 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000344 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
345 !traits::IsTypeOrDerived<T, sem::Type>::value,
346 T>*
347 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000348 AssertNotMoved();
349 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
350 }
351
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000352 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000353 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
354 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000355 /// Types are unique (de-aliased), and so calling create() for the same `T`
356 /// and arguments will return the same pointer.
357 /// @warning Use this method to acquire a type only if all of its type
358 /// information is provided in the constructor arguments `args`.<br>
359 /// If the type requires additional configuration after construction that
360 /// affect its fundamental type, build the type with `std::make_unique`, make
361 /// any necessary alterations and then call unique_type() instead.
362 /// @param args the arguments to pass to the type constructor
363 /// @returns the de-aliased type pointer
364 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000365 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
366 static_assert(std::is_base_of<sem::Type, T>::value,
367 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368 AssertNotMoved();
369 return types_.Get<T>(std::forward<ARGS>(args)...);
370 }
371
372 /// Marks this builder as moved, preventing any further use of the builder.
373 void MarkAsMoved();
374
375 //////////////////////////////////////////////////////////////////////////////
376 // TypesBuilder
377 //////////////////////////////////////////////////////////////////////////////
378
379 /// TypesBuilder holds basic `tint` types and methods for constructing
380 /// complex types.
381 class TypesBuilder {
382 public:
383 /// Constructor
384 /// @param builder the program builder
385 explicit TypesBuilder(ProgramBuilder* builder);
386
387 /// @return the tint AST type for the C type `T`.
388 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000389 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000390 return CToAST<T>::get(this);
391 }
392
393 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000394 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000395
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000396 /// @param source the Source of the node
397 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000398 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000399 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000400 }
401
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000402 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000403 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000404
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000405 /// @param source the Source of the node
406 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000407 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000408 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000409 }
410
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000411 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000412 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000413
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000414 /// @param source the Source of the node
415 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000416 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000417 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000418 }
419
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000420 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000421 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000422
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000423 /// @param source the Source of the node
424 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000425 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000426 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000427 }
428
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000429 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000430 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000431
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000432 /// @param source the Source of the node
433 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000434 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000435 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000436 }
437
Antonio Maiorano25436862021-04-13 13:32:33 +0000438 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000439 /// @param n vector width in elements
440 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000441 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000442 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000443 }
444
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000445 /// @param source the Source of the node
446 /// @param type vector subtype
447 /// @param n vector width in elements
448 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000449 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000450 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000451 }
452
Antonio Maiorano25436862021-04-13 13:32:33 +0000453 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000454 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000455 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000456
457 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000458 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000459 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000460
461 /// @param type vector subtype
462 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000463 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000464
465 /// @param n vector width in elements
466 /// @return the tint AST type for a `n`-element vector of `type`.
467 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000468 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000469 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000470 }
471
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000472 /// @return the tint AST type for a 2-element vector of the C type `T`.
473 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000474 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000475 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000476 }
477
478 /// @return the tint AST type for a 3-element vector of the C type `T`.
479 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000480 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000481 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000482 }
483
484 /// @return the tint AST type for a 4-element vector of the C type `T`.
485 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000486 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000487 return vec4(Of<T>());
488 }
489
490 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000491 /// @param columns number of columns for the matrix
492 /// @param rows number of rows for the matrix
493 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000494 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000495 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000496 }
497
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000498 /// @param source the Source of the node
499 /// @param type matrix subtype
500 /// @param columns number of columns for the matrix
501 /// @param rows number of rows for the matrix
502 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000503 ast::Matrix* mat(const Source& source,
504 ast::Type* type,
505 uint32_t columns,
506 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000507 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000508 }
509
Ben Claytone204f272021-04-22 14:40:23 +0000510 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000511 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000512 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000513
514 /// @param type matrix subtype
515 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000516 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000517
518 /// @param type matrix subtype
519 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000520 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000521
522 /// @param type matrix subtype
523 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000524 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000525
526 /// @param type matrix subtype
527 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000528 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000529
530 /// @param type matrix subtype
531 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000532 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000533
534 /// @param type matrix subtype
535 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000536 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000537
538 /// @param type matrix subtype
539 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000540 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000541
542 /// @param type matrix subtype
543 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000544 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000545
Ben Claytone204f272021-04-22 14:40:23 +0000546 /// @param columns number of columns for the matrix
547 /// @param rows number of rows for the matrix
548 /// @return the tint AST type for a matrix of `type`
549 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000550 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000551 return mat(Of<T>(), columns, rows);
552 }
553
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000554 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
555 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000556 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000557 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000558 }
559
560 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
561 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000562 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000563 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000564 }
565
566 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
567 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000568 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000569 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000570 }
571
572 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
573 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000574 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000575 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000576 }
577
578 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
579 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000580 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000581 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000582 }
583
584 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
585 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000586 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000587 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000588 }
589
590 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
591 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000592 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000593 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000594 }
595
596 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
597 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000598 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000599 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000600 }
601
602 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
603 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000604 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000605 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000606 }
607
608 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000609 /// @param n the array size. 0 represents a runtime-array
610 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000611 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000612 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000613 uint32_t n = 0,
614 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000615 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000616 }
617
618 /// @param source the Source of the node
619 /// @param subtype the array element type
620 /// @param n the array size. 0 represents a runtime-array
621 /// @param decos the optional decorations for the array
622 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000623 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000624 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000625 uint32_t n = 0,
626 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000627 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000628 }
629
Ben Claytonbab31972021-02-08 21:16:21 +0000630 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000631 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000632 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000633 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000634 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000635 ast::DecorationList decos;
636 if (stride) {
637 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
638 }
639 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000640 }
641
Ben Clayton0f88b312021-05-04 17:36:31 +0000642 /// @param source the Source of the node
643 /// @param subtype the array element type
644 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000645 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000646 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000647 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000648 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000649 uint32_t n,
650 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000651 ast::DecorationList decos;
652 if (stride) {
653 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
654 }
655 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000656 }
657
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000658 /// @return the tint AST type for an array of size `N` of type `T`
659 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000660 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000661 return array(Of<T>(), N);
662 }
663
Ben Claytonbab31972021-02-08 21:16:21 +0000664 /// @param stride the array stride
665 /// @return the tint AST type for an array of size `N` of type `T`
666 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000667 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000668 return array(Of<T>(), N, stride);
669 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000670
Ben Clayton0f88b312021-05-04 17:36:31 +0000671 /// Creates a type name
672 /// @param name the name
673 /// @returns the type name
674 template <typename NAME>
675 ast::TypeName* type_name(NAME&& name) const {
676 return builder->create<ast::TypeName>(
677 builder->Sym(std::forward<NAME>(name)));
678 }
679
680 /// Creates a type name
681 /// @param source the Source of the node
682 /// @param name the name
683 /// @returns the type name
684 template <typename NAME>
685 ast::TypeName* type_name(const Source& source, NAME&& name) const {
686 return builder->create<ast::TypeName>(
687 source, builder->Sym(std::forward<NAME>(name)));
688 }
689
Ben Clayton42d1e092021-02-02 14:29:15 +0000690 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000691 /// @param name the alias name
692 /// @param type the alias type
693 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000694 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000695 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000696 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000697 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000698 }
699
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000700 /// Creates an alias type
701 /// @param source the Source of the node
702 /// @param name the alias name
703 /// @param type the alias type
704 /// @returns the alias pointer
705 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000706 ast::Alias* alias(const Source& source,
707 NAME&& name,
708 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000709 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000710 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000711 }
712
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000713 /// @param type the type of the pointer
714 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000715 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000716 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000717 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000718 ast::StorageClass storage_class,
719 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000720 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000721 }
722
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000723 /// @param source the Source of the node
724 /// @param type the type of the pointer
725 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000726 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000727 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000728 ast::Pointer* pointer(const Source& source,
729 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000730 ast::StorageClass storage_class,
731 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000732 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000733 }
734
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000735 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000736 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000737 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000738 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000739 ast::Pointer* pointer(ast::StorageClass storage_class,
740 ast::Access access = ast::Access::kUndefined) const {
741 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000742 }
743
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000744 /// @param source the Source of the node
745 /// @param type the type of the atomic
746 /// @return the atomic to `type`
747 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
748 return builder->create<ast::Atomic>(source, type);
749 }
750
Ben Clayton313e6182021-06-17 19:56:14 +0000751 /// @param type the type of the atomic
752 /// @return the atomic to `type`
753 ast::Atomic* atomic(ast::Type* type) const {
754 return builder->create<ast::Atomic>(type);
755 }
756
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000757 /// @return the atomic to type `T`
758 template <typename T>
759 ast::Atomic* atomic() const {
760 return atomic(Of<T>());
761 }
762
Ben Claytone204f272021-04-22 14:40:23 +0000763 /// @param kind the kind of sampler
764 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000765 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000766 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000767 }
768
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000769 /// @param source the Source of the node
770 /// @param kind the kind of sampler
771 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000772 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000773 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000774 }
775
Ben Claytone204f272021-04-22 14:40:23 +0000776 /// @param dims the dimensionality of the texture
777 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000778 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000779 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000780 }
781
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000782 /// @param source the Source of the node
783 /// @param dims the dimensionality of the texture
784 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000785 ast::DepthTexture* depth_texture(const Source& source,
786 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000787 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000788 }
789
Ben Claytone204f272021-04-22 14:40:23 +0000790 /// @param dims the dimensionality of the texture
791 /// @param subtype the texture subtype.
792 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000793 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
794 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000795 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000796 }
797
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000798 /// @param source the Source of the node
799 /// @param dims the dimensionality of the texture
800 /// @param subtype the texture subtype.
801 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000802 ast::SampledTexture* sampled_texture(const Source& source,
803 ast::TextureDimension dims,
804 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000805 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000806 }
807
Ben Claytone204f272021-04-22 14:40:23 +0000808 /// @param dims the dimensionality of the texture
809 /// @param subtype the texture subtype.
810 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000811 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
812 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000813 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000814 }
815
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000816 /// @param source the Source of the node
817 /// @param dims the dimensionality of the texture
818 /// @param subtype the texture subtype.
819 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000820 ast::MultisampledTexture* multisampled_texture(const Source& source,
821 ast::TextureDimension dims,
822 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000823 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000824 }
825
Ben Claytone204f272021-04-22 14:40:23 +0000826 /// @param dims the dimensionality of the texture
827 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000828 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000829 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000830 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000831 ast::ImageFormat format,
832 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000833 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000834 return builder->create<ast::StorageTexture>(dims, format, subtype,
835 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000836 }
837
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000838 /// @param source the Source of the node
839 /// @param dims the dimensionality of the texture
840 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000841 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000842 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000843 ast::StorageTexture* storage_texture(const Source& source,
844 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000845 ast::ImageFormat format,
846 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000847 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000848 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
849 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000850 }
851
Brandon Jones7b257692021-05-17 17:40:17 +0000852 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000853 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000854 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000855 }
856
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000857 /// @param source the Source of the node
858 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000859 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000860 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000861 }
862
Ben Clayton19b03192021-05-20 15:04:08 +0000863 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000864 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000865 /// @param type the type
866 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000867 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000868
Ben Clayton8758f102021-06-09 14:32:14 +0000869 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000870 /// @param type the type
871 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000872 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000873
Ben Claytonf5f311e2021-04-28 14:31:23 +0000874 /// The ProgramBuilder
875 ProgramBuilder* const builder;
876
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000877 private:
878 /// CToAST<T> is specialized for various `T` types and each specialization
879 /// contains a single static `get()` method for obtaining the corresponding
880 /// AST type for the C type `T`.
881 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000882 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000883 template <typename T>
884 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000885 };
886
887 //////////////////////////////////////////////////////////////////////////////
888 // AST helper methods
889 //////////////////////////////////////////////////////////////////////////////
890
James Price65ae64d2021-04-29 12:59:14 +0000891 /// @return a new unnamed symbol
892 Symbol Sym() { return Symbols().New(); }
893
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000894 /// @param name the symbol string
895 /// @return a Symbol with the given name
896 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
897
898 /// @param sym the symbol
899 /// @return `sym`
900 Symbol Sym(Symbol sym) { return sym; }
901
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000902 /// @param expr the expression
903 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000904 template <typename T>
905 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
906 return expr;
907 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000908
Ben Claytonb8ea5912021-04-19 16:52:42 +0000909 /// Passthrough for nullptr
910 /// @return nullptr
911 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
912
Ben Claytonfe0910f2021-05-17 15:51:47 +0000913 /// @param source the source information
914 /// @param symbol the identifier symbol
915 /// @return an ast::IdentifierExpression with the given symbol
916 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
917 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000918 }
919
Ben Clayton46d78d72021-02-10 21:34:26 +0000920 /// @param symbol the identifier symbol
921 /// @return an ast::IdentifierExpression with the given symbol
922 ast::IdentifierExpression* Expr(Symbol symbol) {
923 return create<ast::IdentifierExpression>(symbol);
924 }
925
Ben Claytonfe0910f2021-05-17 15:51:47 +0000926 /// @param source the source information
927 /// @param variable the AST variable
928 /// @return an ast::IdentifierExpression with the variable's symbol
929 ast::IdentifierExpression* Expr(const Source& source,
930 ast::Variable* variable) {
931 return create<ast::IdentifierExpression>(source, variable->symbol());
932 }
933
Ben Claytonb8ea5912021-04-19 16:52:42 +0000934 /// @param variable the AST variable
935 /// @return an ast::IdentifierExpression with the variable's symbol
936 ast::IdentifierExpression* Expr(ast::Variable* variable) {
937 return create<ast::IdentifierExpression>(variable->symbol());
938 }
939
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000940 /// @param source the source information
941 /// @param name the identifier name
942 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000943 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
944 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
945 }
946
947 /// @param name the identifier name
948 /// @return an ast::IdentifierExpression with the given name
949 ast::IdentifierExpression* Expr(const char* name) {
950 return create<ast::IdentifierExpression>(Symbols().Register(name));
951 }
952
953 /// @param source the source information
954 /// @param name the identifier name
955 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000956 ast::IdentifierExpression* Expr(const Source& source,
957 const std::string& name) {
958 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
959 }
960
961 /// @param name the identifier name
962 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000963 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000964 return create<ast::IdentifierExpression>(Symbols().Register(name));
965 }
966
Ben Claytonfe0910f2021-05-17 15:51:47 +0000967 /// @param source the source information
968 /// @param value the boolean value
969 /// @return a Scalar constructor for the given value
970 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
971 return create<ast::ScalarConstructorExpression>(source, Literal(value));
972 }
973
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000974 /// @param value the boolean value
975 /// @return a Scalar constructor for the given value
976 ast::ScalarConstructorExpression* Expr(bool value) {
977 return create<ast::ScalarConstructorExpression>(Literal(value));
978 }
979
Ben Claytonfe0910f2021-05-17 15:51:47 +0000980 /// @param source the source information
981 /// @param value the float value
982 /// @return a Scalar constructor for the given value
983 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
984 return create<ast::ScalarConstructorExpression>(source, Literal(value));
985 }
986
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000987 /// @param value the float value
988 /// @return a Scalar constructor for the given value
989 ast::ScalarConstructorExpression* Expr(f32 value) {
990 return create<ast::ScalarConstructorExpression>(Literal(value));
991 }
992
Ben Claytonfe0910f2021-05-17 15:51:47 +0000993 /// @param source the source information
994 /// @param value the integer value
995 /// @return a Scalar constructor for the given value
996 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
997 return create<ast::ScalarConstructorExpression>(source, Literal(value));
998 }
999
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001000 /// @param value the integer value
1001 /// @return a Scalar constructor for the given value
1002 ast::ScalarConstructorExpression* Expr(i32 value) {
1003 return create<ast::ScalarConstructorExpression>(Literal(value));
1004 }
1005
Ben Claytonfe0910f2021-05-17 15:51:47 +00001006 /// @param source the source information
1007 /// @param value the unsigned int value
1008 /// @return a Scalar constructor for the given value
1009 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1010 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1011 }
1012
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001013 /// @param value the unsigned int value
1014 /// @return a Scalar constructor for the given value
1015 ast::ScalarConstructorExpression* Expr(u32 value) {
1016 return create<ast::ScalarConstructorExpression>(Literal(value));
1017 }
1018
1019 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1020 /// `list`.
1021 /// @param list the list to append too
1022 /// @param arg the arg to create
1023 template <typename ARG>
1024 void Append(ast::ExpressionList& list, ARG&& arg) {
1025 list.emplace_back(Expr(std::forward<ARG>(arg)));
1026 }
1027
1028 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1029 /// then appends them to `list`.
1030 /// @param list the list to append too
1031 /// @param arg0 the first argument
1032 /// @param args the rest of the arguments
1033 template <typename ARG0, typename... ARGS>
1034 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1035 Append(list, std::forward<ARG0>(arg0));
1036 Append(list, std::forward<ARGS>(args)...);
1037 }
1038
1039 /// @return an empty list of expressions
1040 ast::ExpressionList ExprList() { return {}; }
1041
1042 /// @param args the list of expressions
1043 /// @return the list of expressions converted to `ast::Expression`s using
1044 /// `Expr()`,
1045 template <typename... ARGS>
1046 ast::ExpressionList ExprList(ARGS&&... args) {
1047 ast::ExpressionList list;
1048 list.reserve(sizeof...(args));
1049 Append(list, std::forward<ARGS>(args)...);
1050 return list;
1051 }
1052
1053 /// @param list the list of expressions
1054 /// @return `list`
1055 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1056
1057 /// @param val the boolan value
1058 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001059 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001060
1061 /// @param val the float value
1062 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001063 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001064
1065 /// @param val the unsigned int value
1066 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001067 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001068
1069 /// @param val the integer value
1070 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001071 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001072
1073 /// @param args the arguments for the type constructor
1074 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1075 /// of `args` converted to `ast::Expression`s using `Expr()`
1076 template <typename T, typename... ARGS>
1077 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001078 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001079 }
1080
1081 /// @param type the type to construct
1082 /// @param args the arguments for the constructor
1083 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1084 /// values `args`.
1085 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001086 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001087 return create<ast::TypeConstructorExpression>(
1088 type, ExprList(std::forward<ARGS>(args)...));
1089 }
1090
Ben Clayton313e6182021-06-17 19:56:14 +00001091 /// @param source the source information
1092 /// @param type the type to construct
1093 /// @param args the arguments for the constructor
1094 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1095 /// values `args`.
1096 template <typename... ARGS>
1097 ast::TypeConstructorExpression* Construct(const Source& source,
1098 ast::Type* type,
1099 ARGS&&... args) {
1100 return create<ast::TypeConstructorExpression>(
1101 source, type, ExprList(std::forward<ARGS>(args)...));
1102 }
1103
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001104 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001105 /// @param type the vector type
1106 /// @param size the vector size
1107 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1108 /// type `type`, constructed with the values `args`.
1109 template <typename... ARGS>
1110 ast::TypeConstructorExpression* vec(ast::Type* type,
1111 uint32_t size,
1112 ARGS&&... args) {
1113 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1114 }
1115
1116 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001117 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1118 /// `T`, constructed with the values `args`.
1119 template <typename T, typename... ARGS>
1120 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001121 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001122 }
1123
1124 /// @param args the arguments for the vector constructor
1125 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1126 /// `T`, constructed with the values `args`.
1127 template <typename T, typename... ARGS>
1128 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001129 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001130 }
1131
1132 /// @param args the arguments for the vector constructor
1133 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1134 /// `T`, constructed with the values `args`.
1135 template <typename T, typename... ARGS>
1136 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001137 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001138 }
1139
1140 /// @param args the arguments for the matrix constructor
1141 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1142 /// `T`, constructed with the values `args`.
1143 template <typename T, typename... ARGS>
1144 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001145 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001146 }
1147
1148 /// @param args the arguments for the matrix constructor
1149 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1150 /// `T`, constructed with the values `args`.
1151 template <typename T, typename... ARGS>
1152 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001153 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001154 }
1155
1156 /// @param args the arguments for the matrix constructor
1157 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1158 /// `T`, constructed with the values `args`.
1159 template <typename T, typename... ARGS>
1160 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001161 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001162 }
1163
1164 /// @param args the arguments for the matrix constructor
1165 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1166 /// `T`, constructed with the values `args`.
1167 template <typename T, typename... ARGS>
1168 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001169 return Construct(ty.mat3x2<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 3x3 matrix of type
1174 /// `T`, constructed with the values `args`.
1175 template <typename T, typename... ARGS>
1176 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001177 return Construct(ty.mat3x3<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 3x4 matrix of type
1182 /// `T`, constructed with the values `args`.
1183 template <typename T, typename... ARGS>
1184 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001185 return Construct(ty.mat3x4<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 4x2 matrix of type
1190 /// `T`, constructed with the values `args`.
1191 template <typename T, typename... ARGS>
1192 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001193 return Construct(ty.mat4x2<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 4x3 matrix of type
1198 /// `T`, constructed with the values `args`.
1199 template <typename T, typename... ARGS>
1200 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001201 return Construct(ty.mat4x3<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 4x4 matrix of type
1206 /// `T`, constructed with the values `args`.
1207 template <typename T, typename... ARGS>
1208 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001209 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001210 }
1211
1212 /// @param args the arguments for the array constructor
1213 /// @return an `ast::TypeConstructorExpression` of an array with element type
1214 /// `T`, constructed with the values `args`.
1215 template <typename T, int N = 0, typename... ARGS>
1216 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001217 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 }
1219
1220 /// @param subtype the array element type
1221 /// @param n the array size. 0 represents a runtime-array.
1222 /// @param args the arguments for the array constructor
1223 /// @return an `ast::TypeConstructorExpression` of an array with element type
1224 /// `subtype`, constructed with the values `args`.
1225 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001226 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227 uint32_t n,
1228 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001229 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230 }
1231
1232 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001233 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001234 /// @param optional the optional variable settings.
1235 /// Can be any of the following, in any order:
1236 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001237 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001238 /// * ast::Expression* - specifies the variable's initializer expression
1239 /// * ast::DecorationList - specifies the variable's decorations
1240 /// Note that repeated arguments of the same type will use the last argument's
1241 /// value.
1242 /// @returns a `ast::Variable` with the given name, type and additional
1243 /// options
1244 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001245 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001246 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001247 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001248 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1249 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001250 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001251 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001252 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253
1254 /// @param source the variable source
1255 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001256 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001257 /// @param optional the optional variable settings.
1258 /// Can be any of the following, in any order:
1259 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001260 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001261 /// * ast::Expression* - specifies the variable's initializer expression
1262 /// * ast::DecorationList - specifies the variable's decorations
1263 /// Note that repeated arguments of the same type will use the last argument's
1264 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001266 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001267 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001268 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001269 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001270 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001271 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1272 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001273 opts.storage, opts.access, type, false,
1274 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001275 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001276
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001277 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001278 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001279 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001280 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001281 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001282 template <typename NAME>
1283 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001284 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001285 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001286 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001287 return create<ast::Variable>(
1288 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1289 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001290 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291
1292 /// @param source the variable source
1293 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001294 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001295 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001296 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001297 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001298 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001299 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001300 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001301 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001302 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001303 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001304 return create<ast::Variable>(
1305 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1306 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001307 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001308
James Pricedaf1f1c2021-04-08 22:15:48 +00001309 /// @param name the parameter name
1310 /// @param type the parameter type
1311 /// @param decorations optional parameter decorations
1312 /// @returns a constant `ast::Variable` with the given name and type
1313 template <typename NAME>
1314 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001315 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001316 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001317 return create<ast::Variable>(
1318 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1319 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001320 }
1321
1322 /// @param source the parameter source
1323 /// @param name the parameter name
1324 /// @param type the parameter type
1325 /// @param decorations optional parameter decorations
1326 /// @returns a constant `ast::Variable` with the given name and type
1327 template <typename NAME>
1328 ast::Variable* Param(const Source& source,
1329 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001330 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001331 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001332 return create<ast::Variable>(
1333 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1334 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001335 }
1336
Ben Clayton42708342021-04-21 17:55:12 +00001337 /// @param name the variable name
1338 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001339 /// @param optional the optional variable settings.
1340 /// Can be any of the following, in any order:
1341 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001342 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001343 /// * ast::Expression* - specifies the variable's initializer expression
1344 /// * ast::DecorationList - specifies the variable's decorations
1345 /// Note that repeated arguments of the same type will use the last argument's
1346 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001347 /// @returns a new `ast::Variable`, which is automatically registered as a
1348 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001349 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001350 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001351 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001352 OPTIONAL&&... optional) {
1353 auto* var = Var(std::forward<NAME>(name), type,
1354 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001355 AST().AddGlobalVariable(var);
1356 return var;
1357 }
1358
1359 /// @param source the variable source
1360 /// @param name the variable name
1361 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001362 /// @param optional the optional variable settings.
1363 /// Can be any of the following, in any order:
1364 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001365 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001366 /// * ast::Expression* - specifies the variable's initializer expression
1367 /// * ast::DecorationList - specifies the variable's decorations
1368 /// Note that repeated arguments of the same type will use the last argument's
1369 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001370 /// @returns a new `ast::Variable`, which is automatically registered as a
1371 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001372 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001373 ast::Variable* Global(const Source& source,
1374 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001375 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001376 OPTIONAL&&... optional) {
1377 auto* var = Var(source, std::forward<NAME>(name), type,
1378 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001379 AST().AddGlobalVariable(var);
1380 return var;
1381 }
1382
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001383 /// @param name the variable name
1384 /// @param type the variable type
1385 /// @param constructor constructor expression
1386 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001387 /// @returns a const `ast::Variable` constructed by calling Var() with the
1388 /// arguments of `args`, which is automatically registered as a global
1389 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001390 template <typename NAME>
1391 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001392 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001393 ast::Expression* constructor,
1394 ast::DecorationList decorations = {}) {
1395 auto* var = Const(std::forward<NAME>(name), type, constructor,
1396 std::move(decorations));
1397 AST().AddGlobalVariable(var);
1398 return var;
1399 }
1400
1401 /// @param source the variable source
1402 /// @param name the variable name
1403 /// @param type the variable type
1404 /// @param constructor constructor expression
1405 /// @param decorations optional variable decorations
1406 /// @returns a const `ast::Variable` constructed by calling Var() with the
1407 /// arguments of `args`, which is automatically registered as a global
1408 /// variable with the ast::Module.
1409 template <typename NAME>
1410 ast::Variable* GlobalConst(const Source& source,
1411 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001412 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001413 ast::Expression* constructor,
1414 ast::DecorationList decorations = {}) {
1415 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1416 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001417 AST().AddGlobalVariable(var);
1418 return var;
1419 }
1420
Ben Claytonfe0910f2021-05-17 15:51:47 +00001421 /// @param source the source information
1422 /// @param expr the expression to take the address of
1423 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1424 template <typename EXPR>
1425 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1426 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1427 Expr(std::forward<EXPR>(expr)));
1428 }
1429
1430 /// @param expr the expression to take the address of
1431 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1432 template <typename EXPR>
1433 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1434 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1435 Expr(std::forward<EXPR>(expr)));
1436 }
1437
1438 /// @param source the source information
1439 /// @param expr the expression to perform an indirection on
1440 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1441 template <typename EXPR>
1442 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1443 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1444 Expr(std::forward<EXPR>(expr)));
1445 }
1446
1447 /// @param expr the expression to perform an indirection on
1448 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1449 template <typename EXPR>
1450 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1451 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1452 Expr(std::forward<EXPR>(expr)));
1453 }
1454
Antonio Maiorano14b34032021-06-09 20:17:59 +00001455 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001456 /// @param func the function name
1457 /// @param args the function call arguments
1458 /// @returns a `ast::CallExpression` to the function `func`, with the
1459 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1460 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001461 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1462 return create<ast::CallExpression>(source, Expr(func),
1463 ExprList(std::forward<ARGS>(args)...));
1464 }
1465
1466 /// @param func the function name
1467 /// @param args the function call arguments
1468 /// @returns a `ast::CallExpression` to the function `func`, with the
1469 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1470 template <typename NAME,
1471 typename... ARGS,
1472 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001473 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1474 return create<ast::CallExpression>(Expr(func),
1475 ExprList(std::forward<ARGS>(args)...));
1476 }
1477
Ben Clayton7fe01062021-06-11 13:22:27 +00001478 /// @param expr the expression to ignore
1479 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1480 /// passed the single `expr` argument
1481 template <typename EXPR>
1482 ast::CallStatement* Ignore(EXPR&& expr) {
1483 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1484 }
1485
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001486 /// @param lhs the left hand argument to the addition operation
1487 /// @param rhs the right hand argument to the addition operation
1488 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1489 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001490 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001491 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1492 Expr(std::forward<LHS>(lhs)),
1493 Expr(std::forward<RHS>(rhs)));
1494 }
1495
James Pricec32e8f62021-06-22 20:08:29 +00001496 /// @param lhs the left hand argument to the and operation
1497 /// @param rhs the right hand argument to the and operation
1498 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1499 template <typename LHS, typename RHS>
1500 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1501 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1502 Expr(std::forward<LHS>(lhs)),
1503 Expr(std::forward<RHS>(rhs)));
1504 }
1505
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001506 /// @param lhs the left hand argument to the subtraction operation
1507 /// @param rhs the right hand argument to the subtraction operation
1508 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1509 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001510 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001511 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1512 Expr(std::forward<LHS>(lhs)),
1513 Expr(std::forward<RHS>(rhs)));
1514 }
1515
1516 /// @param lhs the left hand argument to the multiplication operation
1517 /// @param rhs the right hand argument to the multiplication operation
1518 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1519 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001520 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001521 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1522 Expr(std::forward<LHS>(lhs)),
1523 Expr(std::forward<RHS>(rhs)));
1524 }
1525
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001526 /// @param source the source information
1527 /// @param lhs the left hand argument to the multiplication operation
1528 /// @param rhs the right hand argument to the multiplication operation
1529 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1530 template <typename LHS, typename RHS>
1531 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1532 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1533 Expr(std::forward<LHS>(lhs)),
1534 Expr(std::forward<RHS>(rhs)));
1535 }
1536
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001537 /// @param lhs the left hand argument to the division operation
1538 /// @param rhs the right hand argument to the division operation
1539 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1540 template <typename LHS, typename RHS>
1541 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1542 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1543 Expr(std::forward<LHS>(lhs)),
1544 Expr(std::forward<RHS>(rhs)));
1545 }
1546
Ben Clayton0597a2b2021-06-16 09:19:36 +00001547 /// @param source the source information
1548 /// @param arr the array argument for the array accessor expression
1549 /// @param idx the index argument for the array accessor expression
1550 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1551 template <typename ARR, typename IDX>
1552 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1553 ARR&& arr,
1554 IDX&& idx) {
1555 return create<ast::ArrayAccessorExpression>(
1556 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1557 }
1558
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001559 /// @param arr the array argument for the array accessor expression
1560 /// @param idx the index argument for the array accessor expression
1561 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1562 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001563 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001564 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1565 Expr(std::forward<IDX>(idx)));
1566 }
1567
1568 /// @param obj the object for the member accessor expression
1569 /// @param idx the index argument for the array accessor expression
1570 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1571 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001572 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001573 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1574 Expr(std::forward<IDX>(idx)));
1575 }
1576
Ben Clayton42d1e092021-02-02 14:29:15 +00001577 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001578 /// @param val the offset value
1579 /// @returns the offset decoration pointer
1580 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1581 return create<ast::StructMemberOffsetDecoration>(source_, val);
1582 }
1583
Ben Claytond614dd52021-03-15 10:43:11 +00001584 /// Creates a ast::StructMemberSizeDecoration
1585 /// @param source the source information
1586 /// @param val the size value
1587 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001588 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1589 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001590 return create<ast::StructMemberSizeDecoration>(source, val);
1591 }
1592
1593 /// Creates a ast::StructMemberSizeDecoration
1594 /// @param val the size value
1595 /// @returns the size decoration pointer
1596 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1597 return create<ast::StructMemberSizeDecoration>(source_, val);
1598 }
1599
1600 /// Creates a ast::StructMemberAlignDecoration
1601 /// @param source the source information
1602 /// @param val the align value
1603 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001604 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1605 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001606 return create<ast::StructMemberAlignDecoration>(source, val);
1607 }
1608
1609 /// Creates a ast::StructMemberAlignDecoration
1610 /// @param val the align value
1611 /// @returns the align decoration pointer
1612 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1613 return create<ast::StructMemberAlignDecoration>(source_, val);
1614 }
1615
Ben Clayton42d1e092021-02-02 14:29:15 +00001616 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001617 /// @param source the source information
1618 /// @param name the function name
1619 /// @param params the function parameters
1620 /// @param type the function return type
1621 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001622 /// @param decorations the optional function decorations
1623 /// @param return_type_decorations the optional function return type
1624 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001625 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001626 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001627 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001628 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001629 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001630 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001631 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001632 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001633 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001634 auto* func =
1635 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1636 type, create<ast::BlockStatement>(body),
1637 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001638 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001639 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001640 }
1641
Ben Clayton42d1e092021-02-02 14:29:15 +00001642 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001643 /// @param name the function name
1644 /// @param params the function parameters
1645 /// @param type the function return type
1646 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001647 /// @param decorations the optional function decorations
1648 /// @param return_type_decorations the optional function return type
1649 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001650 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001651 template <typename NAME>
1652 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001653 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001654 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001655 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001656 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001657 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001658 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1659 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001660 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001661 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001662 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001663 }
1664
Ben Clayton49668bb2021-04-17 05:32:01 +00001665 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001666 /// @param source the source information
1667 /// @returns the return statement pointer
1668 ast::ReturnStatement* Return(const Source& source) {
1669 return create<ast::ReturnStatement>(source);
1670 }
1671
1672 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001673 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001674 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1675
1676 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001677 /// @param source the source information
1678 /// @param val the return value
1679 /// @returns the return statement pointer
1680 template <typename EXPR>
1681 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1682 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1683 }
1684
1685 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001686 /// @param val the return value
1687 /// @returns the return statement pointer
1688 template <typename EXPR>
1689 ast::ReturnStatement* Return(EXPR&& val) {
1690 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001691 }
1692
Ben Clayton950809f2021-06-09 14:32:14 +00001693 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001694 /// @param source the source information
1695 /// @param name the alias name
1696 /// @param type the alias target type
1697 /// @returns the alias type
1698 template <typename NAME>
1699 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1700 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001701 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001702 return out;
1703 }
1704
Ben Clayton950809f2021-06-09 14:32:14 +00001705 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001706 /// @param name the alias name
1707 /// @param type the alias target type
1708 /// @returns the alias type
1709 template <typename NAME>
1710 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1711 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001712 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001713 return out;
1714 }
1715
Ben Clayton950809f2021-06-09 14:32:14 +00001716 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001717 /// @param source the source information
1718 /// @param name the struct name
1719 /// @param members the struct members
1720 /// @param decorations the optional struct decorations
1721 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001722 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001723 ast::Struct* Structure(const Source& source,
1724 NAME&& name,
1725 ast::StructMemberList members,
1726 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001727 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001728 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001729 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001730 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001731 return type;
1732 }
1733
Ben Clayton950809f2021-06-09 14:32:14 +00001734 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001735 /// @param name the struct name
1736 /// @param members the struct members
1737 /// @param decorations the optional struct decorations
1738 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001739 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001740 ast::Struct* Structure(NAME&& name,
1741 ast::StructMemberList members,
1742 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001743 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001744 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001745 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001746 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001747 return type;
1748 }
1749
Ben Clayton42d1e092021-02-02 14:29:15 +00001750 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001751 /// @param source the source information
1752 /// @param name the struct member name
1753 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001754 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001755 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001756 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001757 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001758 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001759 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001760 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001761 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1762 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001763 }
1764
Ben Clayton42d1e092021-02-02 14:29:15 +00001765 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001766 /// @param name the struct member name
1767 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001768 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001769 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001770 template <typename NAME>
1771 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001772 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001773 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001774 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1775 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001776 }
1777
Ben Claytonbab31972021-02-08 21:16:21 +00001778 /// Creates a ast::StructMember with the given byte offset
1779 /// @param offset the offset to use in the StructMemberOffsetDecoration
1780 /// @param name the struct member name
1781 /// @param type the struct member type
1782 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001783 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001784 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001785 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001786 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001787 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001788 create<ast::StructMemberOffsetDecoration>(offset),
1789 });
1790 }
1791
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001792 /// Creates a ast::BlockStatement with input statements
1793 /// @param statements statements of block
1794 /// @returns the block statement pointer
1795 template <typename... Statements>
1796 ast::BlockStatement* Block(Statements&&... statements) {
1797 return create<ast::BlockStatement>(
1798 ast::StatementList{std::forward<Statements>(statements)...});
1799 }
1800
1801 /// Creates a ast::ElseStatement with input condition and body
1802 /// @param condition the else condition expression
1803 /// @param body the else body
1804 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001805 template <typename CONDITION>
1806 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1807 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1808 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001809 }
1810
1811 /// Creates a ast::IfStatement with input condition, body, and optional
1812 /// variadic else statements
1813 /// @param condition the if statement condition expression
1814 /// @param body the if statement body
1815 /// @param elseStatements optional variadic else statements
1816 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001817 template <typename CONDITION, typename... ELSE_STATEMENTS>
1818 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001819 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001820 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001821 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001822 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001823 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001824 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001825 }
1826
1827 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001828 /// @param source the source information
1829 /// @param lhs the left hand side expression initializer
1830 /// @param rhs the right hand side expression initializer
1831 /// @returns the assignment statement pointer
1832 template <typename LhsExpressionInit, typename RhsExpressionInit>
1833 ast::AssignmentStatement* Assign(const Source& source,
1834 LhsExpressionInit&& lhs,
1835 RhsExpressionInit&& rhs) {
1836 return create<ast::AssignmentStatement>(
1837 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1838 Expr(std::forward<RhsExpressionInit>(rhs)));
1839 }
1840
1841 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001842 /// @param lhs the left hand side expression initializer
1843 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001844 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001845 template <typename LhsExpressionInit, typename RhsExpressionInit>
1846 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1847 RhsExpressionInit&& rhs) {
1848 return create<ast::AssignmentStatement>(
1849 Expr(std::forward<LhsExpressionInit>(lhs)),
1850 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001851 }
1852
1853 /// Creates a ast::LoopStatement with input body and optional continuing
1854 /// @param body the loop body
1855 /// @param continuing the optional continuing block
1856 /// @returns the loop statement pointer
1857 ast::LoopStatement* Loop(ast::BlockStatement* body,
1858 ast::BlockStatement* continuing = nullptr) {
1859 return create<ast::LoopStatement>(body, continuing);
1860 }
1861
1862 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001863 /// @param source the source information
1864 /// @param var the variable to wrap in a decl statement
1865 /// @returns the variable decl statement pointer
1866 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1867 return create<ast::VariableDeclStatement>(source, var);
1868 }
1869
1870 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001871 /// @param var the variable to wrap in a decl statement
1872 /// @returns the variable decl statement pointer
1873 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1874 return create<ast::VariableDeclStatement>(var);
1875 }
1876
Antonio Maioranocea744d2021-03-25 12:55:27 +00001877 /// Creates a ast::SwitchStatement with input expression and cases
1878 /// @param condition the condition expression initializer
1879 /// @param cases case statements
1880 /// @returns the switch statement pointer
1881 template <typename ExpressionInit, typename... Cases>
1882 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1883 return create<ast::SwitchStatement>(
1884 Expr(std::forward<ExpressionInit>(condition)),
1885 ast::CaseStatementList{std::forward<Cases>(cases)...});
1886 }
1887
1888 /// Creates a ast::CaseStatement with input list of selectors, and body
1889 /// @param selectors list of selectors
1890 /// @param body the case body
1891 /// @returns the case statement pointer
1892 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1893 ast::BlockStatement* body = nullptr) {
1894 return create<ast::CaseStatement>(std::move(selectors),
1895 body ? body : Block());
1896 }
1897
1898 /// Convenient overload that takes a single selector
1899 /// @param selector a single case selector
1900 /// @param body the case body
1901 /// @returns the case statement pointer
1902 ast::CaseStatement* Case(ast::IntLiteral* selector,
1903 ast::BlockStatement* body = nullptr) {
1904 return Case(ast::CaseSelectorList{selector}, body);
1905 }
1906
1907 /// Convenience function that creates a 'default' ast::CaseStatement
1908 /// @param body the case body
1909 /// @returns the case statement pointer
1910 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1911 return Case(ast::CaseSelectorList{}, body);
1912 }
1913
James Price68f558f2021-04-06 15:51:47 +00001914 /// Creates an ast::BuiltinDecoration
1915 /// @param source the source information
1916 /// @param builtin the builtin value
1917 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001918 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001919 return create<ast::BuiltinDecoration>(source, builtin);
1920 }
1921
1922 /// Creates an ast::BuiltinDecoration
1923 /// @param builtin the builtin value
1924 /// @returns the builtin decoration pointer
1925 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1926 return create<ast::BuiltinDecoration>(source_, builtin);
1927 }
1928
James Price989a8e42021-06-28 23:04:43 +00001929 /// Creates an ast::InterpolateDecoration
1930 /// @param source the source information
1931 /// @param type the interpolation type
1932 /// @param sampling the interpolation sampling
1933 /// @returns the interpolate decoration pointer
1934 ast::InterpolateDecoration* Interpolate(const Source& source,
1935 ast::InterpolationType type,
1936 ast::InterpolationSampling sampling) {
1937 return create<ast::InterpolateDecoration>(source, type, sampling);
1938 }
1939
1940 /// Creates an ast::InterpolateDecoration
1941 /// @param type the interpolation type
1942 /// @param sampling the interpolation sampling
1943 /// @returns the interpolate decoration pointer
1944 ast::InterpolateDecoration* Interpolate(ast::InterpolationType type,
1945 ast::InterpolationSampling sampling) {
1946 return create<ast::InterpolateDecoration>(source_, type, sampling);
1947 }
1948
James Price68f558f2021-04-06 15:51:47 +00001949 /// Creates an ast::LocationDecoration
1950 /// @param source the source information
1951 /// @param location the location value
1952 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001953 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001954 return create<ast::LocationDecoration>(source, location);
1955 }
1956
1957 /// Creates an ast::LocationDecoration
1958 /// @param location the location value
1959 /// @returns the location decoration pointer
1960 ast::LocationDecoration* Location(uint32_t location) {
1961 return create<ast::LocationDecoration>(source_, location);
1962 }
1963
James Pricef2f3bfc2021-05-13 20:32:32 +00001964 /// Creates an ast::OverrideDecoration with a specific constant ID
1965 /// @param source the source information
1966 /// @param id the id value
1967 /// @returns the override decoration pointer
1968 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1969 return create<ast::OverrideDecoration>(source, id);
1970 }
1971
1972 /// Creates an ast::OverrideDecoration with a specific constant ID
1973 /// @param id the optional id value
1974 /// @returns the override decoration pointer
1975 ast::OverrideDecoration* Override(uint32_t id) {
1976 return Override(source_, id);
1977 }
1978
1979 /// Creates an ast::OverrideDecoration without a constant ID
1980 /// @param source the source information
1981 /// @returns the override decoration pointer
1982 ast::OverrideDecoration* Override(const Source& source) {
1983 return create<ast::OverrideDecoration>(source);
1984 }
1985
1986 /// Creates an ast::OverrideDecoration without a constant ID
1987 /// @returns the override decoration pointer
1988 ast::OverrideDecoration* Override() { return Override(source_); }
1989
James Price68f558f2021-04-06 15:51:47 +00001990 /// Creates an ast::StageDecoration
1991 /// @param source the source information
1992 /// @param stage the pipeline stage
1993 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001994 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001995 return create<ast::StageDecoration>(source, stage);
1996 }
1997
1998 /// Creates an ast::StageDecoration
1999 /// @param stage the pipeline stage
2000 /// @returns the stage decoration pointer
2001 ast::StageDecoration* Stage(ast::PipelineStage stage) {
2002 return create<ast::StageDecoration>(source_, stage);
2003 }
2004
James Price70f80bb2021-05-19 13:40:08 +00002005 /// Creates an ast::WorkgroupDecoration
2006 /// @param x the x dimension expression
2007 /// @returns the workgroup decoration pointer
2008 template <typename EXPR_X>
2009 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
2010 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
2011 }
2012
2013 /// Creates an ast::WorkgroupDecoration
2014 /// @param x the x dimension expression
2015 /// @param y the y dimension expression
2016 /// @returns the workgroup decoration pointer
2017 template <typename EXPR_X, typename EXPR_Y>
2018 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
2019 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
2020 nullptr);
2021 }
2022
2023 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002024 /// @param source the source information
2025 /// @param x the x dimension expression
2026 /// @param y the y dimension expression
2027 /// @param z the z dimension expression
2028 /// @returns the workgroup decoration pointer
2029 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2030 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2031 EXPR_X&& x,
2032 EXPR_Y&& y,
2033 EXPR_Z&& z) {
2034 return create<ast::WorkgroupDecoration>(
2035 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2036 Expr(std::forward<EXPR_Z>(z)));
2037 }
2038
2039 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002040 /// @param x the x dimension expression
2041 /// @param y the y dimension expression
2042 /// @param z the z dimension expression
2043 /// @returns the workgroup decoration pointer
2044 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2045 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2046 return create<ast::WorkgroupDecoration>(
2047 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2048 Expr(std::forward<EXPR_Z>(z)));
2049 }
2050
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002051 /// Sets the current builder source to `src`
2052 /// @param src the Source used for future create() calls
2053 void SetSource(const Source& src) {
2054 AssertNotMoved();
2055 source_ = src;
2056 }
2057
2058 /// Sets the current builder source to `loc`
2059 /// @param loc the Source used for future create() calls
2060 void SetSource(const Source::Location& loc) {
2061 AssertNotMoved();
2062 source_ = Source(loc);
2063 }
2064
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002065 /// Marks that the given transform has been applied to this program.
2066 /// @param transform the transform that has been applied
2067 void SetTransformApplied(const CastableBase* transform) {
2068 transforms_applied_.emplace(&transform->TypeInfo());
2069 }
2070
2071 /// Marks that the given transform `T` has been applied to this program.
2072 template <typename T>
2073 void SetTransformApplied() {
2074 transforms_applied_.emplace(&TypeInfo::Of<T>());
2075 }
2076
2077 /// Marks that the transforms with the given TypeInfos have been applied to
2078 /// this program.
2079 /// @param transforms the set of transform TypeInfos that has been applied
2080 void SetTransformApplied(
2081 const std::unordered_set<const TypeInfo*>& transforms) {
2082 for (auto* transform : transforms) {
2083 transforms_applied_.emplace(transform);
2084 }
2085 }
2086
2087 /// @returns true if the transform of type `T` was applied.
2088 template <typename T>
2089 bool HasTransformApplied() {
2090 return transforms_applied_.count(&TypeInfo::Of<T>());
2091 }
2092
2093 /// @return the TypeInfo pointers of all transforms that have been applied to
2094 /// this program.
2095 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2096 return transforms_applied_;
2097 }
2098
Ben Clayton33352542021-01-29 16:43:41 +00002099 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002100 /// @note As the Resolver is run when the Program is built, this will only be
2101 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002102 /// @param expr the AST expression
2103 /// @return the resolved semantic type for the expression, or nullptr if the
2104 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002105 sem::Type* TypeOf(const ast::Expression* expr) const;
2106
Ben Claytonfe0910f2021-05-17 15:51:47 +00002107 /// Helper for returning the resolved semantic type of the variable `var`.
2108 /// @note As the Resolver is run when the Program is built, this will only be
2109 /// useful for the Resolver itself and tests that use their own Resolver.
2110 /// @param var the AST variable
2111 /// @return the resolved semantic type for the variable, or nullptr if the
2112 /// variable has no resolved type.
2113 sem::Type* TypeOf(const ast::Variable* var) const;
2114
Ben Claytonfbec46f2021-04-30 20:20:19 +00002115 /// Helper for returning the resolved semantic type of the AST type `type`.
2116 /// @note As the Resolver is run when the Program is built, this will only be
2117 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002118 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002119 /// @return the resolved semantic type for the type, or nullptr if the type
2120 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002121 const sem::Type* TypeOf(const ast::Type* type) const;
2122
2123 /// Helper for returning the resolved semantic type of the AST type
2124 /// declaration `type_decl`.
2125 /// @note As the Resolver is run when the Program is built, this will only be
2126 /// useful for the Resolver itself and tests that use their own Resolver.
2127 /// @param type_decl the AST type declaration
2128 /// @return the resolved semantic type for the type declaration, or nullptr if
2129 /// the type declaration has no resolved type.
2130 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002131
Ben Clayton169512e2021-04-17 05:52:11 +00002132 /// Wraps the ast::Literal in a statement. This is used by tests that
2133 /// construct a partial AST and require the Resolver to reach these
2134 /// nodes.
2135 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2136 /// @return the ast::Statement that wraps the ast::Statement
2137 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002138 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002139 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002140 /// nodes.
2141 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2142 /// @return the ast::Statement that wraps the ast::Expression
2143 ast::Statement* WrapInStatement(ast::Expression* expr);
2144 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002145 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002146 /// these nodes.
2147 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2148 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2149 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2150 /// Returns the statement argument. Used as a passthrough-overload by
2151 /// WrapInFunction().
2152 /// @param stmt the ast::Statement
2153 /// @return `stmt`
2154 ast::Statement* WrapInStatement(ast::Statement* stmt);
2155 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002156 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002157 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002158 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002159 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002160 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002161 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002162 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002163 }
2164 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002165 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002166 /// @returns the function
2167 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002168
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002169 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002170 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002171
2172 protected:
2173 /// Asserts that the builder has not been moved.
2174 void AssertNotMoved() const;
2175
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002176 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002177 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002178 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002179 ASTNodeAllocator ast_nodes_;
2180 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002181 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002182 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002183 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002184 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002185 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002186
2187 /// The source to use when creating AST nodes without providing a Source as
2188 /// the first argument.
2189 Source source_;
2190
Ben Clayton5f0ea112021-03-09 10:54:37 +00002191 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002192 /// program when built.
2193 bool resolve_on_build_ = true;
2194
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002195 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2196 bool moved_ = false;
2197};
2198
2199//! @cond Doxygen_Suppress
2200// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2201template <>
2202struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002203 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002204 return t->i32();
2205 }
2206};
2207template <>
2208struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002209 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002210 return t->u32();
2211 }
2212};
2213template <>
2214struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002215 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002216 return t->f32();
2217 }
2218};
2219template <>
2220struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002221 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002222 return t->bool_();
2223 }
2224};
2225template <>
2226struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002227 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002228 return t->void_();
2229 }
2230};
2231//! @endcond
2232
Ben Clayton917b14b2021-04-19 16:50:23 +00002233/// @param builder the ProgramBuilder
2234/// @returns the ProgramID of the ProgramBuilder
2235inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2236 return builder->ID();
2237}
2238
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002239} // namespace tint
2240
2241#endif // SRC_PROGRAM_BUILDER_H_