blob: 1b5da81f9c64e87dc06de99c2eb2cc1ee1487159 [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>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000346 typ::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 {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000561 subtype = MaybeCreateTypename(subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000562 return {builder->create<ast::Array>(subtype, n, decos),
563 builder->create<sem::ArrayType>(subtype, n, decos)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000564 }
565
Ben Claytonbab31972021-02-08 21:16:21 +0000566 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000567 /// @param n the array size. 0 represents a runtime-array
568 /// @param stride the array stride
Ben Claytonbab31972021-02-08 21:16:21 +0000569 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton7241a502021-04-22 14:32:53 +0000570 typ::Array array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000571 subtype = MaybeCreateTypename(subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000572 return array(subtype, n,
573 {builder->create<ast::StrideDecoration>(stride)});
Ben Claytonbab31972021-02-08 21:16:21 +0000574 }
575
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000576 /// @return the tint AST type for an array of size `N` of type `T`
577 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000578 typ::Array array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 return array(Of<T>(), N);
580 }
581
Ben Claytonbab31972021-02-08 21:16:21 +0000582 /// @param stride the array stride
583 /// @return the tint AST type for an array of size `N` of type `T`
584 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000585 typ::Array array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000586 return array(Of<T>(), N, stride);
587 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000588
Ben Clayton42d1e092021-02-02 14:29:15 +0000589 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000590 /// @param name the alias name
591 /// @param type the alias type
592 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000593 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +0000594 typ::Alias alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000595 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000596 auto sym = builder->Sym(std::forward<NAME>(name));
597 return {
598 builder->create<ast::Alias>(sym, type),
599 builder->create<sem::Alias>(sym, type),
600 };
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000601 }
602
Ben Clayton4db10dd2021-04-16 08:47:14 +0000603 /// Creates an access control qualifier type
604 /// @param access the access control
605 /// @param type the inner type
606 /// @returns the access control qualifier type
Ben Claytone204f272021-04-22 14:40:23 +0000607 typ::AccessControl access(ast::AccessControl::Access access,
608 typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000609 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000610 return {builder->create<ast::AccessControl>(access, type),
611 builder->create<sem::AccessControl>(access, type)};
Ben Clayton4db10dd2021-04-16 08:47:14 +0000612 }
613
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000614 /// @param type the type of the pointer
615 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000616 /// @return the pointer to `type` with the given ast::StorageClass
617 typ::Pointer pointer(typ::Type type,
618 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000619 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000620 return {builder->create<ast::Pointer>(type, storage_class),
621 builder->create<sem::Pointer>(type, storage_class)};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000622 }
623
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000624 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000625 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000626 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000627 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000628 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000629 }
630
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000631 /// @param impl the struct implementation
632 /// @returns a struct pointer
Ben Claytone204f272021-04-22 14:40:23 +0000633 typ::Struct struct_(ast::Struct* impl) const {
634 return {impl, builder->create<sem::StructType>(impl)};
635 }
636
637 /// @param kind the kind of sampler
638 /// @returns the sampler
639 typ::Sampler sampler(ast::SamplerKind kind) const {
640 return {builder->create<ast::Sampler>(kind),
641 builder->create<sem::Sampler>(kind)};
642 }
643
644 /// @param dims the dimensionality of the texture
645 /// @returns the depth texture
646 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
647 return {builder->create<ast::DepthTexture>(dims),
648 builder->create<sem::DepthTexture>(dims)};
649 }
650
651 /// @param dims the dimensionality of the texture
652 /// @param subtype the texture subtype.
653 /// @returns the sampled texture
654 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
655 typ::Type subtype) const {
656 return {builder->create<ast::SampledTexture>(dims, subtype),
657 builder->create<sem::SampledTexture>(dims, subtype)};
658 }
659
660 /// @param dims the dimensionality of the texture
661 /// @param subtype the texture subtype.
662 /// @returns the multisampled texture
663 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
664 typ::Type subtype) const {
665 return {builder->create<ast::MultisampledTexture>(dims, subtype),
666 builder->create<sem::MultisampledTexture>(dims, subtype)};
667 }
668
669 /// @param dims the dimensionality of the texture
670 /// @param format the image format of the texture
671 /// @returns the storage texture
672 typ::StorageTexture storage_texture(ast::TextureDimension dims,
673 ast::ImageFormat format) const {
674 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
675 auto* sem_subtype =
676 sem::StorageTexture::SubtypeFor(format, builder->Types());
677 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
678 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000679 }
680
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000681 /// If ty is a ast::Struct or ast::Alias, the returned type is an
682 /// ast::TypeName of the given type's name, otherwise type is returned.
683 /// @param type the type
684 /// @return either type or a pointer to a new ast::TypeName
685 typ::Type MaybeCreateTypename(typ::Type type) const;
686
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000687 private:
688 /// CToAST<T> is specialized for various `T` types and each specialization
689 /// contains a single static `get()` method for obtaining the corresponding
690 /// AST type for the C type `T`.
691 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000692 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000693 template <typename T>
694 struct CToAST {};
695
Ben Claytonc7ca7662021-02-17 16:23:52 +0000696 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000697 };
698
699 //////////////////////////////////////////////////////////////////////////////
700 // AST helper methods
701 //////////////////////////////////////////////////////////////////////////////
702
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000703 /// @param name the symbol string
704 /// @return a Symbol with the given name
705 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
706
707 /// @param sym the symbol
708 /// @return `sym`
709 Symbol Sym(Symbol sym) { return sym; }
710
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000711 /// @param expr the expression
712 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000713 template <typename T>
714 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
715 return expr;
716 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000717
Ben Claytonb8ea5912021-04-19 16:52:42 +0000718 /// Passthrough for nullptr
719 /// @return nullptr
720 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
721
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000722 /// @param name the identifier name
723 /// @return an ast::IdentifierExpression with the given name
724 ast::IdentifierExpression* Expr(const std::string& name) {
725 return create<ast::IdentifierExpression>(Symbols().Register(name));
726 }
727
Ben Clayton46d78d72021-02-10 21:34:26 +0000728 /// @param symbol the identifier symbol
729 /// @return an ast::IdentifierExpression with the given symbol
730 ast::IdentifierExpression* Expr(Symbol symbol) {
731 return create<ast::IdentifierExpression>(symbol);
732 }
733
Ben Claytonb8ea5912021-04-19 16:52:42 +0000734 /// @param variable the AST variable
735 /// @return an ast::IdentifierExpression with the variable's symbol
736 ast::IdentifierExpression* Expr(ast::Variable* variable) {
737 return create<ast::IdentifierExpression>(variable->symbol());
738 }
739
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000740 /// @param source the source information
741 /// @param name the identifier name
742 /// @return an ast::IdentifierExpression with the given name
743 ast::IdentifierExpression* Expr(const Source& source,
744 const std::string& name) {
745 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
746 }
747
748 /// @param name the identifier name
749 /// @return an ast::IdentifierExpression with the given name
750 ast::IdentifierExpression* Expr(const char* name) {
751 return create<ast::IdentifierExpression>(Symbols().Register(name));
752 }
753
754 /// @param value the boolean value
755 /// @return a Scalar constructor for the given value
756 ast::ScalarConstructorExpression* Expr(bool value) {
757 return create<ast::ScalarConstructorExpression>(Literal(value));
758 }
759
760 /// @param value the float value
761 /// @return a Scalar constructor for the given value
762 ast::ScalarConstructorExpression* Expr(f32 value) {
763 return create<ast::ScalarConstructorExpression>(Literal(value));
764 }
765
766 /// @param value the integer value
767 /// @return a Scalar constructor for the given value
768 ast::ScalarConstructorExpression* Expr(i32 value) {
769 return create<ast::ScalarConstructorExpression>(Literal(value));
770 }
771
772 /// @param value the unsigned int value
773 /// @return a Scalar constructor for the given value
774 ast::ScalarConstructorExpression* Expr(u32 value) {
775 return create<ast::ScalarConstructorExpression>(Literal(value));
776 }
777
778 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
779 /// `list`.
780 /// @param list the list to append too
781 /// @param arg the arg to create
782 template <typename ARG>
783 void Append(ast::ExpressionList& list, ARG&& arg) {
784 list.emplace_back(Expr(std::forward<ARG>(arg)));
785 }
786
787 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
788 /// then appends them to `list`.
789 /// @param list the list to append too
790 /// @param arg0 the first argument
791 /// @param args the rest of the arguments
792 template <typename ARG0, typename... ARGS>
793 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
794 Append(list, std::forward<ARG0>(arg0));
795 Append(list, std::forward<ARGS>(args)...);
796 }
797
798 /// @return an empty list of expressions
799 ast::ExpressionList ExprList() { return {}; }
800
801 /// @param args the list of expressions
802 /// @return the list of expressions converted to `ast::Expression`s using
803 /// `Expr()`,
804 template <typename... ARGS>
805 ast::ExpressionList ExprList(ARGS&&... args) {
806 ast::ExpressionList list;
807 list.reserve(sizeof...(args));
808 Append(list, std::forward<ARGS>(args)...);
809 return list;
810 }
811
812 /// @param list the list of expressions
813 /// @return `list`
814 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
815
816 /// @param val the boolan value
817 /// @return a boolean literal with the given value
818 ast::BoolLiteral* Literal(bool val) {
819 return create<ast::BoolLiteral>(ty.bool_(), val);
820 }
821
822 /// @param val the float value
823 /// @return a float literal with the given value
824 ast::FloatLiteral* Literal(f32 val) {
825 return create<ast::FloatLiteral>(ty.f32(), val);
826 }
827
828 /// @param val the unsigned int value
829 /// @return a ast::UintLiteral with the given value
830 ast::UintLiteral* Literal(u32 val) {
831 return create<ast::UintLiteral>(ty.u32(), val);
832 }
833
834 /// @param val the integer value
835 /// @return the ast::SintLiteral with the given value
836 ast::SintLiteral* Literal(i32 val) {
837 return create<ast::SintLiteral>(ty.i32(), val);
838 }
839
840 /// @param args the arguments for the type constructor
841 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
842 /// of `args` converted to `ast::Expression`s using `Expr()`
843 template <typename T, typename... ARGS>
844 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
845 return create<ast::TypeConstructorExpression>(
846 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
847 }
848
849 /// @param type the type to construct
850 /// @param args the arguments for the constructor
851 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
852 /// values `args`.
853 template <typename... ARGS>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000854 ast::TypeConstructorExpression* Construct(typ::Type type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000855 return create<ast::TypeConstructorExpression>(
856 type, ExprList(std::forward<ARGS>(args)...));
857 }
858
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000859 /// Creates a constructor expression that constructs an object of
860 /// `type` filled with `elem_value`. For example,
861 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
862 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
863 /// @param type the type to construct
864 /// @param elem_value the initial or element value (for vec and mat) to
865 /// construct with
866 /// @return the constructor expression
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000867 ast::ConstructorExpression* ConstructValueFilledWith(typ::Type type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000868 int elem_value = 0);
869
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000870 /// @param args the arguments for the vector constructor
871 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
872 /// `T`, constructed with the values `args`.
873 template <typename T, typename... ARGS>
874 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
875 return create<ast::TypeConstructorExpression>(
876 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
877 }
878
879 /// @param args the arguments for the vector constructor
880 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
881 /// `T`, constructed with the values `args`.
882 template <typename T, typename... ARGS>
883 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
884 return create<ast::TypeConstructorExpression>(
885 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
886 }
887
888 /// @param args the arguments for the vector constructor
889 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
890 /// `T`, constructed with the values `args`.
891 template <typename T, typename... ARGS>
892 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
893 return create<ast::TypeConstructorExpression>(
894 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
895 }
896
897 /// @param args the arguments for the matrix constructor
898 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
899 /// `T`, constructed with the values `args`.
900 template <typename T, typename... ARGS>
901 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
902 return create<ast::TypeConstructorExpression>(
903 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
904 }
905
906 /// @param args the arguments for the matrix constructor
907 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
908 /// `T`, constructed with the values `args`.
909 template <typename T, typename... ARGS>
910 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
911 return create<ast::TypeConstructorExpression>(
912 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
913 }
914
915 /// @param args the arguments for the matrix constructor
916 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
917 /// `T`, constructed with the values `args`.
918 template <typename T, typename... ARGS>
919 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
920 return create<ast::TypeConstructorExpression>(
921 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
922 }
923
924 /// @param args the arguments for the matrix constructor
925 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
926 /// `T`, constructed with the values `args`.
927 template <typename T, typename... ARGS>
928 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
929 return create<ast::TypeConstructorExpression>(
930 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
931 }
932
933 /// @param args the arguments for the matrix constructor
934 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
935 /// `T`, constructed with the values `args`.
936 template <typename T, typename... ARGS>
937 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
938 return create<ast::TypeConstructorExpression>(
939 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
940 }
941
942 /// @param args the arguments for the matrix constructor
943 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
944 /// `T`, constructed with the values `args`.
945 template <typename T, typename... ARGS>
946 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
947 return create<ast::TypeConstructorExpression>(
948 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
949 }
950
951 /// @param args the arguments for the matrix constructor
952 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
953 /// `T`, constructed with the values `args`.
954 template <typename T, typename... ARGS>
955 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
956 return create<ast::TypeConstructorExpression>(
957 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
958 }
959
960 /// @param args the arguments for the matrix constructor
961 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
962 /// `T`, constructed with the values `args`.
963 template <typename T, typename... ARGS>
964 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
965 return create<ast::TypeConstructorExpression>(
966 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
967 }
968
969 /// @param args the arguments for the matrix constructor
970 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
971 /// `T`, constructed with the values `args`.
972 template <typename T, typename... ARGS>
973 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
974 return create<ast::TypeConstructorExpression>(
975 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
976 }
977
978 /// @param args the arguments for the array constructor
979 /// @return an `ast::TypeConstructorExpression` of an array with element type
980 /// `T`, constructed with the values `args`.
981 template <typename T, int N = 0, typename... ARGS>
982 ast::TypeConstructorExpression* array(ARGS&&... args) {
983 return create<ast::TypeConstructorExpression>(
984 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
985 }
986
987 /// @param subtype the array element type
988 /// @param n the array size. 0 represents a runtime-array.
989 /// @param args the arguments for the array constructor
990 /// @return an `ast::TypeConstructorExpression` of an array with element type
991 /// `subtype`, constructed with the values `args`.
992 template <typename... ARGS>
Ben Claytone204f272021-04-22 14:40:23 +0000993 ast::TypeConstructorExpression* array(typ::Type subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000994 uint32_t n,
995 ARGS&&... args) {
996 return create<ast::TypeConstructorExpression>(
997 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
998 }
999
1000 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001001 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001002 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001003 /// @param constructor constructor expression
1004 /// @param decorations variable decorations
1005 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001006 template <typename NAME>
1007 ast::Variable* Var(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001008 typ::Type type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001009 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001010 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001011 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001012 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001013 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1014 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001015 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001016
1017 /// @param source the variable source
1018 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001019 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001020 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001021 /// @param constructor constructor expression
1022 /// @param decorations variable decorations
1023 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001024 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001025 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001026 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001027 typ::Type type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001028 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001029 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001030 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001031 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001032 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001033 type, false, constructor, decorations);
1034 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001035
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001036 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001037 /// @param type the variable type
1038 /// @param constructor optional constructor expression
1039 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001040 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001041 template <typename NAME>
1042 ast::Variable* Const(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001043 typ::Type type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001044 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001045 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001046 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001047 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001048 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001049 constructor, decorations);
1050 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001051
1052 /// @param source the variable source
1053 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001054 /// @param type the variable type
1055 /// @param constructor optional constructor expression
1056 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001057 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001058 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001059 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001060 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001061 typ::Type type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001062 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001063 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001064 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001065 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001066 ast::StorageClass::kNone, type, true,
1067 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001068 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001069
James Pricedaf1f1c2021-04-08 22:15:48 +00001070 /// @param name the parameter name
1071 /// @param type the parameter type
1072 /// @param decorations optional parameter decorations
1073 /// @returns a constant `ast::Variable` with the given name and type
1074 template <typename NAME>
1075 ast::Variable* Param(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001076 typ::Type type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001077 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001078 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001079 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1080 ast::StorageClass::kNone, type, true, nullptr,
1081 decorations);
1082 }
1083
1084 /// @param source the parameter source
1085 /// @param name the parameter name
1086 /// @param type the parameter type
1087 /// @param decorations optional parameter decorations
1088 /// @returns a constant `ast::Variable` with the given name and type
1089 template <typename NAME>
1090 ast::Variable* Param(const Source& source,
1091 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001092 typ::Type type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001093 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001094 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001095 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1096 ast::StorageClass::kNone, type, true, nullptr,
1097 decorations);
1098 }
1099
Ben Clayton42708342021-04-21 17:55:12 +00001100 /// @param name the variable name
1101 /// @param type the variable type
1102 /// @param storage the variable storage class
1103 /// @param constructor constructor expression
1104 /// @param decorations variable decorations
1105 /// @returns a new `ast::Variable`, which is automatically registered as a
1106 /// global variable with the ast::Module.
1107 template <typename NAME>
1108 ast::Variable* Global(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001109 typ::Type type,
Ben Clayton42708342021-04-21 17:55:12 +00001110 ast::StorageClass storage,
1111 ast::Expression* constructor = nullptr,
1112 ast::DecorationList decorations = {}) {
1113 auto* var =
1114 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1115 AST().AddGlobalVariable(var);
1116 return var;
1117 }
1118
1119 /// @param source the variable source
1120 /// @param name the variable name
1121 /// @param type the variable type
1122 /// @param storage the variable storage class
1123 /// @param constructor constructor expression
1124 /// @param decorations variable decorations
1125 /// @returns a new `ast::Variable`, which is automatically registered as a
1126 /// global variable with the ast::Module.
1127 template <typename NAME>
1128 ast::Variable* Global(const Source& source,
1129 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001130 typ::Type type,
Ben Clayton42708342021-04-21 17:55:12 +00001131 ast::StorageClass storage,
1132 ast::Expression* constructor = nullptr,
1133 ast::DecorationList decorations = {}) {
1134 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1135 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001136 AST().AddGlobalVariable(var);
1137 return var;
1138 }
1139
1140 /// @param args the arguments to pass to Const()
1141 /// @returns a const `ast::Variable` constructed by calling Var() with the
1142 /// arguments of `args`, which is automatically registered as a global
1143 /// variable with the ast::Module.
1144 template <typename... ARGS>
1145 ast::Variable* GlobalConst(ARGS&&... args) {
1146 auto* var = Const(std::forward<ARGS>(args)...);
1147 AST().AddGlobalVariable(var);
1148 return var;
1149 }
1150
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001151 /// @param func the function name
1152 /// @param args the function call arguments
1153 /// @returns a `ast::CallExpression` to the function `func`, with the
1154 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1155 template <typename NAME, typename... ARGS>
1156 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1157 return create<ast::CallExpression>(Expr(func),
1158 ExprList(std::forward<ARGS>(args)...));
1159 }
1160
1161 /// @param lhs the left hand argument to the addition operation
1162 /// @param rhs the right hand argument to the addition operation
1163 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1164 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001165 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001166 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1167 Expr(std::forward<LHS>(lhs)),
1168 Expr(std::forward<RHS>(rhs)));
1169 }
1170
1171 /// @param lhs the left hand argument to the subtraction operation
1172 /// @param rhs the right hand argument to the subtraction operation
1173 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1174 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001175 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001176 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1177 Expr(std::forward<LHS>(lhs)),
1178 Expr(std::forward<RHS>(rhs)));
1179 }
1180
1181 /// @param lhs the left hand argument to the multiplication operation
1182 /// @param rhs the right hand argument to the multiplication operation
1183 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1184 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001185 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001186 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1187 Expr(std::forward<LHS>(lhs)),
1188 Expr(std::forward<RHS>(rhs)));
1189 }
1190
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001191 /// @param source the source information
1192 /// @param lhs the left hand argument to the multiplication operation
1193 /// @param rhs the right hand argument to the multiplication operation
1194 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1195 template <typename LHS, typename RHS>
1196 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1197 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1198 Expr(std::forward<LHS>(lhs)),
1199 Expr(std::forward<RHS>(rhs)));
1200 }
1201
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001202 /// @param lhs the left hand argument to the division operation
1203 /// @param rhs the right hand argument to the division operation
1204 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1205 template <typename LHS, typename RHS>
1206 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1207 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1208 Expr(std::forward<LHS>(lhs)),
1209 Expr(std::forward<RHS>(rhs)));
1210 }
1211
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001212 /// @param arr the array argument for the array accessor expression
1213 /// @param idx the index argument for the array accessor expression
1214 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1215 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001216 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001217 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1218 Expr(std::forward<IDX>(idx)));
1219 }
1220
1221 /// @param obj the object for the member accessor expression
1222 /// @param idx the index argument for the array accessor expression
1223 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1224 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001225 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001226 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1227 Expr(std::forward<IDX>(idx)));
1228 }
1229
Ben Clayton42d1e092021-02-02 14:29:15 +00001230 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231 /// @param val the offset value
1232 /// @returns the offset decoration pointer
1233 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1234 return create<ast::StructMemberOffsetDecoration>(source_, val);
1235 }
1236
Ben Claytond614dd52021-03-15 10:43:11 +00001237 /// Creates a ast::StructMemberSizeDecoration
1238 /// @param source the source information
1239 /// @param val the size value
1240 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001241 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1242 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001243 return create<ast::StructMemberSizeDecoration>(source, val);
1244 }
1245
1246 /// Creates a ast::StructMemberSizeDecoration
1247 /// @param val the size value
1248 /// @returns the size decoration pointer
1249 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1250 return create<ast::StructMemberSizeDecoration>(source_, val);
1251 }
1252
1253 /// Creates a ast::StructMemberAlignDecoration
1254 /// @param source the source information
1255 /// @param val the align value
1256 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001257 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1258 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001259 return create<ast::StructMemberAlignDecoration>(source, val);
1260 }
1261
1262 /// Creates a ast::StructMemberAlignDecoration
1263 /// @param val the align value
1264 /// @returns the align decoration pointer
1265 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1266 return create<ast::StructMemberAlignDecoration>(source_, val);
1267 }
1268
Ben Clayton42d1e092021-02-02 14:29:15 +00001269 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001270 /// @param source the source information
1271 /// @param name the function name
1272 /// @param params the function parameters
1273 /// @param type the function return type
1274 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001275 /// @param decorations the optional function decorations
1276 /// @param return_type_decorations the optional function return type
1277 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001278 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001279 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001280 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001281 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001282 ast::VariableList params,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001283 typ::Type type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001284 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001285 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001286 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001287 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001288 auto* func =
1289 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1290 type, create<ast::BlockStatement>(body),
1291 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001292 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001293 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001294 }
1295
Ben Clayton42d1e092021-02-02 14:29:15 +00001296 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001297 /// @param name the function name
1298 /// @param params the function parameters
1299 /// @param type the function return type
1300 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001301 /// @param decorations the optional function decorations
1302 /// @param return_type_decorations the optional function return type
1303 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001304 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001305 template <typename NAME>
1306 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001307 ast::VariableList params,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001308 typ::Type type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001309 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001310 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001311 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001312 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001313 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1314 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001315 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001316 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001317 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001318 }
1319
Ben Clayton49668bb2021-04-17 05:32:01 +00001320 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001321 /// @param source the source information
1322 /// @returns the return statement pointer
1323 ast::ReturnStatement* Return(const Source& source) {
1324 return create<ast::ReturnStatement>(source);
1325 }
1326
1327 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001328 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001329 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1330
1331 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001332 /// @param source the source information
1333 /// @param val the return value
1334 /// @returns the return statement pointer
1335 template <typename EXPR>
1336 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1337 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1338 }
1339
1340 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001341 /// @param val the return value
1342 /// @returns the return statement pointer
1343 template <typename EXPR>
1344 ast::ReturnStatement* Return(EXPR&& val) {
1345 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001346 }
1347
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001348 /// Creates a ast::Struct and sem::StructType, registering the
1349 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001350 /// @param source the source information
1351 /// @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(const Source& source,
1357 NAME&& name,
1358 ast::StructMemberList members,
1359 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001360 auto sym = Sym(std::forward<NAME>(name));
1361 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1362 std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001363 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001364 AST().AddConstructedType(type);
1365 return type;
1366 }
1367
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001368 /// Creates a ast::Struct and sem::StructType, registering the
1369 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001370 /// @param name the struct name
1371 /// @param members the struct members
1372 /// @param decorations the optional struct decorations
1373 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001374 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001375 typ::Struct Structure(NAME&& name,
1376 ast::StructMemberList members,
1377 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001378 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001379 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001380 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001381 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001382 AST().AddConstructedType(type);
1383 return type;
1384 }
1385
Ben Clayton42d1e092021-02-02 14:29:15 +00001386 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001387 /// @param source the source information
1388 /// @param name the struct member name
1389 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001390 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001391 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001392 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001393 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001394 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001395 typ::Type type,
Ben Claytond614dd52021-03-15 10:43:11 +00001396 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001397 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001398 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1399 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001400 }
1401
Ben Clayton42d1e092021-02-02 14:29:15 +00001402 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001403 /// @param name the struct member name
1404 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001405 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001406 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001407 template <typename NAME>
1408 ast::StructMember* Member(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001409 typ::Type type,
Ben Claytond614dd52021-03-15 10:43:11 +00001410 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001411 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001412 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1413 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001414 }
1415
Ben Claytonbab31972021-02-08 21:16:21 +00001416 /// Creates a ast::StructMember with the given byte offset
1417 /// @param offset the offset to use in the StructMemberOffsetDecoration
1418 /// @param name the struct member name
1419 /// @param type the struct member type
1420 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001421 template <typename NAME>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001422 ast::StructMember* Member(uint32_t offset, NAME&& name, typ::Type type) {
1423 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001424 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001425 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001426 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001427 create<ast::StructMemberOffsetDecoration>(offset),
1428 });
1429 }
1430
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001431 /// Creates a ast::BlockStatement with input statements
1432 /// @param statements statements of block
1433 /// @returns the block statement pointer
1434 template <typename... Statements>
1435 ast::BlockStatement* Block(Statements&&... statements) {
1436 return create<ast::BlockStatement>(
1437 ast::StatementList{std::forward<Statements>(statements)...});
1438 }
1439
1440 /// Creates a ast::ElseStatement with input condition and body
1441 /// @param condition the else condition expression
1442 /// @param body the else body
1443 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001444 template <typename CONDITION>
1445 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1446 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1447 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001448 }
1449
1450 /// Creates a ast::IfStatement with input condition, body, and optional
1451 /// variadic else statements
1452 /// @param condition the if statement condition expression
1453 /// @param body the if statement body
1454 /// @param elseStatements optional variadic else statements
1455 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001456 template <typename CONDITION, typename... ELSE_STATEMENTS>
1457 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001458 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001459 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001460 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001461 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001462 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001463 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001464 }
1465
1466 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001467 /// @param source the source information
1468 /// @param lhs the left hand side expression initializer
1469 /// @param rhs the right hand side expression initializer
1470 /// @returns the assignment statement pointer
1471 template <typename LhsExpressionInit, typename RhsExpressionInit>
1472 ast::AssignmentStatement* Assign(const Source& source,
1473 LhsExpressionInit&& lhs,
1474 RhsExpressionInit&& rhs) {
1475 return create<ast::AssignmentStatement>(
1476 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1477 Expr(std::forward<RhsExpressionInit>(rhs)));
1478 }
1479
1480 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001481 /// @param lhs the left hand side expression initializer
1482 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001483 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001484 template <typename LhsExpressionInit, typename RhsExpressionInit>
1485 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1486 RhsExpressionInit&& rhs) {
1487 return create<ast::AssignmentStatement>(
1488 Expr(std::forward<LhsExpressionInit>(lhs)),
1489 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001490 }
1491
1492 /// Creates a ast::LoopStatement with input body and optional continuing
1493 /// @param body the loop body
1494 /// @param continuing the optional continuing block
1495 /// @returns the loop statement pointer
1496 ast::LoopStatement* Loop(ast::BlockStatement* body,
1497 ast::BlockStatement* continuing = nullptr) {
1498 return create<ast::LoopStatement>(body, continuing);
1499 }
1500
1501 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001502 /// @param source the source information
1503 /// @param var the variable to wrap in a decl statement
1504 /// @returns the variable decl statement pointer
1505 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1506 return create<ast::VariableDeclStatement>(source, var);
1507 }
1508
1509 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001510 /// @param var the variable to wrap in a decl statement
1511 /// @returns the variable decl statement pointer
1512 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1513 return create<ast::VariableDeclStatement>(var);
1514 }
1515
Antonio Maioranocea744d2021-03-25 12:55:27 +00001516 /// Creates a ast::SwitchStatement with input expression and cases
1517 /// @param condition the condition expression initializer
1518 /// @param cases case statements
1519 /// @returns the switch statement pointer
1520 template <typename ExpressionInit, typename... Cases>
1521 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1522 return create<ast::SwitchStatement>(
1523 Expr(std::forward<ExpressionInit>(condition)),
1524 ast::CaseStatementList{std::forward<Cases>(cases)...});
1525 }
1526
1527 /// Creates a ast::CaseStatement with input list of selectors, and body
1528 /// @param selectors list of selectors
1529 /// @param body the case body
1530 /// @returns the case statement pointer
1531 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1532 ast::BlockStatement* body = nullptr) {
1533 return create<ast::CaseStatement>(std::move(selectors),
1534 body ? body : Block());
1535 }
1536
1537 /// Convenient overload that takes a single selector
1538 /// @param selector a single case selector
1539 /// @param body the case body
1540 /// @returns the case statement pointer
1541 ast::CaseStatement* Case(ast::IntLiteral* selector,
1542 ast::BlockStatement* body = nullptr) {
1543 return Case(ast::CaseSelectorList{selector}, body);
1544 }
1545
1546 /// Convenience function that creates a 'default' ast::CaseStatement
1547 /// @param body the case body
1548 /// @returns the case statement pointer
1549 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1550 return Case(ast::CaseSelectorList{}, body);
1551 }
1552
James Price68f558f2021-04-06 15:51:47 +00001553 /// Creates an ast::BuiltinDecoration
1554 /// @param source the source information
1555 /// @param builtin the builtin value
1556 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001557 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001558 return create<ast::BuiltinDecoration>(source, builtin);
1559 }
1560
1561 /// Creates an ast::BuiltinDecoration
1562 /// @param builtin the builtin value
1563 /// @returns the builtin decoration pointer
1564 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1565 return create<ast::BuiltinDecoration>(source_, builtin);
1566 }
1567
1568 /// Creates an ast::LocationDecoration
1569 /// @param source the source information
1570 /// @param location the location value
1571 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001572 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001573 return create<ast::LocationDecoration>(source, location);
1574 }
1575
1576 /// Creates an ast::LocationDecoration
1577 /// @param location the location value
1578 /// @returns the location decoration pointer
1579 ast::LocationDecoration* Location(uint32_t location) {
1580 return create<ast::LocationDecoration>(source_, location);
1581 }
1582
1583 /// Creates an ast::StageDecoration
1584 /// @param source the source information
1585 /// @param stage the pipeline stage
1586 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001587 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001588 return create<ast::StageDecoration>(source, stage);
1589 }
1590
1591 /// Creates an ast::StageDecoration
1592 /// @param stage the pipeline stage
1593 /// @returns the stage decoration pointer
1594 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1595 return create<ast::StageDecoration>(source_, stage);
1596 }
1597
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001598 /// Sets the current builder source to `src`
1599 /// @param src the Source used for future create() calls
1600 void SetSource(const Source& src) {
1601 AssertNotMoved();
1602 source_ = src;
1603 }
1604
1605 /// Sets the current builder source to `loc`
1606 /// @param loc the Source used for future create() calls
1607 void SetSource(const Source::Location& loc) {
1608 AssertNotMoved();
1609 source_ = Source(loc);
1610 }
1611
Ben Clayton33352542021-01-29 16:43:41 +00001612 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001613 /// @note As the Resolver is run when the Program is built, this will only be
1614 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001615 /// @param expr the AST expression
1616 /// @return the resolved semantic type for the expression, or nullptr if the
1617 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001618 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001619
Ben Clayton169512e2021-04-17 05:52:11 +00001620 /// Wraps the ast::Literal in a statement. This is used by tests that
1621 /// construct a partial AST and require the Resolver to reach these
1622 /// nodes.
1623 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1624 /// @return the ast::Statement that wraps the ast::Statement
1625 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001626 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001627 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001628 /// nodes.
1629 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1630 /// @return the ast::Statement that wraps the ast::Expression
1631 ast::Statement* WrapInStatement(ast::Expression* expr);
1632 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001633 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001634 /// these nodes.
1635 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1636 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1637 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1638 /// Returns the statement argument. Used as a passthrough-overload by
1639 /// WrapInFunction().
1640 /// @param stmt the ast::Statement
1641 /// @return `stmt`
1642 ast::Statement* WrapInStatement(ast::Statement* stmt);
1643 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001644 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001645 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001646 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001647 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001648 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001649 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001650 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001651 }
1652 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001653 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001654 /// @returns the function
1655 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001656
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001657 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001658 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001659
1660 protected:
1661 /// Asserts that the builder has not been moved.
1662 void AssertNotMoved() const;
1663
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001664 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001665 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001666 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001667 ASTNodeAllocator ast_nodes_;
1668 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001669 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001670 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001671 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001672 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001673
1674 /// The source to use when creating AST nodes without providing a Source as
1675 /// the first argument.
1676 Source source_;
1677
Ben Clayton5f0ea112021-03-09 10:54:37 +00001678 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001679 /// program when built.
1680 bool resolve_on_build_ = true;
1681
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001682 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1683 bool moved_ = false;
1684};
1685
1686//! @cond Doxygen_Suppress
1687// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1688template <>
1689struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001690 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001691 return t->i32();
1692 }
1693};
1694template <>
1695struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001696 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001697 return t->u32();
1698 }
1699};
1700template <>
1701struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001702 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001703 return t->f32();
1704 }
1705};
1706template <>
1707struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001708 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001709 return t->bool_();
1710 }
1711};
1712template <>
1713struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001714 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001715 return t->void_();
1716 }
1717};
1718//! @endcond
1719
Ben Clayton917b14b2021-04-19 16:50:23 +00001720/// @param builder the ProgramBuilder
1721/// @returns the ProgramID of the ProgramBuilder
1722inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1723 return builder->ID();
1724}
1725
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001726} // namespace tint
1727
1728#endif // SRC_PROGRAM_BUILDER_H_