blob: 4b009762dc0f1368bcf8de89ebb5791519ed5056 [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"
Ben Claytona922e652021-04-21 13:37:22 +000031#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000032#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000033#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000034#include "src/ast/if_statement.h"
35#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000036#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000037#include "src/ast/member_accessor_expression.h"
38#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000039#include "src/ast/multisampled_texture.h"
40#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000041#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000043#include "src/ast/scalar_constructor_expression.h"
44#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000045#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000046#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000047#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000048#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000049#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000050#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000051#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000052#include "src/ast/type_constructor_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000053#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000055#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000056#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000057#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000058#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000059#include "src/program_id.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000060#include "src/sem/access_control_type.h"
61#include "src/sem/alias_type.h"
62#include "src/sem/array_type.h"
63#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000064#include "src/sem/depth_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000065#include "src/sem/f32_type.h"
66#include "src/sem/i32_type.h"
67#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000068#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000069#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000070#include "src/sem/sampled_texture_type.h"
71#include "src/sem/storage_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000072#include "src/sem/struct_type.h"
73#include "src/sem/u32_type.h"
74#include "src/sem/vector_type.h"
75#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000076#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000077
78namespace tint {
79
Ben Clayton401b96b2021-02-03 17:19:59 +000080// Forward declarations
81namespace ast {
82class VariableDeclStatement;
83} // namespace ast
84
Ben Claytona6b9a8e2021-01-26 16:57:10 +000085class CloneContext;
86
87/// ProgramBuilder is a mutable builder for a Program.
88/// To construct a Program, populate the builder and then `std::move` it to a
89/// Program.
90class ProgramBuilder {
91 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000092 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
93 using ASTNodeAllocator = BlockAllocator<ast::Node>;
94
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000095 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
96 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000097
98 /// `i32` is a type alias to `int`.
99 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
100 /// WGSL syntax.
101 /// Note: this is intentionally not aliased to uint32_t as we want integer
102 /// literals passed to the builder to match WGSL's integer literal types.
103 using i32 = decltype(1);
104 /// `u32` is a type alias to `unsigned int`.
105 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
106 /// WGSL syntax.
107 /// Note: this is intentionally not aliased to uint32_t as we want integer
108 /// literals passed to the builder to match WGSL's integer literal types.
109 using u32 = decltype(1u);
110 /// `f32` is a type alias to `float`
111 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
112 /// WGSL syntax.
113 using f32 = float;
114
115 /// Constructor
116 ProgramBuilder();
117
118 /// Move constructor
119 /// @param rhs the builder to move
120 ProgramBuilder(ProgramBuilder&& rhs);
121
122 /// Destructor
123 virtual ~ProgramBuilder();
124
125 /// Move assignment operator
126 /// @param rhs the builder to move
127 /// @return this builder
128 ProgramBuilder& operator=(ProgramBuilder&& rhs);
129
Ben Claytone43c8302021-01-29 11:59:32 +0000130 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
131 /// making a deep clone of the Program contents.
132 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
133 /// existing immutable program.
134 /// As the returned ProgramBuilder wraps `program`, `program` must not be
135 /// destructed or assigned while using the returned ProgramBuilder.
136 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
137 /// function. See crbug.com/tint/460.
138 /// @param program the immutable Program to wrap
139 /// @return the ProgramBuilder that wraps `program`
140 static ProgramBuilder Wrap(const Program* program);
141
Ben Claytone6995de2021-04-13 23:27:27 +0000142 /// @returns the unique identifier for this program
143 ProgramID ID() const { return id_; }
144
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000145 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000146 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000147 AssertNotMoved();
148 return types_;
149 }
150
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000151 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000152 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000153 AssertNotMoved();
154 return types_;
155 }
156
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000157 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000158 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000159 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000160 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 }
162
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000163 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000164 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000165 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000166 return ast_nodes_;
167 }
168
169 /// @returns a reference to the program's semantic nodes storage
170 SemNodeAllocator& SemNodes() {
171 AssertNotMoved();
172 return sem_nodes_;
173 }
174
175 /// @returns a reference to the program's semantic nodes storage
176 const SemNodeAllocator& SemNodes() const {
177 AssertNotMoved();
178 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000179 }
180
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000181 /// @returns a reference to the program's AST root Module
182 ast::Module& AST() {
183 AssertNotMoved();
184 return *ast_;
185 }
186
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000187 /// @returns a reference to the program's AST root Module
188 const ast::Module& AST() const {
189 AssertNotMoved();
190 return *ast_;
191 }
192
193 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000194 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000195 AssertNotMoved();
196 return sem_;
197 }
198
199 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000200 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000201 AssertNotMoved();
202 return sem_;
203 }
204
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000205 /// @returns a reference to the program's SymbolTable
206 SymbolTable& Symbols() {
207 AssertNotMoved();
208 return symbols_;
209 }
210
Ben Clayton708dc2d2021-01-29 11:22:40 +0000211 /// @returns a reference to the program's SymbolTable
212 const SymbolTable& Symbols() const {
213 AssertNotMoved();
214 return symbols_;
215 }
216
Ben Clayton844217f2021-01-27 18:49:05 +0000217 /// @returns a reference to the program's diagnostics
218 diag::List& Diagnostics() {
219 AssertNotMoved();
220 return diagnostics_;
221 }
222
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000223 /// @returns a reference to the program's diagnostics
224 const diag::List& Diagnostics() const {
225 AssertNotMoved();
226 return diagnostics_;
227 }
228
Ben Clayton5f0ea112021-03-09 10:54:37 +0000229 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000230 /// @param enable the new flag value (defaults to true)
231 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
232
Ben Clayton5f0ea112021-03-09 10:54:37 +0000233 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000234 /// built.
235 bool ResolveOnBuild() const { return resolve_on_build_; }
236
Ben Clayton844217f2021-01-27 18:49:05 +0000237 /// @returns true if the program has no error diagnostics and is not missing
238 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000239 bool IsValid() const;
240
Ben Clayton708dc2d2021-01-29 11:22:40 +0000241 /// Writes a representation of the node to the output stream
242 /// @note unlike str(), to_str() does not automatically demangle the string.
243 /// @param node the AST node
244 /// @param out the stream to write to
245 /// @param indent number of spaces to indent the node when writing
246 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
247 node->to_str(Sem(), out, indent);
248 }
249
250 /// Returns a demangled, string representation of `node`.
251 /// @param node the AST node
252 /// @returns a string representation of the node
253 std::string str(const ast::Node* node) const;
254
Ben Clayton7fdfff12021-01-29 15:17:30 +0000255 /// Creates a new ast::Node owned by the ProgramBuilder. When the
256 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000257 /// @param source the Source of the node
258 /// @param args the arguments to pass to the type constructor
259 /// @returns the node pointer
260 template <typename T, typename... ARGS>
261 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
262 ARGS&&... args) {
263 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000264 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000265 }
266
Ben Clayton7fdfff12021-01-29 15:17:30 +0000267 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
268 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000269 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000270 /// When the ProgramBuilder is destructed, the ast::Node will also be
271 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000272 /// @returns the node pointer
273 template <typename T>
274 traits::EnableIfIsType<T, ast::Node>* create() {
275 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000276 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000277 }
278
Ben Clayton7fdfff12021-01-29 15:17:30 +0000279 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
280 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000281 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000282 /// When the ProgramBuilder is destructed, the ast::Node will also be
283 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000284 /// @param arg0 the first arguments to pass to the type constructor
285 /// @param args the remaining arguments to pass to the type constructor
286 /// @returns the node pointer
287 template <typename T, typename ARG0, typename... ARGS>
288 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
289 traits::IsTypeOrDerived<T, ast::Node>::value &&
290 !traits::IsTypeOrDerived<ARG0, Source>::value,
291 T>*
292 create(ARG0&& arg0, ARGS&&... args) {
293 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000294 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000295 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000296 }
297
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000298 /// Creates a new sem::Node owned by the ProgramBuilder.
299 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000300 /// destructed.
301 /// @param args the arguments to pass to the type constructor
302 /// @returns the node pointer
303 template <typename T, typename... ARGS>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000304 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000305 AssertNotMoved();
306 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
307 }
308
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000309 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000310 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
311 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000312 /// Types are unique (de-aliased), and so calling create() for the same `T`
313 /// and arguments will return the same pointer.
314 /// @warning Use this method to acquire a type only if all of its type
315 /// information is provided in the constructor arguments `args`.<br>
316 /// If the type requires additional configuration after construction that
317 /// affect its fundamental type, build the type with `std::make_unique`, make
318 /// any necessary alterations and then call unique_type() instead.
319 /// @param args the arguments to pass to the type constructor
320 /// @returns the de-aliased type pointer
321 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000322 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
323 static_assert(std::is_base_of<sem::Type, T>::value,
324 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000325 AssertNotMoved();
326 return types_.Get<T>(std::forward<ARGS>(args)...);
327 }
328
329 /// Marks this builder as moved, preventing any further use of the builder.
330 void MarkAsMoved();
331
332 //////////////////////////////////////////////////////////////////////////////
333 // TypesBuilder
334 //////////////////////////////////////////////////////////////////////////////
335
336 /// TypesBuilder holds basic `tint` types and methods for constructing
337 /// complex types.
338 class TypesBuilder {
339 public:
340 /// Constructor
341 /// @param builder the program builder
342 explicit TypesBuilder(ProgramBuilder* builder);
343
344 /// @return the tint AST type for the C type `T`.
345 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000346 sem::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000347 return CToAST<T>::get(this);
348 }
349
350 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000351 typ::Bool bool_() const {
352 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
353 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000354
355 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000356 typ::F32 f32() const {
357 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
358 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000359
360 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000361 typ::I32 i32() const {
362 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
363 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000364
365 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000366 typ::U32 u32() const {
367 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
368 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000369
370 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000371 typ::Void void_() const {
372 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
373 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000374
Antonio Maiorano25436862021-04-13 13:32:33 +0000375 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000376 /// @param n vector width in elements
377 /// @return the tint AST type for a `n`-element vector of `type`.
378 typ::Vector vec(typ::Type type, uint32_t n) const {
379 return {builder->create<ast::Vector>(type, n),
380 builder->create<sem::Vector>(type, n)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000381 }
382
383 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000384 /// @return the tint AST type for a 2-element vector of `type`.
385 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
386
387 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000388 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000389 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000390
391 /// @param type vector subtype
392 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000393 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
394
395 /// @param n vector width in elements
396 /// @return the tint AST type for a `n`-element vector of `type`.
397 template <typename T>
398 typ::Vector vec(uint32_t n) const {
399 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000400 }
401
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000402 /// @return the tint AST type for a 2-element vector of the C type `T`.
403 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000404 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000405 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000406 }
407
408 /// @return the tint AST type for a 3-element vector of the C type `T`.
409 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000410 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000411 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000412 }
413
414 /// @return the tint AST type for a 4-element vector of the C type `T`.
415 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000416 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000417 return vec4(Of<T>());
418 }
419
420 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000421 /// @param columns number of columns for the matrix
422 /// @param rows number of rows for the matrix
423 /// @return the tint AST type for a matrix of `type`
424 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
425 return {builder->create<ast::Matrix>(type, rows, columns),
426 builder->create<sem::Matrix>(type, rows, columns)};
427 }
428
429 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000430 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000431 typ::Matrix mat2x2(typ::Type type) const {
432 return {builder->create<ast::Matrix>(type, 2u, 2u),
433 builder->create<sem::Matrix>(type, 2u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000434 }
435
436 /// @param type matrix subtype
437 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000438 typ::Matrix mat2x3(typ::Type type) const {
439 return {builder->create<ast::Matrix>(type, 3u, 2u),
440 builder->create<sem::Matrix>(type, 3u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000441 }
442
443 /// @param type matrix subtype
444 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000445 typ::Matrix mat2x4(typ::Type type) const {
446 return {builder->create<ast::Matrix>(type, 4u, 2u),
447 builder->create<sem::Matrix>(type, 4u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000448 }
449
450 /// @param type matrix subtype
451 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000452 typ::Matrix mat3x2(typ::Type type) const {
453 return {builder->create<ast::Matrix>(type, 2u, 3u),
454 builder->create<sem::Matrix>(type, 2u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000455 }
456
457 /// @param type matrix subtype
458 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000459 typ::Matrix mat3x3(typ::Type type) const {
460 return {builder->create<ast::Matrix>(type, 3u, 3u),
461 builder->create<sem::Matrix>(type, 3u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000462 }
463
464 /// @param type matrix subtype
465 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000466 typ::Matrix mat3x4(typ::Type type) const {
467 return {builder->create<ast::Matrix>(type, 4u, 3u),
468 builder->create<sem::Matrix>(type, 4u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000469 }
470
471 /// @param type matrix subtype
472 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000473 typ::Matrix mat4x2(typ::Type type) const {
474 return {builder->create<ast::Matrix>(type, 2u, 4u),
475 builder->create<sem::Matrix>(type, 2u, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000476 }
477
478 /// @param type matrix subtype
479 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000480 typ::Matrix mat4x3(typ::Type type) const {
481 return {builder->create<ast::Matrix>(type, 3u, 4u),
482 builder->create<sem::Matrix>(type, 3u, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000483 }
484
485 /// @param type matrix subtype
486 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000487 typ::Matrix mat4x4(typ::Type type) const {
488 return {builder->create<ast::Matrix>(type, 4u, 4u),
489 builder->create<sem::Matrix>(type, 4u, 4u)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000490 }
491
Ben Claytone204f272021-04-22 14:40:23 +0000492 /// @param columns number of columns for the matrix
493 /// @param rows number of rows for the matrix
494 /// @return the tint AST type for a matrix of `type`
495 template <typename T>
496 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
497 return mat(Of<T>(), columns, rows);
498 }
499
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000500 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
501 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000502 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000503 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000504 }
505
506 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
507 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000508 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000509 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000510 }
511
512 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
513 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000514 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000515 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000516 }
517
518 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
519 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000520 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000521 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000522 }
523
524 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
525 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000526 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000527 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000528 }
529
530 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
531 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000532 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000533 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000534 }
535
536 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
537 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000538 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000539 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000540 }
541
542 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
543 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000544 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000545 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000546 }
547
548 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
549 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000550 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000551 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000552 }
553
554 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000555 /// @param n the array size. 0 represents a runtime-array
556 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000557 /// @return the tint AST type for a array of size `n` of type `T`
Ben Claytone204f272021-04-22 14:40:23 +0000558 typ::Array array(typ::Type subtype,
559 uint32_t n = 0,
560 ast::DecorationList decos = {}) const {
561 return {builder->create<ast::Array>(subtype, n, decos),
562 builder->create<sem::ArrayType>(subtype, n, decos)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000563 }
564
Ben Claytonbab31972021-02-08 21:16:21 +0000565 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000566 /// @param n the array size. 0 represents a runtime-array
567 /// @param stride the array stride
Ben Claytonbab31972021-02-08 21:16:21 +0000568 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton7241a502021-04-22 14:32:53 +0000569 typ::Array array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Claytone204f272021-04-22 14:40:23 +0000570 return array(subtype, n,
571 {builder->create<ast::StrideDecoration>(stride)});
Ben Claytonbab31972021-02-08 21:16:21 +0000572 }
573
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000574 /// @return the tint AST type for an array of size `N` of type `T`
575 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000576 typ::Array array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 return array(Of<T>(), N);
578 }
579
Ben Claytonbab31972021-02-08 21:16:21 +0000580 /// @param stride the array stride
581 /// @return the tint AST type for an array of size `N` of type `T`
582 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000583 typ::Array array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000584 return array(Of<T>(), N, stride);
585 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000586
Ben Clayton42d1e092021-02-02 14:29:15 +0000587 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000588 /// @param name the alias name
589 /// @param type the alias type
590 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000591 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +0000592 typ::Alias alias(NAME&& name, typ::Type type) const {
593 auto sym = builder->Sym(std::forward<NAME>(name));
594 return {
595 builder->create<ast::Alias>(sym, type),
596 builder->create<sem::Alias>(sym, type),
597 };
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000598 }
599
Ben Clayton4db10dd2021-04-16 08:47:14 +0000600 /// Creates an access control qualifier type
601 /// @param access the access control
602 /// @param type the inner type
603 /// @returns the access control qualifier type
Ben Claytone204f272021-04-22 14:40:23 +0000604 typ::AccessControl access(ast::AccessControl::Access access,
605 typ::Type type) const {
606 return {builder->create<ast::AccessControl>(access, type),
607 builder->create<sem::AccessControl>(access, type)};
Ben Clayton4db10dd2021-04-16 08:47:14 +0000608 }
609
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000610 /// @param type the type of the pointer
611 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000612 /// @return the pointer to `type` with the given ast::StorageClass
613 typ::Pointer pointer(typ::Type type,
614 ast::StorageClass storage_class) const {
615 return {builder->create<ast::Pointer>(type, storage_class),
616 builder->create<sem::Pointer>(type, storage_class)};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000617 }
618
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000619 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000620 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000621 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000622 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000623 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000624 }
625
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000626 /// @param impl the struct implementation
627 /// @returns a struct pointer
Ben Claytone204f272021-04-22 14:40:23 +0000628 typ::Struct struct_(ast::Struct* impl) const {
629 return {impl, builder->create<sem::StructType>(impl)};
630 }
631
632 /// @param kind the kind of sampler
633 /// @returns the sampler
634 typ::Sampler sampler(ast::SamplerKind kind) const {
635 return {builder->create<ast::Sampler>(kind),
636 builder->create<sem::Sampler>(kind)};
637 }
638
639 /// @param dims the dimensionality of the texture
640 /// @returns the depth texture
641 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
642 return {builder->create<ast::DepthTexture>(dims),
643 builder->create<sem::DepthTexture>(dims)};
644 }
645
646 /// @param dims the dimensionality of the texture
647 /// @param subtype the texture subtype.
648 /// @returns the sampled texture
649 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
650 typ::Type subtype) const {
651 return {builder->create<ast::SampledTexture>(dims, subtype),
652 builder->create<sem::SampledTexture>(dims, subtype)};
653 }
654
655 /// @param dims the dimensionality of the texture
656 /// @param subtype the texture subtype.
657 /// @returns the multisampled texture
658 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
659 typ::Type subtype) const {
660 return {builder->create<ast::MultisampledTexture>(dims, subtype),
661 builder->create<sem::MultisampledTexture>(dims, subtype)};
662 }
663
664 /// @param dims the dimensionality of the texture
665 /// @param format the image format of the texture
666 /// @returns the storage texture
667 typ::StorageTexture storage_texture(ast::TextureDimension dims,
668 ast::ImageFormat format) const {
669 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
670 auto* sem_subtype =
671 sem::StorageTexture::SubtypeFor(format, builder->Types());
672 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
673 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000674 }
675
676 private:
677 /// CToAST<T> is specialized for various `T` types and each specialization
678 /// contains a single static `get()` method for obtaining the corresponding
679 /// AST type for the C type `T`.
680 /// `get()` has the signature:
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000681 /// `static sem::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000682 template <typename T>
683 struct CToAST {};
684
Ben Claytonc7ca7662021-02-17 16:23:52 +0000685 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000686 };
687
688 //////////////////////////////////////////////////////////////////////////////
689 // AST helper methods
690 //////////////////////////////////////////////////////////////////////////////
691
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000692 /// @param name the symbol string
693 /// @return a Symbol with the given name
694 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
695
696 /// @param sym the symbol
697 /// @return `sym`
698 Symbol Sym(Symbol sym) { return sym; }
699
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000700 /// @param expr the expression
701 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000702 template <typename T>
703 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
704 return expr;
705 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000706
Ben Claytonb8ea5912021-04-19 16:52:42 +0000707 /// Passthrough for nullptr
708 /// @return nullptr
709 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
710
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000711 /// @param name the identifier name
712 /// @return an ast::IdentifierExpression with the given name
713 ast::IdentifierExpression* Expr(const std::string& name) {
714 return create<ast::IdentifierExpression>(Symbols().Register(name));
715 }
716
Ben Clayton46d78d72021-02-10 21:34:26 +0000717 /// @param symbol the identifier symbol
718 /// @return an ast::IdentifierExpression with the given symbol
719 ast::IdentifierExpression* Expr(Symbol symbol) {
720 return create<ast::IdentifierExpression>(symbol);
721 }
722
Ben Claytonb8ea5912021-04-19 16:52:42 +0000723 /// @param variable the AST variable
724 /// @return an ast::IdentifierExpression with the variable's symbol
725 ast::IdentifierExpression* Expr(ast::Variable* variable) {
726 return create<ast::IdentifierExpression>(variable->symbol());
727 }
728
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000729 /// @param source the source information
730 /// @param name the identifier name
731 /// @return an ast::IdentifierExpression with the given name
732 ast::IdentifierExpression* Expr(const Source& source,
733 const std::string& name) {
734 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
735 }
736
737 /// @param name the identifier name
738 /// @return an ast::IdentifierExpression with the given name
739 ast::IdentifierExpression* Expr(const char* name) {
740 return create<ast::IdentifierExpression>(Symbols().Register(name));
741 }
742
743 /// @param value the boolean value
744 /// @return a Scalar constructor for the given value
745 ast::ScalarConstructorExpression* Expr(bool value) {
746 return create<ast::ScalarConstructorExpression>(Literal(value));
747 }
748
749 /// @param value the float value
750 /// @return a Scalar constructor for the given value
751 ast::ScalarConstructorExpression* Expr(f32 value) {
752 return create<ast::ScalarConstructorExpression>(Literal(value));
753 }
754
755 /// @param value the integer value
756 /// @return a Scalar constructor for the given value
757 ast::ScalarConstructorExpression* Expr(i32 value) {
758 return create<ast::ScalarConstructorExpression>(Literal(value));
759 }
760
761 /// @param value the unsigned int value
762 /// @return a Scalar constructor for the given value
763 ast::ScalarConstructorExpression* Expr(u32 value) {
764 return create<ast::ScalarConstructorExpression>(Literal(value));
765 }
766
767 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
768 /// `list`.
769 /// @param list the list to append too
770 /// @param arg the arg to create
771 template <typename ARG>
772 void Append(ast::ExpressionList& list, ARG&& arg) {
773 list.emplace_back(Expr(std::forward<ARG>(arg)));
774 }
775
776 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
777 /// then appends them to `list`.
778 /// @param list the list to append too
779 /// @param arg0 the first argument
780 /// @param args the rest of the arguments
781 template <typename ARG0, typename... ARGS>
782 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
783 Append(list, std::forward<ARG0>(arg0));
784 Append(list, std::forward<ARGS>(args)...);
785 }
786
787 /// @return an empty list of expressions
788 ast::ExpressionList ExprList() { return {}; }
789
790 /// @param args the list of expressions
791 /// @return the list of expressions converted to `ast::Expression`s using
792 /// `Expr()`,
793 template <typename... ARGS>
794 ast::ExpressionList ExprList(ARGS&&... args) {
795 ast::ExpressionList list;
796 list.reserve(sizeof...(args));
797 Append(list, std::forward<ARGS>(args)...);
798 return list;
799 }
800
801 /// @param list the list of expressions
802 /// @return `list`
803 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
804
805 /// @param val the boolan value
806 /// @return a boolean literal with the given value
807 ast::BoolLiteral* Literal(bool val) {
808 return create<ast::BoolLiteral>(ty.bool_(), val);
809 }
810
811 /// @param val the float value
812 /// @return a float literal with the given value
813 ast::FloatLiteral* Literal(f32 val) {
814 return create<ast::FloatLiteral>(ty.f32(), val);
815 }
816
817 /// @param val the unsigned int value
818 /// @return a ast::UintLiteral with the given value
819 ast::UintLiteral* Literal(u32 val) {
820 return create<ast::UintLiteral>(ty.u32(), val);
821 }
822
823 /// @param val the integer value
824 /// @return the ast::SintLiteral with the given value
825 ast::SintLiteral* Literal(i32 val) {
826 return create<ast::SintLiteral>(ty.i32(), val);
827 }
828
829 /// @param args the arguments for the type constructor
830 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
831 /// of `args` converted to `ast::Expression`s using `Expr()`
832 template <typename T, typename... ARGS>
833 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
834 return create<ast::TypeConstructorExpression>(
835 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
836 }
837
838 /// @param type the type to construct
839 /// @param args the arguments for the constructor
840 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
841 /// values `args`.
842 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000843 ast::TypeConstructorExpression* Construct(sem::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000844 return create<ast::TypeConstructorExpression>(
845 type, ExprList(std::forward<ARGS>(args)...));
846 }
847
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000848 /// Creates a constructor expression that constructs an object of
849 /// `type` filled with `elem_value`. For example,
850 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
851 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
852 /// @param type the type to construct
853 /// @param elem_value the initial or element value (for vec and mat) to
854 /// construct with
855 /// @return the constructor expression
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000856 ast::ConstructorExpression* ConstructValueFilledWith(sem::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000857 int elem_value = 0);
858
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000859 /// @param args the arguments for the vector constructor
860 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
861 /// `T`, constructed with the values `args`.
862 template <typename T, typename... ARGS>
863 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
864 return create<ast::TypeConstructorExpression>(
865 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
866 }
867
868 /// @param args the arguments for the vector constructor
869 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
870 /// `T`, constructed with the values `args`.
871 template <typename T, typename... ARGS>
872 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
873 return create<ast::TypeConstructorExpression>(
874 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
875 }
876
877 /// @param args the arguments for the vector constructor
878 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
879 /// `T`, constructed with the values `args`.
880 template <typename T, typename... ARGS>
881 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
882 return create<ast::TypeConstructorExpression>(
883 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
884 }
885
886 /// @param args the arguments for the matrix constructor
887 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
888 /// `T`, constructed with the values `args`.
889 template <typename T, typename... ARGS>
890 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
891 return create<ast::TypeConstructorExpression>(
892 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
893 }
894
895 /// @param args the arguments for the matrix constructor
896 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
897 /// `T`, constructed with the values `args`.
898 template <typename T, typename... ARGS>
899 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
900 return create<ast::TypeConstructorExpression>(
901 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
902 }
903
904 /// @param args the arguments for the matrix constructor
905 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
906 /// `T`, constructed with the values `args`.
907 template <typename T, typename... ARGS>
908 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
909 return create<ast::TypeConstructorExpression>(
910 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
911 }
912
913 /// @param args the arguments for the matrix constructor
914 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
915 /// `T`, constructed with the values `args`.
916 template <typename T, typename... ARGS>
917 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
918 return create<ast::TypeConstructorExpression>(
919 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
920 }
921
922 /// @param args the arguments for the matrix constructor
923 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
924 /// `T`, constructed with the values `args`.
925 template <typename T, typename... ARGS>
926 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
927 return create<ast::TypeConstructorExpression>(
928 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
929 }
930
931 /// @param args the arguments for the matrix constructor
932 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
933 /// `T`, constructed with the values `args`.
934 template <typename T, typename... ARGS>
935 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
936 return create<ast::TypeConstructorExpression>(
937 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
938 }
939
940 /// @param args the arguments for the matrix constructor
941 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
942 /// `T`, constructed with the values `args`.
943 template <typename T, typename... ARGS>
944 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
945 return create<ast::TypeConstructorExpression>(
946 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
947 }
948
949 /// @param args the arguments for the matrix constructor
950 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
951 /// `T`, constructed with the values `args`.
952 template <typename T, typename... ARGS>
953 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
954 return create<ast::TypeConstructorExpression>(
955 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
956 }
957
958 /// @param args the arguments for the matrix constructor
959 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
960 /// `T`, constructed with the values `args`.
961 template <typename T, typename... ARGS>
962 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
963 return create<ast::TypeConstructorExpression>(
964 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
965 }
966
967 /// @param args the arguments for the array constructor
968 /// @return an `ast::TypeConstructorExpression` of an array with element type
969 /// `T`, constructed with the values `args`.
970 template <typename T, int N = 0, typename... ARGS>
971 ast::TypeConstructorExpression* array(ARGS&&... args) {
972 return create<ast::TypeConstructorExpression>(
973 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
974 }
975
976 /// @param subtype the array element type
977 /// @param n the array size. 0 represents a runtime-array.
978 /// @param args the arguments for the array constructor
979 /// @return an `ast::TypeConstructorExpression` of an array with element type
980 /// `subtype`, constructed with the values `args`.
981 template <typename... ARGS>
Ben Claytone204f272021-04-22 14:40:23 +0000982 ast::TypeConstructorExpression* array(typ::Type subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000983 uint32_t n,
984 ARGS&&... args) {
985 return create<ast::TypeConstructorExpression>(
986 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
987 }
988
989 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000990 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000991 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000992 /// @param constructor constructor expression
993 /// @param decorations variable decorations
994 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000995 template <typename NAME>
996 ast::Variable* Var(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000997 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000998 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000999 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001000 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001001 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1002 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001003 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001004
1005 /// @param source the variable source
1006 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001007 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001008 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001009 /// @param constructor constructor expression
1010 /// @param decorations variable decorations
1011 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001012 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001013 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001014 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001015 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001016 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001017 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001018 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001019 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001020 type, false, constructor, decorations);
1021 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001022
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001023 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001024 /// @param type the variable type
1025 /// @param constructor optional constructor expression
1026 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001027 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001028 template <typename NAME>
1029 ast::Variable* Const(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001030 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001031 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001032 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001033 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001034 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001035 constructor, decorations);
1036 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001037
1038 /// @param source the variable source
1039 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001040 /// @param type the variable type
1041 /// @param constructor optional constructor expression
1042 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001043 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001044 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001045 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001046 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001047 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001048 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001049 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001050 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001051 ast::StorageClass::kNone, type, true,
1052 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001053 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001054
James Pricedaf1f1c2021-04-08 22:15:48 +00001055 /// @param name the parameter name
1056 /// @param type the parameter type
1057 /// @param decorations optional parameter decorations
1058 /// @returns a constant `ast::Variable` with the given name and type
1059 template <typename NAME>
1060 ast::Variable* Param(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001061 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001062 ast::DecorationList decorations = {}) {
1063 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1064 ast::StorageClass::kNone, type, true, nullptr,
1065 decorations);
1066 }
1067
1068 /// @param source the parameter source
1069 /// @param name the parameter name
1070 /// @param type the parameter type
1071 /// @param decorations optional parameter decorations
1072 /// @returns a constant `ast::Variable` with the given name and type
1073 template <typename NAME>
1074 ast::Variable* Param(const Source& source,
1075 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001076 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001077 ast::DecorationList decorations = {}) {
1078 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1079 ast::StorageClass::kNone, type, true, nullptr,
1080 decorations);
1081 }
1082
Ben Clayton42708342021-04-21 17:55:12 +00001083 /// @param name the variable name
1084 /// @param type the variable type
1085 /// @param storage the variable storage class
1086 /// @param constructor constructor expression
1087 /// @param decorations variable decorations
1088 /// @returns a new `ast::Variable`, which is automatically registered as a
1089 /// global variable with the ast::Module.
1090 template <typename NAME>
1091 ast::Variable* Global(NAME&& name,
1092 sem::Type* type,
1093 ast::StorageClass storage,
1094 ast::Expression* constructor = nullptr,
1095 ast::DecorationList decorations = {}) {
1096 auto* var =
1097 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1098 AST().AddGlobalVariable(var);
1099 return var;
1100 }
1101
1102 /// @param source the variable source
1103 /// @param name the variable name
1104 /// @param type the variable type
1105 /// @param storage the variable storage class
1106 /// @param constructor constructor expression
1107 /// @param decorations variable decorations
1108 /// @returns a new `ast::Variable`, which is automatically registered as a
1109 /// global variable with the ast::Module.
1110 template <typename NAME>
1111 ast::Variable* Global(const Source& source,
1112 NAME&& name,
1113 sem::Type* type,
1114 ast::StorageClass storage,
1115 ast::Expression* constructor = nullptr,
1116 ast::DecorationList decorations = {}) {
1117 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1118 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001119 AST().AddGlobalVariable(var);
1120 return var;
1121 }
1122
1123 /// @param args the arguments to pass to Const()
1124 /// @returns a const `ast::Variable` constructed by calling Var() with the
1125 /// arguments of `args`, which is automatically registered as a global
1126 /// variable with the ast::Module.
1127 template <typename... ARGS>
1128 ast::Variable* GlobalConst(ARGS&&... args) {
1129 auto* var = Const(std::forward<ARGS>(args)...);
1130 AST().AddGlobalVariable(var);
1131 return var;
1132 }
1133
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001134 /// @param func the function name
1135 /// @param args the function call arguments
1136 /// @returns a `ast::CallExpression` to the function `func`, with the
1137 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1138 template <typename NAME, typename... ARGS>
1139 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1140 return create<ast::CallExpression>(Expr(func),
1141 ExprList(std::forward<ARGS>(args)...));
1142 }
1143
1144 /// @param lhs the left hand argument to the addition operation
1145 /// @param rhs the right hand argument to the addition operation
1146 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1147 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001148 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001149 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1150 Expr(std::forward<LHS>(lhs)),
1151 Expr(std::forward<RHS>(rhs)));
1152 }
1153
1154 /// @param lhs the left hand argument to the subtraction operation
1155 /// @param rhs the right hand argument to the subtraction operation
1156 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1157 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001158 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001159 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1160 Expr(std::forward<LHS>(lhs)),
1161 Expr(std::forward<RHS>(rhs)));
1162 }
1163
1164 /// @param lhs the left hand argument to the multiplication operation
1165 /// @param rhs the right hand argument to the multiplication operation
1166 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1167 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001168 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001169 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1170 Expr(std::forward<LHS>(lhs)),
1171 Expr(std::forward<RHS>(rhs)));
1172 }
1173
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001174 /// @param source the source information
1175 /// @param lhs the left hand argument to the multiplication operation
1176 /// @param rhs the right hand argument to the multiplication operation
1177 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1178 template <typename LHS, typename RHS>
1179 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1180 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1181 Expr(std::forward<LHS>(lhs)),
1182 Expr(std::forward<RHS>(rhs)));
1183 }
1184
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001185 /// @param lhs the left hand argument to the division operation
1186 /// @param rhs the right hand argument to the division operation
1187 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1188 template <typename LHS, typename RHS>
1189 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1190 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1191 Expr(std::forward<LHS>(lhs)),
1192 Expr(std::forward<RHS>(rhs)));
1193 }
1194
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001195 /// @param arr the array argument for the array accessor expression
1196 /// @param idx the index argument for the array accessor expression
1197 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1198 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001199 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001200 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1201 Expr(std::forward<IDX>(idx)));
1202 }
1203
1204 /// @param obj the object for the member accessor expression
1205 /// @param idx the index argument for the array accessor expression
1206 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1207 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001208 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001209 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1210 Expr(std::forward<IDX>(idx)));
1211 }
1212
Ben Clayton42d1e092021-02-02 14:29:15 +00001213 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001214 /// @param val the offset value
1215 /// @returns the offset decoration pointer
1216 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1217 return create<ast::StructMemberOffsetDecoration>(source_, val);
1218 }
1219
Ben Claytond614dd52021-03-15 10:43:11 +00001220 /// Creates a ast::StructMemberSizeDecoration
1221 /// @param source the source information
1222 /// @param val the size value
1223 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001224 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1225 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001226 return create<ast::StructMemberSizeDecoration>(source, val);
1227 }
1228
1229 /// Creates a ast::StructMemberSizeDecoration
1230 /// @param val the size value
1231 /// @returns the size decoration pointer
1232 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1233 return create<ast::StructMemberSizeDecoration>(source_, val);
1234 }
1235
1236 /// Creates a ast::StructMemberAlignDecoration
1237 /// @param source the source information
1238 /// @param val the align value
1239 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001240 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1241 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001242 return create<ast::StructMemberAlignDecoration>(source, val);
1243 }
1244
1245 /// Creates a ast::StructMemberAlignDecoration
1246 /// @param val the align value
1247 /// @returns the align decoration pointer
1248 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1249 return create<ast::StructMemberAlignDecoration>(source_, val);
1250 }
1251
Ben Clayton42d1e092021-02-02 14:29:15 +00001252 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253 /// @param source the source information
1254 /// @param name the function name
1255 /// @param params the function parameters
1256 /// @param type the function return type
1257 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001258 /// @param decorations the optional function decorations
1259 /// @param return_type_decorations the optional function return type
1260 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001261 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001262 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001263 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001264 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001266 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001267 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001268 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001269 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001270 auto* func =
1271 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1272 type, create<ast::BlockStatement>(body),
1273 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001274 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001275 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001276 }
1277
Ben Clayton42d1e092021-02-02 14:29:15 +00001278 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001279 /// @param name the function name
1280 /// @param params the function parameters
1281 /// @param type the function return type
1282 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001283 /// @param decorations the optional function decorations
1284 /// @param return_type_decorations the optional function return type
1285 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001286 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001287 template <typename NAME>
1288 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001289 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001290 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001292 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001293 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001294 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1295 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001296 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001297 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001298 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001299 }
1300
Ben Clayton49668bb2021-04-17 05:32:01 +00001301 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001302 /// @param source the source information
1303 /// @returns the return statement pointer
1304 ast::ReturnStatement* Return(const Source& source) {
1305 return create<ast::ReturnStatement>(source);
1306 }
1307
1308 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001309 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001310 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1311
1312 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001313 /// @param source the source information
1314 /// @param val the return value
1315 /// @returns the return statement pointer
1316 template <typename EXPR>
1317 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1318 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1319 }
1320
1321 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001322 /// @param val the return value
1323 /// @returns the return statement pointer
1324 template <typename EXPR>
1325 ast::ReturnStatement* Return(EXPR&& val) {
1326 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001327 }
1328
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001329 /// Creates a ast::Struct and sem::StructType, registering the
1330 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001331 /// @param source the source information
1332 /// @param name the struct name
1333 /// @param members the struct members
1334 /// @param decorations the optional struct decorations
1335 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001336 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001337 typ::Struct Structure(const Source& source,
1338 NAME&& name,
1339 ast::StructMemberList members,
1340 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001341 auto sym = Sym(std::forward<NAME>(name));
1342 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1343 std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001344 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001345 AST().AddConstructedType(type);
1346 return type;
1347 }
1348
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001349 /// Creates a ast::Struct and sem::StructType, registering the
1350 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001351 /// @param name the struct name
1352 /// @param members the struct members
1353 /// @param decorations the optional struct decorations
1354 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001355 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001356 typ::Struct Structure(NAME&& name,
1357 ast::StructMemberList members,
1358 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001359 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001360 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001361 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001362 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001363 AST().AddConstructedType(type);
1364 return type;
1365 }
1366
Ben Clayton42d1e092021-02-02 14:29:15 +00001367 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001368 /// @param source the source information
1369 /// @param name the struct member name
1370 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001371 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001372 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001373 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001374 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001375 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001376 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001377 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001378 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1379 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001380 }
1381
Ben Clayton42d1e092021-02-02 14:29:15 +00001382 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001383 /// @param name the struct member name
1384 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001385 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001386 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001387 template <typename NAME>
1388 ast::StructMember* Member(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001389 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001390 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001391 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1392 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001393 }
1394
Ben Claytonbab31972021-02-08 21:16:21 +00001395 /// Creates a ast::StructMember with the given byte offset
1396 /// @param offset the offset to use in the StructMemberOffsetDecoration
1397 /// @param name the struct member name
1398 /// @param type the struct member type
1399 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001400 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001401 ast::StructMember* Member(uint32_t offset, NAME&& name, sem::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001402 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001403 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001404 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001405 create<ast::StructMemberOffsetDecoration>(offset),
1406 });
1407 }
1408
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001409 /// Creates a ast::BlockStatement with input statements
1410 /// @param statements statements of block
1411 /// @returns the block statement pointer
1412 template <typename... Statements>
1413 ast::BlockStatement* Block(Statements&&... statements) {
1414 return create<ast::BlockStatement>(
1415 ast::StatementList{std::forward<Statements>(statements)...});
1416 }
1417
1418 /// Creates a ast::ElseStatement with input condition and body
1419 /// @param condition the else condition expression
1420 /// @param body the else body
1421 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001422 template <typename CONDITION>
1423 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1424 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1425 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001426 }
1427
1428 /// Creates a ast::IfStatement with input condition, body, and optional
1429 /// variadic else statements
1430 /// @param condition the if statement condition expression
1431 /// @param body the if statement body
1432 /// @param elseStatements optional variadic else statements
1433 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001434 template <typename CONDITION, typename... ELSE_STATEMENTS>
1435 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001436 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001437 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001438 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001439 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001440 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001441 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001442 }
1443
1444 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001445 /// @param source the source information
1446 /// @param lhs the left hand side expression initializer
1447 /// @param rhs the right hand side expression initializer
1448 /// @returns the assignment statement pointer
1449 template <typename LhsExpressionInit, typename RhsExpressionInit>
1450 ast::AssignmentStatement* Assign(const Source& source,
1451 LhsExpressionInit&& lhs,
1452 RhsExpressionInit&& rhs) {
1453 return create<ast::AssignmentStatement>(
1454 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1455 Expr(std::forward<RhsExpressionInit>(rhs)));
1456 }
1457
1458 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001459 /// @param lhs the left hand side expression initializer
1460 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001461 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001462 template <typename LhsExpressionInit, typename RhsExpressionInit>
1463 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1464 RhsExpressionInit&& rhs) {
1465 return create<ast::AssignmentStatement>(
1466 Expr(std::forward<LhsExpressionInit>(lhs)),
1467 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001468 }
1469
1470 /// Creates a ast::LoopStatement with input body and optional continuing
1471 /// @param body the loop body
1472 /// @param continuing the optional continuing block
1473 /// @returns the loop statement pointer
1474 ast::LoopStatement* Loop(ast::BlockStatement* body,
1475 ast::BlockStatement* continuing = nullptr) {
1476 return create<ast::LoopStatement>(body, continuing);
1477 }
1478
1479 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001480 /// @param source the source information
1481 /// @param var the variable to wrap in a decl statement
1482 /// @returns the variable decl statement pointer
1483 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1484 return create<ast::VariableDeclStatement>(source, var);
1485 }
1486
1487 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001488 /// @param var the variable to wrap in a decl statement
1489 /// @returns the variable decl statement pointer
1490 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1491 return create<ast::VariableDeclStatement>(var);
1492 }
1493
Antonio Maioranocea744d2021-03-25 12:55:27 +00001494 /// Creates a ast::SwitchStatement with input expression and cases
1495 /// @param condition the condition expression initializer
1496 /// @param cases case statements
1497 /// @returns the switch statement pointer
1498 template <typename ExpressionInit, typename... Cases>
1499 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1500 return create<ast::SwitchStatement>(
1501 Expr(std::forward<ExpressionInit>(condition)),
1502 ast::CaseStatementList{std::forward<Cases>(cases)...});
1503 }
1504
1505 /// Creates a ast::CaseStatement with input list of selectors, and body
1506 /// @param selectors list of selectors
1507 /// @param body the case body
1508 /// @returns the case statement pointer
1509 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1510 ast::BlockStatement* body = nullptr) {
1511 return create<ast::CaseStatement>(std::move(selectors),
1512 body ? body : Block());
1513 }
1514
1515 /// Convenient overload that takes a single selector
1516 /// @param selector a single case selector
1517 /// @param body the case body
1518 /// @returns the case statement pointer
1519 ast::CaseStatement* Case(ast::IntLiteral* selector,
1520 ast::BlockStatement* body = nullptr) {
1521 return Case(ast::CaseSelectorList{selector}, body);
1522 }
1523
1524 /// Convenience function that creates a 'default' ast::CaseStatement
1525 /// @param body the case body
1526 /// @returns the case statement pointer
1527 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1528 return Case(ast::CaseSelectorList{}, body);
1529 }
1530
James Price68f558f2021-04-06 15:51:47 +00001531 /// Creates an ast::BuiltinDecoration
1532 /// @param source the source information
1533 /// @param builtin the builtin value
1534 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001535 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001536 return create<ast::BuiltinDecoration>(source, builtin);
1537 }
1538
1539 /// Creates an ast::BuiltinDecoration
1540 /// @param builtin the builtin value
1541 /// @returns the builtin decoration pointer
1542 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1543 return create<ast::BuiltinDecoration>(source_, builtin);
1544 }
1545
1546 /// Creates an ast::LocationDecoration
1547 /// @param source the source information
1548 /// @param location the location value
1549 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001550 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001551 return create<ast::LocationDecoration>(source, location);
1552 }
1553
1554 /// Creates an ast::LocationDecoration
1555 /// @param location the location value
1556 /// @returns the location decoration pointer
1557 ast::LocationDecoration* Location(uint32_t location) {
1558 return create<ast::LocationDecoration>(source_, location);
1559 }
1560
1561 /// Creates an ast::StageDecoration
1562 /// @param source the source information
1563 /// @param stage the pipeline stage
1564 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001565 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001566 return create<ast::StageDecoration>(source, stage);
1567 }
1568
1569 /// Creates an ast::StageDecoration
1570 /// @param stage the pipeline stage
1571 /// @returns the stage decoration pointer
1572 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1573 return create<ast::StageDecoration>(source_, stage);
1574 }
1575
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001576 /// Sets the current builder source to `src`
1577 /// @param src the Source used for future create() calls
1578 void SetSource(const Source& src) {
1579 AssertNotMoved();
1580 source_ = src;
1581 }
1582
1583 /// Sets the current builder source to `loc`
1584 /// @param loc the Source used for future create() calls
1585 void SetSource(const Source::Location& loc) {
1586 AssertNotMoved();
1587 source_ = Source(loc);
1588 }
1589
Ben Clayton33352542021-01-29 16:43:41 +00001590 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001591 /// @note As the Resolver is run when the Program is built, this will only be
1592 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001593 /// @param expr the AST expression
1594 /// @return the resolved semantic type for the expression, or nullptr if the
1595 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001596 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001597
Ben Clayton169512e2021-04-17 05:52:11 +00001598 /// Wraps the ast::Literal in a statement. This is used by tests that
1599 /// construct a partial AST and require the Resolver to reach these
1600 /// nodes.
1601 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1602 /// @return the ast::Statement that wraps the ast::Statement
1603 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001604 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001605 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001606 /// nodes.
1607 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1608 /// @return the ast::Statement that wraps the ast::Expression
1609 ast::Statement* WrapInStatement(ast::Expression* expr);
1610 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001611 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001612 /// these nodes.
1613 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1614 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1615 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1616 /// Returns the statement argument. Used as a passthrough-overload by
1617 /// WrapInFunction().
1618 /// @param stmt the ast::Statement
1619 /// @return `stmt`
1620 ast::Statement* WrapInStatement(ast::Statement* stmt);
1621 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001622 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001623 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001624 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001625 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001626 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001627 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001628 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001629 }
1630 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001631 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001632 /// @returns the function
1633 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001634
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001635 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001636 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001637
1638 protected:
1639 /// Asserts that the builder has not been moved.
1640 void AssertNotMoved() const;
1641
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001642 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001643 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001644 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001645 ASTNodeAllocator ast_nodes_;
1646 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001647 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001648 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001649 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001650 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001651
1652 /// The source to use when creating AST nodes without providing a Source as
1653 /// the first argument.
1654 Source source_;
1655
Ben Clayton5f0ea112021-03-09 10:54:37 +00001656 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001657 /// program when built.
1658 bool resolve_on_build_ = true;
1659
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001660 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1661 bool moved_ = false;
1662};
1663
1664//! @cond Doxygen_Suppress
1665// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1666template <>
1667struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001668 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001669 return t->i32();
1670 }
1671};
1672template <>
1673struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001674 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001675 return t->u32();
1676 }
1677};
1678template <>
1679struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001680 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001681 return t->f32();
1682 }
1683};
1684template <>
1685struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001686 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001687 return t->bool_();
1688 }
1689};
1690template <>
1691struct ProgramBuilder::TypesBuilder::CToAST<void> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001692 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001693 return t->void_();
1694 }
1695};
1696//! @endcond
1697
Ben Clayton917b14b2021-04-19 16:50:23 +00001698/// @param builder the ProgramBuilder
1699/// @returns the ProgramID of the ProgramBuilder
1700inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1701 return builder->ID();
1702}
1703
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001704} // namespace tint
1705
1706#endif // SRC_PROGRAM_BUILDER_H_