blob: 41865e50fa5db57c583f8fdd303bdd004fb52e65 [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"
Antonio Maiorano60dae242021-07-15 19:09:25 +000028#include "src/ast/binding_decoration.h"
Ben Claytond9603282021-06-29 12:37:45 +000029#include "src/ast/bitcast_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000030#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000031#include "src/ast/bool_literal.h"
David Netodffa60c2021-07-27 15:12:27 +000032#include "src/ast/break_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000034#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000035#include "src/ast/case_statement.h"
Ben Claytonfd35aa82021-07-26 22:19:48 +000036#include "src/ast/depth_multisampled_texture.h"
Ben Claytone204f272021-04-22 14:40:23 +000037#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000038#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000039#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000040#include "src/ast/float_literal.h"
Ben Clayton65cd2592021-07-02 19:27:42 +000041#include "src/ast/for_loop_statement.h"
Ben Claytona922e652021-04-21 13:37:22 +000042#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000043#include "src/ast/if_statement.h"
James Price989a8e42021-06-28 23:04:43 +000044#include "src/ast/interpolate_decoration.h"
James Price508d2492021-07-12 12:12:32 +000045#include "src/ast/invariant_decoration.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000046#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000047#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000048#include "src/ast/member_accessor_expression.h"
49#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000050#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000051#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000052#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000053#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000054#include "src/ast/sampled_texture.h"
Ben Clayton0f2d95d2021-07-22 13:24:59 +000055#include "src/ast/sampler.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000056#include "src/ast/scalar_constructor_expression.h"
57#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000058#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000059#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000060#include "src/ast/stride_decoration.h"
Antonio Maiorano60dae242021-07-15 19:09:25 +000061#include "src/ast/struct_block_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000062#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000064#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000065#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000066#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000067#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000068#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000069#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000070#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000071#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000072#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000073#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000074#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000075#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000076#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000077#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000078#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000079#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000080#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000081#include "src/sem/f32_type.h"
82#include "src/sem/i32_type.h"
83#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000084#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000085#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000086#include "src/sem/sampled_texture_type.h"
87#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000088#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000089#include "src/sem/u32_type.h"
90#include "src/sem/vector_type.h"
91#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000092
Ben Clayton03d10722021-06-05 12:45:50 +000093#ifdef INCLUDE_TINT_TINT_H_
94#error "internal tint header being #included from tint.h"
95#endif
96
Ben Clayton401b96b2021-02-03 17:19:59 +000097// Forward declarations
Ben Claytonb5cd10c2021-06-25 10:26:26 +000098namespace tint {
Ben Clayton401b96b2021-02-03 17:19:59 +000099namespace ast {
100class VariableDeclStatement;
101} // namespace ast
Ben Claytonb5cd10c2021-06-25 10:26:26 +0000102} // namespace tint
Ben Clayton401b96b2021-02-03 17:19:59 +0000103
Ben Claytonb5cd10c2021-06-25 10:26:26 +0000104namespace tint {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000105class CloneContext;
106
107/// ProgramBuilder is a mutable builder for a Program.
108/// To construct a Program, populate the builder and then `std::move` it to a
109/// Program.
110class ProgramBuilder {
Ben Claytoncd7eb4f2021-07-17 18:09:26 +0000111 /// A helper used to disable overloads if the first type in `TYPES` is a
112 /// Source. Used to avoid ambiguities in overloads that take a Source as the
113 /// first parameter and those that perfectly-forward the first argument.
114 template <typename... TYPES>
115 using DisableIfSource = traits::EnableIfIsNotType<
116 traits::Decay<traits::NthTypeOf<0, TYPES..., void>>,
117 Source>;
118
Ben Clayton620d77e2021-06-04 19:55:08 +0000119 /// VarOptionals is a helper for accepting a number of optional, extra
120 /// arguments for Var() and Global().
121 struct VarOptionals {
122 template <typename... ARGS>
123 explicit VarOptionals(ARGS&&... args) {
124 Apply(std::forward<ARGS>(args)...);
125 }
126 ~VarOptionals();
127
128 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000129 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000130 ast::Expression* constructor = nullptr;
131 ast::DecorationList decorations = {};
132
133 private:
134 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000135 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000136 void Set(ast::Expression* c) { constructor = c; }
137 void Set(const ast::DecorationList& l) { decorations = l; }
138
139 template <typename FIRST, typename... ARGS>
140 void Apply(FIRST&& first, ARGS&&... args) {
141 Set(std::forward<FIRST>(first));
142 Apply(std::forward<ARGS>(args)...);
143 }
144 void Apply() {}
145 };
146
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000147 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000148 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
149 using ASTNodeAllocator = BlockAllocator<ast::Node>;
150
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000151 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
152 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000153
154 /// `i32` is a type alias to `int`.
155 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
156 /// WGSL syntax.
157 /// Note: this is intentionally not aliased to uint32_t as we want integer
158 /// literals passed to the builder to match WGSL's integer literal types.
159 using i32 = decltype(1);
160 /// `u32` is a type alias to `unsigned int`.
161 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
162 /// WGSL syntax.
163 /// Note: this is intentionally not aliased to uint32_t as we want integer
164 /// literals passed to the builder to match WGSL's integer literal types.
165 using u32 = decltype(1u);
166 /// `f32` is a type alias to `float`
167 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
168 /// WGSL syntax.
169 using f32 = float;
170
171 /// Constructor
172 ProgramBuilder();
173
174 /// Move constructor
175 /// @param rhs the builder to move
176 ProgramBuilder(ProgramBuilder&& rhs);
177
178 /// Destructor
179 virtual ~ProgramBuilder();
180
181 /// Move assignment operator
182 /// @param rhs the builder to move
183 /// @return this builder
184 ProgramBuilder& operator=(ProgramBuilder&& rhs);
185
Ben Claytone43c8302021-01-29 11:59:32 +0000186 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
187 /// making a deep clone of the Program contents.
188 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
189 /// existing immutable program.
190 /// As the returned ProgramBuilder wraps `program`, `program` must not be
191 /// destructed or assigned while using the returned ProgramBuilder.
192 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
193 /// function. See crbug.com/tint/460.
194 /// @param program the immutable Program to wrap
195 /// @return the ProgramBuilder that wraps `program`
196 static ProgramBuilder Wrap(const Program* program);
197
Ben Claytone6995de2021-04-13 23:27:27 +0000198 /// @returns the unique identifier for this program
199 ProgramID ID() const { return id_; }
200
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000201 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000202 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000203 AssertNotMoved();
204 return types_;
205 }
206
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000207 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000208 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000209 AssertNotMoved();
210 return types_;
211 }
212
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000213 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000214 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000215 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000216 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000217 }
218
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000219 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000220 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000221 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000222 return ast_nodes_;
223 }
224
225 /// @returns a reference to the program's semantic nodes storage
226 SemNodeAllocator& SemNodes() {
227 AssertNotMoved();
228 return sem_nodes_;
229 }
230
231 /// @returns a reference to the program's semantic nodes storage
232 const SemNodeAllocator& SemNodes() const {
233 AssertNotMoved();
234 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000235 }
236
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000237 /// @returns a reference to the program's AST root Module
238 ast::Module& AST() {
239 AssertNotMoved();
240 return *ast_;
241 }
242
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000243 /// @returns a reference to the program's AST root Module
244 const ast::Module& AST() const {
245 AssertNotMoved();
246 return *ast_;
247 }
248
249 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000250 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000251 AssertNotMoved();
252 return sem_;
253 }
254
255 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000256 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000257 AssertNotMoved();
258 return sem_;
259 }
260
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000261 /// @returns a reference to the program's SymbolTable
262 SymbolTable& Symbols() {
263 AssertNotMoved();
264 return symbols_;
265 }
266
Ben Clayton708dc2d2021-01-29 11:22:40 +0000267 /// @returns a reference to the program's SymbolTable
268 const SymbolTable& Symbols() const {
269 AssertNotMoved();
270 return symbols_;
271 }
272
Ben Clayton844217f2021-01-27 18:49:05 +0000273 /// @returns a reference to the program's diagnostics
274 diag::List& Diagnostics() {
275 AssertNotMoved();
276 return diagnostics_;
277 }
278
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000279 /// @returns a reference to the program's diagnostics
280 const diag::List& Diagnostics() const {
281 AssertNotMoved();
282 return diagnostics_;
283 }
284
Ben Clayton5f0ea112021-03-09 10:54:37 +0000285 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000286 /// @param enable the new flag value (defaults to true)
287 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
288
Ben Clayton5f0ea112021-03-09 10:54:37 +0000289 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000290 /// built.
291 bool ResolveOnBuild() const { return resolve_on_build_; }
292
Ben Clayton844217f2021-01-27 18:49:05 +0000293 /// @returns true if the program has no error diagnostics and is not missing
294 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000295 bool IsValid() const;
296
Ben Clayton708dc2d2021-01-29 11:22:40 +0000297 /// Writes a representation of the node to the output stream
298 /// @note unlike str(), to_str() does not automatically demangle the string.
299 /// @param node the AST node
300 /// @param out the stream to write to
301 /// @param indent number of spaces to indent the node when writing
302 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
303 node->to_str(Sem(), out, indent);
304 }
305
306 /// Returns a demangled, string representation of `node`.
307 /// @param node the AST node
308 /// @returns a string representation of the node
309 std::string str(const ast::Node* node) const;
310
Ben Clayton7fdfff12021-01-29 15:17:30 +0000311 /// Creates a new ast::Node owned by the ProgramBuilder. When the
312 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000313 /// @param source the Source of the node
314 /// @param args the arguments to pass to the type constructor
315 /// @returns the node pointer
316 template <typename T, typename... ARGS>
317 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
318 ARGS&&... args) {
319 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000320 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000321 }
322
Ben Clayton7fdfff12021-01-29 15:17:30 +0000323 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
324 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000325 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000326 /// When the ProgramBuilder is destructed, the ast::Node will also be
327 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000328 /// @returns the node pointer
329 template <typename T>
330 traits::EnableIfIsType<T, ast::Node>* create() {
331 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000332 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000333 }
334
Ben Clayton7fdfff12021-01-29 15:17:30 +0000335 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
336 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000337 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000338 /// When the ProgramBuilder is destructed, the ast::Node will also be
339 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000340 /// @param arg0 the first arguments to pass to the type constructor
341 /// @param args the remaining arguments to pass to the type constructor
342 /// @returns the node pointer
343 template <typename T, typename ARG0, typename... ARGS>
344 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
345 traits::IsTypeOrDerived<T, ast::Node>::value &&
346 !traits::IsTypeOrDerived<ARG0, Source>::value,
347 T>*
348 create(ARG0&& arg0, ARGS&&... args) {
349 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000350 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000351 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000352 }
353
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000354 /// Creates a new sem::Node owned by the ProgramBuilder.
355 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000356 /// destructed.
357 /// @param args the arguments to pass to the type constructor
358 /// @returns the node pointer
359 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000360 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
361 !traits::IsTypeOrDerived<T, sem::Type>::value,
362 T>*
363 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000364 AssertNotMoved();
365 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
366 }
367
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000368 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000369 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
370 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000371 /// Types are unique (de-aliased), and so calling create() for the same `T`
372 /// and arguments will return the same pointer.
373 /// @warning Use this method to acquire a type only if all of its type
374 /// information is provided in the constructor arguments `args`.<br>
375 /// If the type requires additional configuration after construction that
376 /// affect its fundamental type, build the type with `std::make_unique`, make
377 /// any necessary alterations and then call unique_type() instead.
378 /// @param args the arguments to pass to the type constructor
379 /// @returns the de-aliased type pointer
380 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000381 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
382 static_assert(std::is_base_of<sem::Type, T>::value,
383 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000384 AssertNotMoved();
385 return types_.Get<T>(std::forward<ARGS>(args)...);
386 }
387
388 /// Marks this builder as moved, preventing any further use of the builder.
389 void MarkAsMoved();
390
391 //////////////////////////////////////////////////////////////////////////////
392 // TypesBuilder
393 //////////////////////////////////////////////////////////////////////////////
394
395 /// TypesBuilder holds basic `tint` types and methods for constructing
396 /// complex types.
397 class TypesBuilder {
398 public:
399 /// Constructor
400 /// @param builder the program builder
401 explicit TypesBuilder(ProgramBuilder* builder);
402
403 /// @return the tint AST type for the C type `T`.
404 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000405 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000406 return CToAST<T>::get(this);
407 }
408
409 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000410 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000411
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000412 /// @param source the Source of the node
413 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000414 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000415 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000416 }
417
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000418 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000419 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000420
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000421 /// @param source the Source of the node
422 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000423 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000424 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000425 }
426
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000427 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000428 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000429
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000430 /// @param source the Source of the node
431 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000432 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000433 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000434 }
435
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000436 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000437 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000438
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000439 /// @param source the Source of the node
440 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000441 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000442 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000443 }
444
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000445 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000446 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000447
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000448 /// @param source the Source of the node
449 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000450 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000451 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000452 }
453
Antonio Maiorano25436862021-04-13 13:32:33 +0000454 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000455 /// @param n vector width in elements
456 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000457 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000458 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000459 }
460
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000461 /// @param source the Source of the node
462 /// @param type vector subtype
463 /// @param n vector width in elements
464 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000465 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000466 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000467 }
468
Antonio Maiorano25436862021-04-13 13:32:33 +0000469 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000470 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000471 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000472
473 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000474 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000475 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000476
477 /// @param type vector subtype
478 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000479 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000480
481 /// @param n vector width in elements
482 /// @return the tint AST type for a `n`-element vector of `type`.
483 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000484 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000485 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000486 }
487
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000488 /// @return the tint AST type for a 2-element vector of the C type `T`.
489 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000490 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000491 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000492 }
493
494 /// @return the tint AST type for a 3-element vector of the C type `T`.
495 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000496 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000497 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000498 }
499
500 /// @return the tint AST type for a 4-element vector of the C type `T`.
501 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000502 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000503 return vec4(Of<T>());
504 }
505
506 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000507 /// @param columns number of columns for the matrix
508 /// @param rows number of rows for the matrix
509 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000510 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000511 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000512 }
513
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000514 /// @param source the Source of the node
515 /// @param type matrix subtype
516 /// @param columns number of columns for the matrix
517 /// @param rows number of rows for the matrix
518 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000519 ast::Matrix* mat(const Source& source,
520 ast::Type* type,
521 uint32_t columns,
522 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000523 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000524 }
525
Ben Claytone204f272021-04-22 14:40:23 +0000526 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000527 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000528 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000529
530 /// @param type matrix subtype
531 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000532 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000533
534 /// @param type matrix subtype
535 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000536 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000537
538 /// @param type matrix subtype
539 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000540 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000541
542 /// @param type matrix subtype
543 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000544 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000545
546 /// @param type matrix subtype
547 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000548 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000549
550 /// @param type matrix subtype
551 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000552 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000553
554 /// @param type matrix subtype
555 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000556 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000557
558 /// @param type matrix subtype
559 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000560 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561
Ben Claytone204f272021-04-22 14:40:23 +0000562 /// @param columns number of columns for the matrix
563 /// @param rows number of rows for the matrix
564 /// @return the tint AST type for a matrix of `type`
565 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000566 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000567 return mat(Of<T>(), columns, rows);
568 }
569
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000570 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
571 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000572 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000573 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000574 }
575
576 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
577 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000578 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000579 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000580 }
581
582 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
583 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000584 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000585 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000586 }
587
588 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
589 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000590 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000591 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000592 }
593
594 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
595 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000596 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000597 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000598 }
599
600 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
601 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000602 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000603 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000604 }
605
606 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
607 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000608 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000609 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000610 }
611
612 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
613 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000614 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000615 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000616 }
617
618 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
619 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000620 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000621 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000622 }
623
624 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000625 /// @param n the array size. 0 represents a runtime-array
626 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000627 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000628 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000629 uint32_t n = 0,
630 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000631 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000632 }
633
634 /// @param source the Source of the node
635 /// @param subtype the array element type
636 /// @param n the array size. 0 represents a runtime-array
637 /// @param decos the optional decorations for the array
638 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000639 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000640 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000641 uint32_t n = 0,
642 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000643 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000644 }
645
Ben Claytonbab31972021-02-08 21:16:21 +0000646 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000647 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000648 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000649 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000650 ast::Array* array(ast::Type* subtype, uint32_t n, 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(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000656 }
657
Ben Clayton0f88b312021-05-04 17:36:31 +0000658 /// @param source the Source of the node
659 /// @param subtype the array element type
660 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000661 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000662 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000663 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000664 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000665 uint32_t n,
666 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000667 ast::DecorationList decos;
668 if (stride) {
669 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
670 }
671 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000672 }
673
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000674 /// @return the tint AST type for an array of size `N` of type `T`
675 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000676 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000677 return array(Of<T>(), N);
678 }
679
Ben Claytonbab31972021-02-08 21:16:21 +0000680 /// @param stride the array stride
681 /// @return the tint AST type for an array of size `N` of type `T`
682 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000683 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000684 return array(Of<T>(), N, stride);
685 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000686
Ben Clayton0f88b312021-05-04 17:36:31 +0000687 /// Creates a type name
688 /// @param name the name
689 /// @returns the type name
690 template <typename NAME>
691 ast::TypeName* type_name(NAME&& name) const {
692 return builder->create<ast::TypeName>(
693 builder->Sym(std::forward<NAME>(name)));
694 }
695
696 /// Creates a type name
697 /// @param source the Source of the node
698 /// @param name the name
699 /// @returns the type name
700 template <typename NAME>
701 ast::TypeName* type_name(const Source& source, NAME&& name) const {
702 return builder->create<ast::TypeName>(
703 source, builder->Sym(std::forward<NAME>(name)));
704 }
705
Ben Clayton42d1e092021-02-02 14:29:15 +0000706 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000707 /// @param name the alias name
708 /// @param type the alias type
709 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000710 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000711 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000712 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000713 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000714 }
715
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000716 /// Creates an alias type
717 /// @param source the Source of the node
718 /// @param name the alias name
719 /// @param type the alias type
720 /// @returns the alias pointer
721 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000722 ast::Alias* alias(const Source& source,
723 NAME&& name,
724 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000725 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000726 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000727 }
728
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000729 /// @param type the type of the pointer
730 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000731 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000732 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000733 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000734 ast::StorageClass storage_class,
735 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000736 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000737 }
738
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000739 /// @param source the Source of the node
740 /// @param type the type of the pointer
741 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000742 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000743 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000744 ast::Pointer* pointer(const Source& source,
745 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000746 ast::StorageClass storage_class,
747 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000748 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000749 }
750
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000751 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000752 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000753 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000754 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000755 ast::Pointer* pointer(ast::StorageClass storage_class,
756 ast::Access access = ast::Access::kUndefined) const {
757 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000758 }
759
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000760 /// @param source the Source of the node
761 /// @param type the type of the atomic
762 /// @return the atomic to `type`
763 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
764 return builder->create<ast::Atomic>(source, type);
765 }
766
Ben Clayton313e6182021-06-17 19:56:14 +0000767 /// @param type the type of the atomic
768 /// @return the atomic to `type`
769 ast::Atomic* atomic(ast::Type* type) const {
770 return builder->create<ast::Atomic>(type);
771 }
772
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000773 /// @return the atomic to type `T`
774 template <typename T>
775 ast::Atomic* atomic() const {
776 return atomic(Of<T>());
777 }
778
Ben Claytone204f272021-04-22 14:40:23 +0000779 /// @param kind the kind of sampler
780 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000781 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000782 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000783 }
784
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000785 /// @param source the Source of the node
786 /// @param kind the kind of sampler
787 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000788 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000789 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000790 }
791
Ben Claytone204f272021-04-22 14:40:23 +0000792 /// @param dims the dimensionality of the texture
793 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000794 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000795 return builder->create<ast::DepthTexture>(dims);
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 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000801 ast::DepthTexture* depth_texture(const Source& source,
802 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000803 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000804 }
805
Ben Claytone204f272021-04-22 14:40:23 +0000806 /// @param dims the dimensionality of the texture
Ben Claytonfd35aa82021-07-26 22:19:48 +0000807 /// @returns the multisampled depth texture
808 ast::DepthMultisampledTexture* depth_multisampled_texture(
809 ast::TextureDimension dims) const {
810 return builder->create<ast::DepthMultisampledTexture>(dims);
811 }
812
813 /// @param source the Source of the node
814 /// @param dims the dimensionality of the texture
815 /// @returns the multisampled depth texture
816 ast::DepthMultisampledTexture* depth_multisampled_texture(
817 const Source& source,
818 ast::TextureDimension dims) const {
819 return builder->create<ast::DepthMultisampledTexture>(source, dims);
820 }
821
822 /// @param dims the dimensionality of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000823 /// @param subtype the texture subtype.
824 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000825 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
826 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000827 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000828 }
829
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000830 /// @param source the Source of the node
831 /// @param dims the dimensionality of the texture
832 /// @param subtype the texture subtype.
833 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000834 ast::SampledTexture* sampled_texture(const Source& source,
835 ast::TextureDimension dims,
836 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000837 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000838 }
839
Ben Claytone204f272021-04-22 14:40:23 +0000840 /// @param dims the dimensionality of the texture
841 /// @param subtype the texture subtype.
842 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000843 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
844 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000845 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000846 }
847
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000848 /// @param source the Source of the node
849 /// @param dims the dimensionality of the texture
850 /// @param subtype the texture subtype.
851 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000852 ast::MultisampledTexture* multisampled_texture(const Source& source,
853 ast::TextureDimension dims,
854 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000855 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000856 }
857
Ben Claytone204f272021-04-22 14:40:23 +0000858 /// @param dims the dimensionality of the texture
859 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000860 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000861 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000862 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000863 ast::ImageFormat format,
864 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000865 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000866 return builder->create<ast::StorageTexture>(dims, format, subtype,
867 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000868 }
869
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000870 /// @param source the Source of the node
871 /// @param dims the dimensionality of the texture
872 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000873 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000874 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000875 ast::StorageTexture* storage_texture(const Source& source,
876 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000877 ast::ImageFormat format,
878 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000879 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000880 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
881 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000882 }
883
Brandon Jones7b257692021-05-17 17:40:17 +0000884 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000885 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000886 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000887 }
888
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000889 /// @param source the Source of the node
890 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000891 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000892 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000893 }
894
Ben Clayton19b03192021-05-20 15:04:08 +0000895 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000896 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000897 /// @param type the type
898 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000899 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000900
Ben Clayton8758f102021-06-09 14:32:14 +0000901 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000902 /// @param type the type
903 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000904 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000905
Ben Claytonf5f311e2021-04-28 14:31:23 +0000906 /// The ProgramBuilder
907 ProgramBuilder* const builder;
908
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000909 private:
910 /// CToAST<T> is specialized for various `T` types and each specialization
911 /// contains a single static `get()` method for obtaining the corresponding
912 /// AST type for the C type `T`.
913 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000914 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000915 template <typename T>
916 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000917 };
918
919 //////////////////////////////////////////////////////////////////////////////
920 // AST helper methods
921 //////////////////////////////////////////////////////////////////////////////
922
James Price65ae64d2021-04-29 12:59:14 +0000923 /// @return a new unnamed symbol
924 Symbol Sym() { return Symbols().New(); }
925
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000926 /// @param name the symbol string
927 /// @return a Symbol with the given name
928 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
929
930 /// @param sym the symbol
931 /// @return `sym`
932 Symbol Sym(Symbol sym) { return sym; }
933
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000934 /// @param expr the expression
935 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000936 template <typename T>
937 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
938 return expr;
939 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000940
Ben Claytonb8ea5912021-04-19 16:52:42 +0000941 /// Passthrough for nullptr
942 /// @return nullptr
943 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
944
Ben Claytonfe0910f2021-05-17 15:51:47 +0000945 /// @param source the source information
946 /// @param symbol the identifier symbol
947 /// @return an ast::IdentifierExpression with the given symbol
948 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
949 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000950 }
951
Ben Clayton46d78d72021-02-10 21:34:26 +0000952 /// @param symbol the identifier symbol
953 /// @return an ast::IdentifierExpression with the given symbol
954 ast::IdentifierExpression* Expr(Symbol symbol) {
955 return create<ast::IdentifierExpression>(symbol);
956 }
957
Ben Claytonfe0910f2021-05-17 15:51:47 +0000958 /// @param source the source information
959 /// @param variable the AST variable
960 /// @return an ast::IdentifierExpression with the variable's symbol
961 ast::IdentifierExpression* Expr(const Source& source,
962 ast::Variable* variable) {
963 return create<ast::IdentifierExpression>(source, variable->symbol());
964 }
965
Ben Claytonb8ea5912021-04-19 16:52:42 +0000966 /// @param variable the AST variable
967 /// @return an ast::IdentifierExpression with the variable's symbol
968 ast::IdentifierExpression* Expr(ast::Variable* variable) {
969 return create<ast::IdentifierExpression>(variable->symbol());
970 }
971
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000972 /// @param source the source information
973 /// @param name the identifier name
974 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000975 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
976 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
977 }
978
979 /// @param name the identifier name
980 /// @return an ast::IdentifierExpression with the given name
981 ast::IdentifierExpression* Expr(const char* name) {
982 return create<ast::IdentifierExpression>(Symbols().Register(name));
983 }
984
985 /// @param source the source information
986 /// @param name the identifier name
987 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000988 ast::IdentifierExpression* Expr(const Source& source,
989 const std::string& name) {
990 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
991 }
992
993 /// @param name the identifier name
994 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000995 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000996 return create<ast::IdentifierExpression>(Symbols().Register(name));
997 }
998
Ben Claytonfe0910f2021-05-17 15:51:47 +0000999 /// @param source the source information
1000 /// @param value the boolean value
1001 /// @return a Scalar constructor for the given value
1002 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
1003 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1004 }
1005
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001006 /// @param value the boolean value
1007 /// @return a Scalar constructor for the given value
1008 ast::ScalarConstructorExpression* Expr(bool value) {
1009 return create<ast::ScalarConstructorExpression>(Literal(value));
1010 }
1011
Ben Claytonfe0910f2021-05-17 15:51:47 +00001012 /// @param source the source information
1013 /// @param value the float value
1014 /// @return a Scalar constructor for the given value
1015 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
1016 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1017 }
1018
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001019 /// @param value the float value
1020 /// @return a Scalar constructor for the given value
1021 ast::ScalarConstructorExpression* Expr(f32 value) {
1022 return create<ast::ScalarConstructorExpression>(Literal(value));
1023 }
1024
Ben Claytonfe0910f2021-05-17 15:51:47 +00001025 /// @param source the source information
1026 /// @param value the integer value
1027 /// @return a Scalar constructor for the given value
1028 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1029 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1030 }
1031
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001032 /// @param value the integer value
1033 /// @return a Scalar constructor for the given value
1034 ast::ScalarConstructorExpression* Expr(i32 value) {
1035 return create<ast::ScalarConstructorExpression>(Literal(value));
1036 }
1037
Ben Claytonfe0910f2021-05-17 15:51:47 +00001038 /// @param source the source information
1039 /// @param value the unsigned int value
1040 /// @return a Scalar constructor for the given value
1041 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1042 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1043 }
1044
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001045 /// @param value the unsigned int value
1046 /// @return a Scalar constructor for the given value
1047 ast::ScalarConstructorExpression* Expr(u32 value) {
1048 return create<ast::ScalarConstructorExpression>(Literal(value));
1049 }
1050
1051 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1052 /// `list`.
1053 /// @param list the list to append too
1054 /// @param arg the arg to create
1055 template <typename ARG>
1056 void Append(ast::ExpressionList& list, ARG&& arg) {
1057 list.emplace_back(Expr(std::forward<ARG>(arg)));
1058 }
1059
1060 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1061 /// then appends them to `list`.
1062 /// @param list the list to append too
1063 /// @param arg0 the first argument
1064 /// @param args the rest of the arguments
1065 template <typename ARG0, typename... ARGS>
1066 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1067 Append(list, std::forward<ARG0>(arg0));
1068 Append(list, std::forward<ARGS>(args)...);
1069 }
1070
1071 /// @return an empty list of expressions
1072 ast::ExpressionList ExprList() { return {}; }
1073
1074 /// @param args the list of expressions
1075 /// @return the list of expressions converted to `ast::Expression`s using
1076 /// `Expr()`,
1077 template <typename... ARGS>
1078 ast::ExpressionList ExprList(ARGS&&... args) {
1079 ast::ExpressionList list;
1080 list.reserve(sizeof...(args));
1081 Append(list, std::forward<ARGS>(args)...);
1082 return list;
1083 }
1084
1085 /// @param list the list of expressions
1086 /// @return `list`
1087 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1088
1089 /// @param val the boolan value
1090 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001091 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001092
1093 /// @param val the float value
1094 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001095 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001096
1097 /// @param val the unsigned int value
1098 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001099 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001100
1101 /// @param val the integer value
1102 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001103 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001104
1105 /// @param args the arguments for the type constructor
1106 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1107 /// of `args` converted to `ast::Expression`s using `Expr()`
1108 template <typename T, typename... ARGS>
1109 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001110 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001111 }
1112
1113 /// @param type the type to construct
1114 /// @param args the arguments for the constructor
1115 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1116 /// values `args`.
1117 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001118 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 return create<ast::TypeConstructorExpression>(
1120 type, ExprList(std::forward<ARGS>(args)...));
1121 }
1122
Ben Clayton313e6182021-06-17 19:56:14 +00001123 /// @param source the source information
1124 /// @param type the type to construct
1125 /// @param args the arguments for the constructor
1126 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1127 /// values `args`.
1128 template <typename... ARGS>
1129 ast::TypeConstructorExpression* Construct(const Source& source,
1130 ast::Type* type,
1131 ARGS&&... args) {
1132 return create<ast::TypeConstructorExpression>(
1133 source, type, ExprList(std::forward<ARGS>(args)...));
1134 }
1135
Ben Claytond9603282021-06-29 12:37:45 +00001136 /// @param expr the expression for the bitcast
1137 /// @return an `ast::BitcastExpression` of type `ty`, with the values of
1138 /// `expr` converted to `ast::Expression`s using `Expr()`
1139 template <typename T, typename EXPR>
1140 ast::BitcastExpression* Bitcast(EXPR&& expr) {
1141 return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
1142 }
1143
1144 /// @param type the type to cast to
1145 /// @param expr the expression for the bitcast
1146 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1147 /// `expr`.
1148 template <typename EXPR>
1149 ast::BitcastExpression* Bitcast(ast::Type* type, EXPR&& expr) {
1150 return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
1151 }
1152
1153 /// @param source the source information
1154 /// @param type the type to cast to
1155 /// @param expr the expression for the bitcast
1156 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1157 /// `expr`.
1158 template <typename EXPR>
1159 ast::BitcastExpression* Bitcast(const Source& source,
1160 ast::Type* type,
1161 EXPR&& expr) {
1162 return create<ast::BitcastExpression>(source, type,
1163 Expr(std::forward<EXPR>(expr)));
1164 }
1165
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001166 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001167 /// @param type the vector type
1168 /// @param size the vector size
1169 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1170 /// type `type`, constructed with the values `args`.
1171 template <typename... ARGS>
1172 ast::TypeConstructorExpression* vec(ast::Type* type,
1173 uint32_t size,
1174 ARGS&&... args) {
1175 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1176 }
1177
1178 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001179 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1180 /// `T`, constructed with the values `args`.
1181 template <typename T, typename... ARGS>
1182 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001183 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001184 }
1185
1186 /// @param args the arguments for the vector constructor
1187 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1188 /// `T`, constructed with the values `args`.
1189 template <typename T, typename... ARGS>
1190 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001191 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001192 }
1193
1194 /// @param args the arguments for the vector constructor
1195 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1196 /// `T`, constructed with the values `args`.
1197 template <typename T, typename... ARGS>
1198 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001199 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001200 }
1201
1202 /// @param args the arguments for the matrix constructor
1203 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1204 /// `T`, constructed with the values `args`.
1205 template <typename T, typename... ARGS>
1206 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001207 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001208 }
1209
1210 /// @param args the arguments for the matrix constructor
1211 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1212 /// `T`, constructed with the values `args`.
1213 template <typename T, typename... ARGS>
1214 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001215 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216 }
1217
1218 /// @param args the arguments for the matrix constructor
1219 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1220 /// `T`, constructed with the values `args`.
1221 template <typename T, typename... ARGS>
1222 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001223 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001224 }
1225
1226 /// @param args the arguments for the matrix constructor
1227 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1228 /// `T`, constructed with the values `args`.
1229 template <typename T, typename... ARGS>
1230 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001231 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001232 }
1233
1234 /// @param args the arguments for the matrix constructor
1235 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1236 /// `T`, constructed with the values `args`.
1237 template <typename T, typename... ARGS>
1238 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001239 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001240 }
1241
1242 /// @param args the arguments for the matrix constructor
1243 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1244 /// `T`, constructed with the values `args`.
1245 template <typename T, typename... ARGS>
1246 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001247 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001248 }
1249
1250 /// @param args the arguments for the matrix constructor
1251 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1252 /// `T`, constructed with the values `args`.
1253 template <typename T, typename... ARGS>
1254 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001255 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001256 }
1257
1258 /// @param args the arguments for the matrix constructor
1259 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1260 /// `T`, constructed with the values `args`.
1261 template <typename T, typename... ARGS>
1262 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001263 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001264 }
1265
1266 /// @param args the arguments for the matrix constructor
1267 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1268 /// `T`, constructed with the values `args`.
1269 template <typename T, typename... ARGS>
1270 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001271 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001272 }
1273
1274 /// @param args the arguments for the array constructor
1275 /// @return an `ast::TypeConstructorExpression` of an array with element type
1276 /// `T`, constructed with the values `args`.
1277 template <typename T, int N = 0, typename... ARGS>
1278 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001279 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001280 }
1281
1282 /// @param subtype the array element type
1283 /// @param n the array size. 0 represents a runtime-array.
1284 /// @param args the arguments for the array constructor
1285 /// @return an `ast::TypeConstructorExpression` of an array with element type
1286 /// `subtype`, constructed with the values `args`.
1287 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001288 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001289 uint32_t n,
1290 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001291 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001292 }
1293
1294 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001295 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001296 /// @param optional the optional variable settings.
1297 /// Can be any of the following, in any order:
1298 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001299 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001300 /// * ast::Expression* - specifies the variable's initializer expression
1301 /// * ast::DecorationList - specifies the variable's decorations
1302 /// Note that repeated arguments of the same type will use the last argument's
1303 /// value.
1304 /// @returns a `ast::Variable` with the given name, type and additional
1305 /// options
1306 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001307 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001308 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001309 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001310 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1311 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001312 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001313 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001314 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001315
1316 /// @param source the variable source
1317 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001318 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001319 /// @param optional the optional variable settings.
1320 /// Can be any of the following, in any order:
1321 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001322 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001323 /// * ast::Expression* - specifies the variable's initializer expression
1324 /// * ast::DecorationList - specifies the variable's decorations
1325 /// Note that repeated arguments of the same type will use the last argument's
1326 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001327 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001328 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001329 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001330 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001331 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001332 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001333 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1334 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001335 opts.storage, opts.access, type, false,
1336 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001337 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001338
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001339 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001340 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001341 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001342 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001343 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001344 template <typename NAME>
1345 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001346 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001347 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001348 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001349 return create<ast::Variable>(
1350 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1351 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001352 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001353
1354 /// @param source the variable source
1355 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001356 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001357 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001358 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001359 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001360 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001361 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001362 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001363 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001364 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001365 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001366 return create<ast::Variable>(
1367 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1368 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001369 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001370
James Pricedaf1f1c2021-04-08 22:15:48 +00001371 /// @param name the parameter name
1372 /// @param type the parameter type
1373 /// @param decorations optional parameter decorations
1374 /// @returns a constant `ast::Variable` with the given name and type
1375 template <typename NAME>
1376 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001377 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001378 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001379 return create<ast::Variable>(
1380 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1381 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001382 }
1383
1384 /// @param source the parameter source
1385 /// @param name the parameter name
1386 /// @param type the parameter type
1387 /// @param decorations optional parameter decorations
1388 /// @returns a constant `ast::Variable` with the given name and type
1389 template <typename NAME>
1390 ast::Variable* Param(const Source& source,
1391 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001392 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001393 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001394 return create<ast::Variable>(
1395 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1396 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001397 }
1398
Ben Clayton42708342021-04-21 17:55:12 +00001399 /// @param name the variable name
1400 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001401 /// @param optional the optional variable settings.
1402 /// Can be any of the following, in any order:
1403 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001404 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001405 /// * ast::Expression* - specifies the variable's initializer expression
1406 /// * ast::DecorationList - specifies the variable's decorations
1407 /// Note that repeated arguments of the same type will use the last argument's
1408 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001409 /// @returns a new `ast::Variable`, which is automatically registered as a
1410 /// global variable with the ast::Module.
Antonio Maiorano60dae242021-07-15 19:09:25 +00001411 template <typename NAME,
1412 typename... OPTIONAL,
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001413 typename = DisableIfSource<NAME>>
Ben Clayton42708342021-04-21 17:55:12 +00001414 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001415 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001416 OPTIONAL&&... optional) {
1417 auto* var = Var(std::forward<NAME>(name), type,
1418 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001419 AST().AddGlobalVariable(var);
1420 return var;
1421 }
1422
1423 /// @param source the variable source
1424 /// @param name the variable name
1425 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001426 /// @param optional the optional variable settings.
1427 /// Can be any of the following, in any order:
1428 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001429 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001430 /// * ast::Expression* - specifies the variable's initializer expression
1431 /// * ast::DecorationList - specifies the variable's decorations
1432 /// Note that repeated arguments of the same type will use the last argument's
1433 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001434 /// @returns a new `ast::Variable`, which is automatically registered as a
1435 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001436 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001437 ast::Variable* Global(const Source& source,
1438 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001439 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001440 OPTIONAL&&... optional) {
1441 auto* var = Var(source, std::forward<NAME>(name), type,
1442 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001443 AST().AddGlobalVariable(var);
1444 return var;
1445 }
1446
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001447 /// @param name the variable name
1448 /// @param type the variable type
1449 /// @param constructor constructor expression
1450 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001451 /// @returns a const `ast::Variable` constructed by calling Var() with the
1452 /// arguments of `args`, which is automatically registered as a global
1453 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001454 template <typename NAME>
1455 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001456 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001457 ast::Expression* constructor,
1458 ast::DecorationList decorations = {}) {
1459 auto* var = Const(std::forward<NAME>(name), type, constructor,
1460 std::move(decorations));
1461 AST().AddGlobalVariable(var);
1462 return var;
1463 }
1464
1465 /// @param source the variable source
1466 /// @param name the variable name
1467 /// @param type the variable type
1468 /// @param constructor constructor expression
1469 /// @param decorations optional variable decorations
1470 /// @returns a const `ast::Variable` constructed by calling Var() with the
1471 /// arguments of `args`, which is automatically registered as a global
1472 /// variable with the ast::Module.
1473 template <typename NAME>
1474 ast::Variable* GlobalConst(const Source& source,
1475 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001476 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001477 ast::Expression* constructor,
1478 ast::DecorationList decorations = {}) {
1479 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1480 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001481 AST().AddGlobalVariable(var);
1482 return var;
1483 }
1484
Ben Claytonfe0910f2021-05-17 15:51:47 +00001485 /// @param source the source information
1486 /// @param expr the expression to take the address of
1487 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1488 template <typename EXPR>
1489 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1490 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1491 Expr(std::forward<EXPR>(expr)));
1492 }
1493
1494 /// @param expr the expression to take the address of
1495 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1496 template <typename EXPR>
1497 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1498 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1499 Expr(std::forward<EXPR>(expr)));
1500 }
1501
1502 /// @param source the source information
1503 /// @param expr the expression to perform an indirection on
1504 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1505 template <typename EXPR>
1506 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1507 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1508 Expr(std::forward<EXPR>(expr)));
1509 }
1510
1511 /// @param expr the expression to perform an indirection on
1512 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1513 template <typename EXPR>
1514 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1515 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1516 Expr(std::forward<EXPR>(expr)));
1517 }
1518
Antonio Maiorano14b34032021-06-09 20:17:59 +00001519 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001520 /// @param func the function name
1521 /// @param args the function call arguments
1522 /// @returns a `ast::CallExpression` to the function `func`, with the
1523 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1524 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001525 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1526 return create<ast::CallExpression>(source, Expr(func),
1527 ExprList(std::forward<ARGS>(args)...));
1528 }
1529
1530 /// @param func the function name
1531 /// @param args the function call arguments
1532 /// @returns a `ast::CallExpression` to the function `func`, with the
1533 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001534 template <typename NAME, typename... ARGS, typename = DisableIfSource<NAME>>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001535 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1536 return create<ast::CallExpression>(Expr(func),
1537 ExprList(std::forward<ARGS>(args)...));
1538 }
1539
Ben Clayton7fe01062021-06-11 13:22:27 +00001540 /// @param expr the expression to ignore
1541 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1542 /// passed the single `expr` argument
1543 template <typename EXPR>
1544 ast::CallStatement* Ignore(EXPR&& expr) {
1545 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1546 }
1547
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001548 /// @param lhs the left hand argument to the addition operation
1549 /// @param rhs the right hand argument to the addition operation
1550 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1551 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001552 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001553 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1554 Expr(std::forward<LHS>(lhs)),
1555 Expr(std::forward<RHS>(rhs)));
1556 }
1557
James Pricec32e8f62021-06-22 20:08:29 +00001558 /// @param lhs the left hand argument to the and operation
1559 /// @param rhs the right hand argument to the and operation
1560 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1561 template <typename LHS, typename RHS>
1562 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1563 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1564 Expr(std::forward<LHS>(lhs)),
1565 Expr(std::forward<RHS>(rhs)));
1566 }
1567
Ben Claytond9603282021-06-29 12:37:45 +00001568 /// @param lhs the left hand argument to the or operation
1569 /// @param rhs the right hand argument to the or operation
1570 /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
1571 template <typename LHS, typename RHS>
1572 ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
1573 return create<ast::BinaryExpression>(ast::BinaryOp::kOr,
1574 Expr(std::forward<LHS>(lhs)),
1575 Expr(std::forward<RHS>(rhs)));
1576 }
1577
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001578 /// @param lhs the left hand argument to the subtraction operation
1579 /// @param rhs the right hand argument to the subtraction operation
1580 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1581 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001582 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001583 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1584 Expr(std::forward<LHS>(lhs)),
1585 Expr(std::forward<RHS>(rhs)));
1586 }
1587
1588 /// @param lhs the left hand argument to the multiplication operation
1589 /// @param rhs the right hand argument to the multiplication operation
1590 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1591 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001592 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001593 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1594 Expr(std::forward<LHS>(lhs)),
1595 Expr(std::forward<RHS>(rhs)));
1596 }
1597
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001598 /// @param source the source information
1599 /// @param lhs the left hand argument to the multiplication operation
1600 /// @param rhs the right hand argument to the multiplication operation
1601 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1602 template <typename LHS, typename RHS>
1603 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1604 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1605 Expr(std::forward<LHS>(lhs)),
1606 Expr(std::forward<RHS>(rhs)));
1607 }
1608
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001609 /// @param lhs the left hand argument to the division operation
1610 /// @param rhs the right hand argument to the division operation
1611 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1612 template <typename LHS, typename RHS>
1613 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1614 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1615 Expr(std::forward<LHS>(lhs)),
1616 Expr(std::forward<RHS>(rhs)));
1617 }
1618
Ben Claytond9603282021-06-29 12:37:45 +00001619 /// @param lhs the left hand argument to the bit shift right operation
1620 /// @param rhs the right hand argument to the bit shift right operation
1621 /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
1622 template <typename LHS, typename RHS>
1623 ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
1624 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftRight,
1625 Expr(std::forward<LHS>(lhs)),
1626 Expr(std::forward<RHS>(rhs)));
1627 }
1628
1629 /// @param lhs the left hand argument to the bit shift left operation
1630 /// @param rhs the right hand argument to the bit shift left operation
1631 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
1632 template <typename LHS, typename RHS>
1633 ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
1634 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftLeft,
1635 Expr(std::forward<LHS>(lhs)),
1636 Expr(std::forward<RHS>(rhs)));
1637 }
1638
Ben Clayton0597a2b2021-06-16 09:19:36 +00001639 /// @param source the source information
1640 /// @param arr the array argument for the array accessor expression
1641 /// @param idx the index argument for the array accessor expression
1642 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1643 template <typename ARR, typename IDX>
1644 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1645 ARR&& arr,
1646 IDX&& idx) {
1647 return create<ast::ArrayAccessorExpression>(
1648 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1649 }
1650
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001651 /// @param arr the array argument for the array accessor expression
1652 /// @param idx the index argument for the array accessor expression
1653 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1654 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001655 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001656 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1657 Expr(std::forward<IDX>(idx)));
1658 }
1659
1660 /// @param obj the object for the member accessor expression
1661 /// @param idx the index argument for the array accessor expression
1662 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1663 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001664 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001665 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1666 Expr(std::forward<IDX>(idx)));
1667 }
1668
Ben Clayton42d1e092021-02-02 14:29:15 +00001669 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001670 /// @param val the offset value
1671 /// @returns the offset decoration pointer
1672 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1673 return create<ast::StructMemberOffsetDecoration>(source_, val);
1674 }
1675
Ben Claytond614dd52021-03-15 10:43:11 +00001676 /// Creates a ast::StructMemberSizeDecoration
1677 /// @param source the source information
1678 /// @param val the size value
1679 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001680 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1681 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001682 return create<ast::StructMemberSizeDecoration>(source, val);
1683 }
1684
1685 /// Creates a ast::StructMemberSizeDecoration
1686 /// @param val the size value
1687 /// @returns the size decoration pointer
1688 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1689 return create<ast::StructMemberSizeDecoration>(source_, val);
1690 }
1691
1692 /// Creates a ast::StructMemberAlignDecoration
1693 /// @param source the source information
1694 /// @param val the align value
1695 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001696 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1697 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001698 return create<ast::StructMemberAlignDecoration>(source, val);
1699 }
1700
1701 /// Creates a ast::StructMemberAlignDecoration
1702 /// @param val the align value
1703 /// @returns the align decoration pointer
1704 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1705 return create<ast::StructMemberAlignDecoration>(source_, val);
1706 }
1707
Antonio Maiorano60dae242021-07-15 19:09:25 +00001708 /// Creates a ast::StructBlockDecoration
1709 /// @returns the struct block decoration pointer
1710 ast::StructBlockDecoration* StructBlock() {
1711 return create<ast::StructBlockDecoration>();
1712 }
1713
1714 /// Creates the ast::GroupDecoration
1715 /// @param value group decoration index
1716 /// @returns the group decoration pointer
1717 ast::GroupDecoration* Group(uint32_t value) {
1718 return create<ast::GroupDecoration>(value);
1719 }
1720
1721 /// Creates the ast::BindingDecoration
1722 /// @param value the binding index
1723 /// @returns the binding deocration pointer
1724 ast::BindingDecoration* Binding(uint32_t value) {
1725 return create<ast::BindingDecoration>(value);
1726 }
1727
1728 /// Convenience function to create both a ast::GroupDecoration and
1729 /// ast::BindingDecoration
1730 /// @param group the group index
1731 /// @param binding the binding index
1732 /// @returns a decoration list with both the group and binding decorations
1733 ast::DecorationList GroupAndBinding(uint32_t group, uint32_t binding) {
1734 return {Group(group), Binding(binding)};
1735 }
1736
Ben Clayton42d1e092021-02-02 14:29:15 +00001737 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001738 /// @param source the source information
1739 /// @param name the function name
1740 /// @param params the function parameters
1741 /// @param type the function return type
1742 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001743 /// @param decorations the optional function decorations
1744 /// @param return_type_decorations the optional function return type
1745 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001746 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001747 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001748 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001749 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001750 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001751 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001752 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001753 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001754 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001755 auto* func =
1756 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1757 type, create<ast::BlockStatement>(body),
1758 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001759 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001760 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001761 }
1762
Ben Clayton42d1e092021-02-02 14:29:15 +00001763 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001764 /// @param name the function name
1765 /// @param params the function parameters
1766 /// @param type the function return type
1767 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001768 /// @param decorations the optional function decorations
1769 /// @param return_type_decorations the optional function return type
1770 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001771 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001772 template <typename NAME>
1773 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001774 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001775 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001776 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001777 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001778 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001779 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1780 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001781 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001782 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001783 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001784 }
1785
David Netodffa60c2021-07-27 15:12:27 +00001786 /// Creates an ast::BreakStatement
1787 /// @param source the source information
1788 /// @returns the break statement pointer
1789 ast::BreakStatement* Break(const Source& source) {
1790 return create<ast::BreakStatement>(source);
1791 }
1792
1793 /// Creates an ast::BreakStatement
1794 /// @returns the break statement pointer
1795 ast::BreakStatement* Break() { return create<ast::BreakStatement>(); }
1796
Ben Clayton49668bb2021-04-17 05:32:01 +00001797 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001798 /// @param source the source information
1799 /// @returns the return statement pointer
1800 ast::ReturnStatement* Return(const Source& source) {
1801 return create<ast::ReturnStatement>(source);
1802 }
1803
1804 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001805 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001806 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1807
1808 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001809 /// @param source the source information
1810 /// @param val the return value
1811 /// @returns the return statement pointer
1812 template <typename EXPR>
1813 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1814 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1815 }
1816
1817 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001818 /// @param val the return value
1819 /// @returns the return statement pointer
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001820 template <typename EXPR, typename = DisableIfSource<EXPR>>
Ben Clayton49668bb2021-04-17 05:32:01 +00001821 ast::ReturnStatement* Return(EXPR&& val) {
1822 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001823 }
1824
Ben Clayton950809f2021-06-09 14:32:14 +00001825 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001826 /// @param source the source information
1827 /// @param name the alias name
1828 /// @param type the alias target type
1829 /// @returns the alias type
1830 template <typename NAME>
1831 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1832 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001833 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001834 return out;
1835 }
1836
Ben Clayton950809f2021-06-09 14:32:14 +00001837 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001838 /// @param name the alias name
1839 /// @param type the alias target type
1840 /// @returns the alias type
1841 template <typename NAME>
1842 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1843 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001844 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001845 return out;
1846 }
1847
Ben Clayton950809f2021-06-09 14:32:14 +00001848 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001849 /// @param source the source information
1850 /// @param name the struct name
1851 /// @param members the struct members
1852 /// @param decorations the optional struct decorations
1853 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001854 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001855 ast::Struct* Structure(const Source& source,
1856 NAME&& name,
1857 ast::StructMemberList members,
1858 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001859 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001860 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001861 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001862 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001863 return type;
1864 }
1865
Ben Clayton950809f2021-06-09 14:32:14 +00001866 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001867 /// @param name the struct name
1868 /// @param members the struct members
1869 /// @param decorations the optional struct decorations
1870 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001871 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001872 ast::Struct* Structure(NAME&& name,
1873 ast::StructMemberList members,
1874 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001875 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001876 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001877 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001878 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001879 return type;
1880 }
1881
Ben Clayton42d1e092021-02-02 14:29:15 +00001882 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001883 /// @param source the source information
1884 /// @param name the struct member name
1885 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001886 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001887 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001888 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001889 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001890 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001891 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001892 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001893 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1894 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001895 }
1896
Ben Clayton42d1e092021-02-02 14:29:15 +00001897 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001898 /// @param name the struct member name
1899 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001900 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001901 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001902 template <typename NAME>
1903 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001904 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001905 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001906 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1907 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001908 }
1909
Ben Claytonbab31972021-02-08 21:16:21 +00001910 /// Creates a ast::StructMember with the given byte offset
1911 /// @param offset the offset to use in the StructMemberOffsetDecoration
1912 /// @param name the struct member name
1913 /// @param type the struct member type
1914 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001915 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001916 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001917 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001918 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001919 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001920 create<ast::StructMemberOffsetDecoration>(offset),
1921 });
1922 }
1923
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001924 /// Creates a ast::BlockStatement with input statements
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001925 /// @param source the source information for the block
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001926 /// @param statements statements of block
1927 /// @returns the block statement pointer
1928 template <typename... Statements>
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001929 ast::BlockStatement* Block(const Source& source, Statements&&... statements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001930 return create<ast::BlockStatement>(
Ben Claytoncd7eb4f2021-07-17 18:09:26 +00001931 source, ast::StatementList{std::forward<Statements>(statements)...});
1932 }
1933
1934 /// Creates a ast::BlockStatement with input statements
1935 /// @param statements statements of block
1936 /// @returns the block statement pointer
1937 template <typename... STATEMENTS, typename = DisableIfSource<STATEMENTS...>>
1938 ast::BlockStatement* Block(STATEMENTS&&... statements) {
1939 return create<ast::BlockStatement>(
1940 ast::StatementList{std::forward<STATEMENTS>(statements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001941 }
1942
1943 /// Creates a ast::ElseStatement with input condition and body
1944 /// @param condition the else condition expression
1945 /// @param body the else body
1946 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001947 template <typename CONDITION>
1948 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1949 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1950 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001951 }
1952
1953 /// Creates a ast::IfStatement with input condition, body, and optional
1954 /// variadic else statements
1955 /// @param condition the if statement condition expression
1956 /// @param body the if statement body
1957 /// @param elseStatements optional variadic else statements
1958 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001959 template <typename CONDITION, typename... ELSE_STATEMENTS>
1960 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001961 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001962 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001963 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001964 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001965 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001966 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001967 }
1968
1969 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001970 /// @param source the source information
1971 /// @param lhs the left hand side expression initializer
1972 /// @param rhs the right hand side expression initializer
1973 /// @returns the assignment statement pointer
1974 template <typename LhsExpressionInit, typename RhsExpressionInit>
1975 ast::AssignmentStatement* Assign(const Source& source,
1976 LhsExpressionInit&& lhs,
1977 RhsExpressionInit&& rhs) {
1978 return create<ast::AssignmentStatement>(
1979 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1980 Expr(std::forward<RhsExpressionInit>(rhs)));
1981 }
1982
1983 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001984 /// @param lhs the left hand side expression initializer
1985 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001986 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001987 template <typename LhsExpressionInit, typename RhsExpressionInit>
1988 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1989 RhsExpressionInit&& rhs) {
1990 return create<ast::AssignmentStatement>(
1991 Expr(std::forward<LhsExpressionInit>(lhs)),
1992 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001993 }
1994
1995 /// Creates a ast::LoopStatement with input body and optional continuing
1996 /// @param body the loop body
1997 /// @param continuing the optional continuing block
1998 /// @returns the loop statement pointer
1999 ast::LoopStatement* Loop(ast::BlockStatement* body,
2000 ast::BlockStatement* continuing = nullptr) {
2001 return create<ast::LoopStatement>(body, continuing);
2002 }
2003
Ben Clayton65cd2592021-07-02 19:27:42 +00002004 /// Creates a ast::ForLoopStatement with input body and optional initializer,
2005 /// condition and continuing.
2006 /// @param source the source information
2007 /// @param init the optional loop initializer
2008 /// @param cond the optional loop condition
2009 /// @param cont the optional loop continuing
2010 /// @param body the loop body
2011 /// @returns the for loop statement pointer
2012 template <typename COND>
2013 ast::ForLoopStatement* For(const Source& source,
2014 ast::Statement* init,
2015 COND&& cond,
2016 ast::Statement* cont,
2017 ast::BlockStatement* body) {
2018 return create<ast::ForLoopStatement>(
2019 source, init, Expr(std::forward<COND>(cond)), cont, body);
2020 }
2021
2022 /// Creates a ast::ForLoopStatement with input body and optional initializer,
2023 /// condition and continuing.
2024 /// @param init the optional loop initializer
2025 /// @param cond the optional loop condition
2026 /// @param cont the optional loop continuing
2027 /// @param body the loop body
2028 /// @returns the for loop statement pointer
2029 template <typename COND>
2030 ast::ForLoopStatement* For(ast::Statement* init,
2031 COND&& cond,
2032 ast::Statement* cont,
2033 ast::BlockStatement* body) {
2034 return create<ast::ForLoopStatement>(init, Expr(std::forward<COND>(cond)),
2035 cont, body);
2036 }
2037
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00002038 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00002039 /// @param source the source information
2040 /// @param var the variable to wrap in a decl statement
2041 /// @returns the variable decl statement pointer
2042 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
2043 return create<ast::VariableDeclStatement>(source, var);
2044 }
2045
2046 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00002047 /// @param var the variable to wrap in a decl statement
2048 /// @returns the variable decl statement pointer
2049 ast::VariableDeclStatement* Decl(ast::Variable* var) {
2050 return create<ast::VariableDeclStatement>(var);
2051 }
2052
Antonio Maioranocea744d2021-03-25 12:55:27 +00002053 /// Creates a ast::SwitchStatement with input expression and cases
2054 /// @param condition the condition expression initializer
2055 /// @param cases case statements
2056 /// @returns the switch statement pointer
2057 template <typename ExpressionInit, typename... Cases>
2058 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
2059 return create<ast::SwitchStatement>(
2060 Expr(std::forward<ExpressionInit>(condition)),
2061 ast::CaseStatementList{std::forward<Cases>(cases)...});
2062 }
2063
2064 /// Creates a ast::CaseStatement with input list of selectors, and body
2065 /// @param selectors list of selectors
2066 /// @param body the case body
2067 /// @returns the case statement pointer
2068 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
2069 ast::BlockStatement* body = nullptr) {
2070 return create<ast::CaseStatement>(std::move(selectors),
2071 body ? body : Block());
2072 }
2073
2074 /// Convenient overload that takes a single selector
2075 /// @param selector a single case selector
2076 /// @param body the case body
2077 /// @returns the case statement pointer
2078 ast::CaseStatement* Case(ast::IntLiteral* selector,
2079 ast::BlockStatement* body = nullptr) {
2080 return Case(ast::CaseSelectorList{selector}, body);
2081 }
2082
2083 /// Convenience function that creates a 'default' ast::CaseStatement
2084 /// @param body the case body
2085 /// @returns the case statement pointer
2086 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
2087 return Case(ast::CaseSelectorList{}, body);
2088 }
2089
James Price68f558f2021-04-06 15:51:47 +00002090 /// Creates an ast::BuiltinDecoration
2091 /// @param source the source information
2092 /// @param builtin the builtin value
2093 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002094 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00002095 return create<ast::BuiltinDecoration>(source, builtin);
2096 }
2097
2098 /// Creates an ast::BuiltinDecoration
2099 /// @param builtin the builtin value
2100 /// @returns the builtin decoration pointer
2101 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
2102 return create<ast::BuiltinDecoration>(source_, builtin);
2103 }
2104
James Price989a8e42021-06-28 23:04:43 +00002105 /// Creates an ast::InterpolateDecoration
2106 /// @param source the source information
2107 /// @param type the interpolation type
2108 /// @param sampling the interpolation sampling
2109 /// @returns the interpolate decoration pointer
2110 ast::InterpolateDecoration* Interpolate(const Source& source,
2111 ast::InterpolationType type,
2112 ast::InterpolationSampling sampling) {
2113 return create<ast::InterpolateDecoration>(source, type, sampling);
2114 }
2115
2116 /// Creates an ast::InterpolateDecoration
2117 /// @param type the interpolation type
2118 /// @param sampling the interpolation sampling
2119 /// @returns the interpolate decoration pointer
2120 ast::InterpolateDecoration* Interpolate(ast::InterpolationType type,
2121 ast::InterpolationSampling sampling) {
2122 return create<ast::InterpolateDecoration>(source_, type, sampling);
2123 }
2124
James Price508d2492021-07-12 12:12:32 +00002125 /// Creates an ast::InvariantDecoration
2126 /// @param source the source information
2127 /// @returns the invariant decoration pointer
2128 ast::InvariantDecoration* Invariant(const Source& source) {
2129 return create<ast::InvariantDecoration>(source);
2130 }
2131
2132 /// Creates an ast::InvariantDecoration
2133 /// @returns the invariant decoration pointer
2134 ast::InvariantDecoration* Invariant() {
2135 return create<ast::InvariantDecoration>(source_);
2136 }
2137
James Price68f558f2021-04-06 15:51:47 +00002138 /// Creates an ast::LocationDecoration
2139 /// @param source the source information
2140 /// @param location the location value
2141 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002142 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00002143 return create<ast::LocationDecoration>(source, location);
2144 }
2145
2146 /// Creates an ast::LocationDecoration
2147 /// @param location the location value
2148 /// @returns the location decoration pointer
2149 ast::LocationDecoration* Location(uint32_t location) {
2150 return create<ast::LocationDecoration>(source_, location);
2151 }
2152
James Pricef2f3bfc2021-05-13 20:32:32 +00002153 /// Creates an ast::OverrideDecoration with a specific constant ID
2154 /// @param source the source information
2155 /// @param id the id value
2156 /// @returns the override decoration pointer
2157 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
2158 return create<ast::OverrideDecoration>(source, id);
2159 }
2160
2161 /// Creates an ast::OverrideDecoration with a specific constant ID
2162 /// @param id the optional id value
2163 /// @returns the override decoration pointer
2164 ast::OverrideDecoration* Override(uint32_t id) {
2165 return Override(source_, id);
2166 }
2167
2168 /// Creates an ast::OverrideDecoration without a constant ID
2169 /// @param source the source information
2170 /// @returns the override decoration pointer
2171 ast::OverrideDecoration* Override(const Source& source) {
2172 return create<ast::OverrideDecoration>(source);
2173 }
2174
2175 /// Creates an ast::OverrideDecoration without a constant ID
2176 /// @returns the override decoration pointer
2177 ast::OverrideDecoration* Override() { return Override(source_); }
2178
James Price68f558f2021-04-06 15:51:47 +00002179 /// Creates an ast::StageDecoration
2180 /// @param source the source information
2181 /// @param stage the pipeline stage
2182 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002183 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00002184 return create<ast::StageDecoration>(source, stage);
2185 }
2186
2187 /// Creates an ast::StageDecoration
2188 /// @param stage the pipeline stage
2189 /// @returns the stage decoration pointer
2190 ast::StageDecoration* Stage(ast::PipelineStage stage) {
2191 return create<ast::StageDecoration>(source_, stage);
2192 }
2193
James Price70f80bb2021-05-19 13:40:08 +00002194 /// Creates an ast::WorkgroupDecoration
2195 /// @param x the x dimension expression
2196 /// @returns the workgroup decoration pointer
2197 template <typename EXPR_X>
2198 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
2199 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
2200 }
2201
2202 /// Creates an ast::WorkgroupDecoration
2203 /// @param x the x dimension expression
2204 /// @param y the y dimension expression
2205 /// @returns the workgroup decoration pointer
2206 template <typename EXPR_X, typename EXPR_Y>
2207 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
2208 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
2209 nullptr);
2210 }
2211
2212 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002213 /// @param source the source information
2214 /// @param x the x dimension expression
2215 /// @param y the y dimension expression
2216 /// @param z the z dimension expression
2217 /// @returns the workgroup decoration pointer
2218 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2219 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2220 EXPR_X&& x,
2221 EXPR_Y&& y,
2222 EXPR_Z&& z) {
2223 return create<ast::WorkgroupDecoration>(
2224 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2225 Expr(std::forward<EXPR_Z>(z)));
2226 }
2227
2228 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002229 /// @param x the x dimension expression
2230 /// @param y the y dimension expression
2231 /// @param z the z dimension expression
2232 /// @returns the workgroup decoration pointer
2233 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2234 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2235 return create<ast::WorkgroupDecoration>(
2236 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2237 Expr(std::forward<EXPR_Z>(z)));
2238 }
2239
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002240 /// Sets the current builder source to `src`
2241 /// @param src the Source used for future create() calls
2242 void SetSource(const Source& src) {
2243 AssertNotMoved();
2244 source_ = src;
2245 }
2246
2247 /// Sets the current builder source to `loc`
2248 /// @param loc the Source used for future create() calls
2249 void SetSource(const Source::Location& loc) {
2250 AssertNotMoved();
2251 source_ = Source(loc);
2252 }
2253
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002254 /// Marks that the given transform has been applied to this program.
2255 /// @param transform the transform that has been applied
2256 void SetTransformApplied(const CastableBase* transform) {
2257 transforms_applied_.emplace(&transform->TypeInfo());
2258 }
2259
2260 /// Marks that the given transform `T` has been applied to this program.
2261 template <typename T>
2262 void SetTransformApplied() {
2263 transforms_applied_.emplace(&TypeInfo::Of<T>());
2264 }
2265
2266 /// Marks that the transforms with the given TypeInfos have been applied to
2267 /// this program.
2268 /// @param transforms the set of transform TypeInfos that has been applied
2269 void SetTransformApplied(
2270 const std::unordered_set<const TypeInfo*>& transforms) {
2271 for (auto* transform : transforms) {
2272 transforms_applied_.emplace(transform);
2273 }
2274 }
2275
2276 /// @returns true if the transform of type `T` was applied.
2277 template <typename T>
2278 bool HasTransformApplied() {
2279 return transforms_applied_.count(&TypeInfo::Of<T>());
2280 }
2281
2282 /// @return the TypeInfo pointers of all transforms that have been applied to
2283 /// this program.
2284 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2285 return transforms_applied_;
2286 }
2287
Ben Clayton33352542021-01-29 16:43:41 +00002288 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002289 /// @note As the Resolver is run when the Program is built, this will only be
2290 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002291 /// @param expr the AST expression
2292 /// @return the resolved semantic type for the expression, or nullptr if the
2293 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002294 sem::Type* TypeOf(const ast::Expression* expr) const;
2295
Ben Claytonfe0910f2021-05-17 15:51:47 +00002296 /// Helper for returning the resolved semantic type of the variable `var`.
2297 /// @note As the Resolver is run when the Program is built, this will only be
2298 /// useful for the Resolver itself and tests that use their own Resolver.
2299 /// @param var the AST variable
2300 /// @return the resolved semantic type for the variable, or nullptr if the
2301 /// variable has no resolved type.
2302 sem::Type* TypeOf(const ast::Variable* var) const;
2303
Ben Claytonfbec46f2021-04-30 20:20:19 +00002304 /// Helper for returning the resolved semantic type of the AST type `type`.
2305 /// @note As the Resolver is run when the Program is built, this will only be
2306 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002307 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002308 /// @return the resolved semantic type for the type, or nullptr if the type
2309 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002310 const sem::Type* TypeOf(const ast::Type* type) const;
2311
2312 /// Helper for returning the resolved semantic type of the AST type
2313 /// declaration `type_decl`.
2314 /// @note As the Resolver is run when the Program is built, this will only be
2315 /// useful for the Resolver itself and tests that use their own Resolver.
2316 /// @param type_decl the AST type declaration
2317 /// @return the resolved semantic type for the type declaration, or nullptr if
2318 /// the type declaration has no resolved type.
2319 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002320
Ben Clayton169512e2021-04-17 05:52:11 +00002321 /// Wraps the ast::Literal in a statement. This is used by tests that
2322 /// construct a partial AST and require the Resolver to reach these
2323 /// nodes.
2324 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2325 /// @return the ast::Statement that wraps the ast::Statement
2326 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002327 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002328 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002329 /// nodes.
2330 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2331 /// @return the ast::Statement that wraps the ast::Expression
2332 ast::Statement* WrapInStatement(ast::Expression* expr);
2333 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002334 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002335 /// these nodes.
2336 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2337 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2338 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2339 /// Returns the statement argument. Used as a passthrough-overload by
2340 /// WrapInFunction().
2341 /// @param stmt the ast::Statement
2342 /// @return `stmt`
2343 ast::Statement* WrapInStatement(ast::Statement* stmt);
2344 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002345 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002346 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002347 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002348 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002349 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002350 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002351 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002352 }
2353 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002354 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002355 /// @returns the function
2356 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002357
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002358 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002359 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002360
2361 protected:
2362 /// Asserts that the builder has not been moved.
2363 void AssertNotMoved() const;
2364
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002365 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002366 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002367 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002368 ASTNodeAllocator ast_nodes_;
2369 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002370 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002371 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002372 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002373 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002374 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002375
2376 /// The source to use when creating AST nodes without providing a Source as
2377 /// the first argument.
2378 Source source_;
2379
Ben Clayton5f0ea112021-03-09 10:54:37 +00002380 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002381 /// program when built.
2382 bool resolve_on_build_ = true;
2383
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002384 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2385 bool moved_ = false;
2386};
2387
2388//! @cond Doxygen_Suppress
2389// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2390template <>
2391struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002392 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002393 return t->i32();
2394 }
2395};
2396template <>
2397struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002398 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002399 return t->u32();
2400 }
2401};
2402template <>
2403struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002404 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002405 return t->f32();
2406 }
2407};
2408template <>
2409struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002410 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002411 return t->bool_();
2412 }
2413};
2414template <>
2415struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002416 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002417 return t->void_();
2418 }
2419};
2420//! @endcond
2421
Ben Clayton917b14b2021-04-19 16:50:23 +00002422/// @param builder the ProgramBuilder
2423/// @returns the ProgramID of the ProgramBuilder
2424inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2425 return builder->ID();
2426}
2427
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002428} // namespace tint
2429
2430#endif // SRC_PROGRAM_BUILDER_H_