blob: 5125419af737bcc2f89c575f4364173825a2991b [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
21#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000022#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/binary_expression.h"
24#include "src/ast/bool_literal.h"
25#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000026#include "src/ast/case_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/float_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000028#include "src/ast/if_statement.h"
29#include "src/ast/loop_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030#include "src/ast/member_accessor_expression.h"
31#include "src/ast/module.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000032#include "src/ast/return_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/scalar_constructor_expression.h"
34#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000035#include "src/ast/stage_decoration.h"
Ben Claytonbab31972021-02-08 21:16:21 +000036#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000037#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000039#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000040#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000041#include "src/ast/type_constructor_expression.h"
42#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000043#include "src/ast/variable_decl_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000045#include "src/program_id.h"
Ben Clayton4db10dd2021-04-16 08:47:14 +000046#include "src/type/access_control_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000047#include "src/type/alias_type.h"
48#include "src/type/array_type.h"
49#include "src/type/bool_type.h"
50#include "src/type/f32_type.h"
51#include "src/type/i32_type.h"
52#include "src/type/matrix_type.h"
53#include "src/type/pointer_type.h"
54#include "src/type/struct_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000055#include "src/type/u32_type.h"
56#include "src/type/vector_type.h"
57#include "src/type/void_type.h"
58
59namespace tint {
60
Ben Clayton401b96b2021-02-03 17:19:59 +000061// Forward declarations
62namespace ast {
63class VariableDeclStatement;
64} // namespace ast
65
Ben Claytona6b9a8e2021-01-26 16:57:10 +000066class CloneContext;
67
68/// ProgramBuilder is a mutable builder for a Program.
69/// To construct a Program, populate the builder and then `std::move` it to a
70/// Program.
71class ProgramBuilder {
72 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000073 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
74 using ASTNodeAllocator = BlockAllocator<ast::Node>;
75
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000076 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
77 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000078
79 /// `i32` is a type alias to `int`.
80 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
81 /// WGSL syntax.
82 /// Note: this is intentionally not aliased to uint32_t as we want integer
83 /// literals passed to the builder to match WGSL's integer literal types.
84 using i32 = decltype(1);
85 /// `u32` is a type alias to `unsigned int`.
86 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
87 /// WGSL syntax.
88 /// Note: this is intentionally not aliased to uint32_t as we want integer
89 /// literals passed to the builder to match WGSL's integer literal types.
90 using u32 = decltype(1u);
91 /// `f32` is a type alias to `float`
92 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
93 /// WGSL syntax.
94 using f32 = float;
95
96 /// Constructor
97 ProgramBuilder();
98
99 /// Move constructor
100 /// @param rhs the builder to move
101 ProgramBuilder(ProgramBuilder&& rhs);
102
103 /// Destructor
104 virtual ~ProgramBuilder();
105
106 /// Move assignment operator
107 /// @param rhs the builder to move
108 /// @return this builder
109 ProgramBuilder& operator=(ProgramBuilder&& rhs);
110
Ben Claytone43c8302021-01-29 11:59:32 +0000111 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
112 /// making a deep clone of the Program contents.
113 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
114 /// existing immutable program.
115 /// As the returned ProgramBuilder wraps `program`, `program` must not be
116 /// destructed or assigned while using the returned ProgramBuilder.
117 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
118 /// function. See crbug.com/tint/460.
119 /// @param program the immutable Program to wrap
120 /// @return the ProgramBuilder that wraps `program`
121 static ProgramBuilder Wrap(const Program* program);
122
Ben Claytone6995de2021-04-13 23:27:27 +0000123 /// @returns the unique identifier for this program
124 ProgramID ID() const { return id_; }
125
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000126 /// @returns a reference to the program's types
127 type::Manager& Types() {
128 AssertNotMoved();
129 return types_;
130 }
131
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000132 /// @returns a reference to the program's types
133 const type::Manager& Types() const {
134 AssertNotMoved();
135 return types_;
136 }
137
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000138 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000139 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000140 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000141 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000142 }
143
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000144 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000145 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000146 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000147 return ast_nodes_;
148 }
149
150 /// @returns a reference to the program's semantic nodes storage
151 SemNodeAllocator& SemNodes() {
152 AssertNotMoved();
153 return sem_nodes_;
154 }
155
156 /// @returns a reference to the program's semantic nodes storage
157 const SemNodeAllocator& SemNodes() const {
158 AssertNotMoved();
159 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000160 }
161
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000162 /// @returns a reference to the program's AST root Module
163 ast::Module& AST() {
164 AssertNotMoved();
165 return *ast_;
166 }
167
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000168 /// @returns a reference to the program's AST root Module
169 const ast::Module& AST() const {
170 AssertNotMoved();
171 return *ast_;
172 }
173
174 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000175 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000176 AssertNotMoved();
177 return sem_;
178 }
179
180 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000181 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000182 AssertNotMoved();
183 return sem_;
184 }
185
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000186 /// @returns a reference to the program's SymbolTable
187 SymbolTable& Symbols() {
188 AssertNotMoved();
189 return symbols_;
190 }
191
Ben Clayton708dc2d2021-01-29 11:22:40 +0000192 /// @returns a reference to the program's SymbolTable
193 const SymbolTable& Symbols() const {
194 AssertNotMoved();
195 return symbols_;
196 }
197
Ben Clayton844217f2021-01-27 18:49:05 +0000198 /// @returns a reference to the program's diagnostics
199 diag::List& Diagnostics() {
200 AssertNotMoved();
201 return diagnostics_;
202 }
203
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000204 /// @returns a reference to the program's diagnostics
205 const diag::List& Diagnostics() const {
206 AssertNotMoved();
207 return diagnostics_;
208 }
209
Ben Clayton5f0ea112021-03-09 10:54:37 +0000210 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000211 /// @param enable the new flag value (defaults to true)
212 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
213
Ben Clayton5f0ea112021-03-09 10:54:37 +0000214 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000215 /// built.
216 bool ResolveOnBuild() const { return resolve_on_build_; }
217
Ben Clayton844217f2021-01-27 18:49:05 +0000218 /// @returns true if the program has no error diagnostics and is not missing
219 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000220 bool IsValid() const;
221
Ben Clayton708dc2d2021-01-29 11:22:40 +0000222 /// Writes a representation of the node to the output stream
223 /// @note unlike str(), to_str() does not automatically demangle the string.
224 /// @param node the AST node
225 /// @param out the stream to write to
226 /// @param indent number of spaces to indent the node when writing
227 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
228 node->to_str(Sem(), out, indent);
229 }
230
231 /// Returns a demangled, string representation of `node`.
232 /// @param node the AST node
233 /// @returns a string representation of the node
234 std::string str(const ast::Node* node) const;
235
Ben Clayton7fdfff12021-01-29 15:17:30 +0000236 /// Creates a new ast::Node owned by the ProgramBuilder. When the
237 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000238 /// @param source the Source of the node
239 /// @param args the arguments to pass to the type constructor
240 /// @returns the node pointer
241 template <typename T, typename... ARGS>
242 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
243 ARGS&&... args) {
244 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000245 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000246 }
247
Ben Clayton7fdfff12021-01-29 15:17:30 +0000248 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
249 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000250 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000251 /// When the ProgramBuilder is destructed, the ast::Node will also be
252 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000253 /// @returns the node pointer
254 template <typename T>
255 traits::EnableIfIsType<T, ast::Node>* create() {
256 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000257 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000258 }
259
Ben Clayton7fdfff12021-01-29 15:17:30 +0000260 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
261 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000262 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000263 /// When the ProgramBuilder is destructed, the ast::Node will also be
264 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000265 /// @param arg0 the first arguments to pass to the type constructor
266 /// @param args the remaining arguments to pass to the type constructor
267 /// @returns the node pointer
268 template <typename T, typename ARG0, typename... ARGS>
269 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
270 traits::IsTypeOrDerived<T, ast::Node>::value &&
271 !traits::IsTypeOrDerived<ARG0, Source>::value,
272 T>*
273 create(ARG0&& arg0, ARGS&&... args) {
274 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000275 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000276 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000277 }
278
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000279 /// Creates a new sem::Node owned by the ProgramBuilder.
280 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000281 /// destructed.
282 /// @param args the arguments to pass to the type constructor
283 /// @returns the node pointer
284 template <typename T, typename... ARGS>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000285 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000286 AssertNotMoved();
287 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
288 }
289
290 /// Creates a new type::Type owned by the ProgramBuilder.
291 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
292 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000293 /// Types are unique (de-aliased), and so calling create() for the same `T`
294 /// and arguments will return the same pointer.
295 /// @warning Use this method to acquire a type only if all of its type
296 /// information is provided in the constructor arguments `args`.<br>
297 /// If the type requires additional configuration after construction that
298 /// affect its fundamental type, build the type with `std::make_unique`, make
299 /// any necessary alterations and then call unique_type() instead.
300 /// @param args the arguments to pass to the type constructor
301 /// @returns the de-aliased type pointer
302 template <typename T, typename... ARGS>
303 traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) {
304 static_assert(std::is_base_of<type::Type, T>::value,
305 "T does not derive from type::Type");
306 AssertNotMoved();
307 return types_.Get<T>(std::forward<ARGS>(args)...);
308 }
309
310 /// Marks this builder as moved, preventing any further use of the builder.
311 void MarkAsMoved();
312
313 //////////////////////////////////////////////////////////////////////////////
314 // TypesBuilder
315 //////////////////////////////////////////////////////////////////////////////
316
317 /// TypesBuilder holds basic `tint` types and methods for constructing
318 /// complex types.
319 class TypesBuilder {
320 public:
321 /// Constructor
322 /// @param builder the program builder
323 explicit TypesBuilder(ProgramBuilder* builder);
324
325 /// @return the tint AST type for the C type `T`.
326 template <typename T>
327 type::Type* Of() const {
328 return CToAST<T>::get(this);
329 }
330
331 /// @returns a boolean type
332 type::Bool* bool_() const { return builder->create<type::Bool>(); }
333
334 /// @returns a f32 type
335 type::F32* f32() const { return builder->create<type::F32>(); }
336
337 /// @returns a i32 type
338 type::I32* i32() const { return builder->create<type::I32>(); }
339
340 /// @returns a u32 type
341 type::U32* u32() const { return builder->create<type::U32>(); }
342
343 /// @returns a void type
344 type::Void* void_() const { return builder->create<type::Void>(); }
345
Antonio Maiorano25436862021-04-13 13:32:33 +0000346 /// @param type vector subtype
347 /// @return the tint AST type for a 2-element vector of `type`.
348 type::Vector* vec2(type::Type* type) const {
349 return builder->create<type::Vector>(type, 2u);
350 }
351
352 /// @param type vector subtype
353 /// @return the tint AST type for a 3-element vector of `type`.
354 type::Vector* vec3(type::Type* type) const {
355 return builder->create<type::Vector>(type, 3u);
356 }
357
358 /// @param type vector subtype
359 /// @return the tint AST type for a 4-element vector of `type`.
360 type::Type* vec4(type::Type* type) const {
361 return builder->create<type::Vector>(type, 4u);
362 }
363
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000364 /// @return the tint AST type for a 2-element vector of the C type `T`.
365 template <typename T>
366 type::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000367 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368 }
369
370 /// @return the tint AST type for a 3-element vector of the C type `T`.
371 template <typename T>
372 type::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000373 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000374 }
375
376 /// @return the tint AST type for a 4-element vector of the C type `T`.
377 template <typename T>
378 type::Type* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000379 return vec4(Of<T>());
380 }
381
382 /// @param type matrix subtype
383 /// @return the tint AST type for a 2x3 matrix of `type`.
384 type::Matrix* mat2x2(type::Type* type) const {
385 return builder->create<type::Matrix>(type, 2u, 2u);
386 }
387
388 /// @param type matrix subtype
389 /// @return the tint AST type for a 2x3 matrix of `type`.
390 type::Matrix* mat2x3(type::Type* type) const {
391 return builder->create<type::Matrix>(type, 3u, 2u);
392 }
393
394 /// @param type matrix subtype
395 /// @return the tint AST type for a 2x4 matrix of `type`.
396 type::Matrix* mat2x4(type::Type* type) const {
397 return builder->create<type::Matrix>(type, 4u, 2u);
398 }
399
400 /// @param type matrix subtype
401 /// @return the tint AST type for a 3x2 matrix of `type`.
402 type::Matrix* mat3x2(type::Type* type) const {
403 return builder->create<type::Matrix>(type, 2u, 3u);
404 }
405
406 /// @param type matrix subtype
407 /// @return the tint AST type for a 3x3 matrix of `type`.
408 type::Matrix* mat3x3(type::Type* type) const {
409 return builder->create<type::Matrix>(type, 3u, 3u);
410 }
411
412 /// @param type matrix subtype
413 /// @return the tint AST type for a 3x4 matrix of `type`.
414 type::Matrix* mat3x4(type::Type* type) const {
415 return builder->create<type::Matrix>(type, 4u, 3u);
416 }
417
418 /// @param type matrix subtype
419 /// @return the tint AST type for a 4x2 matrix of `type`.
420 type::Matrix* mat4x2(type::Type* type) const {
421 return builder->create<type::Matrix>(type, 2u, 4u);
422 }
423
424 /// @param type matrix subtype
425 /// @return the tint AST type for a 4x3 matrix of `type`.
426 type::Matrix* mat4x3(type::Type* type) const {
427 return builder->create<type::Matrix>(type, 3u, 4u);
428 }
429
430 /// @param type matrix subtype
431 /// @return the tint AST type for a 4x4 matrix of `type`.
432 type::Matrix* mat4x4(type::Type* type) const {
433 return builder->create<type::Matrix>(type, 4u, 4u);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000434 }
435
436 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
437 template <typename T>
438 type::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000439 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000440 }
441
442 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
443 template <typename T>
444 type::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000445 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000446 }
447
448 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
449 template <typename T>
450 type::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000451 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000452 }
453
454 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
455 template <typename T>
456 type::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000457 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000458 }
459
460 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
461 template <typename T>
462 type::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000463 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000464 }
465
466 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
467 template <typename T>
468 type::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000469 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000470 }
471
472 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
473 template <typename T>
474 type::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000475 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000476 }
477
478 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
479 template <typename T>
480 type::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000481 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000482 }
483
484 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
485 template <typename T>
486 type::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000487 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000488 }
489
490 /// @param subtype the array element type
491 /// @param n the array size. 0 represents a runtime-array.
492 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000493 type::Array* array(type::Type* subtype, uint32_t n = 0) const {
James Price95d40772021-03-11 17:39:32 +0000494 return builder->create<type::Array>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000495 }
496
Ben Claytonbab31972021-02-08 21:16:21 +0000497 /// @param subtype the array element type
498 /// @param n the array size. 0 represents a runtime-array.
499 /// @param stride the array stride.
500 /// @return the tint AST type for a array of size `n` of type `T`
501 type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const {
502 return builder->create<type::Array>(
503 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000504 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000505 builder->create<ast::StrideDecoration>(stride),
506 });
507 }
508
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000509 /// @return the tint AST type for an array of size `N` of type `T`
510 template <typename T, int N = 0>
511 type::Array* array() const {
512 return array(Of<T>(), N);
513 }
514
Ben Claytonbab31972021-02-08 21:16:21 +0000515 /// @param stride the array stride
516 /// @return the tint AST type for an array of size `N` of type `T`
517 template <typename T, int N = 0>
518 type::Array* array(uint32_t stride) const {
519 return array(Of<T>(), N, stride);
520 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000521
Ben Clayton42d1e092021-02-02 14:29:15 +0000522 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000523 /// @param name the alias name
524 /// @param type the alias type
525 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000526 template <typename NAME>
527 type::Alias* alias(NAME&& name, type::Type* type) const {
528 return builder->create<type::Alias>(
529 builder->Sym(std::forward<NAME>(name)), type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000530 }
531
Ben Clayton4db10dd2021-04-16 08:47:14 +0000532 /// Creates an access control qualifier type
533 /// @param access the access control
534 /// @param type the inner type
535 /// @returns the access control qualifier type
536 type::AccessControl* access(ast::AccessControl access,
537 type::Type* type) const {
538 return builder->create<type::AccessControl>(access, type);
539 }
540
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000541 /// @return the tint AST pointer to `type` with the given ast::StorageClass
542 /// @param type the type of the pointer
543 /// @param storage_class the storage class of the pointer
544 type::Pointer* pointer(type::Type* type,
545 ast::StorageClass storage_class) const {
546 return builder->create<type::Pointer>(type, storage_class);
547 }
548
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000549 /// @return the tint AST pointer to type `T` with the given
550 /// ast::StorageClass.
551 /// @param storage_class the storage class of the pointer
552 template <typename T>
553 type::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000554 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 }
556
557 /// @param name the struct name
558 /// @param impl the struct implementation
559 /// @returns a struct pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000560 template <typename NAME>
561 type::Struct* struct_(NAME&& name, ast::Struct* impl) const {
562 return builder->create<type::Struct>(
563 builder->Sym(std::forward<NAME>(name)), impl);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000564 }
565
566 private:
567 /// CToAST<T> is specialized for various `T` types and each specialization
568 /// contains a single static `get()` method for obtaining the corresponding
569 /// AST type for the C type `T`.
570 /// `get()` has the signature:
571 /// `static type::Type* get(Types* t)`
572 template <typename T>
573 struct CToAST {};
574
Ben Claytonc7ca7662021-02-17 16:23:52 +0000575 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000576 };
577
578 //////////////////////////////////////////////////////////////////////////////
579 // AST helper methods
580 //////////////////////////////////////////////////////////////////////////////
581
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000582 /// @param name the symbol string
583 /// @return a Symbol with the given name
584 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
585
586 /// @param sym the symbol
587 /// @return `sym`
588 Symbol Sym(Symbol sym) { return sym; }
589
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000590 /// @param expr the expression
591 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000592 template <typename T>
593 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
594 return expr;
595 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000596
597 /// @param name the identifier name
598 /// @return an ast::IdentifierExpression with the given name
599 ast::IdentifierExpression* Expr(const std::string& name) {
600 return create<ast::IdentifierExpression>(Symbols().Register(name));
601 }
602
Ben Clayton46d78d72021-02-10 21:34:26 +0000603 /// @param symbol the identifier symbol
604 /// @return an ast::IdentifierExpression with the given symbol
605 ast::IdentifierExpression* Expr(Symbol symbol) {
606 return create<ast::IdentifierExpression>(symbol);
607 }
608
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000609 /// @param source the source information
610 /// @param name the identifier name
611 /// @return an ast::IdentifierExpression with the given name
612 ast::IdentifierExpression* Expr(const Source& source,
613 const std::string& name) {
614 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
615 }
616
617 /// @param name the identifier name
618 /// @return an ast::IdentifierExpression with the given name
619 ast::IdentifierExpression* Expr(const char* name) {
620 return create<ast::IdentifierExpression>(Symbols().Register(name));
621 }
622
623 /// @param value the boolean value
624 /// @return a Scalar constructor for the given value
625 ast::ScalarConstructorExpression* Expr(bool value) {
626 return create<ast::ScalarConstructorExpression>(Literal(value));
627 }
628
629 /// @param value the float value
630 /// @return a Scalar constructor for the given value
631 ast::ScalarConstructorExpression* Expr(f32 value) {
632 return create<ast::ScalarConstructorExpression>(Literal(value));
633 }
634
635 /// @param value the integer value
636 /// @return a Scalar constructor for the given value
637 ast::ScalarConstructorExpression* Expr(i32 value) {
638 return create<ast::ScalarConstructorExpression>(Literal(value));
639 }
640
641 /// @param value the unsigned int value
642 /// @return a Scalar constructor for the given value
643 ast::ScalarConstructorExpression* Expr(u32 value) {
644 return create<ast::ScalarConstructorExpression>(Literal(value));
645 }
646
647 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
648 /// `list`.
649 /// @param list the list to append too
650 /// @param arg the arg to create
651 template <typename ARG>
652 void Append(ast::ExpressionList& list, ARG&& arg) {
653 list.emplace_back(Expr(std::forward<ARG>(arg)));
654 }
655
656 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
657 /// then appends them to `list`.
658 /// @param list the list to append too
659 /// @param arg0 the first argument
660 /// @param args the rest of the arguments
661 template <typename ARG0, typename... ARGS>
662 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
663 Append(list, std::forward<ARG0>(arg0));
664 Append(list, std::forward<ARGS>(args)...);
665 }
666
667 /// @return an empty list of expressions
668 ast::ExpressionList ExprList() { return {}; }
669
670 /// @param args the list of expressions
671 /// @return the list of expressions converted to `ast::Expression`s using
672 /// `Expr()`,
673 template <typename... ARGS>
674 ast::ExpressionList ExprList(ARGS&&... args) {
675 ast::ExpressionList list;
676 list.reserve(sizeof...(args));
677 Append(list, std::forward<ARGS>(args)...);
678 return list;
679 }
680
681 /// @param list the list of expressions
682 /// @return `list`
683 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
684
685 /// @param val the boolan value
686 /// @return a boolean literal with the given value
687 ast::BoolLiteral* Literal(bool val) {
688 return create<ast::BoolLiteral>(ty.bool_(), val);
689 }
690
691 /// @param val the float value
692 /// @return a float literal with the given value
693 ast::FloatLiteral* Literal(f32 val) {
694 return create<ast::FloatLiteral>(ty.f32(), val);
695 }
696
697 /// @param val the unsigned int value
698 /// @return a ast::UintLiteral with the given value
699 ast::UintLiteral* Literal(u32 val) {
700 return create<ast::UintLiteral>(ty.u32(), val);
701 }
702
703 /// @param val the integer value
704 /// @return the ast::SintLiteral with the given value
705 ast::SintLiteral* Literal(i32 val) {
706 return create<ast::SintLiteral>(ty.i32(), val);
707 }
708
709 /// @param args the arguments for the type constructor
710 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
711 /// of `args` converted to `ast::Expression`s using `Expr()`
712 template <typename T, typename... ARGS>
713 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
714 return create<ast::TypeConstructorExpression>(
715 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
716 }
717
718 /// @param type the type to construct
719 /// @param args the arguments for the constructor
720 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
721 /// values `args`.
722 template <typename... ARGS>
723 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
724 return create<ast::TypeConstructorExpression>(
725 type, ExprList(std::forward<ARGS>(args)...));
726 }
727
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000728 /// Creates a constructor expression that constructs an object of
729 /// `type` filled with `elem_value`. For example,
730 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
731 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
732 /// @param type the type to construct
733 /// @param elem_value the initial or element value (for vec and mat) to
734 /// construct with
735 /// @return the constructor expression
736 ast::ConstructorExpression* ConstructValueFilledWith(type::Type* type,
737 int elem_value = 0);
738
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000739 /// @param args the arguments for the vector constructor
740 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
741 /// `T`, constructed with the values `args`.
742 template <typename T, typename... ARGS>
743 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
744 return create<ast::TypeConstructorExpression>(
745 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
746 }
747
748 /// @param args the arguments for the vector constructor
749 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
750 /// `T`, constructed with the values `args`.
751 template <typename T, typename... ARGS>
752 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
753 return create<ast::TypeConstructorExpression>(
754 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
755 }
756
757 /// @param args the arguments for the vector constructor
758 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
759 /// `T`, constructed with the values `args`.
760 template <typename T, typename... ARGS>
761 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
762 return create<ast::TypeConstructorExpression>(
763 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
764 }
765
766 /// @param args the arguments for the matrix constructor
767 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
768 /// `T`, constructed with the values `args`.
769 template <typename T, typename... ARGS>
770 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
771 return create<ast::TypeConstructorExpression>(
772 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
773 }
774
775 /// @param args the arguments for the matrix constructor
776 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
777 /// `T`, constructed with the values `args`.
778 template <typename T, typename... ARGS>
779 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
780 return create<ast::TypeConstructorExpression>(
781 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
782 }
783
784 /// @param args the arguments for the matrix constructor
785 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
786 /// `T`, constructed with the values `args`.
787 template <typename T, typename... ARGS>
788 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
789 return create<ast::TypeConstructorExpression>(
790 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
791 }
792
793 /// @param args the arguments for the matrix constructor
794 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
795 /// `T`, constructed with the values `args`.
796 template <typename T, typename... ARGS>
797 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
798 return create<ast::TypeConstructorExpression>(
799 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
800 }
801
802 /// @param args the arguments for the matrix constructor
803 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
804 /// `T`, constructed with the values `args`.
805 template <typename T, typename... ARGS>
806 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
807 return create<ast::TypeConstructorExpression>(
808 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
809 }
810
811 /// @param args the arguments for the matrix constructor
812 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
813 /// `T`, constructed with the values `args`.
814 template <typename T, typename... ARGS>
815 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
816 return create<ast::TypeConstructorExpression>(
817 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
818 }
819
820 /// @param args the arguments for the matrix constructor
821 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
822 /// `T`, constructed with the values `args`.
823 template <typename T, typename... ARGS>
824 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
825 return create<ast::TypeConstructorExpression>(
826 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
827 }
828
829 /// @param args the arguments for the matrix constructor
830 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
831 /// `T`, constructed with the values `args`.
832 template <typename T, typename... ARGS>
833 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
834 return create<ast::TypeConstructorExpression>(
835 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
836 }
837
838 /// @param args the arguments for the matrix constructor
839 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
840 /// `T`, constructed with the values `args`.
841 template <typename T, typename... ARGS>
842 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
843 return create<ast::TypeConstructorExpression>(
844 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
845 }
846
847 /// @param args the arguments for the array constructor
848 /// @return an `ast::TypeConstructorExpression` of an array with element type
849 /// `T`, constructed with the values `args`.
850 template <typename T, int N = 0, typename... ARGS>
851 ast::TypeConstructorExpression* array(ARGS&&... args) {
852 return create<ast::TypeConstructorExpression>(
853 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
854 }
855
856 /// @param subtype the array element type
857 /// @param n the array size. 0 represents a runtime-array.
858 /// @param args the arguments for the array constructor
859 /// @return an `ast::TypeConstructorExpression` of an array with element type
860 /// `subtype`, constructed with the values `args`.
861 template <typename... ARGS>
862 ast::TypeConstructorExpression* array(type::Type* subtype,
863 uint32_t n,
864 ARGS&&... args) {
865 return create<ast::TypeConstructorExpression>(
866 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
867 }
868
869 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000870 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000871 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000872 /// @param constructor constructor expression
873 /// @param decorations variable decorations
874 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000875 template <typename NAME>
876 ast::Variable* Var(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000877 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000878 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000879 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000880 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000881 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
882 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000883 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000884
885 /// @param source the variable source
886 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000887 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000888 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000889 /// @param constructor constructor expression
890 /// @param decorations variable decorations
891 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000892 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000893 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000894 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000895 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000896 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000897 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000898 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000899 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000900 type, false, constructor, decorations);
901 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000902
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000903 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000904 /// @param type the variable type
905 /// @param constructor optional constructor expression
906 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000907 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000908 template <typename NAME>
909 ast::Variable* Const(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000910 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000911 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000912 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000913 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000914 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000915 constructor, decorations);
916 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000917
918 /// @param source the variable source
919 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000920 /// @param type the variable type
921 /// @param constructor optional constructor expression
922 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000923 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000924 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000925 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000926 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000927 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000928 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000929 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000930 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000931 ast::StorageClass::kNone, type, true,
932 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000933 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000934
James Pricedaf1f1c2021-04-08 22:15:48 +0000935 /// @param name the parameter name
936 /// @param type the parameter type
937 /// @param decorations optional parameter decorations
938 /// @returns a constant `ast::Variable` with the given name and type
939 template <typename NAME>
940 ast::Variable* Param(NAME&& name,
941 type::Type* type,
942 ast::DecorationList decorations = {}) {
943 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
944 ast::StorageClass::kNone, type, true, nullptr,
945 decorations);
946 }
947
948 /// @param source the parameter source
949 /// @param name the parameter name
950 /// @param type the parameter type
951 /// @param decorations optional parameter decorations
952 /// @returns a constant `ast::Variable` with the given name and type
953 template <typename NAME>
954 ast::Variable* Param(const Source& source,
955 NAME&& name,
956 type::Type* type,
957 ast::DecorationList decorations = {}) {
958 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
959 ast::StorageClass::kNone, type, true, nullptr,
960 decorations);
961 }
962
Ben Clayton401b96b2021-02-03 17:19:59 +0000963 /// @param args the arguments to pass to Var()
964 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
965 /// of `args`, which is automatically registered as a global variable with the
966 /// ast::Module.
967 template <typename... ARGS>
968 ast::Variable* Global(ARGS&&... args) {
969 auto* var = Var(std::forward<ARGS>(args)...);
970 AST().AddGlobalVariable(var);
971 return var;
972 }
973
974 /// @param args the arguments to pass to Const()
975 /// @returns a const `ast::Variable` constructed by calling Var() with the
976 /// arguments of `args`, which is automatically registered as a global
977 /// variable with the ast::Module.
978 template <typename... ARGS>
979 ast::Variable* GlobalConst(ARGS&&... args) {
980 auto* var = Const(std::forward<ARGS>(args)...);
981 AST().AddGlobalVariable(var);
982 return var;
983 }
984
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000985 /// @param func the function name
986 /// @param args the function call arguments
987 /// @returns a `ast::CallExpression` to the function `func`, with the
988 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
989 template <typename NAME, typename... ARGS>
990 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
991 return create<ast::CallExpression>(Expr(func),
992 ExprList(std::forward<ARGS>(args)...));
993 }
994
995 /// @param lhs the left hand argument to the addition operation
996 /// @param rhs the right hand argument to the addition operation
997 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
998 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +0000999 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001000 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1001 Expr(std::forward<LHS>(lhs)),
1002 Expr(std::forward<RHS>(rhs)));
1003 }
1004
1005 /// @param lhs the left hand argument to the subtraction operation
1006 /// @param rhs the right hand argument to the subtraction operation
1007 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1008 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001009 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001010 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1011 Expr(std::forward<LHS>(lhs)),
1012 Expr(std::forward<RHS>(rhs)));
1013 }
1014
1015 /// @param lhs the left hand argument to the multiplication operation
1016 /// @param rhs the right hand argument to the multiplication operation
1017 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1018 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001019 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001020 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1021 Expr(std::forward<LHS>(lhs)),
1022 Expr(std::forward<RHS>(rhs)));
1023 }
1024
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001025 /// @param source the source information
1026 /// @param lhs the left hand argument to the multiplication operation
1027 /// @param rhs the right hand argument to the multiplication operation
1028 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1029 template <typename LHS, typename RHS>
1030 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1031 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1032 Expr(std::forward<LHS>(lhs)),
1033 Expr(std::forward<RHS>(rhs)));
1034 }
1035
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001036 /// @param lhs the left hand argument to the division operation
1037 /// @param rhs the right hand argument to the division operation
1038 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1039 template <typename LHS, typename RHS>
1040 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1041 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1042 Expr(std::forward<LHS>(lhs)),
1043 Expr(std::forward<RHS>(rhs)));
1044 }
1045
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001046 /// @param arr the array argument for the array accessor expression
1047 /// @param idx the index argument for the array accessor expression
1048 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1049 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001050 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001051 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1052 Expr(std::forward<IDX>(idx)));
1053 }
1054
1055 /// @param obj the object for the member accessor expression
1056 /// @param idx the index argument for the array accessor expression
1057 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1058 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001059 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001060 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1061 Expr(std::forward<IDX>(idx)));
1062 }
1063
Ben Clayton42d1e092021-02-02 14:29:15 +00001064 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001065 /// @param val the offset value
1066 /// @returns the offset decoration pointer
1067 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1068 return create<ast::StructMemberOffsetDecoration>(source_, val);
1069 }
1070
Ben Claytond614dd52021-03-15 10:43:11 +00001071 /// Creates a ast::StructMemberSizeDecoration
1072 /// @param source the source information
1073 /// @param val the size value
1074 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001075 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1076 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001077 return create<ast::StructMemberSizeDecoration>(source, val);
1078 }
1079
1080 /// Creates a ast::StructMemberSizeDecoration
1081 /// @param val the size value
1082 /// @returns the size decoration pointer
1083 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1084 return create<ast::StructMemberSizeDecoration>(source_, val);
1085 }
1086
1087 /// Creates a ast::StructMemberAlignDecoration
1088 /// @param source the source information
1089 /// @param val the align value
1090 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001091 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1092 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001093 return create<ast::StructMemberAlignDecoration>(source, val);
1094 }
1095
1096 /// Creates a ast::StructMemberAlignDecoration
1097 /// @param val the align value
1098 /// @returns the align decoration pointer
1099 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1100 return create<ast::StructMemberAlignDecoration>(source_, val);
1101 }
1102
Ben Clayton42d1e092021-02-02 14:29:15 +00001103 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001104 /// @param source the source information
1105 /// @param name the function name
1106 /// @param params the function parameters
1107 /// @param type the function return type
1108 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001109 /// @param decorations the optional function decorations
1110 /// @param return_type_decorations the optional function return type
1111 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001112 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001113 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001114 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001115 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001116 ast::VariableList params,
1117 type::Type* type,
1118 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001119 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001120 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001121 auto* func =
1122 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1123 type, create<ast::BlockStatement>(body),
1124 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001125 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001126 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 }
1128
Ben Clayton42d1e092021-02-02 14:29:15 +00001129 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001130 /// @param name the function name
1131 /// @param params the function parameters
1132 /// @param type the function return type
1133 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001134 /// @param decorations the optional function decorations
1135 /// @param return_type_decorations the optional function return type
1136 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001138 template <typename NAME>
1139 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001140 ast::VariableList params,
1141 type::Type* type,
1142 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001143 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001144 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001145 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1146 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001147 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001148 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001149 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001150 }
1151
Ben Clayton49668bb2021-04-17 05:32:01 +00001152 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001153 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001154 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1155
1156 /// Creates an ast::ReturnStatement with the given return value
1157 /// @param val the return value
1158 /// @returns the return statement pointer
1159 template <typename EXPR>
1160 ast::ReturnStatement* Return(EXPR&& val) {
1161 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001162 }
1163
Ben Claytond614dd52021-03-15 10:43:11 +00001164 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1165 /// the AST().ConstructedTypes().
1166 /// @param source the source information
1167 /// @param name the struct name
1168 /// @param members the struct members
1169 /// @param decorations the optional struct decorations
1170 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001171 template <typename NAME>
Ben Claytond614dd52021-03-15 10:43:11 +00001172 type::Struct* Structure(const Source& source,
Ben Clayton1d618b12021-04-09 16:19:48 +00001173 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001174 ast::StructMemberList members,
1175 ast::DecorationList decorations = {}) {
1176 auto* impl =
1177 create<ast::Struct>(source, std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001178 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001179 AST().AddConstructedType(type);
1180 return type;
1181 }
1182
1183 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1184 /// the AST().ConstructedTypes().
1185 /// @param name the struct name
1186 /// @param members the struct members
1187 /// @param decorations the optional struct decorations
1188 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001189 template <typename NAME>
1190 type::Struct* Structure(NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001191 ast::StructMemberList members,
1192 ast::DecorationList decorations = {}) {
1193 auto* impl =
1194 create<ast::Struct>(std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001195 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001196 AST().AddConstructedType(type);
1197 return type;
1198 }
1199
Ben Clayton42d1e092021-02-02 14:29:15 +00001200 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001201 /// @param source the source information
1202 /// @param name the struct member name
1203 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001204 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001205 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001206 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001208 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001209 type::Type* type,
1210 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001211 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1212 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001213 }
1214
Ben Clayton42d1e092021-02-02 14:29:15 +00001215 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216 /// @param name the struct member name
1217 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001218 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001219 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001220 template <typename NAME>
1221 ast::StructMember* Member(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001222 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001223 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001224 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1225 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001226 }
1227
Ben Claytonbab31972021-02-08 21:16:21 +00001228 /// Creates a ast::StructMember with the given byte offset
1229 /// @param offset the offset to use in the StructMemberOffsetDecoration
1230 /// @param name the struct member name
1231 /// @param type the struct member type
1232 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001233 template <typename NAME>
1234 ast::StructMember* Member(uint32_t offset, NAME&& name, type::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001235 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001236 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001237 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001238 create<ast::StructMemberOffsetDecoration>(offset),
1239 });
1240 }
1241
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001242 /// Creates a ast::BlockStatement with input statements
1243 /// @param statements statements of block
1244 /// @returns the block statement pointer
1245 template <typename... Statements>
1246 ast::BlockStatement* Block(Statements&&... statements) {
1247 return create<ast::BlockStatement>(
1248 ast::StatementList{std::forward<Statements>(statements)...});
1249 }
1250
1251 /// Creates a ast::ElseStatement with input condition and body
1252 /// @param condition the else condition expression
1253 /// @param body the else body
1254 /// @returns the else statement pointer
1255 ast::ElseStatement* Else(ast::Expression* condition,
1256 ast::BlockStatement* body) {
1257 return create<ast::ElseStatement>(condition, body);
1258 }
1259
1260 /// Creates a ast::IfStatement with input condition, body, and optional
1261 /// variadic else statements
1262 /// @param condition the if statement condition expression
1263 /// @param body the if statement body
1264 /// @param elseStatements optional variadic else statements
1265 /// @returns the if statement pointer
1266 template <typename... ElseStatements>
1267 ast::IfStatement* If(ast::Expression* condition,
1268 ast::BlockStatement* body,
1269 ElseStatements&&... elseStatements) {
1270 return create<ast::IfStatement>(
1271 condition, body,
1272 ast::ElseStatementList{
1273 std::forward<ElseStatements>(elseStatements)...});
1274 }
1275
1276 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001277 /// @param lhs the left hand side expression initializer
1278 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001279 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001280 template <typename LhsExpressionInit, typename RhsExpressionInit>
1281 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1282 RhsExpressionInit&& rhs) {
1283 return create<ast::AssignmentStatement>(
1284 Expr(std::forward<LhsExpressionInit>(lhs)),
1285 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001286 }
1287
1288 /// Creates a ast::LoopStatement with input body and optional continuing
1289 /// @param body the loop body
1290 /// @param continuing the optional continuing block
1291 /// @returns the loop statement pointer
1292 ast::LoopStatement* Loop(ast::BlockStatement* body,
1293 ast::BlockStatement* continuing = nullptr) {
1294 return create<ast::LoopStatement>(body, continuing);
1295 }
1296
1297 /// Creates a ast::VariableDeclStatement for the input variable
1298 /// @param var the variable to wrap in a decl statement
1299 /// @returns the variable decl statement pointer
1300 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1301 return create<ast::VariableDeclStatement>(var);
1302 }
1303
Antonio Maioranocea744d2021-03-25 12:55:27 +00001304 /// Creates a ast::SwitchStatement with input expression and cases
1305 /// @param condition the condition expression initializer
1306 /// @param cases case statements
1307 /// @returns the switch statement pointer
1308 template <typename ExpressionInit, typename... Cases>
1309 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1310 return create<ast::SwitchStatement>(
1311 Expr(std::forward<ExpressionInit>(condition)),
1312 ast::CaseStatementList{std::forward<Cases>(cases)...});
1313 }
1314
1315 /// Creates a ast::CaseStatement with input list of selectors, and body
1316 /// @param selectors list of selectors
1317 /// @param body the case body
1318 /// @returns the case statement pointer
1319 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1320 ast::BlockStatement* body = nullptr) {
1321 return create<ast::CaseStatement>(std::move(selectors),
1322 body ? body : Block());
1323 }
1324
1325 /// Convenient overload that takes a single selector
1326 /// @param selector a single case selector
1327 /// @param body the case body
1328 /// @returns the case statement pointer
1329 ast::CaseStatement* Case(ast::IntLiteral* selector,
1330 ast::BlockStatement* body = nullptr) {
1331 return Case(ast::CaseSelectorList{selector}, body);
1332 }
1333
1334 /// Convenience function that creates a 'default' ast::CaseStatement
1335 /// @param body the case body
1336 /// @returns the case statement pointer
1337 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1338 return Case(ast::CaseSelectorList{}, body);
1339 }
1340
James Price68f558f2021-04-06 15:51:47 +00001341 /// Creates an ast::BuiltinDecoration
1342 /// @param source the source information
1343 /// @param builtin the builtin value
1344 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001345 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001346 return create<ast::BuiltinDecoration>(source, builtin);
1347 }
1348
1349 /// Creates an ast::BuiltinDecoration
1350 /// @param builtin the builtin value
1351 /// @returns the builtin decoration pointer
1352 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1353 return create<ast::BuiltinDecoration>(source_, builtin);
1354 }
1355
1356 /// Creates an ast::LocationDecoration
1357 /// @param source the source information
1358 /// @param location the location value
1359 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001360 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001361 return create<ast::LocationDecoration>(source, location);
1362 }
1363
1364 /// Creates an ast::LocationDecoration
1365 /// @param location the location value
1366 /// @returns the location decoration pointer
1367 ast::LocationDecoration* Location(uint32_t location) {
1368 return create<ast::LocationDecoration>(source_, location);
1369 }
1370
1371 /// Creates an ast::StageDecoration
1372 /// @param source the source information
1373 /// @param stage the pipeline stage
1374 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001375 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001376 return create<ast::StageDecoration>(source, stage);
1377 }
1378
1379 /// Creates an ast::StageDecoration
1380 /// @param stage the pipeline stage
1381 /// @returns the stage decoration pointer
1382 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1383 return create<ast::StageDecoration>(source_, stage);
1384 }
1385
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001386 /// Sets the current builder source to `src`
1387 /// @param src the Source used for future create() calls
1388 void SetSource(const Source& src) {
1389 AssertNotMoved();
1390 source_ = src;
1391 }
1392
1393 /// Sets the current builder source to `loc`
1394 /// @param loc the Source used for future create() calls
1395 void SetSource(const Source::Location& loc) {
1396 AssertNotMoved();
1397 source_ = Source(loc);
1398 }
1399
Ben Clayton33352542021-01-29 16:43:41 +00001400 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001401 /// @note As the Resolver is run when the Program is built, this will only be
1402 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001403 /// @param expr the AST expression
1404 /// @return the resolved semantic type for the expression, or nullptr if the
1405 /// expression has no resolved type.
1406 type::Type* TypeOf(ast::Expression* expr) const;
1407
Ben Clayton169512e2021-04-17 05:52:11 +00001408 /// Wraps the ast::Literal in a statement. This is used by tests that
1409 /// construct a partial AST and require the Resolver to reach these
1410 /// nodes.
1411 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1412 /// @return the ast::Statement that wraps the ast::Statement
1413 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001414 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001415 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001416 /// nodes.
1417 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1418 /// @return the ast::Statement that wraps the ast::Expression
1419 ast::Statement* WrapInStatement(ast::Expression* expr);
1420 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001421 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001422 /// these nodes.
1423 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1424 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1425 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1426 /// Returns the statement argument. Used as a passthrough-overload by
1427 /// WrapInFunction().
1428 /// @param stmt the ast::Statement
1429 /// @return `stmt`
1430 ast::Statement* WrapInStatement(ast::Statement* stmt);
1431 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001432 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001433 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001434 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001435 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001436 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001437 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001438 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001439 }
1440 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001441 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001442 /// @returns the function
1443 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001444
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001445 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001446 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001447
1448 protected:
1449 /// Asserts that the builder has not been moved.
1450 void AssertNotMoved() const;
1451
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001452 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001453 ProgramID id_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001454 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001455 ASTNodeAllocator ast_nodes_;
1456 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001457 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001458 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001459 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001460 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001461
1462 /// The source to use when creating AST nodes without providing a Source as
1463 /// the first argument.
1464 Source source_;
1465
Ben Clayton5f0ea112021-03-09 10:54:37 +00001466 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001467 /// program when built.
1468 bool resolve_on_build_ = true;
1469
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001470 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1471 bool moved_ = false;
1472};
1473
1474//! @cond Doxygen_Suppress
1475// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1476template <>
1477struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1478 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1479 return t->i32();
1480 }
1481};
1482template <>
1483struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1484 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1485 return t->u32();
1486 }
1487};
1488template <>
1489struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1490 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1491 return t->f32();
1492 }
1493};
1494template <>
1495struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1496 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1497 return t->bool_();
1498 }
1499};
1500template <>
1501struct ProgramBuilder::TypesBuilder::CToAST<void> {
1502 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1503 return t->void_();
1504 }
1505};
1506//! @endcond
1507
1508} // namespace tint
1509
1510#endif // SRC_PROGRAM_BUILDER_H_