blob: 94f410cffdc02af685eb367a3d107729618d9fde [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"
41#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000042#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000043#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/ast/scalar_constructor_expression.h"
45#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000046#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000047#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000048#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000049#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000050#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000051#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000052#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053#include "src/ast/type_constructor_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000054#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000055#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000056#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000057#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000058#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000059#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000060#include "src/program_id.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000061#include "src/sem/access_control_type.h"
62#include "src/sem/alias_type.h"
63#include "src/sem/array_type.h"
64#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000065#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000066#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000067#include "src/sem/f32_type.h"
68#include "src/sem/i32_type.h"
69#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000070#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000071#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/sampled_texture_type.h"
73#include "src/sem/storage_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000074#include "src/sem/struct_type.h"
75#include "src/sem/u32_type.h"
76#include "src/sem/vector_type.h"
77#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000078#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000079
80namespace tint {
81
Ben Clayton401b96b2021-02-03 17:19:59 +000082// Forward declarations
83namespace ast {
84class VariableDeclStatement;
85} // namespace ast
86
Ben Claytona6b9a8e2021-01-26 16:57:10 +000087class CloneContext;
88
89/// ProgramBuilder is a mutable builder for a Program.
90/// To construct a Program, populate the builder and then `std::move` it to a
91/// Program.
92class ProgramBuilder {
93 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000094 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
95 using ASTNodeAllocator = BlockAllocator<ast::Node>;
96
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000097 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
98 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000099
100 /// `i32` is a type alias to `int`.
101 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
102 /// WGSL syntax.
103 /// Note: this is intentionally not aliased to uint32_t as we want integer
104 /// literals passed to the builder to match WGSL's integer literal types.
105 using i32 = decltype(1);
106 /// `u32` is a type alias to `unsigned int`.
107 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
108 /// WGSL syntax.
109 /// Note: this is intentionally not aliased to uint32_t as we want integer
110 /// literals passed to the builder to match WGSL's integer literal types.
111 using u32 = decltype(1u);
112 /// `f32` is a type alias to `float`
113 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
114 /// WGSL syntax.
115 using f32 = float;
116
117 /// Constructor
118 ProgramBuilder();
119
120 /// Move constructor
121 /// @param rhs the builder to move
122 ProgramBuilder(ProgramBuilder&& rhs);
123
124 /// Destructor
125 virtual ~ProgramBuilder();
126
127 /// Move assignment operator
128 /// @param rhs the builder to move
129 /// @return this builder
130 ProgramBuilder& operator=(ProgramBuilder&& rhs);
131
Ben Claytone43c8302021-01-29 11:59:32 +0000132 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
133 /// making a deep clone of the Program contents.
134 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
135 /// existing immutable program.
136 /// As the returned ProgramBuilder wraps `program`, `program` must not be
137 /// destructed or assigned while using the returned ProgramBuilder.
138 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
139 /// function. See crbug.com/tint/460.
140 /// @param program the immutable Program to wrap
141 /// @return the ProgramBuilder that wraps `program`
142 static ProgramBuilder Wrap(const Program* program);
143
Ben Claytone6995de2021-04-13 23:27:27 +0000144 /// @returns the unique identifier for this program
145 ProgramID ID() const { return id_; }
146
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000147 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000148 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000149 AssertNotMoved();
150 return types_;
151 }
152
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000153 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000154 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 AssertNotMoved();
156 return types_;
157 }
158
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000159 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000160 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000162 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000163 }
164
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000165 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000166 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000168 return ast_nodes_;
169 }
170
171 /// @returns a reference to the program's semantic nodes storage
172 SemNodeAllocator& SemNodes() {
173 AssertNotMoved();
174 return sem_nodes_;
175 }
176
177 /// @returns a reference to the program's semantic nodes storage
178 const SemNodeAllocator& SemNodes() const {
179 AssertNotMoved();
180 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000181 }
182
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000183 /// @returns a reference to the program's AST root Module
184 ast::Module& AST() {
185 AssertNotMoved();
186 return *ast_;
187 }
188
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000189 /// @returns a reference to the program's AST root Module
190 const ast::Module& AST() const {
191 AssertNotMoved();
192 return *ast_;
193 }
194
195 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000196 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000197 AssertNotMoved();
198 return sem_;
199 }
200
201 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000202 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000203 AssertNotMoved();
204 return sem_;
205 }
206
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000207 /// @returns a reference to the program's SymbolTable
208 SymbolTable& Symbols() {
209 AssertNotMoved();
210 return symbols_;
211 }
212
Ben Clayton708dc2d2021-01-29 11:22:40 +0000213 /// @returns a reference to the program's SymbolTable
214 const SymbolTable& Symbols() const {
215 AssertNotMoved();
216 return symbols_;
217 }
218
Ben Clayton844217f2021-01-27 18:49:05 +0000219 /// @returns a reference to the program's diagnostics
220 diag::List& Diagnostics() {
221 AssertNotMoved();
222 return diagnostics_;
223 }
224
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000225 /// @returns a reference to the program's diagnostics
226 const diag::List& Diagnostics() const {
227 AssertNotMoved();
228 return diagnostics_;
229 }
230
Ben Clayton5f0ea112021-03-09 10:54:37 +0000231 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000232 /// @param enable the new flag value (defaults to true)
233 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
234
Ben Clayton5f0ea112021-03-09 10:54:37 +0000235 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000236 /// built.
237 bool ResolveOnBuild() const { return resolve_on_build_; }
238
Ben Clayton844217f2021-01-27 18:49:05 +0000239 /// @returns true if the program has no error diagnostics and is not missing
240 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000241 bool IsValid() const;
242
Ben Clayton708dc2d2021-01-29 11:22:40 +0000243 /// Writes a representation of the node to the output stream
244 /// @note unlike str(), to_str() does not automatically demangle the string.
245 /// @param node the AST node
246 /// @param out the stream to write to
247 /// @param indent number of spaces to indent the node when writing
248 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
249 node->to_str(Sem(), out, indent);
250 }
251
252 /// Returns a demangled, string representation of `node`.
253 /// @param node the AST node
254 /// @returns a string representation of the node
255 std::string str(const ast::Node* node) const;
256
Ben Clayton7fdfff12021-01-29 15:17:30 +0000257 /// Creates a new ast::Node owned by the ProgramBuilder. When the
258 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000259 /// @param source the Source of the node
260 /// @param args the arguments to pass to the type constructor
261 /// @returns the node pointer
262 template <typename T, typename... ARGS>
263 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
264 ARGS&&... args) {
265 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000266 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000267 }
268
Ben Clayton7fdfff12021-01-29 15:17:30 +0000269 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
270 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000271 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000272 /// When the ProgramBuilder is destructed, the ast::Node will also be
273 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000274 /// @returns the node pointer
275 template <typename T>
276 traits::EnableIfIsType<T, ast::Node>* create() {
277 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000278 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000279 }
280
Ben Clayton7fdfff12021-01-29 15:17:30 +0000281 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
282 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000283 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000284 /// When the ProgramBuilder is destructed, the ast::Node will also be
285 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000286 /// @param arg0 the first arguments to pass to the type constructor
287 /// @param args the remaining arguments to pass to the type constructor
288 /// @returns the node pointer
289 template <typename T, typename ARG0, typename... ARGS>
290 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
291 traits::IsTypeOrDerived<T, ast::Node>::value &&
292 !traits::IsTypeOrDerived<ARG0, Source>::value,
293 T>*
294 create(ARG0&& arg0, ARGS&&... args) {
295 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000296 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000297 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000298 }
299
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000300 /// Creates a new sem::Node owned by the ProgramBuilder.
301 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000302 /// destructed.
303 /// @param args the arguments to pass to the type constructor
304 /// @returns the node pointer
305 template <typename T, typename... ARGS>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000306 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000307 AssertNotMoved();
308 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
309 }
310
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000311 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000312 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
313 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000314 /// Types are unique (de-aliased), and so calling create() for the same `T`
315 /// and arguments will return the same pointer.
316 /// @warning Use this method to acquire a type only if all of its type
317 /// information is provided in the constructor arguments `args`.<br>
318 /// If the type requires additional configuration after construction that
319 /// affect its fundamental type, build the type with `std::make_unique`, make
320 /// any necessary alterations and then call unique_type() instead.
321 /// @param args the arguments to pass to the type constructor
322 /// @returns the de-aliased type pointer
323 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000324 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
325 static_assert(std::is_base_of<sem::Type, T>::value,
326 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000327 AssertNotMoved();
328 return types_.Get<T>(std::forward<ARGS>(args)...);
329 }
330
331 /// Marks this builder as moved, preventing any further use of the builder.
332 void MarkAsMoved();
333
334 //////////////////////////////////////////////////////////////////////////////
335 // TypesBuilder
336 //////////////////////////////////////////////////////////////////////////////
337
338 /// TypesBuilder holds basic `tint` types and methods for constructing
339 /// complex types.
340 class TypesBuilder {
341 public:
342 /// Constructor
343 /// @param builder the program builder
344 explicit TypesBuilder(ProgramBuilder* builder);
345
346 /// @return the tint AST type for the C type `T`.
347 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000348 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000349 return CToAST<T>::get(this);
350 }
351
352 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000353 typ::Bool bool_() const {
354 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
355 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000356
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000357 /// @param source the Source of the node
358 /// @returns a boolean type
359 typ::Bool bool_(const Source& source) const {
360 return {builder->create<ast::Bool>(source), builder->create<sem::Bool>()};
361 }
362
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000363 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000364 typ::F32 f32() const {
365 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
366 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000367
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000368 /// @param source the Source of the node
369 /// @returns a f32 type
370 typ::F32 f32(const Source& source) const {
371 return {builder->create<ast::F32>(source), builder->create<sem::F32>()};
372 }
373
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000374 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000375 typ::I32 i32() const {
376 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
377 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000378
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000379 /// @param source the Source of the node
380 /// @returns a i32 type
381 typ::I32 i32(const Source& source) const {
382 return {builder->create<ast::I32>(source), builder->create<sem::I32>()};
383 }
384
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000385 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000386 typ::U32 u32() const {
387 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
388 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000389
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000390 /// @param source the Source of the node
391 /// @returns a u32 type
392 typ::U32 u32(const Source& source) const {
393 return {builder->create<ast::U32>(source), builder->create<sem::U32>()};
394 }
395
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000396 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000397 typ::Void void_() const {
398 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
399 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000400
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000401 /// @param source the Source of the node
402 /// @returns a void type
403 typ::Void void_(const Source& source) const {
404 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
405 }
406
Antonio Maiorano25436862021-04-13 13:32:33 +0000407 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000408 /// @param n vector width in elements
409 /// @return the tint AST type for a `n`-element vector of `type`.
410 typ::Vector vec(typ::Type type, uint32_t n) const {
411 return {builder->create<ast::Vector>(type, n),
412 builder->create<sem::Vector>(type, n)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000413 }
414
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000415 /// @param source the Source of the node
416 /// @param type vector subtype
417 /// @param n vector width in elements
418 /// @return the tint AST type for a `n`-element vector of `type`.
419 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
420 return {builder->create<ast::Vector>(source, type, n),
421 builder->create<sem::Vector>(type, n)};
422 }
423
Antonio Maiorano25436862021-04-13 13:32:33 +0000424 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000425 /// @return the tint AST type for a 2-element vector of `type`.
426 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
427
428 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000429 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000430 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000431
432 /// @param type vector subtype
433 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000434 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
435
436 /// @param n vector width in elements
437 /// @return the tint AST type for a `n`-element vector of `type`.
438 template <typename T>
439 typ::Vector vec(uint32_t n) const {
440 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000441 }
442
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000443 /// @return the tint AST type for a 2-element vector of the C type `T`.
444 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000445 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000446 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000447 }
448
449 /// @return the tint AST type for a 3-element vector of the C type `T`.
450 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000451 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000452 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000453 }
454
455 /// @return the tint AST type for a 4-element vector of the C type `T`.
456 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000457 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000458 return vec4(Of<T>());
459 }
460
461 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000462 /// @param columns number of columns for the matrix
463 /// @param rows number of rows for the matrix
464 /// @return the tint AST type for a matrix of `type`
465 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
466 return {builder->create<ast::Matrix>(type, rows, columns),
467 builder->create<sem::Matrix>(type, rows, columns)};
468 }
469
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000470 /// @param source the Source of the node
471 /// @param type matrix subtype
472 /// @param columns number of columns for the matrix
473 /// @param rows number of rows for the matrix
474 /// @return the tint AST type for a matrix of `type`
475 typ::Matrix mat(const Source& source,
476 typ::Type type,
477 uint32_t columns,
478 uint32_t rows) const {
479 return {builder->create<ast::Matrix>(source, type, rows, columns),
480 builder->create<sem::Matrix>(type, rows, columns)};
481 }
482
Ben Claytone204f272021-04-22 14:40:23 +0000483 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000484 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000485 typ::Matrix mat2x2(typ::Type type) const {
486 return {builder->create<ast::Matrix>(type, 2u, 2u),
487 builder->create<sem::Matrix>(type, 2u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000488 }
489
490 /// @param type matrix subtype
491 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000492 typ::Matrix mat2x3(typ::Type type) const {
493 return {builder->create<ast::Matrix>(type, 3u, 2u),
494 builder->create<sem::Matrix>(type, 3u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000495 }
496
497 /// @param type matrix subtype
498 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000499 typ::Matrix mat2x4(typ::Type type) const {
500 return {builder->create<ast::Matrix>(type, 4u, 2u),
501 builder->create<sem::Matrix>(type, 4u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000502 }
503
504 /// @param type matrix subtype
505 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000506 typ::Matrix mat3x2(typ::Type type) const {
507 return {builder->create<ast::Matrix>(type, 2u, 3u),
508 builder->create<sem::Matrix>(type, 2u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000509 }
510
511 /// @param type matrix subtype
512 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000513 typ::Matrix mat3x3(typ::Type type) const {
514 return {builder->create<ast::Matrix>(type, 3u, 3u),
515 builder->create<sem::Matrix>(type, 3u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000516 }
517
518 /// @param type matrix subtype
519 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000520 typ::Matrix mat3x4(typ::Type type) const {
521 return {builder->create<ast::Matrix>(type, 4u, 3u),
522 builder->create<sem::Matrix>(type, 4u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000523 }
524
525 /// @param type matrix subtype
526 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000527 typ::Matrix mat4x2(typ::Type type) const {
528 return {builder->create<ast::Matrix>(type, 2u, 4u),
529 builder->create<sem::Matrix>(type, 2u, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000530 }
531
532 /// @param type matrix subtype
533 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000534 typ::Matrix mat4x3(typ::Type type) const {
535 return {builder->create<ast::Matrix>(type, 3u, 4u),
536 builder->create<sem::Matrix>(type, 3u, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000537 }
538
539 /// @param type matrix subtype
540 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000541 typ::Matrix mat4x4(typ::Type type) const {
542 return {builder->create<ast::Matrix>(type, 4u, 4u),
543 builder->create<sem::Matrix>(type, 4u, 4u)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000544 }
545
Ben Claytone204f272021-04-22 14:40:23 +0000546 /// @param columns number of columns for the matrix
547 /// @param rows number of rows for the matrix
548 /// @return the tint AST type for a matrix of `type`
549 template <typename T>
550 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
551 return mat(Of<T>(), columns, rows);
552 }
553
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000554 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
555 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000556 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000557 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000558 }
559
560 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
561 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000562 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000563 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000564 }
565
566 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
567 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000568 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000569 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000570 }
571
572 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
573 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000574 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000575 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000576 }
577
578 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
579 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000580 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000581 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000582 }
583
584 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
585 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000586 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000587 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000588 }
589
590 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
591 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000592 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000593 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000594 }
595
596 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
597 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000598 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000599 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000600 }
601
602 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
603 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000604 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000605 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000606 }
607
608 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000609 /// @param n the array size. 0 represents a runtime-array
610 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000611 /// @return the tint AST type for a array of size `n` of type `T`
Ben Claytone204f272021-04-22 14:40:23 +0000612 typ::Array array(typ::Type subtype,
613 uint32_t n = 0,
614 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000615 subtype = MaybeCreateTypename(subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000616 return {builder->create<ast::Array>(subtype, n, decos),
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000617 builder->create<sem::ArrayType>(subtype, n, std::move(decos))};
618 }
619
620 /// @param source the Source of the node
621 /// @param subtype the array element type
622 /// @param n the array size. 0 represents a runtime-array
623 /// @param decos the optional decorations for the array
624 /// @return the tint AST type for a array of size `n` of type `T`
625 typ::Array array(const Source& source,
626 typ::Type subtype,
627 uint32_t n = 0,
628 ast::DecorationList decos = {}) const {
629 subtype = MaybeCreateTypename(subtype);
630 return {builder->create<ast::Array>(source, subtype, n, decos),
631 builder->create<sem::ArrayType>(subtype, n, std::move(decos))};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000632 }
633
Ben Claytonbab31972021-02-08 21:16:21 +0000634 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000635 /// @param n the array size. 0 represents a runtime-array
636 /// @param stride the array stride
Ben Claytonbab31972021-02-08 21:16:21 +0000637 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton7241a502021-04-22 14:32:53 +0000638 typ::Array array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000639 subtype = MaybeCreateTypename(subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000640 return array(subtype, n,
641 {builder->create<ast::StrideDecoration>(stride)});
Ben Claytonbab31972021-02-08 21:16:21 +0000642 }
643
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000644 /// @return the tint AST type for an array of size `N` of type `T`
645 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000646 typ::Array array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000647 return array(Of<T>(), N);
648 }
649
Ben Claytonbab31972021-02-08 21:16:21 +0000650 /// @param stride the array stride
651 /// @return the tint AST type for an array of size `N` of type `T`
652 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000653 typ::Array array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000654 return array(Of<T>(), N, stride);
655 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000656
Ben Clayton42d1e092021-02-02 14:29:15 +0000657 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000658 /// @param name the alias name
659 /// @param type the alias type
660 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000661 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +0000662 typ::Alias alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000663 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000664 auto sym = builder->Sym(std::forward<NAME>(name));
665 return {
666 builder->create<ast::Alias>(sym, type),
667 builder->create<sem::Alias>(sym, type),
668 };
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000669 }
670
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000671 /// Creates an alias type
672 /// @param source the Source of the node
673 /// @param name the alias name
674 /// @param type the alias type
675 /// @returns the alias pointer
676 template <typename NAME>
677 typ::Alias alias(const Source& source, NAME&& name, typ::Type type) const {
678 type = MaybeCreateTypename(type);
679 auto sym = builder->Sym(std::forward<NAME>(name));
680 return {
681 builder->create<ast::Alias>(source, sym, type),
682 builder->create<sem::Alias>(sym, type),
683 };
684 }
685
Ben Clayton4db10dd2021-04-16 08:47:14 +0000686 /// Creates an access control qualifier type
687 /// @param access the access control
688 /// @param type the inner type
689 /// @returns the access control qualifier type
Ben Claytone204f272021-04-22 14:40:23 +0000690 typ::AccessControl access(ast::AccessControl::Access access,
691 typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000692 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000693 return {builder->create<ast::AccessControl>(access, type),
694 builder->create<sem::AccessControl>(access, type)};
Ben Clayton4db10dd2021-04-16 08:47:14 +0000695 }
696
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000697 /// Creates an access control qualifier type
698 /// @param source the Source of the node
699 /// @param access the access control
700 /// @param type the inner type
701 /// @returns the access control qualifier type
702 typ::AccessControl access(const Source& source,
703 ast::AccessControl::Access access,
704 typ::Type type) const {
705 type = MaybeCreateTypename(type);
706 return {builder->create<ast::AccessControl>(source, access, type),
707 builder->create<sem::AccessControl>(access, type)};
708 }
709
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000710 /// @param type the type of the pointer
711 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000712 /// @return the pointer to `type` with the given ast::StorageClass
713 typ::Pointer pointer(typ::Type type,
714 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000715 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000716 return {builder->create<ast::Pointer>(type, storage_class),
717 builder->create<sem::Pointer>(type, storage_class)};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000718 }
719
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000720 /// @param source the Source of the node
721 /// @param type the type of the pointer
722 /// @param storage_class the storage class of the pointer
723 /// @return the pointer to `type` with the given ast::StorageClass
724 typ::Pointer pointer(const Source& source,
725 typ::Type type,
726 ast::StorageClass storage_class) const {
727 type = MaybeCreateTypename(type);
728 return {builder->create<ast::Pointer>(source, type, storage_class),
729 builder->create<sem::Pointer>(type, storage_class)};
730 }
731
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000732 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000733 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000734 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000735 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000736 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000737 }
738
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000739 /// @param impl the struct implementation
740 /// @returns a struct pointer
Ben Claytone204f272021-04-22 14:40:23 +0000741 typ::Struct struct_(ast::Struct* impl) const {
742 return {impl, builder->create<sem::StructType>(impl)};
743 }
744
745 /// @param kind the kind of sampler
746 /// @returns the sampler
747 typ::Sampler sampler(ast::SamplerKind kind) const {
748 return {builder->create<ast::Sampler>(kind),
749 builder->create<sem::Sampler>(kind)};
750 }
751
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000752 /// @param source the Source of the node
753 /// @param kind the kind of sampler
754 /// @returns the sampler
755 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
756 return {builder->create<ast::Sampler>(source, kind),
757 builder->create<sem::Sampler>(kind)};
758 }
759
Ben Claytone204f272021-04-22 14:40:23 +0000760 /// @param dims the dimensionality of the texture
761 /// @returns the depth texture
762 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
763 return {builder->create<ast::DepthTexture>(dims),
764 builder->create<sem::DepthTexture>(dims)};
765 }
766
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000767 /// @param source the Source of the node
768 /// @param dims the dimensionality of the texture
769 /// @returns the depth texture
770 typ::DepthTexture depth_texture(const Source& source,
771 ast::TextureDimension dims) const {
772 return {builder->create<ast::DepthTexture>(source, dims),
773 builder->create<sem::DepthTexture>(dims)};
774 }
775
Ben Claytone204f272021-04-22 14:40:23 +0000776 /// @param dims the dimensionality of the texture
777 /// @param subtype the texture subtype.
778 /// @returns the sampled texture
779 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
780 typ::Type subtype) const {
781 return {builder->create<ast::SampledTexture>(dims, subtype),
782 builder->create<sem::SampledTexture>(dims, subtype)};
783 }
784
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000785 /// @param source the Source of the node
786 /// @param dims the dimensionality of the texture
787 /// @param subtype the texture subtype.
788 /// @returns the sampled texture
789 typ::SampledTexture sampled_texture(const Source& source,
790 ast::TextureDimension dims,
791 typ::Type subtype) const {
792 return {builder->create<ast::SampledTexture>(source, dims, subtype),
793 builder->create<sem::SampledTexture>(dims, subtype)};
794 }
795
Ben Claytone204f272021-04-22 14:40:23 +0000796 /// @param dims the dimensionality of the texture
797 /// @param subtype the texture subtype.
798 /// @returns the multisampled texture
799 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
800 typ::Type subtype) const {
801 return {builder->create<ast::MultisampledTexture>(dims, subtype),
802 builder->create<sem::MultisampledTexture>(dims, subtype)};
803 }
804
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000805 /// @param source the Source of the node
806 /// @param dims the dimensionality of the texture
807 /// @param subtype the texture subtype.
808 /// @returns the multisampled texture
809 typ::MultisampledTexture multisampled_texture(const Source& source,
810 ast::TextureDimension dims,
811 typ::Type subtype) const {
812 return {builder->create<ast::MultisampledTexture>(source, dims, subtype),
813 builder->create<sem::MultisampledTexture>(dims, subtype)};
814 }
815
Ben Claytone204f272021-04-22 14:40:23 +0000816 /// @param dims the dimensionality of the texture
817 /// @param format the image format of the texture
818 /// @returns the storage texture
819 typ::StorageTexture storage_texture(ast::TextureDimension dims,
820 ast::ImageFormat format) const {
821 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
822 auto* sem_subtype =
823 sem::StorageTexture::SubtypeFor(format, builder->Types());
824 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
825 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000826 }
827
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000828 /// @param source the Source of the node
829 /// @param dims the dimensionality of the texture
830 /// @param format the image format of the texture
831 /// @returns the storage texture
832 typ::StorageTexture storage_texture(const Source& source,
833 ast::TextureDimension dims,
834 ast::ImageFormat format) const {
835 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
836 auto* sem_subtype =
837 sem::StorageTexture::SubtypeFor(format, builder->Types());
838 return {builder->create<ast::StorageTexture>(source, dims, format,
839 ast_subtype),
840 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
841 }
842
843 /// @param source the Source of the node
844 /// @returns the external texture
845 typ::ExternalTexture external_texture(const Source& source) const {
846 return {builder->create<ast::ExternalTexture>(source),
847 builder->create<sem::ExternalTexture>()};
848 }
849
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000850 /// If ty is a ast::Struct or ast::Alias, the returned type is an
851 /// ast::TypeName of the given type's name, otherwise type is returned.
852 /// @param type the type
853 /// @return either type or a pointer to a new ast::TypeName
854 typ::Type MaybeCreateTypename(typ::Type type) const;
855
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000856 private:
857 /// CToAST<T> is specialized for various `T` types and each specialization
858 /// contains a single static `get()` method for obtaining the corresponding
859 /// AST type for the C type `T`.
860 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000861 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000862 template <typename T>
863 struct CToAST {};
864
Ben Claytonc7ca7662021-02-17 16:23:52 +0000865 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000866 };
867
868 //////////////////////////////////////////////////////////////////////////////
869 // AST helper methods
870 //////////////////////////////////////////////////////////////////////////////
871
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000872 /// @param name the symbol string
873 /// @return a Symbol with the given name
874 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
875
876 /// @param sym the symbol
877 /// @return `sym`
878 Symbol Sym(Symbol sym) { return sym; }
879
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000880 /// @param expr the expression
881 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000882 template <typename T>
883 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
884 return expr;
885 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000886
Ben Claytonb8ea5912021-04-19 16:52:42 +0000887 /// Passthrough for nullptr
888 /// @return nullptr
889 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
890
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000891 /// @param name the identifier name
892 /// @return an ast::IdentifierExpression with the given name
893 ast::IdentifierExpression* Expr(const std::string& name) {
894 return create<ast::IdentifierExpression>(Symbols().Register(name));
895 }
896
Ben Clayton46d78d72021-02-10 21:34:26 +0000897 /// @param symbol the identifier symbol
898 /// @return an ast::IdentifierExpression with the given symbol
899 ast::IdentifierExpression* Expr(Symbol symbol) {
900 return create<ast::IdentifierExpression>(symbol);
901 }
902
Ben Claytonb8ea5912021-04-19 16:52:42 +0000903 /// @param variable the AST variable
904 /// @return an ast::IdentifierExpression with the variable's symbol
905 ast::IdentifierExpression* Expr(ast::Variable* variable) {
906 return create<ast::IdentifierExpression>(variable->symbol());
907 }
908
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000909 /// @param source the source information
910 /// @param name the identifier name
911 /// @return an ast::IdentifierExpression with the given name
912 ast::IdentifierExpression* Expr(const Source& source,
913 const std::string& name) {
914 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
915 }
916
917 /// @param name the identifier name
918 /// @return an ast::IdentifierExpression with the given name
919 ast::IdentifierExpression* Expr(const char* name) {
920 return create<ast::IdentifierExpression>(Symbols().Register(name));
921 }
922
923 /// @param value the boolean value
924 /// @return a Scalar constructor for the given value
925 ast::ScalarConstructorExpression* Expr(bool value) {
926 return create<ast::ScalarConstructorExpression>(Literal(value));
927 }
928
929 /// @param value the float value
930 /// @return a Scalar constructor for the given value
931 ast::ScalarConstructorExpression* Expr(f32 value) {
932 return create<ast::ScalarConstructorExpression>(Literal(value));
933 }
934
935 /// @param value the integer value
936 /// @return a Scalar constructor for the given value
937 ast::ScalarConstructorExpression* Expr(i32 value) {
938 return create<ast::ScalarConstructorExpression>(Literal(value));
939 }
940
941 /// @param value the unsigned int value
942 /// @return a Scalar constructor for the given value
943 ast::ScalarConstructorExpression* Expr(u32 value) {
944 return create<ast::ScalarConstructorExpression>(Literal(value));
945 }
946
947 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
948 /// `list`.
949 /// @param list the list to append too
950 /// @param arg the arg to create
951 template <typename ARG>
952 void Append(ast::ExpressionList& list, ARG&& arg) {
953 list.emplace_back(Expr(std::forward<ARG>(arg)));
954 }
955
956 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
957 /// then appends them to `list`.
958 /// @param list the list to append too
959 /// @param arg0 the first argument
960 /// @param args the rest of the arguments
961 template <typename ARG0, typename... ARGS>
962 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
963 Append(list, std::forward<ARG0>(arg0));
964 Append(list, std::forward<ARGS>(args)...);
965 }
966
967 /// @return an empty list of expressions
968 ast::ExpressionList ExprList() { return {}; }
969
970 /// @param args the list of expressions
971 /// @return the list of expressions converted to `ast::Expression`s using
972 /// `Expr()`,
973 template <typename... ARGS>
974 ast::ExpressionList ExprList(ARGS&&... args) {
975 ast::ExpressionList list;
976 list.reserve(sizeof...(args));
977 Append(list, std::forward<ARGS>(args)...);
978 return list;
979 }
980
981 /// @param list the list of expressions
982 /// @return `list`
983 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
984
985 /// @param val the boolan value
986 /// @return a boolean literal with the given value
987 ast::BoolLiteral* Literal(bool val) {
988 return create<ast::BoolLiteral>(ty.bool_(), val);
989 }
990
991 /// @param val the float value
992 /// @return a float literal with the given value
993 ast::FloatLiteral* Literal(f32 val) {
994 return create<ast::FloatLiteral>(ty.f32(), val);
995 }
996
997 /// @param val the unsigned int value
998 /// @return a ast::UintLiteral with the given value
999 ast::UintLiteral* Literal(u32 val) {
1000 return create<ast::UintLiteral>(ty.u32(), val);
1001 }
1002
1003 /// @param val the integer value
1004 /// @return the ast::SintLiteral with the given value
1005 ast::SintLiteral* Literal(i32 val) {
1006 return create<ast::SintLiteral>(ty.i32(), val);
1007 }
1008
1009 /// @param args the arguments for the type constructor
1010 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1011 /// of `args` converted to `ast::Expression`s using `Expr()`
1012 template <typename T, typename... ARGS>
1013 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
1014 return create<ast::TypeConstructorExpression>(
1015 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
1016 }
1017
1018 /// @param type the type to construct
1019 /// @param args the arguments for the constructor
1020 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1021 /// values `args`.
1022 template <typename... ARGS>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001023 ast::TypeConstructorExpression* Construct(typ::Type type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001024 return create<ast::TypeConstructorExpression>(
1025 type, ExprList(std::forward<ARGS>(args)...));
1026 }
1027
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001028 /// Creates a constructor expression that constructs an object of
1029 /// `type` filled with `elem_value`. For example,
1030 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1031 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1032 /// @param type the type to construct
1033 /// @param elem_value the initial or element value (for vec and mat) to
1034 /// construct with
1035 /// @return the constructor expression
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001036 ast::ConstructorExpression* ConstructValueFilledWith(typ::Type type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001037 int elem_value = 0);
1038
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001039 /// @param args the arguments for the vector constructor
1040 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1041 /// `T`, constructed with the values `args`.
1042 template <typename T, typename... ARGS>
1043 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
1044 return create<ast::TypeConstructorExpression>(
1045 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
1046 }
1047
1048 /// @param args the arguments for the vector constructor
1049 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1050 /// `T`, constructed with the values `args`.
1051 template <typename T, typename... ARGS>
1052 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
1053 return create<ast::TypeConstructorExpression>(
1054 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
1055 }
1056
1057 /// @param args the arguments for the vector constructor
1058 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1059 /// `T`, constructed with the values `args`.
1060 template <typename T, typename... ARGS>
1061 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
1062 return create<ast::TypeConstructorExpression>(
1063 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
1064 }
1065
1066 /// @param args the arguments for the matrix constructor
1067 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1068 /// `T`, constructed with the values `args`.
1069 template <typename T, typename... ARGS>
1070 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
1071 return create<ast::TypeConstructorExpression>(
1072 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
1073 }
1074
1075 /// @param args the arguments for the matrix constructor
1076 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1077 /// `T`, constructed with the values `args`.
1078 template <typename T, typename... ARGS>
1079 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
1080 return create<ast::TypeConstructorExpression>(
1081 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
1082 }
1083
1084 /// @param args the arguments for the matrix constructor
1085 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1086 /// `T`, constructed with the values `args`.
1087 template <typename T, typename... ARGS>
1088 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
1089 return create<ast::TypeConstructorExpression>(
1090 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
1091 }
1092
1093 /// @param args the arguments for the matrix constructor
1094 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1095 /// `T`, constructed with the values `args`.
1096 template <typename T, typename... ARGS>
1097 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
1098 return create<ast::TypeConstructorExpression>(
1099 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
1100 }
1101
1102 /// @param args the arguments for the matrix constructor
1103 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1104 /// `T`, constructed with the values `args`.
1105 template <typename T, typename... ARGS>
1106 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
1107 return create<ast::TypeConstructorExpression>(
1108 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
1109 }
1110
1111 /// @param args the arguments for the matrix constructor
1112 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1113 /// `T`, constructed with the values `args`.
1114 template <typename T, typename... ARGS>
1115 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
1116 return create<ast::TypeConstructorExpression>(
1117 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
1118 }
1119
1120 /// @param args the arguments for the matrix constructor
1121 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1122 /// `T`, constructed with the values `args`.
1123 template <typename T, typename... ARGS>
1124 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
1125 return create<ast::TypeConstructorExpression>(
1126 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
1127 }
1128
1129 /// @param args the arguments for the matrix constructor
1130 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1131 /// `T`, constructed with the values `args`.
1132 template <typename T, typename... ARGS>
1133 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
1134 return create<ast::TypeConstructorExpression>(
1135 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
1136 }
1137
1138 /// @param args the arguments for the matrix constructor
1139 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1140 /// `T`, constructed with the values `args`.
1141 template <typename T, typename... ARGS>
1142 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
1143 return create<ast::TypeConstructorExpression>(
1144 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
1145 }
1146
1147 /// @param args the arguments for the array constructor
1148 /// @return an `ast::TypeConstructorExpression` of an array with element type
1149 /// `T`, constructed with the values `args`.
1150 template <typename T, int N = 0, typename... ARGS>
1151 ast::TypeConstructorExpression* array(ARGS&&... args) {
1152 return create<ast::TypeConstructorExpression>(
1153 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
1154 }
1155
1156 /// @param subtype the array element type
1157 /// @param n the array size. 0 represents a runtime-array.
1158 /// @param args the arguments for the array constructor
1159 /// @return an `ast::TypeConstructorExpression` of an array with element type
1160 /// `subtype`, constructed with the values `args`.
1161 template <typename... ARGS>
Ben Claytone204f272021-04-22 14:40:23 +00001162 ast::TypeConstructorExpression* array(typ::Type subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001163 uint32_t n,
1164 ARGS&&... args) {
1165 return create<ast::TypeConstructorExpression>(
1166 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
1167 }
1168
1169 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001170 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001171 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001172 /// @param constructor constructor expression
1173 /// @param decorations variable decorations
1174 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001175 template <typename NAME>
1176 ast::Variable* Var(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001177 typ::Type type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001178 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001179 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001180 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001181 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001182 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1183 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001184 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001185
1186 /// @param source the variable source
1187 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001188 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001189 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001190 /// @param constructor constructor expression
1191 /// @param decorations variable decorations
1192 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001193 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001194 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001195 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001196 typ::Type type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001197 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001198 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001199 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001200 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001201 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001202 type, false, constructor, decorations);
1203 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001204
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001205 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001206 /// @param type the variable type
1207 /// @param constructor optional constructor expression
1208 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001209 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001210 template <typename NAME>
1211 ast::Variable* Const(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001212 typ::Type type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001213 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001214 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001215 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001216 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001217 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001218 constructor, decorations);
1219 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001220
1221 /// @param source the variable source
1222 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001223 /// @param type the variable type
1224 /// @param constructor optional constructor expression
1225 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001226 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001227 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001229 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001230 typ::Type type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001231 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001232 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001233 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001234 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001235 ast::StorageClass::kNone, type, true,
1236 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001237 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238
James Pricedaf1f1c2021-04-08 22:15:48 +00001239 /// @param name the parameter name
1240 /// @param type the parameter type
1241 /// @param decorations optional parameter decorations
1242 /// @returns a constant `ast::Variable` with the given name and type
1243 template <typename NAME>
1244 ast::Variable* Param(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001245 typ::Type type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001246 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001247 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001248 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1249 ast::StorageClass::kNone, type, true, nullptr,
1250 decorations);
1251 }
1252
1253 /// @param source the parameter source
1254 /// @param name the parameter name
1255 /// @param type the parameter type
1256 /// @param decorations optional parameter decorations
1257 /// @returns a constant `ast::Variable` with the given name and type
1258 template <typename NAME>
1259 ast::Variable* Param(const Source& source,
1260 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001261 typ::Type type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001262 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001263 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001264 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1265 ast::StorageClass::kNone, type, true, nullptr,
1266 decorations);
1267 }
1268
Ben Clayton42708342021-04-21 17:55:12 +00001269 /// @param name the variable name
1270 /// @param type the variable type
1271 /// @param storage the variable storage class
1272 /// @param constructor constructor expression
1273 /// @param decorations variable decorations
1274 /// @returns a new `ast::Variable`, which is automatically registered as a
1275 /// global variable with the ast::Module.
1276 template <typename NAME>
1277 ast::Variable* Global(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001278 typ::Type type,
Ben Clayton42708342021-04-21 17:55:12 +00001279 ast::StorageClass storage,
1280 ast::Expression* constructor = nullptr,
1281 ast::DecorationList decorations = {}) {
1282 auto* var =
1283 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1284 AST().AddGlobalVariable(var);
1285 return var;
1286 }
1287
1288 /// @param source the variable source
1289 /// @param name the variable name
1290 /// @param type the variable type
1291 /// @param storage the variable storage class
1292 /// @param constructor constructor expression
1293 /// @param decorations variable decorations
1294 /// @returns a new `ast::Variable`, which is automatically registered as a
1295 /// global variable with the ast::Module.
1296 template <typename NAME>
1297 ast::Variable* Global(const Source& source,
1298 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001299 typ::Type type,
Ben Clayton42708342021-04-21 17:55:12 +00001300 ast::StorageClass storage,
1301 ast::Expression* constructor = nullptr,
1302 ast::DecorationList decorations = {}) {
1303 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1304 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001305 AST().AddGlobalVariable(var);
1306 return var;
1307 }
1308
1309 /// @param args the arguments to pass to Const()
1310 /// @returns a const `ast::Variable` constructed by calling Var() with the
1311 /// arguments of `args`, which is automatically registered as a global
1312 /// variable with the ast::Module.
1313 template <typename... ARGS>
1314 ast::Variable* GlobalConst(ARGS&&... args) {
1315 auto* var = Const(std::forward<ARGS>(args)...);
1316 AST().AddGlobalVariable(var);
1317 return var;
1318 }
1319
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001320 /// @param func the function name
1321 /// @param args the function call arguments
1322 /// @returns a `ast::CallExpression` to the function `func`, with the
1323 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1324 template <typename NAME, typename... ARGS>
1325 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1326 return create<ast::CallExpression>(Expr(func),
1327 ExprList(std::forward<ARGS>(args)...));
1328 }
1329
1330 /// @param lhs the left hand argument to the addition operation
1331 /// @param rhs the right hand argument to the addition operation
1332 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1333 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001334 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001335 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1336 Expr(std::forward<LHS>(lhs)),
1337 Expr(std::forward<RHS>(rhs)));
1338 }
1339
1340 /// @param lhs the left hand argument to the subtraction operation
1341 /// @param rhs the right hand argument to the subtraction operation
1342 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1343 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001344 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001345 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1346 Expr(std::forward<LHS>(lhs)),
1347 Expr(std::forward<RHS>(rhs)));
1348 }
1349
1350 /// @param lhs the left hand argument to the multiplication operation
1351 /// @param rhs the right hand argument to the multiplication operation
1352 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1353 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001354 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001355 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1356 Expr(std::forward<LHS>(lhs)),
1357 Expr(std::forward<RHS>(rhs)));
1358 }
1359
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001360 /// @param source the source information
1361 /// @param lhs the left hand argument to the multiplication operation
1362 /// @param rhs the right hand argument to the multiplication operation
1363 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1364 template <typename LHS, typename RHS>
1365 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1366 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1367 Expr(std::forward<LHS>(lhs)),
1368 Expr(std::forward<RHS>(rhs)));
1369 }
1370
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001371 /// @param lhs the left hand argument to the division operation
1372 /// @param rhs the right hand argument to the division operation
1373 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1374 template <typename LHS, typename RHS>
1375 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1376 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1377 Expr(std::forward<LHS>(lhs)),
1378 Expr(std::forward<RHS>(rhs)));
1379 }
1380
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001381 /// @param arr the array argument for the array accessor expression
1382 /// @param idx the index argument for the array accessor expression
1383 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1384 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001385 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001386 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1387 Expr(std::forward<IDX>(idx)));
1388 }
1389
1390 /// @param obj the object for the member accessor expression
1391 /// @param idx the index argument for the array accessor expression
1392 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1393 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001394 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001395 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1396 Expr(std::forward<IDX>(idx)));
1397 }
1398
Ben Clayton42d1e092021-02-02 14:29:15 +00001399 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001400 /// @param val the offset value
1401 /// @returns the offset decoration pointer
1402 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1403 return create<ast::StructMemberOffsetDecoration>(source_, val);
1404 }
1405
Ben Claytond614dd52021-03-15 10:43:11 +00001406 /// Creates a ast::StructMemberSizeDecoration
1407 /// @param source the source information
1408 /// @param val the size value
1409 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001410 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1411 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001412 return create<ast::StructMemberSizeDecoration>(source, val);
1413 }
1414
1415 /// Creates a ast::StructMemberSizeDecoration
1416 /// @param val the size value
1417 /// @returns the size decoration pointer
1418 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1419 return create<ast::StructMemberSizeDecoration>(source_, val);
1420 }
1421
1422 /// Creates a ast::StructMemberAlignDecoration
1423 /// @param source the source information
1424 /// @param val the align value
1425 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001426 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1427 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001428 return create<ast::StructMemberAlignDecoration>(source, val);
1429 }
1430
1431 /// Creates a ast::StructMemberAlignDecoration
1432 /// @param val the align value
1433 /// @returns the align decoration pointer
1434 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1435 return create<ast::StructMemberAlignDecoration>(source_, val);
1436 }
1437
Ben Clayton42d1e092021-02-02 14:29:15 +00001438 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001439 /// @param source the source information
1440 /// @param name the function name
1441 /// @param params the function parameters
1442 /// @param type the function return type
1443 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001444 /// @param decorations the optional function decorations
1445 /// @param return_type_decorations the optional function return type
1446 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001447 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001448 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001449 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001450 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001451 ast::VariableList params,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001452 typ::Type type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001453 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001454 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001455 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001456 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001457 auto* func =
1458 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1459 type, create<ast::BlockStatement>(body),
1460 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001461 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001462 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001463 }
1464
Ben Clayton42d1e092021-02-02 14:29:15 +00001465 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001466 /// @param name the function name
1467 /// @param params the function parameters
1468 /// @param type the function return type
1469 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001470 /// @param decorations the optional function decorations
1471 /// @param return_type_decorations the optional function return type
1472 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001473 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001474 template <typename NAME>
1475 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001476 ast::VariableList params,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001477 typ::Type type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001478 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001479 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001480 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001481 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001482 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1483 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001484 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001485 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001486 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001487 }
1488
Ben Clayton49668bb2021-04-17 05:32:01 +00001489 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001490 /// @param source the source information
1491 /// @returns the return statement pointer
1492 ast::ReturnStatement* Return(const Source& source) {
1493 return create<ast::ReturnStatement>(source);
1494 }
1495
1496 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001497 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001498 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1499
1500 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001501 /// @param source the source information
1502 /// @param val the return value
1503 /// @returns the return statement pointer
1504 template <typename EXPR>
1505 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1506 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1507 }
1508
1509 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001510 /// @param val the return value
1511 /// @returns the return statement pointer
1512 template <typename EXPR>
1513 ast::ReturnStatement* Return(EXPR&& val) {
1514 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001515 }
1516
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001517 /// Creates a ast::Struct and sem::StructType, registering the
1518 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001519 /// @param source the source information
1520 /// @param name the struct name
1521 /// @param members the struct members
1522 /// @param decorations the optional struct decorations
1523 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001524 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001525 typ::Struct Structure(const Source& source,
1526 NAME&& name,
1527 ast::StructMemberList members,
1528 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001529 auto sym = Sym(std::forward<NAME>(name));
1530 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1531 std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001532 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001533 AST().AddConstructedType(type);
1534 return type;
1535 }
1536
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001537 /// Creates a ast::Struct and sem::StructType, registering the
1538 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001539 /// @param name the struct name
1540 /// @param members the struct members
1541 /// @param decorations the optional struct decorations
1542 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001543 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001544 typ::Struct Structure(NAME&& name,
1545 ast::StructMemberList members,
1546 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001547 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001548 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001549 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001550 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001551 AST().AddConstructedType(type);
1552 return type;
1553 }
1554
Ben Clayton42d1e092021-02-02 14:29:15 +00001555 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001556 /// @param source the source information
1557 /// @param name the struct member name
1558 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001559 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001560 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001561 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001562 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001563 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001564 typ::Type type,
Ben Claytond614dd52021-03-15 10:43:11 +00001565 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001566 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001567 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1568 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001569 }
1570
Ben Clayton42d1e092021-02-02 14:29:15 +00001571 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001572 /// @param name the struct member name
1573 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001574 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001575 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001576 template <typename NAME>
1577 ast::StructMember* Member(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001578 typ::Type type,
Ben Claytond614dd52021-03-15 10:43:11 +00001579 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001580 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001581 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1582 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001583 }
1584
Ben Claytonbab31972021-02-08 21:16:21 +00001585 /// Creates a ast::StructMember with the given byte offset
1586 /// @param offset the offset to use in the StructMemberOffsetDecoration
1587 /// @param name the struct member name
1588 /// @param type the struct member type
1589 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001590 template <typename NAME>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001591 ast::StructMember* Member(uint32_t offset, NAME&& name, typ::Type type) {
1592 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001593 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001594 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001595 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001596 create<ast::StructMemberOffsetDecoration>(offset),
1597 });
1598 }
1599
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001600 /// Creates a ast::BlockStatement with input statements
1601 /// @param statements statements of block
1602 /// @returns the block statement pointer
1603 template <typename... Statements>
1604 ast::BlockStatement* Block(Statements&&... statements) {
1605 return create<ast::BlockStatement>(
1606 ast::StatementList{std::forward<Statements>(statements)...});
1607 }
1608
1609 /// Creates a ast::ElseStatement with input condition and body
1610 /// @param condition the else condition expression
1611 /// @param body the else body
1612 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001613 template <typename CONDITION>
1614 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1615 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1616 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001617 }
1618
1619 /// Creates a ast::IfStatement with input condition, body, and optional
1620 /// variadic else statements
1621 /// @param condition the if statement condition expression
1622 /// @param body the if statement body
1623 /// @param elseStatements optional variadic else statements
1624 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001625 template <typename CONDITION, typename... ELSE_STATEMENTS>
1626 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001627 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001628 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001629 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001630 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001631 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001632 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001633 }
1634
1635 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001636 /// @param source the source information
1637 /// @param lhs the left hand side expression initializer
1638 /// @param rhs the right hand side expression initializer
1639 /// @returns the assignment statement pointer
1640 template <typename LhsExpressionInit, typename RhsExpressionInit>
1641 ast::AssignmentStatement* Assign(const Source& source,
1642 LhsExpressionInit&& lhs,
1643 RhsExpressionInit&& rhs) {
1644 return create<ast::AssignmentStatement>(
1645 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1646 Expr(std::forward<RhsExpressionInit>(rhs)));
1647 }
1648
1649 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001650 /// @param lhs the left hand side expression initializer
1651 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001652 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001653 template <typename LhsExpressionInit, typename RhsExpressionInit>
1654 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1655 RhsExpressionInit&& rhs) {
1656 return create<ast::AssignmentStatement>(
1657 Expr(std::forward<LhsExpressionInit>(lhs)),
1658 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001659 }
1660
1661 /// Creates a ast::LoopStatement with input body and optional continuing
1662 /// @param body the loop body
1663 /// @param continuing the optional continuing block
1664 /// @returns the loop statement pointer
1665 ast::LoopStatement* Loop(ast::BlockStatement* body,
1666 ast::BlockStatement* continuing = nullptr) {
1667 return create<ast::LoopStatement>(body, continuing);
1668 }
1669
1670 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001671 /// @param source the source information
1672 /// @param var the variable to wrap in a decl statement
1673 /// @returns the variable decl statement pointer
1674 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1675 return create<ast::VariableDeclStatement>(source, var);
1676 }
1677
1678 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001679 /// @param var the variable to wrap in a decl statement
1680 /// @returns the variable decl statement pointer
1681 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1682 return create<ast::VariableDeclStatement>(var);
1683 }
1684
Antonio Maioranocea744d2021-03-25 12:55:27 +00001685 /// Creates a ast::SwitchStatement with input expression and cases
1686 /// @param condition the condition expression initializer
1687 /// @param cases case statements
1688 /// @returns the switch statement pointer
1689 template <typename ExpressionInit, typename... Cases>
1690 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1691 return create<ast::SwitchStatement>(
1692 Expr(std::forward<ExpressionInit>(condition)),
1693 ast::CaseStatementList{std::forward<Cases>(cases)...});
1694 }
1695
1696 /// Creates a ast::CaseStatement with input list of selectors, and body
1697 /// @param selectors list of selectors
1698 /// @param body the case body
1699 /// @returns the case statement pointer
1700 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1701 ast::BlockStatement* body = nullptr) {
1702 return create<ast::CaseStatement>(std::move(selectors),
1703 body ? body : Block());
1704 }
1705
1706 /// Convenient overload that takes a single selector
1707 /// @param selector a single case selector
1708 /// @param body the case body
1709 /// @returns the case statement pointer
1710 ast::CaseStatement* Case(ast::IntLiteral* selector,
1711 ast::BlockStatement* body = nullptr) {
1712 return Case(ast::CaseSelectorList{selector}, body);
1713 }
1714
1715 /// Convenience function that creates a 'default' ast::CaseStatement
1716 /// @param body the case body
1717 /// @returns the case statement pointer
1718 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1719 return Case(ast::CaseSelectorList{}, body);
1720 }
1721
James Price68f558f2021-04-06 15:51:47 +00001722 /// Creates an ast::BuiltinDecoration
1723 /// @param source the source information
1724 /// @param builtin the builtin value
1725 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001726 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001727 return create<ast::BuiltinDecoration>(source, builtin);
1728 }
1729
1730 /// Creates an ast::BuiltinDecoration
1731 /// @param builtin the builtin value
1732 /// @returns the builtin decoration pointer
1733 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1734 return create<ast::BuiltinDecoration>(source_, builtin);
1735 }
1736
1737 /// Creates an ast::LocationDecoration
1738 /// @param source the source information
1739 /// @param location the location value
1740 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001741 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001742 return create<ast::LocationDecoration>(source, location);
1743 }
1744
1745 /// Creates an ast::LocationDecoration
1746 /// @param location the location value
1747 /// @returns the location decoration pointer
1748 ast::LocationDecoration* Location(uint32_t location) {
1749 return create<ast::LocationDecoration>(source_, location);
1750 }
1751
1752 /// Creates an ast::StageDecoration
1753 /// @param source the source information
1754 /// @param stage the pipeline stage
1755 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001756 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001757 return create<ast::StageDecoration>(source, stage);
1758 }
1759
1760 /// Creates an ast::StageDecoration
1761 /// @param stage the pipeline stage
1762 /// @returns the stage decoration pointer
1763 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1764 return create<ast::StageDecoration>(source_, stage);
1765 }
1766
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001767 /// Sets the current builder source to `src`
1768 /// @param src the Source used for future create() calls
1769 void SetSource(const Source& src) {
1770 AssertNotMoved();
1771 source_ = src;
1772 }
1773
1774 /// Sets the current builder source to `loc`
1775 /// @param loc the Source used for future create() calls
1776 void SetSource(const Source::Location& loc) {
1777 AssertNotMoved();
1778 source_ = Source(loc);
1779 }
1780
Ben Clayton33352542021-01-29 16:43:41 +00001781 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001782 /// @note As the Resolver is run when the Program is built, this will only be
1783 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001784 /// @param expr the AST expression
1785 /// @return the resolved semantic type for the expression, or nullptr if the
1786 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001787 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001788
Ben Clayton169512e2021-04-17 05:52:11 +00001789 /// Wraps the ast::Literal in a statement. This is used by tests that
1790 /// construct a partial AST and require the Resolver to reach these
1791 /// nodes.
1792 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1793 /// @return the ast::Statement that wraps the ast::Statement
1794 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001795 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001796 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001797 /// nodes.
1798 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1799 /// @return the ast::Statement that wraps the ast::Expression
1800 ast::Statement* WrapInStatement(ast::Expression* expr);
1801 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001802 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001803 /// these nodes.
1804 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1805 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1806 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1807 /// Returns the statement argument. Used as a passthrough-overload by
1808 /// WrapInFunction().
1809 /// @param stmt the ast::Statement
1810 /// @return `stmt`
1811 ast::Statement* WrapInStatement(ast::Statement* stmt);
1812 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001813 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001814 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001815 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001816 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001817 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001818 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001819 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001820 }
1821 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001822 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001823 /// @returns the function
1824 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001825
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001826 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001827 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001828
1829 protected:
1830 /// Asserts that the builder has not been moved.
1831 void AssertNotMoved() const;
1832
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001833 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001834 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001835 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001836 ASTNodeAllocator ast_nodes_;
1837 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001838 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001839 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001840 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001841 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001842
1843 /// The source to use when creating AST nodes without providing a Source as
1844 /// the first argument.
1845 Source source_;
1846
Ben Clayton5f0ea112021-03-09 10:54:37 +00001847 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001848 /// program when built.
1849 bool resolve_on_build_ = true;
1850
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001851 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1852 bool moved_ = false;
1853};
1854
1855//! @cond Doxygen_Suppress
1856// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1857template <>
1858struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001859 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001860 return t->i32();
1861 }
1862};
1863template <>
1864struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001865 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001866 return t->u32();
1867 }
1868};
1869template <>
1870struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001871 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001872 return t->f32();
1873 }
1874};
1875template <>
1876struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001877 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001878 return t->bool_();
1879 }
1880};
1881template <>
1882struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001883 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001884 return t->void_();
1885 }
1886};
1887//! @endcond
1888
Ben Clayton917b14b2021-04-19 16:50:23 +00001889/// @param builder the ProgramBuilder
1890/// @returns the ProgramID of the ProgramBuilder
1891inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1892 return builder->ID();
1893}
1894
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001895} // namespace tint
1896
1897#endif // SRC_PROGRAM_BUILDER_H_