blob: 57695458933886bf902955a0d7c8b3e33d5d544c [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000026#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/bool_literal.h"
28#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000029#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000030#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000031#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000032#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000035#include "src/ast/if_statement.h"
36#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000037#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/member_accessor_expression.h"
39#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000040#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000041#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000043#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/scalar_constructor_expression.h"
46#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000047#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000049#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000050#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000053#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000055#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000056#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000058#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000059#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000060#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000061#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000062#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000064#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000065#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000066#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000067#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000068#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000069#include "src/sem/f32_type.h"
70#include "src/sem/i32_type.h"
71#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000073#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/sampled_texture_type.h"
75#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000076#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000077#include "src/sem/u32_type.h"
78#include "src/sem/vector_type.h"
79#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000080
Ben Clayton03d10722021-06-05 12:45:50 +000081#ifdef INCLUDE_TINT_TINT_H_
82#error "internal tint header being #included from tint.h"
83#endif
84
Ben Claytona6b9a8e2021-01-26 16:57:10 +000085namespace tint {
86
Ben Clayton401b96b2021-02-03 17:19:59 +000087// Forward declarations
88namespace ast {
89class VariableDeclStatement;
90} // namespace ast
91
Ben Claytona6b9a8e2021-01-26 16:57:10 +000092class CloneContext;
93
94/// ProgramBuilder is a mutable builder for a Program.
95/// To construct a Program, populate the builder and then `std::move` it to a
96/// Program.
97class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +000098 /// VarOptionals is a helper for accepting a number of optional, extra
99 /// arguments for Var() and Global().
100 struct VarOptionals {
101 template <typename... ARGS>
102 explicit VarOptionals(ARGS&&... args) {
103 Apply(std::forward<ARGS>(args)...);
104 }
105 ~VarOptionals();
106
107 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000108 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000109 ast::Expression* constructor = nullptr;
110 ast::DecorationList decorations = {};
111
112 private:
113 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000114 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000115 void Set(ast::Expression* c) { constructor = c; }
116 void Set(const ast::DecorationList& l) { decorations = l; }
117
118 template <typename FIRST, typename... ARGS>
119 void Apply(FIRST&& first, ARGS&&... args) {
120 Set(std::forward<FIRST>(first));
121 Apply(std::forward<ARGS>(args)...);
122 }
123 void Apply() {}
124 };
125
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000126 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000127 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
128 using ASTNodeAllocator = BlockAllocator<ast::Node>;
129
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000130 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
131 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000132
133 /// `i32` is a type alias to `int`.
134 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
135 /// WGSL syntax.
136 /// Note: this is intentionally not aliased to uint32_t as we want integer
137 /// literals passed to the builder to match WGSL's integer literal types.
138 using i32 = decltype(1);
139 /// `u32` is a type alias to `unsigned int`.
140 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
141 /// WGSL syntax.
142 /// Note: this is intentionally not aliased to uint32_t as we want integer
143 /// literals passed to the builder to match WGSL's integer literal types.
144 using u32 = decltype(1u);
145 /// `f32` is a type alias to `float`
146 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
147 /// WGSL syntax.
148 using f32 = float;
149
150 /// Constructor
151 ProgramBuilder();
152
153 /// Move constructor
154 /// @param rhs the builder to move
155 ProgramBuilder(ProgramBuilder&& rhs);
156
157 /// Destructor
158 virtual ~ProgramBuilder();
159
160 /// Move assignment operator
161 /// @param rhs the builder to move
162 /// @return this builder
163 ProgramBuilder& operator=(ProgramBuilder&& rhs);
164
Ben Claytone43c8302021-01-29 11:59:32 +0000165 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
166 /// making a deep clone of the Program contents.
167 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
168 /// existing immutable program.
169 /// As the returned ProgramBuilder wraps `program`, `program` must not be
170 /// destructed or assigned while using the returned ProgramBuilder.
171 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
172 /// function. See crbug.com/tint/460.
173 /// @param program the immutable Program to wrap
174 /// @return the ProgramBuilder that wraps `program`
175 static ProgramBuilder Wrap(const Program* program);
176
Ben Claytone6995de2021-04-13 23:27:27 +0000177 /// @returns the unique identifier for this program
178 ProgramID ID() const { return id_; }
179
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000180 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000181 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000182 AssertNotMoved();
183 return types_;
184 }
185
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000186 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000187 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000188 AssertNotMoved();
189 return types_;
190 }
191
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000192 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000193 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000194 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000195 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000196 }
197
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000198 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000199 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000200 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000201 return ast_nodes_;
202 }
203
204 /// @returns a reference to the program's semantic nodes storage
205 SemNodeAllocator& SemNodes() {
206 AssertNotMoved();
207 return sem_nodes_;
208 }
209
210 /// @returns a reference to the program's semantic nodes storage
211 const SemNodeAllocator& SemNodes() const {
212 AssertNotMoved();
213 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000214 }
215
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000216 /// @returns a reference to the program's AST root Module
217 ast::Module& AST() {
218 AssertNotMoved();
219 return *ast_;
220 }
221
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000222 /// @returns a reference to the program's AST root Module
223 const ast::Module& AST() const {
224 AssertNotMoved();
225 return *ast_;
226 }
227
228 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000229 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000230 AssertNotMoved();
231 return sem_;
232 }
233
234 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000235 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000236 AssertNotMoved();
237 return sem_;
238 }
239
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000240 /// @returns a reference to the program's SymbolTable
241 SymbolTable& Symbols() {
242 AssertNotMoved();
243 return symbols_;
244 }
245
Ben Clayton708dc2d2021-01-29 11:22:40 +0000246 /// @returns a reference to the program's SymbolTable
247 const SymbolTable& Symbols() const {
248 AssertNotMoved();
249 return symbols_;
250 }
251
Ben Clayton844217f2021-01-27 18:49:05 +0000252 /// @returns a reference to the program's diagnostics
253 diag::List& Diagnostics() {
254 AssertNotMoved();
255 return diagnostics_;
256 }
257
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000258 /// @returns a reference to the program's diagnostics
259 const diag::List& Diagnostics() const {
260 AssertNotMoved();
261 return diagnostics_;
262 }
263
Ben Clayton5f0ea112021-03-09 10:54:37 +0000264 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000265 /// @param enable the new flag value (defaults to true)
266 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
267
Ben Clayton5f0ea112021-03-09 10:54:37 +0000268 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000269 /// built.
270 bool ResolveOnBuild() const { return resolve_on_build_; }
271
Ben Clayton844217f2021-01-27 18:49:05 +0000272 /// @returns true if the program has no error diagnostics and is not missing
273 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000274 bool IsValid() const;
275
Ben Clayton708dc2d2021-01-29 11:22:40 +0000276 /// Writes a representation of the node to the output stream
277 /// @note unlike str(), to_str() does not automatically demangle the string.
278 /// @param node the AST node
279 /// @param out the stream to write to
280 /// @param indent number of spaces to indent the node when writing
281 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
282 node->to_str(Sem(), out, indent);
283 }
284
285 /// Returns a demangled, string representation of `node`.
286 /// @param node the AST node
287 /// @returns a string representation of the node
288 std::string str(const ast::Node* node) const;
289
Ben Clayton7fdfff12021-01-29 15:17:30 +0000290 /// Creates a new ast::Node owned by the ProgramBuilder. When the
291 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000292 /// @param source the Source of the node
293 /// @param args the arguments to pass to the type constructor
294 /// @returns the node pointer
295 template <typename T, typename... ARGS>
296 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
297 ARGS&&... args) {
298 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000299 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000300 }
301
Ben Clayton7fdfff12021-01-29 15:17:30 +0000302 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
303 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000304 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000305 /// When the ProgramBuilder is destructed, the ast::Node will also be
306 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000307 /// @returns the node pointer
308 template <typename T>
309 traits::EnableIfIsType<T, ast::Node>* create() {
310 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000311 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000312 }
313
Ben Clayton7fdfff12021-01-29 15:17:30 +0000314 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
315 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000316 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000317 /// When the ProgramBuilder is destructed, the ast::Node will also be
318 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000319 /// @param arg0 the first arguments to pass to the type constructor
320 /// @param args the remaining arguments to pass to the type constructor
321 /// @returns the node pointer
322 template <typename T, typename ARG0, typename... ARGS>
323 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
324 traits::IsTypeOrDerived<T, ast::Node>::value &&
325 !traits::IsTypeOrDerived<ARG0, Source>::value,
326 T>*
327 create(ARG0&& arg0, ARGS&&... args) {
328 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000329 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000330 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000331 }
332
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000333 /// Creates a new sem::Node owned by the ProgramBuilder.
334 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000335 /// destructed.
336 /// @param args the arguments to pass to the type constructor
337 /// @returns the node pointer
338 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000339 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
340 !traits::IsTypeOrDerived<T, sem::Type>::value,
341 T>*
342 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000343 AssertNotMoved();
344 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
345 }
346
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000347 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000348 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
349 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000350 /// Types are unique (de-aliased), and so calling create() for the same `T`
351 /// and arguments will return the same pointer.
352 /// @warning Use this method to acquire a type only if all of its type
353 /// information is provided in the constructor arguments `args`.<br>
354 /// If the type requires additional configuration after construction that
355 /// affect its fundamental type, build the type with `std::make_unique`, make
356 /// any necessary alterations and then call unique_type() instead.
357 /// @param args the arguments to pass to the type constructor
358 /// @returns the de-aliased type pointer
359 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000360 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
361 static_assert(std::is_base_of<sem::Type, T>::value,
362 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000363 AssertNotMoved();
364 return types_.Get<T>(std::forward<ARGS>(args)...);
365 }
366
367 /// Marks this builder as moved, preventing any further use of the builder.
368 void MarkAsMoved();
369
370 //////////////////////////////////////////////////////////////////////////////
371 // TypesBuilder
372 //////////////////////////////////////////////////////////////////////////////
373
374 /// TypesBuilder holds basic `tint` types and methods for constructing
375 /// complex types.
376 class TypesBuilder {
377 public:
378 /// Constructor
379 /// @param builder the program builder
380 explicit TypesBuilder(ProgramBuilder* builder);
381
382 /// @return the tint AST type for the C type `T`.
383 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000384 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000385 return CToAST<T>::get(this);
386 }
387
388 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000389 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000390
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000391 /// @param source the Source of the node
392 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000393 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000394 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000395 }
396
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000397 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000398 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000399
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000400 /// @param source the Source of the node
401 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000402 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000403 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000404 }
405
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000406 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000407 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000408
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000409 /// @param source the Source of the node
410 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000411 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000412 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000413 }
414
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000415 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000416 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000417
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000418 /// @param source the Source of the node
419 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000420 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000421 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000422 }
423
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000424 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000425 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000426
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000427 /// @param source the Source of the node
428 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000429 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000430 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000431 }
432
Antonio Maiorano25436862021-04-13 13:32:33 +0000433 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000434 /// @param n vector width in elements
435 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000436 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000437 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000438 }
439
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000440 /// @param source the Source of the node
441 /// @param type vector subtype
442 /// @param n vector width in elements
443 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000444 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000445 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000446 }
447
Antonio Maiorano25436862021-04-13 13:32:33 +0000448 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000449 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000450 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000451
452 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000453 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000454 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000455
456 /// @param type vector subtype
457 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000458 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000459
460 /// @param n vector width in elements
461 /// @return the tint AST type for a `n`-element vector of `type`.
462 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000463 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000464 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000465 }
466
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000467 /// @return the tint AST type for a 2-element vector of the C type `T`.
468 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000469 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000470 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000471 }
472
473 /// @return the tint AST type for a 3-element vector of the C type `T`.
474 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000475 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000476 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000477 }
478
479 /// @return the tint AST type for a 4-element vector of the C type `T`.
480 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000481 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000482 return vec4(Of<T>());
483 }
484
485 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000486 /// @param columns number of columns for the matrix
487 /// @param rows number of rows for the matrix
488 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000489 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000490 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000491 }
492
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000493 /// @param source the Source of the node
494 /// @param type matrix subtype
495 /// @param columns number of columns for the matrix
496 /// @param rows number of rows for the matrix
497 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000498 ast::Matrix* mat(const Source& source,
499 ast::Type* type,
500 uint32_t columns,
501 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000502 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000503 }
504
Ben Claytone204f272021-04-22 14:40:23 +0000505 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000506 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000507 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000508
509 /// @param type matrix subtype
510 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000511 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000512
513 /// @param type matrix subtype
514 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000515 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000516
517 /// @param type matrix subtype
518 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000519 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000520
521 /// @param type matrix subtype
522 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000523 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000524
525 /// @param type matrix subtype
526 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000527 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000528
529 /// @param type matrix subtype
530 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000531 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000532
533 /// @param type matrix subtype
534 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000535 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000536
537 /// @param type matrix subtype
538 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000539 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000540
Ben Claytone204f272021-04-22 14:40:23 +0000541 /// @param columns number of columns for the matrix
542 /// @param rows number of rows for the matrix
543 /// @return the tint AST type for a matrix of `type`
544 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000545 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000546 return mat(Of<T>(), columns, rows);
547 }
548
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000549 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
550 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000551 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000552 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000553 }
554
555 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
556 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000557 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000558 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559 }
560
561 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
562 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000563 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000564 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000565 }
566
567 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
568 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000569 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000570 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 }
572
573 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
574 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000575 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000576 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 }
578
579 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
580 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000581 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000582 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000583 }
584
585 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
586 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000587 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000588 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000589 }
590
591 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
592 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000593 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000594 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000595 }
596
597 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
598 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000599 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000600 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000601 }
602
603 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000604 /// @param n the array size. 0 represents a runtime-array
605 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000606 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000607 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000608 uint32_t n = 0,
609 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000610 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000611 }
612
613 /// @param source the Source of the node
614 /// @param subtype the array element type
615 /// @param n the array size. 0 represents a runtime-array
616 /// @param decos the optional decorations for the array
617 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000618 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000619 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000620 uint32_t n = 0,
621 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000622 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000623 }
624
Ben Claytonbab31972021-02-08 21:16:21 +0000625 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000626 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000627 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000628 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000629 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000630 ast::DecorationList decos;
631 if (stride) {
632 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
633 }
634 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000635 }
636
Ben Clayton0f88b312021-05-04 17:36:31 +0000637 /// @param source the Source of the node
638 /// @param subtype the array element type
639 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000640 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000641 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000642 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000643 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000644 uint32_t n,
645 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000646 ast::DecorationList decos;
647 if (stride) {
648 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
649 }
650 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000651 }
652
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000653 /// @return the tint AST type for an array of size `N` of type `T`
654 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000655 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000656 return array(Of<T>(), N);
657 }
658
Ben Claytonbab31972021-02-08 21:16:21 +0000659 /// @param stride the array stride
660 /// @return the tint AST type for an array of size `N` of type `T`
661 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000662 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000663 return array(Of<T>(), N, stride);
664 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000665
Ben Clayton0f88b312021-05-04 17:36:31 +0000666 /// Creates a type name
667 /// @param name the name
668 /// @returns the type name
669 template <typename NAME>
670 ast::TypeName* type_name(NAME&& name) const {
671 return builder->create<ast::TypeName>(
672 builder->Sym(std::forward<NAME>(name)));
673 }
674
675 /// Creates a type name
676 /// @param source the Source of the node
677 /// @param name the name
678 /// @returns the type name
679 template <typename NAME>
680 ast::TypeName* type_name(const Source& source, NAME&& name) const {
681 return builder->create<ast::TypeName>(
682 source, builder->Sym(std::forward<NAME>(name)));
683 }
684
Ben Clayton42d1e092021-02-02 14:29:15 +0000685 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000686 /// @param name the alias name
687 /// @param type the alias type
688 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000689 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000690 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000691 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000692 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000693 }
694
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000695 /// Creates an alias type
696 /// @param source the Source of the node
697 /// @param name the alias name
698 /// @param type the alias type
699 /// @returns the alias pointer
700 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000701 ast::Alias* alias(const Source& source,
702 NAME&& name,
703 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000704 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000705 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000706 }
707
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000708 /// @param type the type of the pointer
709 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000710 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000711 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000712 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000713 ast::StorageClass storage_class,
714 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000715 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000716 }
717
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000718 /// @param source the Source of the node
719 /// @param type the type of the pointer
720 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000721 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000722 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000723 ast::Pointer* pointer(const Source& source,
724 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000725 ast::StorageClass storage_class,
726 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000727 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000728 }
729
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000730 /// @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 `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000733 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000734 ast::Pointer* pointer(ast::StorageClass storage_class,
735 ast::Access access = ast::Access::kUndefined) const {
736 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000737 }
738
Ben Claytone204f272021-04-22 14:40:23 +0000739 /// @param kind the kind of sampler
740 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000741 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000742 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000743 }
744
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000745 /// @param source the Source of the node
746 /// @param kind the kind of sampler
747 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000748 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000749 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000750 }
751
Ben Claytone204f272021-04-22 14:40:23 +0000752 /// @param dims the dimensionality of the texture
753 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000754 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000755 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000756 }
757
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000758 /// @param source the Source of the node
759 /// @param dims the dimensionality of the texture
760 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000761 ast::DepthTexture* depth_texture(const Source& source,
762 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000763 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000764 }
765
Ben Claytone204f272021-04-22 14:40:23 +0000766 /// @param dims the dimensionality of the texture
767 /// @param subtype the texture subtype.
768 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000769 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
770 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000771 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000772 }
773
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000774 /// @param source the Source of the node
775 /// @param dims the dimensionality of the texture
776 /// @param subtype the texture subtype.
777 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000778 ast::SampledTexture* sampled_texture(const Source& source,
779 ast::TextureDimension dims,
780 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000781 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000782 }
783
Ben Claytone204f272021-04-22 14:40:23 +0000784 /// @param dims the dimensionality of the texture
785 /// @param subtype the texture subtype.
786 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000787 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
788 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000789 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000790 }
791
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000792 /// @param source the Source of the node
793 /// @param dims the dimensionality of the texture
794 /// @param subtype the texture subtype.
795 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000796 ast::MultisampledTexture* multisampled_texture(const Source& source,
797 ast::TextureDimension dims,
798 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000799 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000800 }
801
Ben Claytone204f272021-04-22 14:40:23 +0000802 /// @param dims the dimensionality of the texture
803 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000804 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000805 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000806 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000807 ast::ImageFormat format,
808 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000809 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000810 return builder->create<ast::StorageTexture>(dims, format, subtype,
811 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000812 }
813
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000814 /// @param source the Source of the node
815 /// @param dims the dimensionality of the texture
816 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000817 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000818 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000819 ast::StorageTexture* storage_texture(const Source& source,
820 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000821 ast::ImageFormat format,
822 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000823 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000824 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
825 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000826 }
827
Brandon Jones7b257692021-05-17 17:40:17 +0000828 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000829 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000830 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000831 }
832
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000833 /// @param source the Source of the node
834 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000835 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000836 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000837 }
838
Ben Clayton19b03192021-05-20 15:04:08 +0000839 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000840 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000841 /// @param type the type
842 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000843 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000844
Ben Clayton8758f102021-06-09 14:32:14 +0000845 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000846 /// @param type the type
847 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000848 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000849
Ben Claytonf5f311e2021-04-28 14:31:23 +0000850 /// The ProgramBuilder
851 ProgramBuilder* const builder;
852
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000853 private:
854 /// CToAST<T> is specialized for various `T` types and each specialization
855 /// contains a single static `get()` method for obtaining the corresponding
856 /// AST type for the C type `T`.
857 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000858 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000859 template <typename T>
860 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000861 };
862
863 //////////////////////////////////////////////////////////////////////////////
864 // AST helper methods
865 //////////////////////////////////////////////////////////////////////////////
866
James Price65ae64d2021-04-29 12:59:14 +0000867 /// @return a new unnamed symbol
868 Symbol Sym() { return Symbols().New(); }
869
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000870 /// @param name the symbol string
871 /// @return a Symbol with the given name
872 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
873
874 /// @param sym the symbol
875 /// @return `sym`
876 Symbol Sym(Symbol sym) { return sym; }
877
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000878 /// @param expr the expression
879 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000880 template <typename T>
881 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
882 return expr;
883 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000884
Ben Claytonb8ea5912021-04-19 16:52:42 +0000885 /// Passthrough for nullptr
886 /// @return nullptr
887 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
888
Ben Claytonfe0910f2021-05-17 15:51:47 +0000889 /// @param source the source information
890 /// @param symbol the identifier symbol
891 /// @return an ast::IdentifierExpression with the given symbol
892 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
893 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000894 }
895
Ben Clayton46d78d72021-02-10 21:34:26 +0000896 /// @param symbol the identifier symbol
897 /// @return an ast::IdentifierExpression with the given symbol
898 ast::IdentifierExpression* Expr(Symbol symbol) {
899 return create<ast::IdentifierExpression>(symbol);
900 }
901
Ben Claytonfe0910f2021-05-17 15:51:47 +0000902 /// @param source the source information
903 /// @param variable the AST variable
904 /// @return an ast::IdentifierExpression with the variable's symbol
905 ast::IdentifierExpression* Expr(const Source& source,
906 ast::Variable* variable) {
907 return create<ast::IdentifierExpression>(source, variable->symbol());
908 }
909
Ben Claytonb8ea5912021-04-19 16:52:42 +0000910 /// @param variable the AST variable
911 /// @return an ast::IdentifierExpression with the variable's symbol
912 ast::IdentifierExpression* Expr(ast::Variable* variable) {
913 return create<ast::IdentifierExpression>(variable->symbol());
914 }
915
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000916 /// @param source the source information
917 /// @param name the identifier name
918 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000919 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
920 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
921 }
922
923 /// @param name the identifier name
924 /// @return an ast::IdentifierExpression with the given name
925 ast::IdentifierExpression* Expr(const char* name) {
926 return create<ast::IdentifierExpression>(Symbols().Register(name));
927 }
928
929 /// @param source the source information
930 /// @param name the identifier name
931 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 ast::IdentifierExpression* Expr(const Source& source,
933 const std::string& name) {
934 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
935 }
936
937 /// @param name the identifier name
938 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000939 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000940 return create<ast::IdentifierExpression>(Symbols().Register(name));
941 }
942
Ben Claytonfe0910f2021-05-17 15:51:47 +0000943 /// @param source the source information
944 /// @param value the boolean value
945 /// @return a Scalar constructor for the given value
946 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
947 return create<ast::ScalarConstructorExpression>(source, Literal(value));
948 }
949
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000950 /// @param value the boolean value
951 /// @return a Scalar constructor for the given value
952 ast::ScalarConstructorExpression* Expr(bool value) {
953 return create<ast::ScalarConstructorExpression>(Literal(value));
954 }
955
Ben Claytonfe0910f2021-05-17 15:51:47 +0000956 /// @param source the source information
957 /// @param value the float value
958 /// @return a Scalar constructor for the given value
959 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
960 return create<ast::ScalarConstructorExpression>(source, Literal(value));
961 }
962
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000963 /// @param value the float value
964 /// @return a Scalar constructor for the given value
965 ast::ScalarConstructorExpression* Expr(f32 value) {
966 return create<ast::ScalarConstructorExpression>(Literal(value));
967 }
968
Ben Claytonfe0910f2021-05-17 15:51:47 +0000969 /// @param source the source information
970 /// @param value the integer value
971 /// @return a Scalar constructor for the given value
972 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
973 return create<ast::ScalarConstructorExpression>(source, Literal(value));
974 }
975
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000976 /// @param value the integer value
977 /// @return a Scalar constructor for the given value
978 ast::ScalarConstructorExpression* Expr(i32 value) {
979 return create<ast::ScalarConstructorExpression>(Literal(value));
980 }
981
Ben Claytonfe0910f2021-05-17 15:51:47 +0000982 /// @param source the source information
983 /// @param value the unsigned int value
984 /// @return a Scalar constructor for the given value
985 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
986 return create<ast::ScalarConstructorExpression>(source, Literal(value));
987 }
988
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000989 /// @param value the unsigned int value
990 /// @return a Scalar constructor for the given value
991 ast::ScalarConstructorExpression* Expr(u32 value) {
992 return create<ast::ScalarConstructorExpression>(Literal(value));
993 }
994
995 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
996 /// `list`.
997 /// @param list the list to append too
998 /// @param arg the arg to create
999 template <typename ARG>
1000 void Append(ast::ExpressionList& list, ARG&& arg) {
1001 list.emplace_back(Expr(std::forward<ARG>(arg)));
1002 }
1003
1004 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1005 /// then appends them to `list`.
1006 /// @param list the list to append too
1007 /// @param arg0 the first argument
1008 /// @param args the rest of the arguments
1009 template <typename ARG0, typename... ARGS>
1010 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1011 Append(list, std::forward<ARG0>(arg0));
1012 Append(list, std::forward<ARGS>(args)...);
1013 }
1014
1015 /// @return an empty list of expressions
1016 ast::ExpressionList ExprList() { return {}; }
1017
1018 /// @param args the list of expressions
1019 /// @return the list of expressions converted to `ast::Expression`s using
1020 /// `Expr()`,
1021 template <typename... ARGS>
1022 ast::ExpressionList ExprList(ARGS&&... args) {
1023 ast::ExpressionList list;
1024 list.reserve(sizeof...(args));
1025 Append(list, std::forward<ARGS>(args)...);
1026 return list;
1027 }
1028
1029 /// @param list the list of expressions
1030 /// @return `list`
1031 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1032
1033 /// @param val the boolan value
1034 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001035 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001036
1037 /// @param val the float value
1038 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001039 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001040
1041 /// @param val the unsigned int value
1042 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001043 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001044
1045 /// @param val the integer value
1046 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001047 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001048
1049 /// @param args the arguments for the type constructor
1050 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1051 /// of `args` converted to `ast::Expression`s using `Expr()`
1052 template <typename T, typename... ARGS>
1053 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001054 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001055 }
1056
1057 /// @param type the type to construct
1058 /// @param args the arguments for the constructor
1059 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1060 /// values `args`.
1061 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001062 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001063 return create<ast::TypeConstructorExpression>(
1064 type, ExprList(std::forward<ARGS>(args)...));
1065 }
1066
1067 /// @param args the arguments for the vector constructor
1068 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1069 /// `T`, constructed with the values `args`.
1070 template <typename T, typename... ARGS>
1071 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001072 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001073 }
1074
1075 /// @param args the arguments for the vector constructor
1076 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1077 /// `T`, constructed with the values `args`.
1078 template <typename T, typename... ARGS>
1079 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001080 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001081 }
1082
1083 /// @param args the arguments for the vector constructor
1084 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1085 /// `T`, constructed with the values `args`.
1086 template <typename T, typename... ARGS>
1087 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001088 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 }
1090
1091 /// @param args the arguments for the matrix constructor
1092 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1093 /// `T`, constructed with the values `args`.
1094 template <typename T, typename... ARGS>
1095 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001096 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001097 }
1098
1099 /// @param args the arguments for the matrix constructor
1100 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1101 /// `T`, constructed with the values `args`.
1102 template <typename T, typename... ARGS>
1103 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001104 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001105 }
1106
1107 /// @param args the arguments for the matrix constructor
1108 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1109 /// `T`, constructed with the values `args`.
1110 template <typename T, typename... ARGS>
1111 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001112 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001113 }
1114
1115 /// @param args the arguments for the matrix constructor
1116 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1117 /// `T`, constructed with the values `args`.
1118 template <typename T, typename... ARGS>
1119 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001120 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 }
1122
1123 /// @param args the arguments for the matrix constructor
1124 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1125 /// `T`, constructed with the values `args`.
1126 template <typename T, typename... ARGS>
1127 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001128 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001129 }
1130
1131 /// @param args the arguments for the matrix constructor
1132 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1133 /// `T`, constructed with the values `args`.
1134 template <typename T, typename... ARGS>
1135 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001136 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 }
1138
1139 /// @param args the arguments for the matrix constructor
1140 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1141 /// `T`, constructed with the values `args`.
1142 template <typename T, typename... ARGS>
1143 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001144 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001145 }
1146
1147 /// @param args the arguments for the matrix constructor
1148 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1149 /// `T`, constructed with the values `args`.
1150 template <typename T, typename... ARGS>
1151 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001152 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001153 }
1154
1155 /// @param args the arguments for the matrix constructor
1156 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1157 /// `T`, constructed with the values `args`.
1158 template <typename T, typename... ARGS>
1159 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001160 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001161 }
1162
1163 /// @param args the arguments for the array constructor
1164 /// @return an `ast::TypeConstructorExpression` of an array with element type
1165 /// `T`, constructed with the values `args`.
1166 template <typename T, int N = 0, typename... ARGS>
1167 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001168 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001169 }
1170
1171 /// @param subtype the array element type
1172 /// @param n the array size. 0 represents a runtime-array.
1173 /// @param args the arguments for the array constructor
1174 /// @return an `ast::TypeConstructorExpression` of an array with element type
1175 /// `subtype`, constructed with the values `args`.
1176 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001177 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001178 uint32_t n,
1179 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001180 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181 }
1182
1183 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001184 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001185 /// @param optional the optional variable settings.
1186 /// Can be any of the following, in any order:
1187 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001188 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001189 /// * ast::Expression* - specifies the variable's initializer expression
1190 /// * ast::DecorationList - specifies the variable's decorations
1191 /// Note that repeated arguments of the same type will use the last argument's
1192 /// value.
1193 /// @returns a `ast::Variable` with the given name, type and additional
1194 /// options
1195 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001196 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001197 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001198 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001199 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1200 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001201 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001202 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001203 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001204
1205 /// @param source the variable source
1206 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001208 /// @param optional the optional variable settings.
1209 /// Can be any of the following, in any order:
1210 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001211 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001212 /// * ast::Expression* - specifies the variable's initializer expression
1213 /// * ast::DecorationList - specifies the variable's decorations
1214 /// Note that repeated arguments of the same type will use the last argument's
1215 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001217 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001219 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001220 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001221 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001222 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1223 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001224 opts.storage, opts.access, type, false,
1225 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001226 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001229 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001230 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001232 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001233 template <typename NAME>
1234 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001235 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001236 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001237 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001238 return create<ast::Variable>(
1239 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1240 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001241 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001242
1243 /// @param source the variable source
1244 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001245 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001246 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001247 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001248 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001249 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001251 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001252 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001253 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001254 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001255 return create<ast::Variable>(
1256 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1257 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001258 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001259
James Pricedaf1f1c2021-04-08 22:15:48 +00001260 /// @param name the parameter name
1261 /// @param type the parameter type
1262 /// @param decorations optional parameter decorations
1263 /// @returns a constant `ast::Variable` with the given name and type
1264 template <typename NAME>
1265 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001266 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001267 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001268 return create<ast::Variable>(
1269 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1270 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001271 }
1272
1273 /// @param source the parameter source
1274 /// @param name the parameter name
1275 /// @param type the parameter type
1276 /// @param decorations optional parameter decorations
1277 /// @returns a constant `ast::Variable` with the given name and type
1278 template <typename NAME>
1279 ast::Variable* Param(const Source& source,
1280 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001281 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001282 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001283 return create<ast::Variable>(
1284 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1285 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001286 }
1287
Ben Clayton42708342021-04-21 17:55:12 +00001288 /// @param name the variable name
1289 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001290 /// @param optional the optional variable settings.
1291 /// Can be any of the following, in any order:
1292 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001293 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001294 /// * ast::Expression* - specifies the variable's initializer expression
1295 /// * ast::DecorationList - specifies the variable's decorations
1296 /// Note that repeated arguments of the same type will use the last argument's
1297 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001298 /// @returns a new `ast::Variable`, which is automatically registered as a
1299 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001300 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001301 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001302 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001303 OPTIONAL&&... optional) {
1304 auto* var = Var(std::forward<NAME>(name), type,
1305 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001306 AST().AddGlobalVariable(var);
1307 return var;
1308 }
1309
1310 /// @param source the variable source
1311 /// @param name the variable name
1312 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001313 /// @param optional the optional variable settings.
1314 /// Can be any of the following, in any order:
1315 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001316 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001317 /// * ast::Expression* - specifies the variable's initializer expression
1318 /// * ast::DecorationList - specifies the variable's decorations
1319 /// Note that repeated arguments of the same type will use the last argument's
1320 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001321 /// @returns a new `ast::Variable`, which is automatically registered as a
1322 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001323 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001324 ast::Variable* Global(const Source& source,
1325 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001326 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001327 OPTIONAL&&... optional) {
1328 auto* var = Var(source, std::forward<NAME>(name), type,
1329 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001330 AST().AddGlobalVariable(var);
1331 return var;
1332 }
1333
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001334 /// @param name the variable name
1335 /// @param type the variable type
1336 /// @param constructor constructor expression
1337 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001338 /// @returns a const `ast::Variable` constructed by calling Var() with the
1339 /// arguments of `args`, which is automatically registered as a global
1340 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001341 template <typename NAME>
1342 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001343 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001344 ast::Expression* constructor,
1345 ast::DecorationList decorations = {}) {
1346 auto* var = Const(std::forward<NAME>(name), type, constructor,
1347 std::move(decorations));
1348 AST().AddGlobalVariable(var);
1349 return var;
1350 }
1351
1352 /// @param source the variable source
1353 /// @param name the variable name
1354 /// @param type the variable type
1355 /// @param constructor constructor expression
1356 /// @param decorations optional variable decorations
1357 /// @returns a const `ast::Variable` constructed by calling Var() with the
1358 /// arguments of `args`, which is automatically registered as a global
1359 /// variable with the ast::Module.
1360 template <typename NAME>
1361 ast::Variable* GlobalConst(const Source& source,
1362 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001363 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001364 ast::Expression* constructor,
1365 ast::DecorationList decorations = {}) {
1366 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1367 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001368 AST().AddGlobalVariable(var);
1369 return var;
1370 }
1371
Ben Claytonfe0910f2021-05-17 15:51:47 +00001372 /// @param source the source information
1373 /// @param expr the expression to take the address of
1374 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1375 template <typename EXPR>
1376 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1377 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1378 Expr(std::forward<EXPR>(expr)));
1379 }
1380
1381 /// @param expr the expression to take the address of
1382 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1383 template <typename EXPR>
1384 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1385 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1386 Expr(std::forward<EXPR>(expr)));
1387 }
1388
1389 /// @param source the source information
1390 /// @param expr the expression to perform an indirection on
1391 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1392 template <typename EXPR>
1393 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1394 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1395 Expr(std::forward<EXPR>(expr)));
1396 }
1397
1398 /// @param expr the expression to perform an indirection on
1399 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1400 template <typename EXPR>
1401 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1402 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1403 Expr(std::forward<EXPR>(expr)));
1404 }
1405
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001406 /// @param func the function name
1407 /// @param args the function call arguments
1408 /// @returns a `ast::CallExpression` to the function `func`, with the
1409 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1410 template <typename NAME, typename... ARGS>
1411 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1412 return create<ast::CallExpression>(Expr(func),
1413 ExprList(std::forward<ARGS>(args)...));
1414 }
1415
1416 /// @param lhs the left hand argument to the addition operation
1417 /// @param rhs the right hand argument to the addition operation
1418 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1419 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001420 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001421 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1422 Expr(std::forward<LHS>(lhs)),
1423 Expr(std::forward<RHS>(rhs)));
1424 }
1425
1426 /// @param lhs the left hand argument to the subtraction operation
1427 /// @param rhs the right hand argument to the subtraction operation
1428 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1429 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001430 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001431 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1432 Expr(std::forward<LHS>(lhs)),
1433 Expr(std::forward<RHS>(rhs)));
1434 }
1435
1436 /// @param lhs the left hand argument to the multiplication operation
1437 /// @param rhs the right hand argument to the multiplication operation
1438 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1439 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001440 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001441 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1442 Expr(std::forward<LHS>(lhs)),
1443 Expr(std::forward<RHS>(rhs)));
1444 }
1445
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001446 /// @param source the source information
1447 /// @param lhs the left hand argument to the multiplication operation
1448 /// @param rhs the right hand argument to the multiplication operation
1449 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1450 template <typename LHS, typename RHS>
1451 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1452 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1453 Expr(std::forward<LHS>(lhs)),
1454 Expr(std::forward<RHS>(rhs)));
1455 }
1456
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001457 /// @param lhs the left hand argument to the division operation
1458 /// @param rhs the right hand argument to the division operation
1459 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1460 template <typename LHS, typename RHS>
1461 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1462 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1463 Expr(std::forward<LHS>(lhs)),
1464 Expr(std::forward<RHS>(rhs)));
1465 }
1466
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001467 /// @param arr the array argument for the array accessor expression
1468 /// @param idx the index argument for the array accessor expression
1469 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1470 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001471 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001472 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1473 Expr(std::forward<IDX>(idx)));
1474 }
1475
1476 /// @param obj the object for the member accessor expression
1477 /// @param idx the index argument for the array accessor expression
1478 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1479 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001480 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001481 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1482 Expr(std::forward<IDX>(idx)));
1483 }
1484
Ben Clayton42d1e092021-02-02 14:29:15 +00001485 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001486 /// @param val the offset value
1487 /// @returns the offset decoration pointer
1488 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1489 return create<ast::StructMemberOffsetDecoration>(source_, val);
1490 }
1491
Ben Claytond614dd52021-03-15 10:43:11 +00001492 /// Creates a ast::StructMemberSizeDecoration
1493 /// @param source the source information
1494 /// @param val the size value
1495 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001496 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1497 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001498 return create<ast::StructMemberSizeDecoration>(source, val);
1499 }
1500
1501 /// Creates a ast::StructMemberSizeDecoration
1502 /// @param val the size value
1503 /// @returns the size decoration pointer
1504 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1505 return create<ast::StructMemberSizeDecoration>(source_, val);
1506 }
1507
1508 /// Creates a ast::StructMemberAlignDecoration
1509 /// @param source the source information
1510 /// @param val the align value
1511 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001512 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1513 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001514 return create<ast::StructMemberAlignDecoration>(source, val);
1515 }
1516
1517 /// Creates a ast::StructMemberAlignDecoration
1518 /// @param val the align value
1519 /// @returns the align decoration pointer
1520 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1521 return create<ast::StructMemberAlignDecoration>(source_, val);
1522 }
1523
Ben Clayton42d1e092021-02-02 14:29:15 +00001524 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001525 /// @param source the source information
1526 /// @param name the function name
1527 /// @param params the function parameters
1528 /// @param type the function return type
1529 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001530 /// @param decorations the optional function decorations
1531 /// @param return_type_decorations the optional function return type
1532 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001533 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001534 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001535 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001536 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001537 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001538 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001539 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001540 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001541 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001542 auto* func =
1543 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1544 type, create<ast::BlockStatement>(body),
1545 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001546 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001547 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001548 }
1549
Ben Clayton42d1e092021-02-02 14:29:15 +00001550 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001551 /// @param name the function name
1552 /// @param params the function parameters
1553 /// @param type the function return type
1554 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001555 /// @param decorations the optional function decorations
1556 /// @param return_type_decorations the optional function return type
1557 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001558 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001559 template <typename NAME>
1560 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001561 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001562 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001563 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001564 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001565 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001566 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1567 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001568 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001569 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001570 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001571 }
1572
Ben Clayton49668bb2021-04-17 05:32:01 +00001573 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001574 /// @param source the source information
1575 /// @returns the return statement pointer
1576 ast::ReturnStatement* Return(const Source& source) {
1577 return create<ast::ReturnStatement>(source);
1578 }
1579
1580 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001581 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001582 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1583
1584 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001585 /// @param source the source information
1586 /// @param val the return value
1587 /// @returns the return statement pointer
1588 template <typename EXPR>
1589 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1590 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1591 }
1592
1593 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001594 /// @param val the return value
1595 /// @returns the return statement pointer
1596 template <typename EXPR>
1597 ast::ReturnStatement* Return(EXPR&& val) {
1598 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001599 }
1600
Ben Clayton950809f2021-06-09 14:32:14 +00001601 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001602 /// @param source the source information
1603 /// @param name the alias name
1604 /// @param type the alias target type
1605 /// @returns the alias type
1606 template <typename NAME>
1607 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1608 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001609 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001610 return out;
1611 }
1612
Ben Clayton950809f2021-06-09 14:32:14 +00001613 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001614 /// @param name the alias name
1615 /// @param type the alias target type
1616 /// @returns the alias type
1617 template <typename NAME>
1618 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1619 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001620 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001621 return out;
1622 }
1623
Ben Clayton950809f2021-06-09 14:32:14 +00001624 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001625 /// @param source the source information
1626 /// @param name the struct name
1627 /// @param members the struct members
1628 /// @param decorations the optional struct decorations
1629 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001630 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001631 ast::Struct* Structure(const Source& source,
1632 NAME&& name,
1633 ast::StructMemberList members,
1634 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001635 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001636 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001637 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001638 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001639 return type;
1640 }
1641
Ben Clayton950809f2021-06-09 14:32:14 +00001642 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001643 /// @param name the struct name
1644 /// @param members the struct members
1645 /// @param decorations the optional struct decorations
1646 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001647 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001648 ast::Struct* Structure(NAME&& name,
1649 ast::StructMemberList members,
1650 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001651 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001652 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001653 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001654 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001655 return type;
1656 }
1657
Ben Clayton42d1e092021-02-02 14:29:15 +00001658 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001659 /// @param source the source information
1660 /// @param name the struct member name
1661 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001662 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001663 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001664 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001665 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001666 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001667 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001668 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001669 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1670 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001671 }
1672
Ben Clayton42d1e092021-02-02 14:29:15 +00001673 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001674 /// @param name the struct member name
1675 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001676 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001677 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001678 template <typename NAME>
1679 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001680 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001681 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001682 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1683 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001684 }
1685
Ben Claytonbab31972021-02-08 21:16:21 +00001686 /// Creates a ast::StructMember with the given byte offset
1687 /// @param offset the offset to use in the StructMemberOffsetDecoration
1688 /// @param name the struct member name
1689 /// @param type the struct member type
1690 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001691 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001692 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001693 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001694 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001695 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001696 create<ast::StructMemberOffsetDecoration>(offset),
1697 });
1698 }
1699
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001700 /// Creates a ast::BlockStatement with input statements
1701 /// @param statements statements of block
1702 /// @returns the block statement pointer
1703 template <typename... Statements>
1704 ast::BlockStatement* Block(Statements&&... statements) {
1705 return create<ast::BlockStatement>(
1706 ast::StatementList{std::forward<Statements>(statements)...});
1707 }
1708
1709 /// Creates a ast::ElseStatement with input condition and body
1710 /// @param condition the else condition expression
1711 /// @param body the else body
1712 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001713 template <typename CONDITION>
1714 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1715 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1716 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001717 }
1718
1719 /// Creates a ast::IfStatement with input condition, body, and optional
1720 /// variadic else statements
1721 /// @param condition the if statement condition expression
1722 /// @param body the if statement body
1723 /// @param elseStatements optional variadic else statements
1724 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001725 template <typename CONDITION, typename... ELSE_STATEMENTS>
1726 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001727 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001728 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001729 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001730 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001731 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001732 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001733 }
1734
1735 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001736 /// @param source the source information
1737 /// @param lhs the left hand side expression initializer
1738 /// @param rhs the right hand side expression initializer
1739 /// @returns the assignment statement pointer
1740 template <typename LhsExpressionInit, typename RhsExpressionInit>
1741 ast::AssignmentStatement* Assign(const Source& source,
1742 LhsExpressionInit&& lhs,
1743 RhsExpressionInit&& rhs) {
1744 return create<ast::AssignmentStatement>(
1745 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1746 Expr(std::forward<RhsExpressionInit>(rhs)));
1747 }
1748
1749 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001750 /// @param lhs the left hand side expression initializer
1751 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001752 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001753 template <typename LhsExpressionInit, typename RhsExpressionInit>
1754 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1755 RhsExpressionInit&& rhs) {
1756 return create<ast::AssignmentStatement>(
1757 Expr(std::forward<LhsExpressionInit>(lhs)),
1758 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001759 }
1760
1761 /// Creates a ast::LoopStatement with input body and optional continuing
1762 /// @param body the loop body
1763 /// @param continuing the optional continuing block
1764 /// @returns the loop statement pointer
1765 ast::LoopStatement* Loop(ast::BlockStatement* body,
1766 ast::BlockStatement* continuing = nullptr) {
1767 return create<ast::LoopStatement>(body, continuing);
1768 }
1769
1770 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001771 /// @param source the source information
1772 /// @param var the variable to wrap in a decl statement
1773 /// @returns the variable decl statement pointer
1774 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1775 return create<ast::VariableDeclStatement>(source, var);
1776 }
1777
1778 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001779 /// @param var the variable to wrap in a decl statement
1780 /// @returns the variable decl statement pointer
1781 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1782 return create<ast::VariableDeclStatement>(var);
1783 }
1784
Antonio Maioranocea744d2021-03-25 12:55:27 +00001785 /// Creates a ast::SwitchStatement with input expression and cases
1786 /// @param condition the condition expression initializer
1787 /// @param cases case statements
1788 /// @returns the switch statement pointer
1789 template <typename ExpressionInit, typename... Cases>
1790 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1791 return create<ast::SwitchStatement>(
1792 Expr(std::forward<ExpressionInit>(condition)),
1793 ast::CaseStatementList{std::forward<Cases>(cases)...});
1794 }
1795
1796 /// Creates a ast::CaseStatement with input list of selectors, and body
1797 /// @param selectors list of selectors
1798 /// @param body the case body
1799 /// @returns the case statement pointer
1800 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1801 ast::BlockStatement* body = nullptr) {
1802 return create<ast::CaseStatement>(std::move(selectors),
1803 body ? body : Block());
1804 }
1805
1806 /// Convenient overload that takes a single selector
1807 /// @param selector a single case selector
1808 /// @param body the case body
1809 /// @returns the case statement pointer
1810 ast::CaseStatement* Case(ast::IntLiteral* selector,
1811 ast::BlockStatement* body = nullptr) {
1812 return Case(ast::CaseSelectorList{selector}, body);
1813 }
1814
1815 /// Convenience function that creates a 'default' ast::CaseStatement
1816 /// @param body the case body
1817 /// @returns the case statement pointer
1818 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1819 return Case(ast::CaseSelectorList{}, body);
1820 }
1821
James Price68f558f2021-04-06 15:51:47 +00001822 /// Creates an ast::BuiltinDecoration
1823 /// @param source the source information
1824 /// @param builtin the builtin value
1825 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001826 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001827 return create<ast::BuiltinDecoration>(source, builtin);
1828 }
1829
1830 /// Creates an ast::BuiltinDecoration
1831 /// @param builtin the builtin value
1832 /// @returns the builtin decoration pointer
1833 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1834 return create<ast::BuiltinDecoration>(source_, builtin);
1835 }
1836
1837 /// Creates an ast::LocationDecoration
1838 /// @param source the source information
1839 /// @param location the location value
1840 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001841 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001842 return create<ast::LocationDecoration>(source, location);
1843 }
1844
1845 /// Creates an ast::LocationDecoration
1846 /// @param location the location value
1847 /// @returns the location decoration pointer
1848 ast::LocationDecoration* Location(uint32_t location) {
1849 return create<ast::LocationDecoration>(source_, location);
1850 }
1851
James Pricef2f3bfc2021-05-13 20:32:32 +00001852 /// Creates an ast::OverrideDecoration with a specific constant ID
1853 /// @param source the source information
1854 /// @param id the id value
1855 /// @returns the override decoration pointer
1856 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1857 return create<ast::OverrideDecoration>(source, id);
1858 }
1859
1860 /// Creates an ast::OverrideDecoration with a specific constant ID
1861 /// @param id the optional id value
1862 /// @returns the override decoration pointer
1863 ast::OverrideDecoration* Override(uint32_t id) {
1864 return Override(source_, id);
1865 }
1866
1867 /// Creates an ast::OverrideDecoration without a constant ID
1868 /// @param source the source information
1869 /// @returns the override decoration pointer
1870 ast::OverrideDecoration* Override(const Source& source) {
1871 return create<ast::OverrideDecoration>(source);
1872 }
1873
1874 /// Creates an ast::OverrideDecoration without a constant ID
1875 /// @returns the override decoration pointer
1876 ast::OverrideDecoration* Override() { return Override(source_); }
1877
James Price68f558f2021-04-06 15:51:47 +00001878 /// Creates an ast::StageDecoration
1879 /// @param source the source information
1880 /// @param stage the pipeline stage
1881 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001882 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001883 return create<ast::StageDecoration>(source, stage);
1884 }
1885
1886 /// Creates an ast::StageDecoration
1887 /// @param stage the pipeline stage
1888 /// @returns the stage decoration pointer
1889 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1890 return create<ast::StageDecoration>(source_, stage);
1891 }
1892
James Price70f80bb2021-05-19 13:40:08 +00001893 /// Creates an ast::WorkgroupDecoration
1894 /// @param x the x dimension expression
1895 /// @returns the workgroup decoration pointer
1896 template <typename EXPR_X>
1897 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1898 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1899 }
1900
1901 /// Creates an ast::WorkgroupDecoration
1902 /// @param x the x dimension expression
1903 /// @param y the y dimension expression
1904 /// @returns the workgroup decoration pointer
1905 template <typename EXPR_X, typename EXPR_Y>
1906 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1907 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1908 nullptr);
1909 }
1910
1911 /// Creates an ast::WorkgroupDecoration
1912 /// @param x the x dimension expression
1913 /// @param y the y dimension expression
1914 /// @param z the z dimension expression
1915 /// @returns the workgroup decoration pointer
1916 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1917 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1918 return create<ast::WorkgroupDecoration>(
1919 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1920 Expr(std::forward<EXPR_Z>(z)));
1921 }
1922
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001923 /// Sets the current builder source to `src`
1924 /// @param src the Source used for future create() calls
1925 void SetSource(const Source& src) {
1926 AssertNotMoved();
1927 source_ = src;
1928 }
1929
1930 /// Sets the current builder source to `loc`
1931 /// @param loc the Source used for future create() calls
1932 void SetSource(const Source::Location& loc) {
1933 AssertNotMoved();
1934 source_ = Source(loc);
1935 }
1936
Ben Clayton33352542021-01-29 16:43:41 +00001937 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001938 /// @note As the Resolver is run when the Program is built, this will only be
1939 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001940 /// @param expr the AST expression
1941 /// @return the resolved semantic type for the expression, or nullptr if the
1942 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001943 sem::Type* TypeOf(const ast::Expression* expr) const;
1944
Ben Claytonfe0910f2021-05-17 15:51:47 +00001945 /// Helper for returning the resolved semantic type of the variable `var`.
1946 /// @note As the Resolver is run when the Program is built, this will only be
1947 /// useful for the Resolver itself and tests that use their own Resolver.
1948 /// @param var the AST variable
1949 /// @return the resolved semantic type for the variable, or nullptr if the
1950 /// variable has no resolved type.
1951 sem::Type* TypeOf(const ast::Variable* var) const;
1952
Ben Claytonfbec46f2021-04-30 20:20:19 +00001953 /// Helper for returning the resolved semantic type of the AST type `type`.
1954 /// @note As the Resolver is run when the Program is built, this will only be
1955 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00001956 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00001957 /// @return the resolved semantic type for the type, or nullptr if the type
1958 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00001959 const sem::Type* TypeOf(const ast::Type* type) const;
1960
1961 /// Helper for returning the resolved semantic type of the AST type
1962 /// declaration `type_decl`.
1963 /// @note As the Resolver is run when the Program is built, this will only be
1964 /// useful for the Resolver itself and tests that use their own Resolver.
1965 /// @param type_decl the AST type declaration
1966 /// @return the resolved semantic type for the type declaration, or nullptr if
1967 /// the type declaration has no resolved type.
1968 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00001969
Ben Clayton169512e2021-04-17 05:52:11 +00001970 /// Wraps the ast::Literal in a statement. This is used by tests that
1971 /// construct a partial AST and require the Resolver to reach these
1972 /// nodes.
1973 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1974 /// @return the ast::Statement that wraps the ast::Statement
1975 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001976 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001977 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001978 /// nodes.
1979 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1980 /// @return the ast::Statement that wraps the ast::Expression
1981 ast::Statement* WrapInStatement(ast::Expression* expr);
1982 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001983 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001984 /// these nodes.
1985 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1986 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1987 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1988 /// Returns the statement argument. Used as a passthrough-overload by
1989 /// WrapInFunction().
1990 /// @param stmt the ast::Statement
1991 /// @return `stmt`
1992 ast::Statement* WrapInStatement(ast::Statement* stmt);
1993 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001994 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001995 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001996 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001997 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001998 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001999 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002000 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002001 }
2002 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002003 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002004 /// @returns the function
2005 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002006
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002007 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002008 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002009
2010 protected:
2011 /// Asserts that the builder has not been moved.
2012 void AssertNotMoved() const;
2013
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002014 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002015 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002016 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002017 ASTNodeAllocator ast_nodes_;
2018 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002019 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002020 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002021 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002022 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002023
2024 /// The source to use when creating AST nodes without providing a Source as
2025 /// the first argument.
2026 Source source_;
2027
Ben Clayton5f0ea112021-03-09 10:54:37 +00002028 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002029 /// program when built.
2030 bool resolve_on_build_ = true;
2031
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002032 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2033 bool moved_ = false;
2034};
2035
2036//! @cond Doxygen_Suppress
2037// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2038template <>
2039struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002040 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002041 return t->i32();
2042 }
2043};
2044template <>
2045struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002046 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002047 return t->u32();
2048 }
2049};
2050template <>
2051struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002052 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002053 return t->f32();
2054 }
2055};
2056template <>
2057struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002058 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002059 return t->bool_();
2060 }
2061};
2062template <>
2063struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002064 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002065 return t->void_();
2066 }
2067};
2068//! @endcond
2069
Ben Clayton917b14b2021-04-19 16:50:23 +00002070/// @param builder the ProgramBuilder
2071/// @returns the ProgramID of the ProgramBuilder
2072inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2073 return builder->ID();
2074}
2075
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002076} // namespace tint
2077
2078#endif // SRC_PROGRAM_BUILDER_H_