blob: 5dc5accce9ebc26fc8685b0c12ed90e47ab3efaf [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 Clayton7241a502021-04-22 14:32:53 +000021#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000022#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000023#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000024#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000025#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000026#include "src/ast/bool_literal.h"
27#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000028#include "src/ast/case_statement.h"
Ben Claytona922e652021-04-21 13:37:22 +000029#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000031#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000032#include "src/ast/if_statement.h"
33#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000034#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000035#include "src/ast/member_accessor_expression.h"
36#include "src/ast/module.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000037#include "src/ast/return_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/scalar_constructor_expression.h"
39#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000040#include "src/ast/stage_decoration.h"
Ben Claytonbab31972021-02-08 21:16:21 +000041#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000042#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000043#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000044#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000045#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000046#include "src/ast/type_constructor_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000047#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000048#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000049#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000050#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000051#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000052#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000053#include "src/program_id.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000054#include "src/sem/access_control_type.h"
55#include "src/sem/alias_type.h"
56#include "src/sem/array_type.h"
57#include "src/sem/bool_type.h"
58#include "src/sem/f32_type.h"
59#include "src/sem/i32_type.h"
60#include "src/sem/matrix_type.h"
61#include "src/sem/pointer_type.h"
62#include "src/sem/struct_type.h"
63#include "src/sem/u32_type.h"
64#include "src/sem/vector_type.h"
65#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000066#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000067
68namespace tint {
69
Ben Clayton401b96b2021-02-03 17:19:59 +000070// Forward declarations
71namespace ast {
72class VariableDeclStatement;
73} // namespace ast
74
Ben Claytona6b9a8e2021-01-26 16:57:10 +000075class CloneContext;
76
77/// ProgramBuilder is a mutable builder for a Program.
78/// To construct a Program, populate the builder and then `std::move` it to a
79/// Program.
80class ProgramBuilder {
81 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000082 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
83 using ASTNodeAllocator = BlockAllocator<ast::Node>;
84
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000085 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
86 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000087
88 /// `i32` is a type alias to `int`.
89 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
90 /// WGSL syntax.
91 /// Note: this is intentionally not aliased to uint32_t as we want integer
92 /// literals passed to the builder to match WGSL's integer literal types.
93 using i32 = decltype(1);
94 /// `u32` is a type alias to `unsigned int`.
95 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
96 /// WGSL syntax.
97 /// Note: this is intentionally not aliased to uint32_t as we want integer
98 /// literals passed to the builder to match WGSL's integer literal types.
99 using u32 = decltype(1u);
100 /// `f32` is a type alias to `float`
101 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
102 /// WGSL syntax.
103 using f32 = float;
104
105 /// Constructor
106 ProgramBuilder();
107
108 /// Move constructor
109 /// @param rhs the builder to move
110 ProgramBuilder(ProgramBuilder&& rhs);
111
112 /// Destructor
113 virtual ~ProgramBuilder();
114
115 /// Move assignment operator
116 /// @param rhs the builder to move
117 /// @return this builder
118 ProgramBuilder& operator=(ProgramBuilder&& rhs);
119
Ben Claytone43c8302021-01-29 11:59:32 +0000120 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
121 /// making a deep clone of the Program contents.
122 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
123 /// existing immutable program.
124 /// As the returned ProgramBuilder wraps `program`, `program` must not be
125 /// destructed or assigned while using the returned ProgramBuilder.
126 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
127 /// function. See crbug.com/tint/460.
128 /// @param program the immutable Program to wrap
129 /// @return the ProgramBuilder that wraps `program`
130 static ProgramBuilder Wrap(const Program* program);
131
Ben Claytone6995de2021-04-13 23:27:27 +0000132 /// @returns the unique identifier for this program
133 ProgramID ID() const { return id_; }
134
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000135 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000136 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000137 AssertNotMoved();
138 return types_;
139 }
140
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000141 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000142 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000143 AssertNotMoved();
144 return types_;
145 }
146
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000147 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000148 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000149 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000150 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000151 }
152
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000153 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000154 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000156 return ast_nodes_;
157 }
158
159 /// @returns a reference to the program's semantic nodes storage
160 SemNodeAllocator& SemNodes() {
161 AssertNotMoved();
162 return sem_nodes_;
163 }
164
165 /// @returns a reference to the program's semantic nodes storage
166 const SemNodeAllocator& SemNodes() const {
167 AssertNotMoved();
168 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000169 }
170
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000171 /// @returns a reference to the program's AST root Module
172 ast::Module& AST() {
173 AssertNotMoved();
174 return *ast_;
175 }
176
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000177 /// @returns a reference to the program's AST root Module
178 const ast::Module& AST() const {
179 AssertNotMoved();
180 return *ast_;
181 }
182
183 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000184 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000185 AssertNotMoved();
186 return sem_;
187 }
188
189 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000190 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000191 AssertNotMoved();
192 return sem_;
193 }
194
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000195 /// @returns a reference to the program's SymbolTable
196 SymbolTable& Symbols() {
197 AssertNotMoved();
198 return symbols_;
199 }
200
Ben Clayton708dc2d2021-01-29 11:22:40 +0000201 /// @returns a reference to the program's SymbolTable
202 const SymbolTable& Symbols() const {
203 AssertNotMoved();
204 return symbols_;
205 }
206
Ben Clayton844217f2021-01-27 18:49:05 +0000207 /// @returns a reference to the program's diagnostics
208 diag::List& Diagnostics() {
209 AssertNotMoved();
210 return diagnostics_;
211 }
212
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000213 /// @returns a reference to the program's diagnostics
214 const diag::List& Diagnostics() const {
215 AssertNotMoved();
216 return diagnostics_;
217 }
218
Ben Clayton5f0ea112021-03-09 10:54:37 +0000219 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000220 /// @param enable the new flag value (defaults to true)
221 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
222
Ben Clayton5f0ea112021-03-09 10:54:37 +0000223 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000224 /// built.
225 bool ResolveOnBuild() const { return resolve_on_build_; }
226
Ben Clayton844217f2021-01-27 18:49:05 +0000227 /// @returns true if the program has no error diagnostics and is not missing
228 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000229 bool IsValid() const;
230
Ben Clayton708dc2d2021-01-29 11:22:40 +0000231 /// Writes a representation of the node to the output stream
232 /// @note unlike str(), to_str() does not automatically demangle the string.
233 /// @param node the AST node
234 /// @param out the stream to write to
235 /// @param indent number of spaces to indent the node when writing
236 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
237 node->to_str(Sem(), out, indent);
238 }
239
240 /// Returns a demangled, string representation of `node`.
241 /// @param node the AST node
242 /// @returns a string representation of the node
243 std::string str(const ast::Node* node) const;
244
Ben Clayton7fdfff12021-01-29 15:17:30 +0000245 /// Creates a new ast::Node owned by the ProgramBuilder. When the
246 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000247 /// @param source the Source of the node
248 /// @param args the arguments to pass to the type constructor
249 /// @returns the node pointer
250 template <typename T, typename... ARGS>
251 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
252 ARGS&&... args) {
253 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000254 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000255 }
256
Ben Clayton7fdfff12021-01-29 15:17:30 +0000257 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
258 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000259 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000260 /// When the ProgramBuilder is destructed, the ast::Node will also be
261 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000262 /// @returns the node pointer
263 template <typename T>
264 traits::EnableIfIsType<T, ast::Node>* create() {
265 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000266 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000267 }
268
Ben Clayton7fdfff12021-01-29 15:17:30 +0000269 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
270 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000271 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000272 /// When the ProgramBuilder is destructed, the ast::Node will also be
273 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000274 /// @param arg0 the first arguments to pass to the type constructor
275 /// @param args the remaining arguments to pass to the type constructor
276 /// @returns the node pointer
277 template <typename T, typename ARG0, typename... ARGS>
278 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
279 traits::IsTypeOrDerived<T, ast::Node>::value &&
280 !traits::IsTypeOrDerived<ARG0, Source>::value,
281 T>*
282 create(ARG0&& arg0, ARGS&&... args) {
283 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000284 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000285 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000286 }
287
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000288 /// Creates a new sem::Node owned by the ProgramBuilder.
289 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000290 /// destructed.
291 /// @param args the arguments to pass to the type constructor
292 /// @returns the node pointer
293 template <typename T, typename... ARGS>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000294 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000295 AssertNotMoved();
296 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
297 }
298
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000299 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000300 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
301 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000302 /// Types are unique (de-aliased), and so calling create() for the same `T`
303 /// and arguments will return the same pointer.
304 /// @warning Use this method to acquire a type only if all of its type
305 /// information is provided in the constructor arguments `args`.<br>
306 /// If the type requires additional configuration after construction that
307 /// affect its fundamental type, build the type with `std::make_unique`, make
308 /// any necessary alterations and then call unique_type() instead.
309 /// @param args the arguments to pass to the type constructor
310 /// @returns the de-aliased type pointer
311 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000312 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
313 static_assert(std::is_base_of<sem::Type, T>::value,
314 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000315 AssertNotMoved();
316 return types_.Get<T>(std::forward<ARGS>(args)...);
317 }
318
319 /// Marks this builder as moved, preventing any further use of the builder.
320 void MarkAsMoved();
321
322 //////////////////////////////////////////////////////////////////////////////
323 // TypesBuilder
324 //////////////////////////////////////////////////////////////////////////////
325
326 /// TypesBuilder holds basic `tint` types and methods for constructing
327 /// complex types.
328 class TypesBuilder {
329 public:
330 /// Constructor
331 /// @param builder the program builder
332 explicit TypesBuilder(ProgramBuilder* builder);
333
334 /// @return the tint AST type for the C type `T`.
335 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000336 sem::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000337 return CToAST<T>::get(this);
338 }
339
340 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000341 typ::Bool bool_() const {
342 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
343 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000344
345 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000346 typ::F32 f32() const {
347 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
348 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000349
350 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000351 typ::I32 i32() const {
352 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
353 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000354
355 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000356 typ::U32 u32() const {
357 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
358 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000359
360 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000361 typ::Void void_() const {
362 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
363 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000364
Antonio Maiorano25436862021-04-13 13:32:33 +0000365 /// @param type vector subtype
366 /// @return the tint AST type for a 2-element vector of `type`.
Ben Claytone30ffeb2021-04-22 14:24:43 +0000367 typ::Vector vec2(typ::Type type) const {
368 return {builder->create<ast::Vector>(type, 2u),
369 builder->create<sem::Vector>(type, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000370 }
371
372 /// @param type vector subtype
373 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone30ffeb2021-04-22 14:24:43 +0000374 typ::Vector vec3(typ::Type type) const {
375 return {builder->create<ast::Vector>(type, 3u),
376 builder->create<sem::Vector>(type, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000377 }
378
379 /// @param type vector subtype
380 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone30ffeb2021-04-22 14:24:43 +0000381 typ::Vector vec4(typ::Type type) const {
382 return {builder->create<ast::Vector>(type, 4u),
383 builder->create<sem::Vector>(type, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000384 }
385
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000386 /// @return the tint AST type for a 2-element vector of the C type `T`.
387 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000388 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000389 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000390 }
391
392 /// @return the tint AST type for a 3-element vector of the C type `T`.
393 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000394 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000395 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000396 }
397
398 /// @return the tint AST type for a 4-element vector of the C type `T`.
399 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000400 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000401 return vec4(Of<T>());
402 }
403
404 /// @param type matrix subtype
405 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000406 typ::Matrix mat2x2(typ::Type type) const {
407 return {builder->create<ast::Matrix>(type, 2u, 2u),
408 builder->create<sem::Matrix>(type, 2u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000409 }
410
411 /// @param type matrix subtype
412 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000413 typ::Matrix mat2x3(typ::Type type) const {
414 return {builder->create<ast::Matrix>(type, 3u, 2u),
415 builder->create<sem::Matrix>(type, 3u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000416 }
417
418 /// @param type matrix subtype
419 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000420 typ::Matrix mat2x4(typ::Type type) const {
421 return {builder->create<ast::Matrix>(type, 4u, 2u),
422 builder->create<sem::Matrix>(type, 4u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000423 }
424
425 /// @param type matrix subtype
426 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000427 typ::Matrix mat3x2(typ::Type type) const {
428 return {builder->create<ast::Matrix>(type, 2u, 3u),
429 builder->create<sem::Matrix>(type, 2u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000430 }
431
432 /// @param type matrix subtype
433 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000434 typ::Matrix mat3x3(typ::Type type) const {
435 return {builder->create<ast::Matrix>(type, 3u, 3u),
436 builder->create<sem::Matrix>(type, 3u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000437 }
438
439 /// @param type matrix subtype
440 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000441 typ::Matrix mat3x4(typ::Type type) const {
442 return {builder->create<ast::Matrix>(type, 4u, 3u),
443 builder->create<sem::Matrix>(type, 4u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000444 }
445
446 /// @param type matrix subtype
447 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000448 typ::Matrix mat4x2(typ::Type type) const {
449 return {builder->create<ast::Matrix>(type, 2u, 4u),
450 builder->create<sem::Matrix>(type, 2u, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000451 }
452
453 /// @param type matrix subtype
454 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000455 typ::Matrix mat4x3(typ::Type type) const {
456 return {builder->create<ast::Matrix>(type, 3u, 4u),
457 builder->create<sem::Matrix>(type, 3u, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000458 }
459
460 /// @param type matrix subtype
461 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000462 typ::Matrix mat4x4(typ::Type type) const {
463 return {builder->create<ast::Matrix>(type, 4u, 4u),
464 builder->create<sem::Matrix>(type, 4u, 4u)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000465 }
466
467 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
468 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000469 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000470 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000471 }
472
473 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
474 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000475 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000476 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000477 }
478
479 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
480 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000481 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000482 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000483 }
484
485 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
486 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000487 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000488 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000489 }
490
491 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
492 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000493 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000494 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000495 }
496
497 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
498 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000499 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000500 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000501 }
502
503 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
504 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000505 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000506 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000507 }
508
509 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
510 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000511 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000512 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000513 }
514
515 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
516 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000517 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000518 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000519 }
520
521 /// @param subtype the array element type
522 /// @param n the array size. 0 represents a runtime-array.
523 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton7241a502021-04-22 14:32:53 +0000524 typ::Array array(typ::Type subtype, uint32_t n = 0) const {
525 return {
526 builder->create<ast::Array>(subtype, n, ast::DecorationList{}),
527 builder->create<sem::ArrayType>(subtype, n, ast::DecorationList{})};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000528 }
529
Ben Claytonbab31972021-02-08 21:16:21 +0000530 /// @param subtype the array element type
531 /// @param n the array size. 0 represents a runtime-array.
532 /// @param stride the array stride.
533 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton7241a502021-04-22 14:32:53 +0000534 typ::Array array(typ::Type subtype, uint32_t n, uint32_t stride) const {
535 return {builder->create<ast::Array>(
536 subtype, n,
537 ast::DecorationList{
538 builder->create<ast::StrideDecoration>(stride),
539 }),
540 builder->create<sem::ArrayType>(
541 subtype, n,
542 ast::DecorationList{
543 builder->create<ast::StrideDecoration>(stride),
544 })};
Ben Claytonbab31972021-02-08 21:16:21 +0000545 }
546
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000547 /// @return the tint AST type for an array of size `N` of type `T`
548 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000549 typ::Array array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000550 return array(Of<T>(), N);
551 }
552
Ben Claytonbab31972021-02-08 21:16:21 +0000553 /// @param stride the array stride
554 /// @return the tint AST type for an array of size `N` of type `T`
555 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000556 typ::Array array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000557 return array(Of<T>(), N, stride);
558 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000559
Ben Clayton42d1e092021-02-02 14:29:15 +0000560 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 /// @param name the alias name
562 /// @param type the alias type
563 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000564 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000565 sem::Alias* alias(NAME&& name, sem::Type* type) const {
566 return builder->create<sem::Alias>(builder->Sym(std::forward<NAME>(name)),
567 type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000568 }
569
Ben Clayton4db10dd2021-04-16 08:47:14 +0000570 /// Creates an access control qualifier type
571 /// @param access the access control
572 /// @param type the inner type
573 /// @returns the access control qualifier type
Ben Clayton8a8d26b2021-04-20 15:04:21 +0000574 sem::AccessControl* access(ast::AccessControl::Access access,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000575 sem::Type* type) const {
576 return builder->create<sem::AccessControl>(access, type);
Ben Clayton4db10dd2021-04-16 08:47:14 +0000577 }
578
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000579 /// @return the tint AST pointer to `type` with the given ast::StorageClass
580 /// @param type the type of the pointer
581 /// @param storage_class the storage class of the pointer
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000582 sem::Pointer* pointer(sem::Type* type,
583 ast::StorageClass storage_class) const {
584 return builder->create<sem::Pointer>(type, storage_class);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000585 }
586
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000587 /// @return the tint AST pointer to type `T` with the given
588 /// ast::StorageClass.
589 /// @param storage_class the storage class of the pointer
590 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000591 sem::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000592 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000593 }
594
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000595 /// @param impl the struct implementation
596 /// @returns a struct pointer
Ben Clayton913a2f42021-04-20 15:21:21 +0000597 sem::StructType* struct_(ast::Struct* impl) const {
598 return builder->create<sem::StructType>(impl);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000599 }
600
601 private:
602 /// CToAST<T> is specialized for various `T` types and each specialization
603 /// contains a single static `get()` method for obtaining the corresponding
604 /// AST type for the C type `T`.
605 /// `get()` has the signature:
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000606 /// `static sem::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000607 template <typename T>
608 struct CToAST {};
609
Ben Claytonc7ca7662021-02-17 16:23:52 +0000610 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000611 };
612
613 //////////////////////////////////////////////////////////////////////////////
614 // AST helper methods
615 //////////////////////////////////////////////////////////////////////////////
616
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000617 /// @param name the symbol string
618 /// @return a Symbol with the given name
619 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
620
621 /// @param sym the symbol
622 /// @return `sym`
623 Symbol Sym(Symbol sym) { return sym; }
624
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000625 /// @param expr the expression
626 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000627 template <typename T>
628 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
629 return expr;
630 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000631
Ben Claytonb8ea5912021-04-19 16:52:42 +0000632 /// Passthrough for nullptr
633 /// @return nullptr
634 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
635
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000636 /// @param name the identifier name
637 /// @return an ast::IdentifierExpression with the given name
638 ast::IdentifierExpression* Expr(const std::string& name) {
639 return create<ast::IdentifierExpression>(Symbols().Register(name));
640 }
641
Ben Clayton46d78d72021-02-10 21:34:26 +0000642 /// @param symbol the identifier symbol
643 /// @return an ast::IdentifierExpression with the given symbol
644 ast::IdentifierExpression* Expr(Symbol symbol) {
645 return create<ast::IdentifierExpression>(symbol);
646 }
647
Ben Claytonb8ea5912021-04-19 16:52:42 +0000648 /// @param variable the AST variable
649 /// @return an ast::IdentifierExpression with the variable's symbol
650 ast::IdentifierExpression* Expr(ast::Variable* variable) {
651 return create<ast::IdentifierExpression>(variable->symbol());
652 }
653
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000654 /// @param source the source information
655 /// @param name the identifier name
656 /// @return an ast::IdentifierExpression with the given name
657 ast::IdentifierExpression* Expr(const Source& source,
658 const std::string& name) {
659 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
660 }
661
662 /// @param name the identifier name
663 /// @return an ast::IdentifierExpression with the given name
664 ast::IdentifierExpression* Expr(const char* name) {
665 return create<ast::IdentifierExpression>(Symbols().Register(name));
666 }
667
668 /// @param value the boolean value
669 /// @return a Scalar constructor for the given value
670 ast::ScalarConstructorExpression* Expr(bool value) {
671 return create<ast::ScalarConstructorExpression>(Literal(value));
672 }
673
674 /// @param value the float value
675 /// @return a Scalar constructor for the given value
676 ast::ScalarConstructorExpression* Expr(f32 value) {
677 return create<ast::ScalarConstructorExpression>(Literal(value));
678 }
679
680 /// @param value the integer value
681 /// @return a Scalar constructor for the given value
682 ast::ScalarConstructorExpression* Expr(i32 value) {
683 return create<ast::ScalarConstructorExpression>(Literal(value));
684 }
685
686 /// @param value the unsigned int value
687 /// @return a Scalar constructor for the given value
688 ast::ScalarConstructorExpression* Expr(u32 value) {
689 return create<ast::ScalarConstructorExpression>(Literal(value));
690 }
691
692 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
693 /// `list`.
694 /// @param list the list to append too
695 /// @param arg the arg to create
696 template <typename ARG>
697 void Append(ast::ExpressionList& list, ARG&& arg) {
698 list.emplace_back(Expr(std::forward<ARG>(arg)));
699 }
700
701 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
702 /// then appends them to `list`.
703 /// @param list the list to append too
704 /// @param arg0 the first argument
705 /// @param args the rest of the arguments
706 template <typename ARG0, typename... ARGS>
707 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
708 Append(list, std::forward<ARG0>(arg0));
709 Append(list, std::forward<ARGS>(args)...);
710 }
711
712 /// @return an empty list of expressions
713 ast::ExpressionList ExprList() { return {}; }
714
715 /// @param args the list of expressions
716 /// @return the list of expressions converted to `ast::Expression`s using
717 /// `Expr()`,
718 template <typename... ARGS>
719 ast::ExpressionList ExprList(ARGS&&... args) {
720 ast::ExpressionList list;
721 list.reserve(sizeof...(args));
722 Append(list, std::forward<ARGS>(args)...);
723 return list;
724 }
725
726 /// @param list the list of expressions
727 /// @return `list`
728 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
729
730 /// @param val the boolan value
731 /// @return a boolean literal with the given value
732 ast::BoolLiteral* Literal(bool val) {
733 return create<ast::BoolLiteral>(ty.bool_(), val);
734 }
735
736 /// @param val the float value
737 /// @return a float literal with the given value
738 ast::FloatLiteral* Literal(f32 val) {
739 return create<ast::FloatLiteral>(ty.f32(), val);
740 }
741
742 /// @param val the unsigned int value
743 /// @return a ast::UintLiteral with the given value
744 ast::UintLiteral* Literal(u32 val) {
745 return create<ast::UintLiteral>(ty.u32(), val);
746 }
747
748 /// @param val the integer value
749 /// @return the ast::SintLiteral with the given value
750 ast::SintLiteral* Literal(i32 val) {
751 return create<ast::SintLiteral>(ty.i32(), val);
752 }
753
754 /// @param args the arguments for the type constructor
755 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
756 /// of `args` converted to `ast::Expression`s using `Expr()`
757 template <typename T, typename... ARGS>
758 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
759 return create<ast::TypeConstructorExpression>(
760 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
761 }
762
763 /// @param type the type to construct
764 /// @param args the arguments for the constructor
765 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
766 /// values `args`.
767 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000768 ast::TypeConstructorExpression* Construct(sem::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000769 return create<ast::TypeConstructorExpression>(
770 type, ExprList(std::forward<ARGS>(args)...));
771 }
772
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000773 /// Creates a constructor expression that constructs an object of
774 /// `type` filled with `elem_value`. For example,
775 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
776 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
777 /// @param type the type to construct
778 /// @param elem_value the initial or element value (for vec and mat) to
779 /// construct with
780 /// @return the constructor expression
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000781 ast::ConstructorExpression* ConstructValueFilledWith(sem::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000782 int elem_value = 0);
783
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000784 /// @param args the arguments for the vector constructor
785 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
786 /// `T`, constructed with the values `args`.
787 template <typename T, typename... ARGS>
788 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
789 return create<ast::TypeConstructorExpression>(
790 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
791 }
792
793 /// @param args the arguments for the vector constructor
794 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
795 /// `T`, constructed with the values `args`.
796 template <typename T, typename... ARGS>
797 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
798 return create<ast::TypeConstructorExpression>(
799 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
800 }
801
802 /// @param args the arguments for the vector constructor
803 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
804 /// `T`, constructed with the values `args`.
805 template <typename T, typename... ARGS>
806 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
807 return create<ast::TypeConstructorExpression>(
808 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
809 }
810
811 /// @param args the arguments for the matrix constructor
812 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
813 /// `T`, constructed with the values `args`.
814 template <typename T, typename... ARGS>
815 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
816 return create<ast::TypeConstructorExpression>(
817 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
818 }
819
820 /// @param args the arguments for the matrix constructor
821 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
822 /// `T`, constructed with the values `args`.
823 template <typename T, typename... ARGS>
824 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
825 return create<ast::TypeConstructorExpression>(
826 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
827 }
828
829 /// @param args the arguments for the matrix constructor
830 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
831 /// `T`, constructed with the values `args`.
832 template <typename T, typename... ARGS>
833 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
834 return create<ast::TypeConstructorExpression>(
835 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
836 }
837
838 /// @param args the arguments for the matrix constructor
839 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
840 /// `T`, constructed with the values `args`.
841 template <typename T, typename... ARGS>
842 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
843 return create<ast::TypeConstructorExpression>(
844 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
845 }
846
847 /// @param args the arguments for the matrix constructor
848 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
849 /// `T`, constructed with the values `args`.
850 template <typename T, typename... ARGS>
851 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
852 return create<ast::TypeConstructorExpression>(
853 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
854 }
855
856 /// @param args the arguments for the matrix constructor
857 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
858 /// `T`, constructed with the values `args`.
859 template <typename T, typename... ARGS>
860 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
861 return create<ast::TypeConstructorExpression>(
862 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
863 }
864
865 /// @param args the arguments for the matrix constructor
866 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
867 /// `T`, constructed with the values `args`.
868 template <typename T, typename... ARGS>
869 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
870 return create<ast::TypeConstructorExpression>(
871 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
872 }
873
874 /// @param args the arguments for the matrix constructor
875 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
876 /// `T`, constructed with the values `args`.
877 template <typename T, typename... ARGS>
878 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
879 return create<ast::TypeConstructorExpression>(
880 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
881 }
882
883 /// @param args the arguments for the matrix constructor
884 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
885 /// `T`, constructed with the values `args`.
886 template <typename T, typename... ARGS>
887 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
888 return create<ast::TypeConstructorExpression>(
889 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
890 }
891
892 /// @param args the arguments for the array constructor
893 /// @return an `ast::TypeConstructorExpression` of an array with element type
894 /// `T`, constructed with the values `args`.
895 template <typename T, int N = 0, typename... ARGS>
896 ast::TypeConstructorExpression* array(ARGS&&... args) {
897 return create<ast::TypeConstructorExpression>(
898 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
899 }
900
901 /// @param subtype the array element type
902 /// @param n the array size. 0 represents a runtime-array.
903 /// @param args the arguments for the array constructor
904 /// @return an `ast::TypeConstructorExpression` of an array with element type
905 /// `subtype`, constructed with the values `args`.
906 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000907 ast::TypeConstructorExpression* array(sem::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000908 uint32_t n,
909 ARGS&&... args) {
910 return create<ast::TypeConstructorExpression>(
911 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
912 }
913
914 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000915 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000916 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000917 /// @param constructor constructor expression
918 /// @param decorations variable decorations
919 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000920 template <typename NAME>
921 ast::Variable* Var(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000922 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000923 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000924 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000925 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000926 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
927 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000928 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000929
930 /// @param source the variable source
931 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000933 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000934 /// @param constructor constructor expression
935 /// @param decorations variable decorations
936 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000937 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000938 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000939 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000940 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000941 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000942 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000943 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000944 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000945 type, false, constructor, decorations);
946 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000947
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000948 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000949 /// @param type the variable type
950 /// @param constructor optional constructor expression
951 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000952 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000953 template <typename NAME>
954 ast::Variable* Const(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000955 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000956 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000957 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000958 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000959 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000960 constructor, decorations);
961 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000962
963 /// @param source the variable source
964 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000965 /// @param type the variable type
966 /// @param constructor optional constructor expression
967 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000968 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000969 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000970 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000971 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000972 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000973 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000974 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000975 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000976 ast::StorageClass::kNone, type, true,
977 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000978 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000979
James Pricedaf1f1c2021-04-08 22:15:48 +0000980 /// @param name the parameter name
981 /// @param type the parameter type
982 /// @param decorations optional parameter decorations
983 /// @returns a constant `ast::Variable` with the given name and type
984 template <typename NAME>
985 ast::Variable* Param(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000986 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000987 ast::DecorationList decorations = {}) {
988 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
989 ast::StorageClass::kNone, type, true, nullptr,
990 decorations);
991 }
992
993 /// @param source the parameter source
994 /// @param name the parameter name
995 /// @param type the parameter type
996 /// @param decorations optional parameter decorations
997 /// @returns a constant `ast::Variable` with the given name and type
998 template <typename NAME>
999 ast::Variable* Param(const Source& source,
1000 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001001 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001002 ast::DecorationList decorations = {}) {
1003 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1004 ast::StorageClass::kNone, type, true, nullptr,
1005 decorations);
1006 }
1007
Ben Clayton42708342021-04-21 17:55:12 +00001008 /// @param name the variable name
1009 /// @param type the variable type
1010 /// @param storage the variable storage class
1011 /// @param constructor constructor expression
1012 /// @param decorations variable decorations
1013 /// @returns a new `ast::Variable`, which is automatically registered as a
1014 /// global variable with the ast::Module.
1015 template <typename NAME>
1016 ast::Variable* Global(NAME&& name,
1017 sem::Type* type,
1018 ast::StorageClass storage,
1019 ast::Expression* constructor = nullptr,
1020 ast::DecorationList decorations = {}) {
1021 auto* var =
1022 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1023 AST().AddGlobalVariable(var);
1024 return var;
1025 }
1026
1027 /// @param source the variable source
1028 /// @param name the variable name
1029 /// @param type the variable type
1030 /// @param storage the variable storage class
1031 /// @param constructor constructor expression
1032 /// @param decorations variable decorations
1033 /// @returns a new `ast::Variable`, which is automatically registered as a
1034 /// global variable with the ast::Module.
1035 template <typename NAME>
1036 ast::Variable* Global(const Source& source,
1037 NAME&& name,
1038 sem::Type* type,
1039 ast::StorageClass storage,
1040 ast::Expression* constructor = nullptr,
1041 ast::DecorationList decorations = {}) {
1042 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1043 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001044 AST().AddGlobalVariable(var);
1045 return var;
1046 }
1047
1048 /// @param args the arguments to pass to Const()
1049 /// @returns a const `ast::Variable` constructed by calling Var() with the
1050 /// arguments of `args`, which is automatically registered as a global
1051 /// variable with the ast::Module.
1052 template <typename... ARGS>
1053 ast::Variable* GlobalConst(ARGS&&... args) {
1054 auto* var = Const(std::forward<ARGS>(args)...);
1055 AST().AddGlobalVariable(var);
1056 return var;
1057 }
1058
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001059 /// @param func the function name
1060 /// @param args the function call arguments
1061 /// @returns a `ast::CallExpression` to the function `func`, with the
1062 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1063 template <typename NAME, typename... ARGS>
1064 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1065 return create<ast::CallExpression>(Expr(func),
1066 ExprList(std::forward<ARGS>(args)...));
1067 }
1068
1069 /// @param lhs the left hand argument to the addition operation
1070 /// @param rhs the right hand argument to the addition operation
1071 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1072 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001073 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1075 Expr(std::forward<LHS>(lhs)),
1076 Expr(std::forward<RHS>(rhs)));
1077 }
1078
1079 /// @param lhs the left hand argument to the subtraction operation
1080 /// @param rhs the right hand argument to the subtraction operation
1081 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1082 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001083 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001084 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1085 Expr(std::forward<LHS>(lhs)),
1086 Expr(std::forward<RHS>(rhs)));
1087 }
1088
1089 /// @param lhs the left hand argument to the multiplication operation
1090 /// @param rhs the right hand argument to the multiplication operation
1091 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1092 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001093 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001094 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1095 Expr(std::forward<LHS>(lhs)),
1096 Expr(std::forward<RHS>(rhs)));
1097 }
1098
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001099 /// @param source the source information
1100 /// @param lhs the left hand argument to the multiplication operation
1101 /// @param rhs the right hand argument to the multiplication operation
1102 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1103 template <typename LHS, typename RHS>
1104 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1105 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1106 Expr(std::forward<LHS>(lhs)),
1107 Expr(std::forward<RHS>(rhs)));
1108 }
1109
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001110 /// @param lhs the left hand argument to the division operation
1111 /// @param rhs the right hand argument to the division operation
1112 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1113 template <typename LHS, typename RHS>
1114 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1115 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1116 Expr(std::forward<LHS>(lhs)),
1117 Expr(std::forward<RHS>(rhs)));
1118 }
1119
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001120 /// @param arr the array argument for the array accessor expression
1121 /// @param idx the index argument for the array accessor expression
1122 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1123 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001124 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001125 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1126 Expr(std::forward<IDX>(idx)));
1127 }
1128
1129 /// @param obj the object for the member accessor expression
1130 /// @param idx the index argument for the array accessor expression
1131 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1132 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001133 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001134 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1135 Expr(std::forward<IDX>(idx)));
1136 }
1137
Ben Clayton42d1e092021-02-02 14:29:15 +00001138 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001139 /// @param val the offset value
1140 /// @returns the offset decoration pointer
1141 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1142 return create<ast::StructMemberOffsetDecoration>(source_, val);
1143 }
1144
Ben Claytond614dd52021-03-15 10:43:11 +00001145 /// Creates a ast::StructMemberSizeDecoration
1146 /// @param source the source information
1147 /// @param val the size value
1148 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001149 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1150 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001151 return create<ast::StructMemberSizeDecoration>(source, val);
1152 }
1153
1154 /// Creates a ast::StructMemberSizeDecoration
1155 /// @param val the size value
1156 /// @returns the size decoration pointer
1157 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1158 return create<ast::StructMemberSizeDecoration>(source_, val);
1159 }
1160
1161 /// Creates a ast::StructMemberAlignDecoration
1162 /// @param source the source information
1163 /// @param val the align value
1164 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001165 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1166 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001167 return create<ast::StructMemberAlignDecoration>(source, val);
1168 }
1169
1170 /// Creates a ast::StructMemberAlignDecoration
1171 /// @param val the align value
1172 /// @returns the align decoration pointer
1173 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1174 return create<ast::StructMemberAlignDecoration>(source_, val);
1175 }
1176
Ben Clayton42d1e092021-02-02 14:29:15 +00001177 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001178 /// @param source the source information
1179 /// @param name the function name
1180 /// @param params the function parameters
1181 /// @param type the function return type
1182 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001183 /// @param decorations the optional function decorations
1184 /// @param return_type_decorations the optional function return type
1185 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001186 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001187 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001188 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001189 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001190 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001191 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001192 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001193 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001194 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001195 auto* func =
1196 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1197 type, create<ast::BlockStatement>(body),
1198 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001199 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001200 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001201 }
1202
Ben Clayton42d1e092021-02-02 14:29:15 +00001203 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001204 /// @param name the function name
1205 /// @param params the function parameters
1206 /// @param type the function return type
1207 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001208 /// @param decorations the optional function decorations
1209 /// @param return_type_decorations the optional function return type
1210 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001211 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001212 template <typename NAME>
1213 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001214 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001215 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001217 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001218 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001219 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1220 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001221 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001222 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001223 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001224 }
1225
Ben Clayton49668bb2021-04-17 05:32:01 +00001226 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001227 /// @param source the source information
1228 /// @returns the return statement pointer
1229 ast::ReturnStatement* Return(const Source& source) {
1230 return create<ast::ReturnStatement>(source);
1231 }
1232
1233 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001234 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001235 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1236
1237 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001238 /// @param source the source information
1239 /// @param val the return value
1240 /// @returns the return statement pointer
1241 template <typename EXPR>
1242 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1243 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1244 }
1245
1246 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001247 /// @param val the return value
1248 /// @returns the return statement pointer
1249 template <typename EXPR>
1250 ast::ReturnStatement* Return(EXPR&& val) {
1251 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001252 }
1253
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001254 /// Creates a ast::Struct and sem::StructType, registering the
1255 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001256 /// @param source the source information
1257 /// @param name the struct name
1258 /// @param members the struct members
1259 /// @param decorations the optional struct decorations
1260 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001261 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001262 sem::StructType* Structure(const Source& source,
1263 NAME&& name,
1264 ast::StructMemberList members,
1265 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001266 auto sym = Sym(std::forward<NAME>(name));
1267 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1268 std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001269 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001270 AST().AddConstructedType(type);
1271 return type;
1272 }
1273
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001274 /// Creates a ast::Struct and sem::StructType, registering the
1275 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001276 /// @param name the struct name
1277 /// @param members the struct members
1278 /// @param decorations the optional struct decorations
1279 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001280 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001281 sem::StructType* Structure(NAME&& name,
1282 ast::StructMemberList members,
1283 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001284 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001285 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001286 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001287 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001288 AST().AddConstructedType(type);
1289 return type;
1290 }
1291
Ben Clayton42d1e092021-02-02 14:29:15 +00001292 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001293 /// @param source the source information
1294 /// @param name the struct member name
1295 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001296 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001297 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001298 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001299 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001300 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001301 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001302 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001303 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1304 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001305 }
1306
Ben Clayton42d1e092021-02-02 14:29:15 +00001307 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001308 /// @param name the struct member name
1309 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001310 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001311 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001312 template <typename NAME>
1313 ast::StructMember* Member(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001314 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001315 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001316 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1317 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001318 }
1319
Ben Claytonbab31972021-02-08 21:16:21 +00001320 /// Creates a ast::StructMember with the given byte offset
1321 /// @param offset the offset to use in the StructMemberOffsetDecoration
1322 /// @param name the struct member name
1323 /// @param type the struct member type
1324 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001325 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001326 ast::StructMember* Member(uint32_t offset, NAME&& name, sem::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001327 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001328 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001329 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001330 create<ast::StructMemberOffsetDecoration>(offset),
1331 });
1332 }
1333
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001334 /// Creates a ast::BlockStatement with input statements
1335 /// @param statements statements of block
1336 /// @returns the block statement pointer
1337 template <typename... Statements>
1338 ast::BlockStatement* Block(Statements&&... statements) {
1339 return create<ast::BlockStatement>(
1340 ast::StatementList{std::forward<Statements>(statements)...});
1341 }
1342
1343 /// Creates a ast::ElseStatement with input condition and body
1344 /// @param condition the else condition expression
1345 /// @param body the else body
1346 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001347 template <typename CONDITION>
1348 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1349 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1350 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001351 }
1352
1353 /// Creates a ast::IfStatement with input condition, body, and optional
1354 /// variadic else statements
1355 /// @param condition the if statement condition expression
1356 /// @param body the if statement body
1357 /// @param elseStatements optional variadic else statements
1358 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001359 template <typename CONDITION, typename... ELSE_STATEMENTS>
1360 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001361 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001362 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001363 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001364 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001365 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001366 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001367 }
1368
1369 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001370 /// @param source the source information
1371 /// @param lhs the left hand side expression initializer
1372 /// @param rhs the right hand side expression initializer
1373 /// @returns the assignment statement pointer
1374 template <typename LhsExpressionInit, typename RhsExpressionInit>
1375 ast::AssignmentStatement* Assign(const Source& source,
1376 LhsExpressionInit&& lhs,
1377 RhsExpressionInit&& rhs) {
1378 return create<ast::AssignmentStatement>(
1379 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1380 Expr(std::forward<RhsExpressionInit>(rhs)));
1381 }
1382
1383 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001384 /// @param lhs the left hand side expression initializer
1385 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001386 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001387 template <typename LhsExpressionInit, typename RhsExpressionInit>
1388 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1389 RhsExpressionInit&& rhs) {
1390 return create<ast::AssignmentStatement>(
1391 Expr(std::forward<LhsExpressionInit>(lhs)),
1392 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001393 }
1394
1395 /// Creates a ast::LoopStatement with input body and optional continuing
1396 /// @param body the loop body
1397 /// @param continuing the optional continuing block
1398 /// @returns the loop statement pointer
1399 ast::LoopStatement* Loop(ast::BlockStatement* body,
1400 ast::BlockStatement* continuing = nullptr) {
1401 return create<ast::LoopStatement>(body, continuing);
1402 }
1403
1404 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001405 /// @param source the source information
1406 /// @param var the variable to wrap in a decl statement
1407 /// @returns the variable decl statement pointer
1408 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1409 return create<ast::VariableDeclStatement>(source, var);
1410 }
1411
1412 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001413 /// @param var the variable to wrap in a decl statement
1414 /// @returns the variable decl statement pointer
1415 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1416 return create<ast::VariableDeclStatement>(var);
1417 }
1418
Antonio Maioranocea744d2021-03-25 12:55:27 +00001419 /// Creates a ast::SwitchStatement with input expression and cases
1420 /// @param condition the condition expression initializer
1421 /// @param cases case statements
1422 /// @returns the switch statement pointer
1423 template <typename ExpressionInit, typename... Cases>
1424 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1425 return create<ast::SwitchStatement>(
1426 Expr(std::forward<ExpressionInit>(condition)),
1427 ast::CaseStatementList{std::forward<Cases>(cases)...});
1428 }
1429
1430 /// Creates a ast::CaseStatement with input list of selectors, and body
1431 /// @param selectors list of selectors
1432 /// @param body the case body
1433 /// @returns the case statement pointer
1434 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1435 ast::BlockStatement* body = nullptr) {
1436 return create<ast::CaseStatement>(std::move(selectors),
1437 body ? body : Block());
1438 }
1439
1440 /// Convenient overload that takes a single selector
1441 /// @param selector a single case selector
1442 /// @param body the case body
1443 /// @returns the case statement pointer
1444 ast::CaseStatement* Case(ast::IntLiteral* selector,
1445 ast::BlockStatement* body = nullptr) {
1446 return Case(ast::CaseSelectorList{selector}, body);
1447 }
1448
1449 /// Convenience function that creates a 'default' ast::CaseStatement
1450 /// @param body the case body
1451 /// @returns the case statement pointer
1452 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1453 return Case(ast::CaseSelectorList{}, body);
1454 }
1455
James Price68f558f2021-04-06 15:51:47 +00001456 /// Creates an ast::BuiltinDecoration
1457 /// @param source the source information
1458 /// @param builtin the builtin value
1459 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001460 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001461 return create<ast::BuiltinDecoration>(source, builtin);
1462 }
1463
1464 /// Creates an ast::BuiltinDecoration
1465 /// @param builtin the builtin value
1466 /// @returns the builtin decoration pointer
1467 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1468 return create<ast::BuiltinDecoration>(source_, builtin);
1469 }
1470
1471 /// Creates an ast::LocationDecoration
1472 /// @param source the source information
1473 /// @param location the location value
1474 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001475 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001476 return create<ast::LocationDecoration>(source, location);
1477 }
1478
1479 /// Creates an ast::LocationDecoration
1480 /// @param location the location value
1481 /// @returns the location decoration pointer
1482 ast::LocationDecoration* Location(uint32_t location) {
1483 return create<ast::LocationDecoration>(source_, location);
1484 }
1485
1486 /// Creates an ast::StageDecoration
1487 /// @param source the source information
1488 /// @param stage the pipeline stage
1489 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001490 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001491 return create<ast::StageDecoration>(source, stage);
1492 }
1493
1494 /// Creates an ast::StageDecoration
1495 /// @param stage the pipeline stage
1496 /// @returns the stage decoration pointer
1497 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1498 return create<ast::StageDecoration>(source_, stage);
1499 }
1500
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001501 /// Sets the current builder source to `src`
1502 /// @param src the Source used for future create() calls
1503 void SetSource(const Source& src) {
1504 AssertNotMoved();
1505 source_ = src;
1506 }
1507
1508 /// Sets the current builder source to `loc`
1509 /// @param loc the Source used for future create() calls
1510 void SetSource(const Source::Location& loc) {
1511 AssertNotMoved();
1512 source_ = Source(loc);
1513 }
1514
Ben Clayton33352542021-01-29 16:43:41 +00001515 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001516 /// @note As the Resolver is run when the Program is built, this will only be
1517 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001518 /// @param expr the AST expression
1519 /// @return the resolved semantic type for the expression, or nullptr if the
1520 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001521 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001522
Ben Clayton169512e2021-04-17 05:52:11 +00001523 /// Wraps the ast::Literal in a statement. This is used by tests that
1524 /// construct a partial AST and require the Resolver to reach these
1525 /// nodes.
1526 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1527 /// @return the ast::Statement that wraps the ast::Statement
1528 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001529 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001530 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001531 /// nodes.
1532 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1533 /// @return the ast::Statement that wraps the ast::Expression
1534 ast::Statement* WrapInStatement(ast::Expression* expr);
1535 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001536 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001537 /// these nodes.
1538 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1539 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1540 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1541 /// Returns the statement argument. Used as a passthrough-overload by
1542 /// WrapInFunction().
1543 /// @param stmt the ast::Statement
1544 /// @return `stmt`
1545 ast::Statement* WrapInStatement(ast::Statement* stmt);
1546 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001547 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001548 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001549 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001550 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001551 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001552 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001553 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001554 }
1555 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001556 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001557 /// @returns the function
1558 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001559
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001560 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001561 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001562
1563 protected:
1564 /// Asserts that the builder has not been moved.
1565 void AssertNotMoved() const;
1566
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001567 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001568 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001569 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001570 ASTNodeAllocator ast_nodes_;
1571 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001572 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001573 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001574 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001575 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001576
1577 /// The source to use when creating AST nodes without providing a Source as
1578 /// the first argument.
1579 Source source_;
1580
Ben Clayton5f0ea112021-03-09 10:54:37 +00001581 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001582 /// program when built.
1583 bool resolve_on_build_ = true;
1584
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001585 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1586 bool moved_ = false;
1587};
1588
1589//! @cond Doxygen_Suppress
1590// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1591template <>
1592struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001593 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001594 return t->i32();
1595 }
1596};
1597template <>
1598struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001599 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001600 return t->u32();
1601 }
1602};
1603template <>
1604struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001605 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001606 return t->f32();
1607 }
1608};
1609template <>
1610struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001611 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001612 return t->bool_();
1613 }
1614};
1615template <>
1616struct ProgramBuilder::TypesBuilder::CToAST<void> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001617 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001618 return t->void_();
1619 }
1620};
1621//! @endcond
1622
Ben Clayton917b14b2021-04-19 16:50:23 +00001623/// @param builder the ProgramBuilder
1624/// @returns the ProgramID of the ProgramBuilder
1625inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1626 return builder->ID();
1627}
1628
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001629} // namespace tint
1630
1631#endif // SRC_PROGRAM_BUILDER_H_