blob: 1dec6d9f1d6d353026e5a345826fa0ace7d21433 [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"
Ben Claytona922e652021-04-21 13:37:22 +000024#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/bool_literal.h"
26#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000027#include "src/ast/case_statement.h"
Ben Claytona922e652021-04-21 13:37:22 +000028#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000029#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000030#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000031#include "src/ast/if_statement.h"
32#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000033#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000034#include "src/ast/member_accessor_expression.h"
35#include "src/ast/module.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000036#include "src/ast/return_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000037#include "src/ast/scalar_constructor_expression.h"
38#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000039#include "src/ast/stage_decoration.h"
Ben Claytonbab31972021-02-08 21:16:21 +000040#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000041#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000042#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000043#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000044#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/type_constructor_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000046#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000047#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000048#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000049#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000050#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000052#include "src/program_id.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000053#include "src/sem/access_control_type.h"
54#include "src/sem/alias_type.h"
55#include "src/sem/array_type.h"
56#include "src/sem/bool_type.h"
57#include "src/sem/f32_type.h"
58#include "src/sem/i32_type.h"
59#include "src/sem/matrix_type.h"
60#include "src/sem/pointer_type.h"
61#include "src/sem/struct_type.h"
62#include "src/sem/u32_type.h"
63#include "src/sem/vector_type.h"
64#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000065#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000066
67namespace tint {
68
Ben Clayton401b96b2021-02-03 17:19:59 +000069// Forward declarations
70namespace ast {
71class VariableDeclStatement;
72} // namespace ast
73
Ben Claytona6b9a8e2021-01-26 16:57:10 +000074class CloneContext;
75
76/// ProgramBuilder is a mutable builder for a Program.
77/// To construct a Program, populate the builder and then `std::move` it to a
78/// Program.
79class ProgramBuilder {
80 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000081 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
82 using ASTNodeAllocator = BlockAllocator<ast::Node>;
83
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000084 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
85 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000086
87 /// `i32` is a type alias to `int`.
88 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
89 /// WGSL syntax.
90 /// Note: this is intentionally not aliased to uint32_t as we want integer
91 /// literals passed to the builder to match WGSL's integer literal types.
92 using i32 = decltype(1);
93 /// `u32` is a type alias to `unsigned int`.
94 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
95 /// WGSL syntax.
96 /// Note: this is intentionally not aliased to uint32_t as we want integer
97 /// literals passed to the builder to match WGSL's integer literal types.
98 using u32 = decltype(1u);
99 /// `f32` is a type alias to `float`
100 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
101 /// WGSL syntax.
102 using f32 = float;
103
104 /// Constructor
105 ProgramBuilder();
106
107 /// Move constructor
108 /// @param rhs the builder to move
109 ProgramBuilder(ProgramBuilder&& rhs);
110
111 /// Destructor
112 virtual ~ProgramBuilder();
113
114 /// Move assignment operator
115 /// @param rhs the builder to move
116 /// @return this builder
117 ProgramBuilder& operator=(ProgramBuilder&& rhs);
118
Ben Claytone43c8302021-01-29 11:59:32 +0000119 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
120 /// making a deep clone of the Program contents.
121 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
122 /// existing immutable program.
123 /// As the returned ProgramBuilder wraps `program`, `program` must not be
124 /// destructed or assigned while using the returned ProgramBuilder.
125 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
126 /// function. See crbug.com/tint/460.
127 /// @param program the immutable Program to wrap
128 /// @return the ProgramBuilder that wraps `program`
129 static ProgramBuilder Wrap(const Program* program);
130
Ben Claytone6995de2021-04-13 23:27:27 +0000131 /// @returns the unique identifier for this program
132 ProgramID ID() const { return id_; }
133
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000134 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000135 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000136 AssertNotMoved();
137 return types_;
138 }
139
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000140 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000141 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000142 AssertNotMoved();
143 return types_;
144 }
145
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000146 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000147 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000148 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000149 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000150 }
151
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000152 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000153 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000154 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000155 return ast_nodes_;
156 }
157
158 /// @returns a reference to the program's semantic nodes storage
159 SemNodeAllocator& SemNodes() {
160 AssertNotMoved();
161 return sem_nodes_;
162 }
163
164 /// @returns a reference to the program's semantic nodes storage
165 const SemNodeAllocator& SemNodes() const {
166 AssertNotMoved();
167 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000168 }
169
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000170 /// @returns a reference to the program's AST root Module
171 ast::Module& AST() {
172 AssertNotMoved();
173 return *ast_;
174 }
175
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000176 /// @returns a reference to the program's AST root Module
177 const ast::Module& AST() const {
178 AssertNotMoved();
179 return *ast_;
180 }
181
182 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000183 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000184 AssertNotMoved();
185 return sem_;
186 }
187
188 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000189 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000190 AssertNotMoved();
191 return sem_;
192 }
193
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000194 /// @returns a reference to the program's SymbolTable
195 SymbolTable& Symbols() {
196 AssertNotMoved();
197 return symbols_;
198 }
199
Ben Clayton708dc2d2021-01-29 11:22:40 +0000200 /// @returns a reference to the program's SymbolTable
201 const SymbolTable& Symbols() const {
202 AssertNotMoved();
203 return symbols_;
204 }
205
Ben Clayton844217f2021-01-27 18:49:05 +0000206 /// @returns a reference to the program's diagnostics
207 diag::List& Diagnostics() {
208 AssertNotMoved();
209 return diagnostics_;
210 }
211
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000212 /// @returns a reference to the program's diagnostics
213 const diag::List& Diagnostics() const {
214 AssertNotMoved();
215 return diagnostics_;
216 }
217
Ben Clayton5f0ea112021-03-09 10:54:37 +0000218 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000219 /// @param enable the new flag value (defaults to true)
220 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
221
Ben Clayton5f0ea112021-03-09 10:54:37 +0000222 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000223 /// built.
224 bool ResolveOnBuild() const { return resolve_on_build_; }
225
Ben Clayton844217f2021-01-27 18:49:05 +0000226 /// @returns true if the program has no error diagnostics and is not missing
227 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000228 bool IsValid() const;
229
Ben Clayton708dc2d2021-01-29 11:22:40 +0000230 /// Writes a representation of the node to the output stream
231 /// @note unlike str(), to_str() does not automatically demangle the string.
232 /// @param node the AST node
233 /// @param out the stream to write to
234 /// @param indent number of spaces to indent the node when writing
235 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
236 node->to_str(Sem(), out, indent);
237 }
238
239 /// Returns a demangled, string representation of `node`.
240 /// @param node the AST node
241 /// @returns a string representation of the node
242 std::string str(const ast::Node* node) const;
243
Ben Clayton7fdfff12021-01-29 15:17:30 +0000244 /// Creates a new ast::Node owned by the ProgramBuilder. When the
245 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000246 /// @param source the Source of the node
247 /// @param args the arguments to pass to the type constructor
248 /// @returns the node pointer
249 template <typename T, typename... ARGS>
250 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
251 ARGS&&... args) {
252 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000253 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000254 }
255
Ben Clayton7fdfff12021-01-29 15:17:30 +0000256 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
257 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000258 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000259 /// When the ProgramBuilder is destructed, the ast::Node will also be
260 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000261 /// @returns the node pointer
262 template <typename T>
263 traits::EnableIfIsType<T, ast::Node>* create() {
264 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000265 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000266 }
267
Ben Clayton7fdfff12021-01-29 15:17:30 +0000268 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
269 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000270 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000271 /// When the ProgramBuilder is destructed, the ast::Node will also be
272 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000273 /// @param arg0 the first arguments to pass to the type constructor
274 /// @param args the remaining arguments to pass to the type constructor
275 /// @returns the node pointer
276 template <typename T, typename ARG0, typename... ARGS>
277 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
278 traits::IsTypeOrDerived<T, ast::Node>::value &&
279 !traits::IsTypeOrDerived<ARG0, Source>::value,
280 T>*
281 create(ARG0&& arg0, ARGS&&... args) {
282 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000283 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000284 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000285 }
286
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000287 /// Creates a new sem::Node owned by the ProgramBuilder.
288 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000289 /// destructed.
290 /// @param args the arguments to pass to the type constructor
291 /// @returns the node pointer
292 template <typename T, typename... ARGS>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000293 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000294 AssertNotMoved();
295 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
296 }
297
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000298 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000299 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
300 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000301 /// Types are unique (de-aliased), and so calling create() for the same `T`
302 /// and arguments will return the same pointer.
303 /// @warning Use this method to acquire a type only if all of its type
304 /// information is provided in the constructor arguments `args`.<br>
305 /// If the type requires additional configuration after construction that
306 /// affect its fundamental type, build the type with `std::make_unique`, make
307 /// any necessary alterations and then call unique_type() instead.
308 /// @param args the arguments to pass to the type constructor
309 /// @returns the de-aliased type pointer
310 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000311 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
312 static_assert(std::is_base_of<sem::Type, T>::value,
313 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000314 AssertNotMoved();
315 return types_.Get<T>(std::forward<ARGS>(args)...);
316 }
317
318 /// Marks this builder as moved, preventing any further use of the builder.
319 void MarkAsMoved();
320
321 //////////////////////////////////////////////////////////////////////////////
322 // TypesBuilder
323 //////////////////////////////////////////////////////////////////////////////
324
325 /// TypesBuilder holds basic `tint` types and methods for constructing
326 /// complex types.
327 class TypesBuilder {
328 public:
329 /// Constructor
330 /// @param builder the program builder
331 explicit TypesBuilder(ProgramBuilder* builder);
332
333 /// @return the tint AST type for the C type `T`.
334 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000335 sem::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000336 return CToAST<T>::get(this);
337 }
338
339 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000340 typ::Bool bool_() const {
341 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
342 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000343
344 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000345 typ::F32 f32() const {
346 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
347 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000348
349 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000350 typ::I32 i32() const {
351 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
352 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000353
354 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000355 typ::U32 u32() const {
356 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
357 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000358
359 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000360 typ::Void void_() const {
361 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
362 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000363
Antonio Maiorano25436862021-04-13 13:32:33 +0000364 /// @param type vector subtype
365 /// @return the tint AST type for a 2-element vector of `type`.
Ben Claytone30ffeb2021-04-22 14:24:43 +0000366 typ::Vector vec2(typ::Type type) const {
367 return {builder->create<ast::Vector>(type, 2u),
368 builder->create<sem::Vector>(type, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000369 }
370
371 /// @param type vector subtype
372 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone30ffeb2021-04-22 14:24:43 +0000373 typ::Vector vec3(typ::Type type) const {
374 return {builder->create<ast::Vector>(type, 3u),
375 builder->create<sem::Vector>(type, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000376 }
377
378 /// @param type vector subtype
379 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone30ffeb2021-04-22 14:24:43 +0000380 typ::Vector vec4(typ::Type type) const {
381 return {builder->create<ast::Vector>(type, 4u),
382 builder->create<sem::Vector>(type, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000383 }
384
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000385 /// @return the tint AST type for a 2-element vector of the C type `T`.
386 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000387 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000388 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000389 }
390
391 /// @return the tint AST type for a 3-element vector of the C type `T`.
392 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000393 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000394 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000395 }
396
397 /// @return the tint AST type for a 4-element vector of the C type `T`.
398 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000399 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000400 return vec4(Of<T>());
401 }
402
403 /// @param type matrix subtype
404 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000405 typ::Matrix mat2x2(typ::Type type) const {
406 return {builder->create<ast::Matrix>(type, 2u, 2u),
407 builder->create<sem::Matrix>(type, 2u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000408 }
409
410 /// @param type matrix subtype
411 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000412 typ::Matrix mat2x3(typ::Type type) const {
413 return {builder->create<ast::Matrix>(type, 3u, 2u),
414 builder->create<sem::Matrix>(type, 3u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000415 }
416
417 /// @param type matrix subtype
418 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000419 typ::Matrix mat2x4(typ::Type type) const {
420 return {builder->create<ast::Matrix>(type, 4u, 2u),
421 builder->create<sem::Matrix>(type, 4u, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000422 }
423
424 /// @param type matrix subtype
425 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000426 typ::Matrix mat3x2(typ::Type type) const {
427 return {builder->create<ast::Matrix>(type, 2u, 3u),
428 builder->create<sem::Matrix>(type, 2u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000429 }
430
431 /// @param type matrix subtype
432 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000433 typ::Matrix mat3x3(typ::Type type) const {
434 return {builder->create<ast::Matrix>(type, 3u, 3u),
435 builder->create<sem::Matrix>(type, 3u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000436 }
437
438 /// @param type matrix subtype
439 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000440 typ::Matrix mat3x4(typ::Type type) const {
441 return {builder->create<ast::Matrix>(type, 4u, 3u),
442 builder->create<sem::Matrix>(type, 4u, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000443 }
444
445 /// @param type matrix subtype
446 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000447 typ::Matrix mat4x2(typ::Type type) const {
448 return {builder->create<ast::Matrix>(type, 2u, 4u),
449 builder->create<sem::Matrix>(type, 2u, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000450 }
451
452 /// @param type matrix subtype
453 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000454 typ::Matrix mat4x3(typ::Type type) const {
455 return {builder->create<ast::Matrix>(type, 3u, 4u),
456 builder->create<sem::Matrix>(type, 3u, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000457 }
458
459 /// @param type matrix subtype
460 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000461 typ::Matrix mat4x4(typ::Type type) const {
462 return {builder->create<ast::Matrix>(type, 4u, 4u),
463 builder->create<sem::Matrix>(type, 4u, 4u)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000464 }
465
466 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
467 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000468 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000469 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000470 }
471
472 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
473 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000474 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000475 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000476 }
477
478 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
479 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000480 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000481 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000482 }
483
484 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
485 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000486 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000487 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000488 }
489
490 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
491 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000492 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000493 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000494 }
495
496 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
497 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000498 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000499 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000500 }
501
502 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
503 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000504 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000505 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000506 }
507
508 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
509 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000510 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000511 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000512 }
513
514 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
515 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000516 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000517 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000518 }
519
520 /// @param subtype the array element type
521 /// @param n the array size. 0 represents a runtime-array.
522 /// @return the tint AST type for a array of size `n` of type `T`
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000523 sem::ArrayType* array(sem::Type* subtype, uint32_t n = 0) const {
524 return builder->create<sem::ArrayType>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000525 }
526
Ben Claytonbab31972021-02-08 21:16:21 +0000527 /// @param subtype the array element type
528 /// @param n the array size. 0 represents a runtime-array.
529 /// @param stride the array stride.
530 /// @return the tint AST type for a array of size `n` of type `T`
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000531 sem::ArrayType* array(sem::Type* subtype,
532 uint32_t n,
533 uint32_t stride) const {
534 return builder->create<sem::ArrayType>(
Ben Claytonbab31972021-02-08 21:16:21 +0000535 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000536 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000537 builder->create<ast::StrideDecoration>(stride),
538 });
539 }
540
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000541 /// @return the tint AST type for an array of size `N` of type `T`
542 template <typename T, int N = 0>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000543 sem::ArrayType* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000544 return array(Of<T>(), N);
545 }
546
Ben Claytonbab31972021-02-08 21:16:21 +0000547 /// @param stride the array stride
548 /// @return the tint AST type for an array of size `N` of type `T`
549 template <typename T, int N = 0>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000550 sem::ArrayType* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000551 return array(Of<T>(), N, stride);
552 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000553
Ben Clayton42d1e092021-02-02 14:29:15 +0000554 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 /// @param name the alias name
556 /// @param type the alias type
557 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000558 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000559 sem::Alias* alias(NAME&& name, sem::Type* type) const {
560 return builder->create<sem::Alias>(builder->Sym(std::forward<NAME>(name)),
561 type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000562 }
563
Ben Clayton4db10dd2021-04-16 08:47:14 +0000564 /// Creates an access control qualifier type
565 /// @param access the access control
566 /// @param type the inner type
567 /// @returns the access control qualifier type
Ben Clayton8a8d26b2021-04-20 15:04:21 +0000568 sem::AccessControl* access(ast::AccessControl::Access access,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000569 sem::Type* type) const {
570 return builder->create<sem::AccessControl>(access, type);
Ben Clayton4db10dd2021-04-16 08:47:14 +0000571 }
572
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000573 /// @return the tint AST pointer to `type` with the given ast::StorageClass
574 /// @param type the type of the pointer
575 /// @param storage_class the storage class of the pointer
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000576 sem::Pointer* pointer(sem::Type* type,
577 ast::StorageClass storage_class) const {
578 return builder->create<sem::Pointer>(type, storage_class);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000579 }
580
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000581 /// @return the tint AST pointer to type `T` with the given
582 /// ast::StorageClass.
583 /// @param storage_class the storage class of the pointer
584 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000585 sem::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000586 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000587 }
588
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000589 /// @param impl the struct implementation
590 /// @returns a struct pointer
Ben Clayton913a2f42021-04-20 15:21:21 +0000591 sem::StructType* struct_(ast::Struct* impl) const {
592 return builder->create<sem::StructType>(impl);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000593 }
594
595 private:
596 /// CToAST<T> is specialized for various `T` types and each specialization
597 /// contains a single static `get()` method for obtaining the corresponding
598 /// AST type for the C type `T`.
599 /// `get()` has the signature:
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000600 /// `static sem::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000601 template <typename T>
602 struct CToAST {};
603
Ben Claytonc7ca7662021-02-17 16:23:52 +0000604 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000605 };
606
607 //////////////////////////////////////////////////////////////////////////////
608 // AST helper methods
609 //////////////////////////////////////////////////////////////////////////////
610
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000611 /// @param name the symbol string
612 /// @return a Symbol with the given name
613 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
614
615 /// @param sym the symbol
616 /// @return `sym`
617 Symbol Sym(Symbol sym) { return sym; }
618
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000619 /// @param expr the expression
620 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000621 template <typename T>
622 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
623 return expr;
624 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000625
Ben Claytonb8ea5912021-04-19 16:52:42 +0000626 /// Passthrough for nullptr
627 /// @return nullptr
628 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
629
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000630 /// @param name the identifier name
631 /// @return an ast::IdentifierExpression with the given name
632 ast::IdentifierExpression* Expr(const std::string& name) {
633 return create<ast::IdentifierExpression>(Symbols().Register(name));
634 }
635
Ben Clayton46d78d72021-02-10 21:34:26 +0000636 /// @param symbol the identifier symbol
637 /// @return an ast::IdentifierExpression with the given symbol
638 ast::IdentifierExpression* Expr(Symbol symbol) {
639 return create<ast::IdentifierExpression>(symbol);
640 }
641
Ben Claytonb8ea5912021-04-19 16:52:42 +0000642 /// @param variable the AST variable
643 /// @return an ast::IdentifierExpression with the variable's symbol
644 ast::IdentifierExpression* Expr(ast::Variable* variable) {
645 return create<ast::IdentifierExpression>(variable->symbol());
646 }
647
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000648 /// @param source the source information
649 /// @param name the identifier name
650 /// @return an ast::IdentifierExpression with the given name
651 ast::IdentifierExpression* Expr(const Source& source,
652 const std::string& name) {
653 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
654 }
655
656 /// @param name the identifier name
657 /// @return an ast::IdentifierExpression with the given name
658 ast::IdentifierExpression* Expr(const char* name) {
659 return create<ast::IdentifierExpression>(Symbols().Register(name));
660 }
661
662 /// @param value the boolean value
663 /// @return a Scalar constructor for the given value
664 ast::ScalarConstructorExpression* Expr(bool value) {
665 return create<ast::ScalarConstructorExpression>(Literal(value));
666 }
667
668 /// @param value the float value
669 /// @return a Scalar constructor for the given value
670 ast::ScalarConstructorExpression* Expr(f32 value) {
671 return create<ast::ScalarConstructorExpression>(Literal(value));
672 }
673
674 /// @param value the integer value
675 /// @return a Scalar constructor for the given value
676 ast::ScalarConstructorExpression* Expr(i32 value) {
677 return create<ast::ScalarConstructorExpression>(Literal(value));
678 }
679
680 /// @param value the unsigned int value
681 /// @return a Scalar constructor for the given value
682 ast::ScalarConstructorExpression* Expr(u32 value) {
683 return create<ast::ScalarConstructorExpression>(Literal(value));
684 }
685
686 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
687 /// `list`.
688 /// @param list the list to append too
689 /// @param arg the arg to create
690 template <typename ARG>
691 void Append(ast::ExpressionList& list, ARG&& arg) {
692 list.emplace_back(Expr(std::forward<ARG>(arg)));
693 }
694
695 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
696 /// then appends them to `list`.
697 /// @param list the list to append too
698 /// @param arg0 the first argument
699 /// @param args the rest of the arguments
700 template <typename ARG0, typename... ARGS>
701 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
702 Append(list, std::forward<ARG0>(arg0));
703 Append(list, std::forward<ARGS>(args)...);
704 }
705
706 /// @return an empty list of expressions
707 ast::ExpressionList ExprList() { return {}; }
708
709 /// @param args the list of expressions
710 /// @return the list of expressions converted to `ast::Expression`s using
711 /// `Expr()`,
712 template <typename... ARGS>
713 ast::ExpressionList ExprList(ARGS&&... args) {
714 ast::ExpressionList list;
715 list.reserve(sizeof...(args));
716 Append(list, std::forward<ARGS>(args)...);
717 return list;
718 }
719
720 /// @param list the list of expressions
721 /// @return `list`
722 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
723
724 /// @param val the boolan value
725 /// @return a boolean literal with the given value
726 ast::BoolLiteral* Literal(bool val) {
727 return create<ast::BoolLiteral>(ty.bool_(), val);
728 }
729
730 /// @param val the float value
731 /// @return a float literal with the given value
732 ast::FloatLiteral* Literal(f32 val) {
733 return create<ast::FloatLiteral>(ty.f32(), val);
734 }
735
736 /// @param val the unsigned int value
737 /// @return a ast::UintLiteral with the given value
738 ast::UintLiteral* Literal(u32 val) {
739 return create<ast::UintLiteral>(ty.u32(), val);
740 }
741
742 /// @param val the integer value
743 /// @return the ast::SintLiteral with the given value
744 ast::SintLiteral* Literal(i32 val) {
745 return create<ast::SintLiteral>(ty.i32(), val);
746 }
747
748 /// @param args the arguments for the type constructor
749 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
750 /// of `args` converted to `ast::Expression`s using `Expr()`
751 template <typename T, typename... ARGS>
752 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
753 return create<ast::TypeConstructorExpression>(
754 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
755 }
756
757 /// @param type the type to construct
758 /// @param args the arguments for the constructor
759 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
760 /// values `args`.
761 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000762 ast::TypeConstructorExpression* Construct(sem::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000763 return create<ast::TypeConstructorExpression>(
764 type, ExprList(std::forward<ARGS>(args)...));
765 }
766
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000767 /// Creates a constructor expression that constructs an object of
768 /// `type` filled with `elem_value`. For example,
769 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
770 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
771 /// @param type the type to construct
772 /// @param elem_value the initial or element value (for vec and mat) to
773 /// construct with
774 /// @return the constructor expression
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000775 ast::ConstructorExpression* ConstructValueFilledWith(sem::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000776 int elem_value = 0);
777
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000778 /// @param args the arguments for the vector constructor
779 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
780 /// `T`, constructed with the values `args`.
781 template <typename T, typename... ARGS>
782 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
783 return create<ast::TypeConstructorExpression>(
784 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
785 }
786
787 /// @param args the arguments for the vector constructor
788 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
789 /// `T`, constructed with the values `args`.
790 template <typename T, typename... ARGS>
791 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
792 return create<ast::TypeConstructorExpression>(
793 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
794 }
795
796 /// @param args the arguments for the vector constructor
797 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
798 /// `T`, constructed with the values `args`.
799 template <typename T, typename... ARGS>
800 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
801 return create<ast::TypeConstructorExpression>(
802 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
803 }
804
805 /// @param args the arguments for the matrix constructor
806 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
807 /// `T`, constructed with the values `args`.
808 template <typename T, typename... ARGS>
809 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
810 return create<ast::TypeConstructorExpression>(
811 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
812 }
813
814 /// @param args the arguments for the matrix constructor
815 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
816 /// `T`, constructed with the values `args`.
817 template <typename T, typename... ARGS>
818 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
819 return create<ast::TypeConstructorExpression>(
820 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
821 }
822
823 /// @param args the arguments for the matrix constructor
824 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
825 /// `T`, constructed with the values `args`.
826 template <typename T, typename... ARGS>
827 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
828 return create<ast::TypeConstructorExpression>(
829 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
830 }
831
832 /// @param args the arguments for the matrix constructor
833 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
834 /// `T`, constructed with the values `args`.
835 template <typename T, typename... ARGS>
836 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
837 return create<ast::TypeConstructorExpression>(
838 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
839 }
840
841 /// @param args the arguments for the matrix constructor
842 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
843 /// `T`, constructed with the values `args`.
844 template <typename T, typename... ARGS>
845 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
846 return create<ast::TypeConstructorExpression>(
847 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
848 }
849
850 /// @param args the arguments for the matrix constructor
851 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
852 /// `T`, constructed with the values `args`.
853 template <typename T, typename... ARGS>
854 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
855 return create<ast::TypeConstructorExpression>(
856 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
857 }
858
859 /// @param args the arguments for the matrix constructor
860 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
861 /// `T`, constructed with the values `args`.
862 template <typename T, typename... ARGS>
863 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
864 return create<ast::TypeConstructorExpression>(
865 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
866 }
867
868 /// @param args the arguments for the matrix constructor
869 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
870 /// `T`, constructed with the values `args`.
871 template <typename T, typename... ARGS>
872 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
873 return create<ast::TypeConstructorExpression>(
874 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
875 }
876
877 /// @param args the arguments for the matrix constructor
878 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
879 /// `T`, constructed with the values `args`.
880 template <typename T, typename... ARGS>
881 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
882 return create<ast::TypeConstructorExpression>(
883 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
884 }
885
886 /// @param args the arguments for the array constructor
887 /// @return an `ast::TypeConstructorExpression` of an array with element type
888 /// `T`, constructed with the values `args`.
889 template <typename T, int N = 0, typename... ARGS>
890 ast::TypeConstructorExpression* array(ARGS&&... args) {
891 return create<ast::TypeConstructorExpression>(
892 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
893 }
894
895 /// @param subtype the array element type
896 /// @param n the array size. 0 represents a runtime-array.
897 /// @param args the arguments for the array constructor
898 /// @return an `ast::TypeConstructorExpression` of an array with element type
899 /// `subtype`, constructed with the values `args`.
900 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000901 ast::TypeConstructorExpression* array(sem::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000902 uint32_t n,
903 ARGS&&... args) {
904 return create<ast::TypeConstructorExpression>(
905 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
906 }
907
908 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000909 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000910 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000911 /// @param constructor constructor expression
912 /// @param decorations variable decorations
913 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000914 template <typename NAME>
915 ast::Variable* Var(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000916 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000917 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000918 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000919 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000920 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
921 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000922 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000923
924 /// @param source the variable source
925 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000926 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000927 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000928 /// @param constructor constructor expression
929 /// @param decorations variable decorations
930 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000931 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000933 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000934 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000935 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000936 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000937 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000938 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000939 type, false, constructor, decorations);
940 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000941
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000942 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000943 /// @param type the variable type
944 /// @param constructor optional constructor expression
945 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000946 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000947 template <typename NAME>
948 ast::Variable* Const(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000949 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000950 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000951 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000952 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000953 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000954 constructor, decorations);
955 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000956
957 /// @param source the variable source
958 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000959 /// @param type the variable type
960 /// @param constructor optional constructor expression
961 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000962 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000963 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000964 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000965 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000966 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000967 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000968 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000969 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000970 ast::StorageClass::kNone, type, true,
971 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000972 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000973
James Pricedaf1f1c2021-04-08 22:15:48 +0000974 /// @param name the parameter name
975 /// @param type the parameter type
976 /// @param decorations optional parameter decorations
977 /// @returns a constant `ast::Variable` with the given name and type
978 template <typename NAME>
979 ast::Variable* Param(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000980 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000981 ast::DecorationList decorations = {}) {
982 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
983 ast::StorageClass::kNone, type, true, nullptr,
984 decorations);
985 }
986
987 /// @param source the parameter source
988 /// @param name the parameter name
989 /// @param type the parameter type
990 /// @param decorations optional parameter decorations
991 /// @returns a constant `ast::Variable` with the given name and type
992 template <typename NAME>
993 ast::Variable* Param(const Source& source,
994 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000995 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000996 ast::DecorationList decorations = {}) {
997 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
998 ast::StorageClass::kNone, type, true, nullptr,
999 decorations);
1000 }
1001
Ben Clayton42708342021-04-21 17:55:12 +00001002 /// @param name the variable name
1003 /// @param type the variable type
1004 /// @param storage the variable storage class
1005 /// @param constructor constructor expression
1006 /// @param decorations variable decorations
1007 /// @returns a new `ast::Variable`, which is automatically registered as a
1008 /// global variable with the ast::Module.
1009 template <typename NAME>
1010 ast::Variable* Global(NAME&& name,
1011 sem::Type* type,
1012 ast::StorageClass storage,
1013 ast::Expression* constructor = nullptr,
1014 ast::DecorationList decorations = {}) {
1015 auto* var =
1016 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1017 AST().AddGlobalVariable(var);
1018 return var;
1019 }
1020
1021 /// @param source the variable source
1022 /// @param name the variable name
1023 /// @param type the variable type
1024 /// @param storage the variable storage class
1025 /// @param constructor constructor expression
1026 /// @param decorations variable decorations
1027 /// @returns a new `ast::Variable`, which is automatically registered as a
1028 /// global variable with the ast::Module.
1029 template <typename NAME>
1030 ast::Variable* Global(const Source& source,
1031 NAME&& name,
1032 sem::Type* type,
1033 ast::StorageClass storage,
1034 ast::Expression* constructor = nullptr,
1035 ast::DecorationList decorations = {}) {
1036 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1037 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001038 AST().AddGlobalVariable(var);
1039 return var;
1040 }
1041
1042 /// @param args the arguments to pass to Const()
1043 /// @returns a const `ast::Variable` constructed by calling Var() with the
1044 /// arguments of `args`, which is automatically registered as a global
1045 /// variable with the ast::Module.
1046 template <typename... ARGS>
1047 ast::Variable* GlobalConst(ARGS&&... args) {
1048 auto* var = Const(std::forward<ARGS>(args)...);
1049 AST().AddGlobalVariable(var);
1050 return var;
1051 }
1052
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001053 /// @param func the function name
1054 /// @param args the function call arguments
1055 /// @returns a `ast::CallExpression` to the function `func`, with the
1056 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1057 template <typename NAME, typename... ARGS>
1058 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1059 return create<ast::CallExpression>(Expr(func),
1060 ExprList(std::forward<ARGS>(args)...));
1061 }
1062
1063 /// @param lhs the left hand argument to the addition operation
1064 /// @param rhs the right hand argument to the addition operation
1065 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1066 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001067 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001068 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1069 Expr(std::forward<LHS>(lhs)),
1070 Expr(std::forward<RHS>(rhs)));
1071 }
1072
1073 /// @param lhs the left hand argument to the subtraction operation
1074 /// @param rhs the right hand argument to the subtraction operation
1075 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1076 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001077 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1079 Expr(std::forward<LHS>(lhs)),
1080 Expr(std::forward<RHS>(rhs)));
1081 }
1082
1083 /// @param lhs the left hand argument to the multiplication operation
1084 /// @param rhs the right hand argument to the multiplication operation
1085 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1086 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001087 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001088 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1089 Expr(std::forward<LHS>(lhs)),
1090 Expr(std::forward<RHS>(rhs)));
1091 }
1092
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001093 /// @param source the source information
1094 /// @param lhs the left hand argument to the multiplication operation
1095 /// @param rhs the right hand argument to the multiplication operation
1096 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1097 template <typename LHS, typename RHS>
1098 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1099 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1100 Expr(std::forward<LHS>(lhs)),
1101 Expr(std::forward<RHS>(rhs)));
1102 }
1103
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001104 /// @param lhs the left hand argument to the division operation
1105 /// @param rhs the right hand argument to the division operation
1106 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1107 template <typename LHS, typename RHS>
1108 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1109 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1110 Expr(std::forward<LHS>(lhs)),
1111 Expr(std::forward<RHS>(rhs)));
1112 }
1113
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 /// @param arr the array argument for the array accessor expression
1115 /// @param idx the index argument for the array accessor expression
1116 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1117 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001118 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1120 Expr(std::forward<IDX>(idx)));
1121 }
1122
1123 /// @param obj the object for the member accessor expression
1124 /// @param idx the index argument for the array accessor expression
1125 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1126 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001127 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001128 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1129 Expr(std::forward<IDX>(idx)));
1130 }
1131
Ben Clayton42d1e092021-02-02 14:29:15 +00001132 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001133 /// @param val the offset value
1134 /// @returns the offset decoration pointer
1135 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1136 return create<ast::StructMemberOffsetDecoration>(source_, val);
1137 }
1138
Ben Claytond614dd52021-03-15 10:43:11 +00001139 /// Creates a ast::StructMemberSizeDecoration
1140 /// @param source the source information
1141 /// @param val the size value
1142 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001143 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1144 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001145 return create<ast::StructMemberSizeDecoration>(source, val);
1146 }
1147
1148 /// Creates a ast::StructMemberSizeDecoration
1149 /// @param val the size value
1150 /// @returns the size decoration pointer
1151 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1152 return create<ast::StructMemberSizeDecoration>(source_, val);
1153 }
1154
1155 /// Creates a ast::StructMemberAlignDecoration
1156 /// @param source the source information
1157 /// @param val the align value
1158 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001159 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1160 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001161 return create<ast::StructMemberAlignDecoration>(source, val);
1162 }
1163
1164 /// Creates a ast::StructMemberAlignDecoration
1165 /// @param val the align value
1166 /// @returns the align decoration pointer
1167 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1168 return create<ast::StructMemberAlignDecoration>(source_, val);
1169 }
1170
Ben Clayton42d1e092021-02-02 14:29:15 +00001171 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001172 /// @param source the source information
1173 /// @param name the function name
1174 /// @param params the function parameters
1175 /// @param type the function return type
1176 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001177 /// @param decorations the optional function decorations
1178 /// @param return_type_decorations the optional function return type
1179 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001180 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001181 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001182 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001183 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001184 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001185 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001186 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001187 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001188 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001189 auto* func =
1190 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1191 type, create<ast::BlockStatement>(body),
1192 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001193 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001194 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001195 }
1196
Ben Clayton42d1e092021-02-02 14:29:15 +00001197 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001198 /// @param name the function name
1199 /// @param params the function parameters
1200 /// @param type the function return type
1201 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001202 /// @param decorations the optional function decorations
1203 /// @param return_type_decorations the optional function return type
1204 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001205 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001206 template <typename NAME>
1207 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001208 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001209 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001210 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001211 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001212 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001213 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1214 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001215 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001216 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001217 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 }
1219
Ben Clayton49668bb2021-04-17 05:32:01 +00001220 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001221 /// @param source the source information
1222 /// @returns the return statement pointer
1223 ast::ReturnStatement* Return(const Source& source) {
1224 return create<ast::ReturnStatement>(source);
1225 }
1226
1227 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001228 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001229 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1230
1231 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001232 /// @param source the source information
1233 /// @param val the return value
1234 /// @returns the return statement pointer
1235 template <typename EXPR>
1236 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1237 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1238 }
1239
1240 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001241 /// @param val the return value
1242 /// @returns the return statement pointer
1243 template <typename EXPR>
1244 ast::ReturnStatement* Return(EXPR&& val) {
1245 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001246 }
1247
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001248 /// Creates a ast::Struct and sem::StructType, registering the
1249 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001250 /// @param source the source information
1251 /// @param name the struct name
1252 /// @param members the struct members
1253 /// @param decorations the optional struct decorations
1254 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001255 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001256 sem::StructType* Structure(const Source& source,
1257 NAME&& name,
1258 ast::StructMemberList members,
1259 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001260 auto sym = Sym(std::forward<NAME>(name));
1261 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1262 std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001263 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001264 AST().AddConstructedType(type);
1265 return type;
1266 }
1267
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001268 /// Creates a ast::Struct and sem::StructType, registering the
1269 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001270 /// @param name the struct name
1271 /// @param members the struct members
1272 /// @param decorations the optional struct decorations
1273 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001274 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001275 sem::StructType* Structure(NAME&& name,
1276 ast::StructMemberList members,
1277 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001278 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001279 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001280 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001281 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001282 AST().AddConstructedType(type);
1283 return type;
1284 }
1285
Ben Clayton42d1e092021-02-02 14:29:15 +00001286 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001287 /// @param source the source information
1288 /// @param name the struct member name
1289 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001290 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001292 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001293 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001294 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001295 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001296 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001297 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1298 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001299 }
1300
Ben Clayton42d1e092021-02-02 14:29:15 +00001301 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001302 /// @param name the struct member name
1303 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001304 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001305 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001306 template <typename NAME>
1307 ast::StructMember* Member(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001308 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001309 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001310 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1311 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001312 }
1313
Ben Claytonbab31972021-02-08 21:16:21 +00001314 /// Creates a ast::StructMember with the given byte offset
1315 /// @param offset the offset to use in the StructMemberOffsetDecoration
1316 /// @param name the struct member name
1317 /// @param type the struct member type
1318 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001319 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001320 ast::StructMember* Member(uint32_t offset, NAME&& name, sem::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001321 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001322 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001323 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001324 create<ast::StructMemberOffsetDecoration>(offset),
1325 });
1326 }
1327
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001328 /// Creates a ast::BlockStatement with input statements
1329 /// @param statements statements of block
1330 /// @returns the block statement pointer
1331 template <typename... Statements>
1332 ast::BlockStatement* Block(Statements&&... statements) {
1333 return create<ast::BlockStatement>(
1334 ast::StatementList{std::forward<Statements>(statements)...});
1335 }
1336
1337 /// Creates a ast::ElseStatement with input condition and body
1338 /// @param condition the else condition expression
1339 /// @param body the else body
1340 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001341 template <typename CONDITION>
1342 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1343 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1344 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001345 }
1346
1347 /// Creates a ast::IfStatement with input condition, body, and optional
1348 /// variadic else statements
1349 /// @param condition the if statement condition expression
1350 /// @param body the if statement body
1351 /// @param elseStatements optional variadic else statements
1352 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001353 template <typename CONDITION, typename... ELSE_STATEMENTS>
1354 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001355 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001356 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001357 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001358 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001359 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001360 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001361 }
1362
1363 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001364 /// @param source the source information
1365 /// @param lhs the left hand side expression initializer
1366 /// @param rhs the right hand side expression initializer
1367 /// @returns the assignment statement pointer
1368 template <typename LhsExpressionInit, typename RhsExpressionInit>
1369 ast::AssignmentStatement* Assign(const Source& source,
1370 LhsExpressionInit&& lhs,
1371 RhsExpressionInit&& rhs) {
1372 return create<ast::AssignmentStatement>(
1373 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1374 Expr(std::forward<RhsExpressionInit>(rhs)));
1375 }
1376
1377 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001378 /// @param lhs the left hand side expression initializer
1379 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001380 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001381 template <typename LhsExpressionInit, typename RhsExpressionInit>
1382 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1383 RhsExpressionInit&& rhs) {
1384 return create<ast::AssignmentStatement>(
1385 Expr(std::forward<LhsExpressionInit>(lhs)),
1386 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001387 }
1388
1389 /// Creates a ast::LoopStatement with input body and optional continuing
1390 /// @param body the loop body
1391 /// @param continuing the optional continuing block
1392 /// @returns the loop statement pointer
1393 ast::LoopStatement* Loop(ast::BlockStatement* body,
1394 ast::BlockStatement* continuing = nullptr) {
1395 return create<ast::LoopStatement>(body, continuing);
1396 }
1397
1398 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001399 /// @param source the source information
1400 /// @param var the variable to wrap in a decl statement
1401 /// @returns the variable decl statement pointer
1402 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1403 return create<ast::VariableDeclStatement>(source, var);
1404 }
1405
1406 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001407 /// @param var the variable to wrap in a decl statement
1408 /// @returns the variable decl statement pointer
1409 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1410 return create<ast::VariableDeclStatement>(var);
1411 }
1412
Antonio Maioranocea744d2021-03-25 12:55:27 +00001413 /// Creates a ast::SwitchStatement with input expression and cases
1414 /// @param condition the condition expression initializer
1415 /// @param cases case statements
1416 /// @returns the switch statement pointer
1417 template <typename ExpressionInit, typename... Cases>
1418 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1419 return create<ast::SwitchStatement>(
1420 Expr(std::forward<ExpressionInit>(condition)),
1421 ast::CaseStatementList{std::forward<Cases>(cases)...});
1422 }
1423
1424 /// Creates a ast::CaseStatement with input list of selectors, and body
1425 /// @param selectors list of selectors
1426 /// @param body the case body
1427 /// @returns the case statement pointer
1428 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1429 ast::BlockStatement* body = nullptr) {
1430 return create<ast::CaseStatement>(std::move(selectors),
1431 body ? body : Block());
1432 }
1433
1434 /// Convenient overload that takes a single selector
1435 /// @param selector a single case selector
1436 /// @param body the case body
1437 /// @returns the case statement pointer
1438 ast::CaseStatement* Case(ast::IntLiteral* selector,
1439 ast::BlockStatement* body = nullptr) {
1440 return Case(ast::CaseSelectorList{selector}, body);
1441 }
1442
1443 /// Convenience function that creates a 'default' ast::CaseStatement
1444 /// @param body the case body
1445 /// @returns the case statement pointer
1446 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1447 return Case(ast::CaseSelectorList{}, body);
1448 }
1449
James Price68f558f2021-04-06 15:51:47 +00001450 /// Creates an ast::BuiltinDecoration
1451 /// @param source the source information
1452 /// @param builtin the builtin value
1453 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001454 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001455 return create<ast::BuiltinDecoration>(source, builtin);
1456 }
1457
1458 /// Creates an ast::BuiltinDecoration
1459 /// @param builtin the builtin value
1460 /// @returns the builtin decoration pointer
1461 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1462 return create<ast::BuiltinDecoration>(source_, builtin);
1463 }
1464
1465 /// Creates an ast::LocationDecoration
1466 /// @param source the source information
1467 /// @param location the location value
1468 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001469 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001470 return create<ast::LocationDecoration>(source, location);
1471 }
1472
1473 /// Creates an ast::LocationDecoration
1474 /// @param location the location value
1475 /// @returns the location decoration pointer
1476 ast::LocationDecoration* Location(uint32_t location) {
1477 return create<ast::LocationDecoration>(source_, location);
1478 }
1479
1480 /// Creates an ast::StageDecoration
1481 /// @param source the source information
1482 /// @param stage the pipeline stage
1483 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001484 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001485 return create<ast::StageDecoration>(source, stage);
1486 }
1487
1488 /// Creates an ast::StageDecoration
1489 /// @param stage the pipeline stage
1490 /// @returns the stage decoration pointer
1491 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1492 return create<ast::StageDecoration>(source_, stage);
1493 }
1494
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001495 /// Sets the current builder source to `src`
1496 /// @param src the Source used for future create() calls
1497 void SetSource(const Source& src) {
1498 AssertNotMoved();
1499 source_ = src;
1500 }
1501
1502 /// Sets the current builder source to `loc`
1503 /// @param loc the Source used for future create() calls
1504 void SetSource(const Source::Location& loc) {
1505 AssertNotMoved();
1506 source_ = Source(loc);
1507 }
1508
Ben Clayton33352542021-01-29 16:43:41 +00001509 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001510 /// @note As the Resolver is run when the Program is built, this will only be
1511 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001512 /// @param expr the AST expression
1513 /// @return the resolved semantic type for the expression, or nullptr if the
1514 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001515 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001516
Ben Clayton169512e2021-04-17 05:52:11 +00001517 /// Wraps the ast::Literal in a statement. This is used by tests that
1518 /// construct a partial AST and require the Resolver to reach these
1519 /// nodes.
1520 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1521 /// @return the ast::Statement that wraps the ast::Statement
1522 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001523 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001524 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001525 /// nodes.
1526 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1527 /// @return the ast::Statement that wraps the ast::Expression
1528 ast::Statement* WrapInStatement(ast::Expression* expr);
1529 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001530 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001531 /// these nodes.
1532 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1533 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1534 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1535 /// Returns the statement argument. Used as a passthrough-overload by
1536 /// WrapInFunction().
1537 /// @param stmt the ast::Statement
1538 /// @return `stmt`
1539 ast::Statement* WrapInStatement(ast::Statement* stmt);
1540 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001541 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001542 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001543 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001544 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001545 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001546 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001547 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001548 }
1549 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001550 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001551 /// @returns the function
1552 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001553
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001554 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001555 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001556
1557 protected:
1558 /// Asserts that the builder has not been moved.
1559 void AssertNotMoved() const;
1560
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001561 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001562 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001563 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001564 ASTNodeAllocator ast_nodes_;
1565 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001566 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001567 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001568 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001569 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001570
1571 /// The source to use when creating AST nodes without providing a Source as
1572 /// the first argument.
1573 Source source_;
1574
Ben Clayton5f0ea112021-03-09 10:54:37 +00001575 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001576 /// program when built.
1577 bool resolve_on_build_ = true;
1578
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001579 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1580 bool moved_ = false;
1581};
1582
1583//! @cond Doxygen_Suppress
1584// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1585template <>
1586struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001587 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001588 return t->i32();
1589 }
1590};
1591template <>
1592struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001593 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001594 return t->u32();
1595 }
1596};
1597template <>
1598struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001599 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001600 return t->f32();
1601 }
1602};
1603template <>
1604struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001605 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001606 return t->bool_();
1607 }
1608};
1609template <>
1610struct ProgramBuilder::TypesBuilder::CToAST<void> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001611 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001612 return t->void_();
1613 }
1614};
1615//! @endcond
1616
Ben Clayton917b14b2021-04-19 16:50:23 +00001617/// @param builder the ProgramBuilder
1618/// @returns the ProgramID of the ProgramBuilder
1619inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1620 return builder->ID();
1621}
1622
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001623} // namespace tint
1624
1625#endif // SRC_PROGRAM_BUILDER_H_