blob: 8e3802c81e48f689b0aba756900f4b29921fede6 [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
Ben Claytonb8ea5912021-04-19 16:52:42 +0000597 /// Passthrough for nullptr
598 /// @return nullptr
599 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
600
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000601 /// @param name the identifier name
602 /// @return an ast::IdentifierExpression with the given name
603 ast::IdentifierExpression* Expr(const std::string& name) {
604 return create<ast::IdentifierExpression>(Symbols().Register(name));
605 }
606
Ben Clayton46d78d72021-02-10 21:34:26 +0000607 /// @param symbol the identifier symbol
608 /// @return an ast::IdentifierExpression with the given symbol
609 ast::IdentifierExpression* Expr(Symbol symbol) {
610 return create<ast::IdentifierExpression>(symbol);
611 }
612
Ben Claytonb8ea5912021-04-19 16:52:42 +0000613 /// @param variable the AST variable
614 /// @return an ast::IdentifierExpression with the variable's symbol
615 ast::IdentifierExpression* Expr(ast::Variable* variable) {
616 return create<ast::IdentifierExpression>(variable->symbol());
617 }
618
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000619 /// @param source the source information
620 /// @param name the identifier name
621 /// @return an ast::IdentifierExpression with the given name
622 ast::IdentifierExpression* Expr(const Source& source,
623 const std::string& name) {
624 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
625 }
626
627 /// @param name the identifier name
628 /// @return an ast::IdentifierExpression with the given name
629 ast::IdentifierExpression* Expr(const char* name) {
630 return create<ast::IdentifierExpression>(Symbols().Register(name));
631 }
632
633 /// @param value the boolean value
634 /// @return a Scalar constructor for the given value
635 ast::ScalarConstructorExpression* Expr(bool value) {
636 return create<ast::ScalarConstructorExpression>(Literal(value));
637 }
638
639 /// @param value the float value
640 /// @return a Scalar constructor for the given value
641 ast::ScalarConstructorExpression* Expr(f32 value) {
642 return create<ast::ScalarConstructorExpression>(Literal(value));
643 }
644
645 /// @param value the integer value
646 /// @return a Scalar constructor for the given value
647 ast::ScalarConstructorExpression* Expr(i32 value) {
648 return create<ast::ScalarConstructorExpression>(Literal(value));
649 }
650
651 /// @param value the unsigned int value
652 /// @return a Scalar constructor for the given value
653 ast::ScalarConstructorExpression* Expr(u32 value) {
654 return create<ast::ScalarConstructorExpression>(Literal(value));
655 }
656
657 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
658 /// `list`.
659 /// @param list the list to append too
660 /// @param arg the arg to create
661 template <typename ARG>
662 void Append(ast::ExpressionList& list, ARG&& arg) {
663 list.emplace_back(Expr(std::forward<ARG>(arg)));
664 }
665
666 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
667 /// then appends them to `list`.
668 /// @param list the list to append too
669 /// @param arg0 the first argument
670 /// @param args the rest of the arguments
671 template <typename ARG0, typename... ARGS>
672 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
673 Append(list, std::forward<ARG0>(arg0));
674 Append(list, std::forward<ARGS>(args)...);
675 }
676
677 /// @return an empty list of expressions
678 ast::ExpressionList ExprList() { return {}; }
679
680 /// @param args the list of expressions
681 /// @return the list of expressions converted to `ast::Expression`s using
682 /// `Expr()`,
683 template <typename... ARGS>
684 ast::ExpressionList ExprList(ARGS&&... args) {
685 ast::ExpressionList list;
686 list.reserve(sizeof...(args));
687 Append(list, std::forward<ARGS>(args)...);
688 return list;
689 }
690
691 /// @param list the list of expressions
692 /// @return `list`
693 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
694
695 /// @param val the boolan value
696 /// @return a boolean literal with the given value
697 ast::BoolLiteral* Literal(bool val) {
698 return create<ast::BoolLiteral>(ty.bool_(), val);
699 }
700
701 /// @param val the float value
702 /// @return a float literal with the given value
703 ast::FloatLiteral* Literal(f32 val) {
704 return create<ast::FloatLiteral>(ty.f32(), val);
705 }
706
707 /// @param val the unsigned int value
708 /// @return a ast::UintLiteral with the given value
709 ast::UintLiteral* Literal(u32 val) {
710 return create<ast::UintLiteral>(ty.u32(), val);
711 }
712
713 /// @param val the integer value
714 /// @return the ast::SintLiteral with the given value
715 ast::SintLiteral* Literal(i32 val) {
716 return create<ast::SintLiteral>(ty.i32(), val);
717 }
718
719 /// @param args the arguments for the type constructor
720 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
721 /// of `args` converted to `ast::Expression`s using `Expr()`
722 template <typename T, typename... ARGS>
723 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
724 return create<ast::TypeConstructorExpression>(
725 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
726 }
727
728 /// @param type the type to construct
729 /// @param args the arguments for the constructor
730 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
731 /// values `args`.
732 template <typename... ARGS>
733 ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) {
734 return create<ast::TypeConstructorExpression>(
735 type, ExprList(std::forward<ARGS>(args)...));
736 }
737
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000738 /// Creates a constructor expression that constructs an object of
739 /// `type` filled with `elem_value`. For example,
740 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
741 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
742 /// @param type the type to construct
743 /// @param elem_value the initial or element value (for vec and mat) to
744 /// construct with
745 /// @return the constructor expression
746 ast::ConstructorExpression* ConstructValueFilledWith(type::Type* type,
747 int elem_value = 0);
748
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000749 /// @param args the arguments for the vector constructor
750 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
751 /// `T`, constructed with the values `args`.
752 template <typename T, typename... ARGS>
753 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
754 return create<ast::TypeConstructorExpression>(
755 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
756 }
757
758 /// @param args the arguments for the vector constructor
759 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
760 /// `T`, constructed with the values `args`.
761 template <typename T, typename... ARGS>
762 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
763 return create<ast::TypeConstructorExpression>(
764 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
765 }
766
767 /// @param args the arguments for the vector constructor
768 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
769 /// `T`, constructed with the values `args`.
770 template <typename T, typename... ARGS>
771 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
772 return create<ast::TypeConstructorExpression>(
773 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
774 }
775
776 /// @param args the arguments for the matrix constructor
777 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
778 /// `T`, constructed with the values `args`.
779 template <typename T, typename... ARGS>
780 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
781 return create<ast::TypeConstructorExpression>(
782 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
783 }
784
785 /// @param args the arguments for the matrix constructor
786 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
787 /// `T`, constructed with the values `args`.
788 template <typename T, typename... ARGS>
789 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
790 return create<ast::TypeConstructorExpression>(
791 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
792 }
793
794 /// @param args the arguments for the matrix constructor
795 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
796 /// `T`, constructed with the values `args`.
797 template <typename T, typename... ARGS>
798 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
799 return create<ast::TypeConstructorExpression>(
800 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
801 }
802
803 /// @param args the arguments for the matrix constructor
804 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
805 /// `T`, constructed with the values `args`.
806 template <typename T, typename... ARGS>
807 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
808 return create<ast::TypeConstructorExpression>(
809 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
810 }
811
812 /// @param args the arguments for the matrix constructor
813 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
814 /// `T`, constructed with the values `args`.
815 template <typename T, typename... ARGS>
816 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
817 return create<ast::TypeConstructorExpression>(
818 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
819 }
820
821 /// @param args the arguments for the matrix constructor
822 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
823 /// `T`, constructed with the values `args`.
824 template <typename T, typename... ARGS>
825 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
826 return create<ast::TypeConstructorExpression>(
827 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
828 }
829
830 /// @param args the arguments for the matrix constructor
831 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
832 /// `T`, constructed with the values `args`.
833 template <typename T, typename... ARGS>
834 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
835 return create<ast::TypeConstructorExpression>(
836 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
837 }
838
839 /// @param args the arguments for the matrix constructor
840 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
841 /// `T`, constructed with the values `args`.
842 template <typename T, typename... ARGS>
843 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
844 return create<ast::TypeConstructorExpression>(
845 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
846 }
847
848 /// @param args the arguments for the matrix constructor
849 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
850 /// `T`, constructed with the values `args`.
851 template <typename T, typename... ARGS>
852 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
853 return create<ast::TypeConstructorExpression>(
854 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
855 }
856
857 /// @param args the arguments for the array constructor
858 /// @return an `ast::TypeConstructorExpression` of an array with element type
859 /// `T`, constructed with the values `args`.
860 template <typename T, int N = 0, typename... ARGS>
861 ast::TypeConstructorExpression* array(ARGS&&... args) {
862 return create<ast::TypeConstructorExpression>(
863 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
864 }
865
866 /// @param subtype the array element type
867 /// @param n the array size. 0 represents a runtime-array.
868 /// @param args the arguments for the array constructor
869 /// @return an `ast::TypeConstructorExpression` of an array with element type
870 /// `subtype`, constructed with the values `args`.
871 template <typename... ARGS>
872 ast::TypeConstructorExpression* array(type::Type* subtype,
873 uint32_t n,
874 ARGS&&... args) {
875 return create<ast::TypeConstructorExpression>(
876 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
877 }
878
879 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000880 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000881 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000882 /// @param constructor constructor expression
883 /// @param decorations variable decorations
884 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000885 template <typename NAME>
886 ast::Variable* Var(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000887 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000888 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000889 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000890 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000891 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
892 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000893 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000894
895 /// @param source the variable source
896 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000897 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000898 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 /// @param constructor constructor expression
900 /// @param decorations variable decorations
901 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000902 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000903 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000904 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000905 type::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000906 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000907 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000908 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000909 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000910 type, false, constructor, decorations);
911 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000912
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000913 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000914 /// @param type the variable type
915 /// @param constructor optional constructor expression
916 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000917 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000918 template <typename NAME>
919 ast::Variable* Const(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000920 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000921 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000922 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000923 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000924 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000925 constructor, decorations);
926 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000927
928 /// @param source the variable source
929 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000930 /// @param type the variable type
931 /// @param constructor optional constructor expression
932 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000933 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000934 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000935 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000936 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000937 type::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000938 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000939 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000940 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000941 ast::StorageClass::kNone, type, true,
942 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000943 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000944
James Pricedaf1f1c2021-04-08 22:15:48 +0000945 /// @param name the parameter name
946 /// @param type the parameter type
947 /// @param decorations optional parameter decorations
948 /// @returns a constant `ast::Variable` with the given name and type
949 template <typename NAME>
950 ast::Variable* Param(NAME&& name,
951 type::Type* type,
952 ast::DecorationList decorations = {}) {
953 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
954 ast::StorageClass::kNone, type, true, nullptr,
955 decorations);
956 }
957
958 /// @param source the parameter source
959 /// @param name the parameter name
960 /// @param type the parameter type
961 /// @param decorations optional parameter decorations
962 /// @returns a constant `ast::Variable` with the given name and type
963 template <typename NAME>
964 ast::Variable* Param(const Source& source,
965 NAME&& name,
966 type::Type* type,
967 ast::DecorationList decorations = {}) {
968 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
969 ast::StorageClass::kNone, type, true, nullptr,
970 decorations);
971 }
972
Ben Clayton401b96b2021-02-03 17:19:59 +0000973 /// @param args the arguments to pass to Var()
974 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
975 /// of `args`, which is automatically registered as a global variable with the
976 /// ast::Module.
977 template <typename... ARGS>
978 ast::Variable* Global(ARGS&&... args) {
979 auto* var = Var(std::forward<ARGS>(args)...);
980 AST().AddGlobalVariable(var);
981 return var;
982 }
983
984 /// @param args the arguments to pass to Const()
985 /// @returns a const `ast::Variable` constructed by calling Var() with the
986 /// arguments of `args`, which is automatically registered as a global
987 /// variable with the ast::Module.
988 template <typename... ARGS>
989 ast::Variable* GlobalConst(ARGS&&... args) {
990 auto* var = Const(std::forward<ARGS>(args)...);
991 AST().AddGlobalVariable(var);
992 return var;
993 }
994
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000995 /// @param func the function name
996 /// @param args the function call arguments
997 /// @returns a `ast::CallExpression` to the function `func`, with the
998 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
999 template <typename NAME, typename... ARGS>
1000 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1001 return create<ast::CallExpression>(Expr(func),
1002 ExprList(std::forward<ARGS>(args)...));
1003 }
1004
1005 /// @param lhs the left hand argument to the addition operation
1006 /// @param rhs the right hand argument to the addition operation
1007 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1008 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001009 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001010 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1011 Expr(std::forward<LHS>(lhs)),
1012 Expr(std::forward<RHS>(rhs)));
1013 }
1014
1015 /// @param lhs the left hand argument to the subtraction operation
1016 /// @param rhs the right hand argument to the subtraction operation
1017 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1018 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001019 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001020 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1021 Expr(std::forward<LHS>(lhs)),
1022 Expr(std::forward<RHS>(rhs)));
1023 }
1024
1025 /// @param lhs the left hand argument to the multiplication operation
1026 /// @param rhs the right hand argument to the multiplication operation
1027 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1028 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001029 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001030 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1031 Expr(std::forward<LHS>(lhs)),
1032 Expr(std::forward<RHS>(rhs)));
1033 }
1034
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001035 /// @param source the source information
1036 /// @param lhs the left hand argument to the multiplication operation
1037 /// @param rhs the right hand argument to the multiplication operation
1038 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1039 template <typename LHS, typename RHS>
1040 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1041 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1042 Expr(std::forward<LHS>(lhs)),
1043 Expr(std::forward<RHS>(rhs)));
1044 }
1045
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001046 /// @param lhs the left hand argument to the division operation
1047 /// @param rhs the right hand argument to the division operation
1048 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1049 template <typename LHS, typename RHS>
1050 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1051 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1052 Expr(std::forward<LHS>(lhs)),
1053 Expr(std::forward<RHS>(rhs)));
1054 }
1055
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001056 /// @param arr the array argument for the array accessor expression
1057 /// @param idx the index argument for the array accessor expression
1058 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1059 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001060 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001061 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1062 Expr(std::forward<IDX>(idx)));
1063 }
1064
1065 /// @param obj the object for the member accessor expression
1066 /// @param idx the index argument for the array accessor expression
1067 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1068 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001069 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001070 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1071 Expr(std::forward<IDX>(idx)));
1072 }
1073
Ben Clayton42d1e092021-02-02 14:29:15 +00001074 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001075 /// @param val the offset value
1076 /// @returns the offset decoration pointer
1077 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1078 return create<ast::StructMemberOffsetDecoration>(source_, val);
1079 }
1080
Ben Claytond614dd52021-03-15 10:43:11 +00001081 /// Creates a ast::StructMemberSizeDecoration
1082 /// @param source the source information
1083 /// @param val the size value
1084 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001085 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1086 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001087 return create<ast::StructMemberSizeDecoration>(source, val);
1088 }
1089
1090 /// Creates a ast::StructMemberSizeDecoration
1091 /// @param val the size value
1092 /// @returns the size decoration pointer
1093 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1094 return create<ast::StructMemberSizeDecoration>(source_, val);
1095 }
1096
1097 /// Creates a ast::StructMemberAlignDecoration
1098 /// @param source the source information
1099 /// @param val the align value
1100 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001101 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1102 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001103 return create<ast::StructMemberAlignDecoration>(source, val);
1104 }
1105
1106 /// Creates a ast::StructMemberAlignDecoration
1107 /// @param val the align value
1108 /// @returns the align decoration pointer
1109 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1110 return create<ast::StructMemberAlignDecoration>(source_, val);
1111 }
1112
Ben Clayton42d1e092021-02-02 14:29:15 +00001113 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 /// @param source the source information
1115 /// @param name the function name
1116 /// @param params the function parameters
1117 /// @param type the function return type
1118 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001119 /// @param decorations the optional function decorations
1120 /// @param return_type_decorations the optional function return type
1121 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001122 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001123 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001124 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001125 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001126 ast::VariableList params,
1127 type::Type* type,
1128 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001129 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001130 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001131 auto* func =
1132 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1133 type, create<ast::BlockStatement>(body),
1134 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001135 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001136 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 }
1138
Ben Clayton42d1e092021-02-02 14:29:15 +00001139 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001140 /// @param name the function name
1141 /// @param params the function parameters
1142 /// @param type the function return type
1143 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001144 /// @param decorations the optional function decorations
1145 /// @param return_type_decorations the optional function return type
1146 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001147 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001148 template <typename NAME>
1149 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001150 ast::VariableList params,
1151 type::Type* type,
1152 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001153 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001154 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001155 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1156 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001157 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001158 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001159 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001160 }
1161
Ben Clayton49668bb2021-04-17 05:32:01 +00001162 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001163 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001164 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1165
1166 /// Creates an ast::ReturnStatement with the given return value
1167 /// @param val the return value
1168 /// @returns the return statement pointer
1169 template <typename EXPR>
1170 ast::ReturnStatement* Return(EXPR&& val) {
1171 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001172 }
1173
Ben Claytond614dd52021-03-15 10:43:11 +00001174 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1175 /// the AST().ConstructedTypes().
1176 /// @param source the source information
1177 /// @param name the struct name
1178 /// @param members the struct members
1179 /// @param decorations the optional struct decorations
1180 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001181 template <typename NAME>
Ben Claytond614dd52021-03-15 10:43:11 +00001182 type::Struct* Structure(const Source& source,
Ben Clayton1d618b12021-04-09 16:19:48 +00001183 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001184 ast::StructMemberList members,
1185 ast::DecorationList decorations = {}) {
1186 auto* impl =
1187 create<ast::Struct>(source, std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001188 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001189 AST().AddConstructedType(type);
1190 return type;
1191 }
1192
1193 /// Creates a ast::Struct and type::Struct, registering the type::Struct with
1194 /// the AST().ConstructedTypes().
1195 /// @param name the struct name
1196 /// @param members the struct members
1197 /// @param decorations the optional struct decorations
1198 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001199 template <typename NAME>
1200 type::Struct* Structure(NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001201 ast::StructMemberList members,
1202 ast::DecorationList decorations = {}) {
1203 auto* impl =
1204 create<ast::Struct>(std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001205 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001206 AST().AddConstructedType(type);
1207 return type;
1208 }
1209
Ben Clayton42d1e092021-02-02 14:29:15 +00001210 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001211 /// @param source the source information
1212 /// @param name the struct member name
1213 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001214 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001216 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001217 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001218 NAME&& name,
Ben Claytond614dd52021-03-15 10:43:11 +00001219 type::Type* type,
1220 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001221 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1222 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001223 }
1224
Ben Clayton42d1e092021-02-02 14:29:15 +00001225 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001226 /// @param name the struct member name
1227 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001228 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001229 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001230 template <typename NAME>
1231 ast::StructMember* Member(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001232 type::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001233 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001234 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1235 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001236 }
1237
Ben Claytonbab31972021-02-08 21:16:21 +00001238 /// Creates a ast::StructMember with the given byte offset
1239 /// @param offset the offset to use in the StructMemberOffsetDecoration
1240 /// @param name the struct member name
1241 /// @param type the struct member type
1242 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001243 template <typename NAME>
1244 ast::StructMember* Member(uint32_t offset, NAME&& name, type::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001245 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001246 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001247 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001248 create<ast::StructMemberOffsetDecoration>(offset),
1249 });
1250 }
1251
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001252 /// Creates a ast::BlockStatement with input statements
1253 /// @param statements statements of block
1254 /// @returns the block statement pointer
1255 template <typename... Statements>
1256 ast::BlockStatement* Block(Statements&&... statements) {
1257 return create<ast::BlockStatement>(
1258 ast::StatementList{std::forward<Statements>(statements)...});
1259 }
1260
1261 /// Creates a ast::ElseStatement with input condition and body
1262 /// @param condition the else condition expression
1263 /// @param body the else body
1264 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001265 template <typename CONDITION>
1266 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1267 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1268 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001269 }
1270
1271 /// Creates a ast::IfStatement with input condition, body, and optional
1272 /// variadic else statements
1273 /// @param condition the if statement condition expression
1274 /// @param body the if statement body
1275 /// @param elseStatements optional variadic else statements
1276 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001277 template <typename CONDITION, typename... ELSE_STATEMENTS>
1278 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001279 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001280 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001281 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001282 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001283 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001284 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001285 }
1286
1287 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001288 /// @param lhs the left hand side expression initializer
1289 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001290 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001291 template <typename LhsExpressionInit, typename RhsExpressionInit>
1292 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1293 RhsExpressionInit&& rhs) {
1294 return create<ast::AssignmentStatement>(
1295 Expr(std::forward<LhsExpressionInit>(lhs)),
1296 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001297 }
1298
1299 /// Creates a ast::LoopStatement with input body and optional continuing
1300 /// @param body the loop body
1301 /// @param continuing the optional continuing block
1302 /// @returns the loop statement pointer
1303 ast::LoopStatement* Loop(ast::BlockStatement* body,
1304 ast::BlockStatement* continuing = nullptr) {
1305 return create<ast::LoopStatement>(body, continuing);
1306 }
1307
1308 /// Creates a ast::VariableDeclStatement for the input variable
1309 /// @param var the variable to wrap in a decl statement
1310 /// @returns the variable decl statement pointer
1311 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1312 return create<ast::VariableDeclStatement>(var);
1313 }
1314
Antonio Maioranocea744d2021-03-25 12:55:27 +00001315 /// Creates a ast::SwitchStatement with input expression and cases
1316 /// @param condition the condition expression initializer
1317 /// @param cases case statements
1318 /// @returns the switch statement pointer
1319 template <typename ExpressionInit, typename... Cases>
1320 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1321 return create<ast::SwitchStatement>(
1322 Expr(std::forward<ExpressionInit>(condition)),
1323 ast::CaseStatementList{std::forward<Cases>(cases)...});
1324 }
1325
1326 /// Creates a ast::CaseStatement with input list of selectors, and body
1327 /// @param selectors list of selectors
1328 /// @param body the case body
1329 /// @returns the case statement pointer
1330 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1331 ast::BlockStatement* body = nullptr) {
1332 return create<ast::CaseStatement>(std::move(selectors),
1333 body ? body : Block());
1334 }
1335
1336 /// Convenient overload that takes a single selector
1337 /// @param selector a single case selector
1338 /// @param body the case body
1339 /// @returns the case statement pointer
1340 ast::CaseStatement* Case(ast::IntLiteral* selector,
1341 ast::BlockStatement* body = nullptr) {
1342 return Case(ast::CaseSelectorList{selector}, body);
1343 }
1344
1345 /// Convenience function that creates a 'default' ast::CaseStatement
1346 /// @param body the case body
1347 /// @returns the case statement pointer
1348 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1349 return Case(ast::CaseSelectorList{}, body);
1350 }
1351
James Price68f558f2021-04-06 15:51:47 +00001352 /// Creates an ast::BuiltinDecoration
1353 /// @param source the source information
1354 /// @param builtin the builtin value
1355 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001356 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001357 return create<ast::BuiltinDecoration>(source, builtin);
1358 }
1359
1360 /// Creates an ast::BuiltinDecoration
1361 /// @param builtin the builtin value
1362 /// @returns the builtin decoration pointer
1363 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1364 return create<ast::BuiltinDecoration>(source_, builtin);
1365 }
1366
1367 /// Creates an ast::LocationDecoration
1368 /// @param source the source information
1369 /// @param location the location value
1370 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001371 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001372 return create<ast::LocationDecoration>(source, location);
1373 }
1374
1375 /// Creates an ast::LocationDecoration
1376 /// @param location the location value
1377 /// @returns the location decoration pointer
1378 ast::LocationDecoration* Location(uint32_t location) {
1379 return create<ast::LocationDecoration>(source_, location);
1380 }
1381
1382 /// Creates an ast::StageDecoration
1383 /// @param source the source information
1384 /// @param stage the pipeline stage
1385 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001386 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001387 return create<ast::StageDecoration>(source, stage);
1388 }
1389
1390 /// Creates an ast::StageDecoration
1391 /// @param stage the pipeline stage
1392 /// @returns the stage decoration pointer
1393 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1394 return create<ast::StageDecoration>(source_, stage);
1395 }
1396
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001397 /// Sets the current builder source to `src`
1398 /// @param src the Source used for future create() calls
1399 void SetSource(const Source& src) {
1400 AssertNotMoved();
1401 source_ = src;
1402 }
1403
1404 /// Sets the current builder source to `loc`
1405 /// @param loc the Source used for future create() calls
1406 void SetSource(const Source::Location& loc) {
1407 AssertNotMoved();
1408 source_ = Source(loc);
1409 }
1410
Ben Clayton33352542021-01-29 16:43:41 +00001411 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001412 /// @note As the Resolver is run when the Program is built, this will only be
1413 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001414 /// @param expr the AST expression
1415 /// @return the resolved semantic type for the expression, or nullptr if the
1416 /// expression has no resolved type.
1417 type::Type* TypeOf(ast::Expression* expr) const;
1418
Ben Clayton169512e2021-04-17 05:52:11 +00001419 /// Wraps the ast::Literal in a statement. This is used by tests that
1420 /// construct a partial AST and require the Resolver to reach these
1421 /// nodes.
1422 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1423 /// @return the ast::Statement that wraps the ast::Statement
1424 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001425 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001426 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001427 /// nodes.
1428 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1429 /// @return the ast::Statement that wraps the ast::Expression
1430 ast::Statement* WrapInStatement(ast::Expression* expr);
1431 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001432 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001433 /// these nodes.
1434 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1435 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1436 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1437 /// Returns the statement argument. Used as a passthrough-overload by
1438 /// WrapInFunction().
1439 /// @param stmt the ast::Statement
1440 /// @return `stmt`
1441 ast::Statement* WrapInStatement(ast::Statement* stmt);
1442 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001443 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001444 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001445 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001446 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001447 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001448 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001449 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001450 }
1451 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001452 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001453 /// @returns the function
1454 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001455
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001456 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001457 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001458
1459 protected:
1460 /// Asserts that the builder has not been moved.
1461 void AssertNotMoved() const;
1462
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001463 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001464 ProgramID id_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001465 type::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001466 ASTNodeAllocator ast_nodes_;
1467 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001468 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001469 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001470 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001471 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001472
1473 /// The source to use when creating AST nodes without providing a Source as
1474 /// the first argument.
1475 Source source_;
1476
Ben Clayton5f0ea112021-03-09 10:54:37 +00001477 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001478 /// program when built.
1479 bool resolve_on_build_ = true;
1480
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001481 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1482 bool moved_ = false;
1483};
1484
1485//! @cond Doxygen_Suppress
1486// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1487template <>
1488struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
1489 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1490 return t->i32();
1491 }
1492};
1493template <>
1494struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
1495 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1496 return t->u32();
1497 }
1498};
1499template <>
1500struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
1501 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1502 return t->f32();
1503 }
1504};
1505template <>
1506struct ProgramBuilder::TypesBuilder::CToAST<bool> {
1507 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1508 return t->bool_();
1509 }
1510};
1511template <>
1512struct ProgramBuilder::TypesBuilder::CToAST<void> {
1513 static type::Type* get(const ProgramBuilder::TypesBuilder* t) {
1514 return t->void_();
1515 }
1516};
1517//! @endcond
1518
Ben Clayton917b14b2021-04-19 16:50:23 +00001519/// @param builder the ProgramBuilder
1520/// @returns the ProgramID of the ProgramBuilder
1521inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1522 return builder->ID();
1523}
1524
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001525} // namespace tint
1526
1527#endif // SRC_PROGRAM_BUILDER_H_