blob: 2c6145033f09f0183873d9e10a4d64cc0f98a26f [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 Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/member_accessor_expression.h"
34#include "src/ast/module.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000035#include "src/ast/return_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000036#include "src/ast/scalar_constructor_expression.h"
37#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000038#include "src/ast/stage_decoration.h"
Ben Claytonbab31972021-02-08 21:16:21 +000039#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000040#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000041#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000042#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000043#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/ast/type_constructor_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000045#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000046#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000047#include "src/ast/variable_decl_statement.h"
Ben Claytona922e652021-04-21 13:37:22 +000048#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000049#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000050#include "src/program_id.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000051#include "src/sem/access_control_type.h"
52#include "src/sem/alias_type.h"
53#include "src/sem/array_type.h"
54#include "src/sem/bool_type.h"
55#include "src/sem/f32_type.h"
56#include "src/sem/i32_type.h"
57#include "src/sem/matrix_type.h"
58#include "src/sem/pointer_type.h"
59#include "src/sem/struct_type.h"
60#include "src/sem/u32_type.h"
61#include "src/sem/vector_type.h"
62#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000063#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000064
65namespace tint {
66
Ben Clayton401b96b2021-02-03 17:19:59 +000067// Forward declarations
68namespace ast {
69class VariableDeclStatement;
70} // namespace ast
71
Ben Claytona6b9a8e2021-01-26 16:57:10 +000072class CloneContext;
73
74/// ProgramBuilder is a mutable builder for a Program.
75/// To construct a Program, populate the builder and then `std::move` it to a
76/// Program.
77class ProgramBuilder {
78 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000079 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
80 using ASTNodeAllocator = BlockAllocator<ast::Node>;
81
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000082 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
83 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000084
85 /// `i32` is a type alias to `int`.
86 /// Useful for passing to template methods such as `vec2<i32>()` 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 i32 = decltype(1);
91 /// `u32` is a type alias to `unsigned int`.
92 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
93 /// WGSL syntax.
94 /// Note: this is intentionally not aliased to uint32_t as we want integer
95 /// literals passed to the builder to match WGSL's integer literal types.
96 using u32 = decltype(1u);
97 /// `f32` is a type alias to `float`
98 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
99 /// WGSL syntax.
100 using f32 = float;
101
102 /// Constructor
103 ProgramBuilder();
104
105 /// Move constructor
106 /// @param rhs the builder to move
107 ProgramBuilder(ProgramBuilder&& rhs);
108
109 /// Destructor
110 virtual ~ProgramBuilder();
111
112 /// Move assignment operator
113 /// @param rhs the builder to move
114 /// @return this builder
115 ProgramBuilder& operator=(ProgramBuilder&& rhs);
116
Ben Claytone43c8302021-01-29 11:59:32 +0000117 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
118 /// making a deep clone of the Program contents.
119 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
120 /// existing immutable program.
121 /// As the returned ProgramBuilder wraps `program`, `program` must not be
122 /// destructed or assigned while using the returned ProgramBuilder.
123 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
124 /// function. See crbug.com/tint/460.
125 /// @param program the immutable Program to wrap
126 /// @return the ProgramBuilder that wraps `program`
127 static ProgramBuilder Wrap(const Program* program);
128
Ben Claytone6995de2021-04-13 23:27:27 +0000129 /// @returns the unique identifier for this program
130 ProgramID ID() const { return id_; }
131
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000132 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000133 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000134 AssertNotMoved();
135 return types_;
136 }
137
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000138 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000139 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000140 AssertNotMoved();
141 return types_;
142 }
143
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000144 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000145 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000146 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000147 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000148 }
149
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000150 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000151 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000152 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000153 return ast_nodes_;
154 }
155
156 /// @returns a reference to the program's semantic nodes storage
157 SemNodeAllocator& SemNodes() {
158 AssertNotMoved();
159 return sem_nodes_;
160 }
161
162 /// @returns a reference to the program's semantic nodes storage
163 const SemNodeAllocator& SemNodes() const {
164 AssertNotMoved();
165 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000166 }
167
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000168 /// @returns a reference to the program's AST root Module
169 ast::Module& AST() {
170 AssertNotMoved();
171 return *ast_;
172 }
173
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000174 /// @returns a reference to the program's AST root Module
175 const ast::Module& AST() const {
176 AssertNotMoved();
177 return *ast_;
178 }
179
180 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000181 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000182 AssertNotMoved();
183 return sem_;
184 }
185
186 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000187 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000188 AssertNotMoved();
189 return sem_;
190 }
191
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000192 /// @returns a reference to the program's SymbolTable
193 SymbolTable& Symbols() {
194 AssertNotMoved();
195 return symbols_;
196 }
197
Ben Clayton708dc2d2021-01-29 11:22:40 +0000198 /// @returns a reference to the program's SymbolTable
199 const SymbolTable& Symbols() const {
200 AssertNotMoved();
201 return symbols_;
202 }
203
Ben Clayton844217f2021-01-27 18:49:05 +0000204 /// @returns a reference to the program's diagnostics
205 diag::List& Diagnostics() {
206 AssertNotMoved();
207 return diagnostics_;
208 }
209
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000210 /// @returns a reference to the program's diagnostics
211 const diag::List& Diagnostics() const {
212 AssertNotMoved();
213 return diagnostics_;
214 }
215
Ben Clayton5f0ea112021-03-09 10:54:37 +0000216 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000217 /// @param enable the new flag value (defaults to true)
218 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
219
Ben Clayton5f0ea112021-03-09 10:54:37 +0000220 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000221 /// built.
222 bool ResolveOnBuild() const { return resolve_on_build_; }
223
Ben Clayton844217f2021-01-27 18:49:05 +0000224 /// @returns true if the program has no error diagnostics and is not missing
225 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000226 bool IsValid() const;
227
Ben Clayton708dc2d2021-01-29 11:22:40 +0000228 /// Writes a representation of the node to the output stream
229 /// @note unlike str(), to_str() does not automatically demangle the string.
230 /// @param node the AST node
231 /// @param out the stream to write to
232 /// @param indent number of spaces to indent the node when writing
233 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
234 node->to_str(Sem(), out, indent);
235 }
236
237 /// Returns a demangled, string representation of `node`.
238 /// @param node the AST node
239 /// @returns a string representation of the node
240 std::string str(const ast::Node* node) const;
241
Ben Clayton7fdfff12021-01-29 15:17:30 +0000242 /// Creates a new ast::Node owned by the ProgramBuilder. When the
243 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000244 /// @param source the Source of the node
245 /// @param args the arguments to pass to the type constructor
246 /// @returns the node pointer
247 template <typename T, typename... ARGS>
248 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
249 ARGS&&... args) {
250 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000251 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000252 }
253
Ben Clayton7fdfff12021-01-29 15:17:30 +0000254 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
255 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000256 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000257 /// When the ProgramBuilder is destructed, the ast::Node will also be
258 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000259 /// @returns the node pointer
260 template <typename T>
261 traits::EnableIfIsType<T, ast::Node>* create() {
262 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000263 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000264 }
265
Ben Clayton7fdfff12021-01-29 15:17:30 +0000266 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
267 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000268 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000269 /// When the ProgramBuilder is destructed, the ast::Node will also be
270 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000271 /// @param arg0 the first arguments to pass to the type constructor
272 /// @param args the remaining arguments to pass to the type constructor
273 /// @returns the node pointer
274 template <typename T, typename ARG0, typename... ARGS>
275 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
276 traits::IsTypeOrDerived<T, ast::Node>::value &&
277 !traits::IsTypeOrDerived<ARG0, Source>::value,
278 T>*
279 create(ARG0&& arg0, ARGS&&... args) {
280 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000281 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000282 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000283 }
284
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000285 /// Creates a new sem::Node owned by the ProgramBuilder.
286 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000287 /// destructed.
288 /// @param args the arguments to pass to the type constructor
289 /// @returns the node pointer
290 template <typename T, typename... ARGS>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000291 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000292 AssertNotMoved();
293 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
294 }
295
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000296 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000297 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
298 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000299 /// Types are unique (de-aliased), and so calling create() for the same `T`
300 /// and arguments will return the same pointer.
301 /// @warning Use this method to acquire a type only if all of its type
302 /// information is provided in the constructor arguments `args`.<br>
303 /// If the type requires additional configuration after construction that
304 /// affect its fundamental type, build the type with `std::make_unique`, make
305 /// any necessary alterations and then call unique_type() instead.
306 /// @param args the arguments to pass to the type constructor
307 /// @returns the de-aliased type pointer
308 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000309 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
310 static_assert(std::is_base_of<sem::Type, T>::value,
311 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000312 AssertNotMoved();
313 return types_.Get<T>(std::forward<ARGS>(args)...);
314 }
315
316 /// Marks this builder as moved, preventing any further use of the builder.
317 void MarkAsMoved();
318
319 //////////////////////////////////////////////////////////////////////////////
320 // TypesBuilder
321 //////////////////////////////////////////////////////////////////////////////
322
323 /// TypesBuilder holds basic `tint` types and methods for constructing
324 /// complex types.
325 class TypesBuilder {
326 public:
327 /// Constructor
328 /// @param builder the program builder
329 explicit TypesBuilder(ProgramBuilder* builder);
330
331 /// @return the tint AST type for the C type `T`.
332 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000333 sem::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000334 return CToAST<T>::get(this);
335 }
336
337 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000338 typ::Bool bool_() const {
339 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
340 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000341
342 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000343 typ::F32 f32() const {
344 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
345 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000346
347 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000348 typ::I32 i32() const {
349 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
350 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000351
352 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000353 typ::U32 u32() const {
354 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
355 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000356
357 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000358 typ::Void void_() const {
359 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
360 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000361
Antonio Maiorano25436862021-04-13 13:32:33 +0000362 /// @param type vector subtype
363 /// @return the tint AST type for a 2-element vector of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000364 sem::Vector* vec2(sem::Type* type) const {
365 return builder->create<sem::Vector>(type, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000366 }
367
368 /// @param type vector subtype
369 /// @return the tint AST type for a 3-element vector of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000370 sem::Vector* vec3(sem::Type* type) const {
371 return builder->create<sem::Vector>(type, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000372 }
373
374 /// @param type vector subtype
375 /// @return the tint AST type for a 4-element vector of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000376 sem::Type* vec4(sem::Type* type) const {
377 return builder->create<sem::Vector>(type, 4u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000378 }
379
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000380 /// @return the tint AST type for a 2-element vector of the C type `T`.
381 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000382 sem::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000383 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000384 }
385
386 /// @return the tint AST type for a 3-element vector of the C type `T`.
387 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000388 sem::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000389 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000390 }
391
392 /// @return the tint AST type for a 4-element vector of the C type `T`.
393 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000394 sem::Type* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000395 return vec4(Of<T>());
396 }
397
398 /// @param type matrix subtype
399 /// @return the tint AST type for a 2x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000400 sem::Matrix* mat2x2(sem::Type* type) const {
401 return builder->create<sem::Matrix>(type, 2u, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000402 }
403
404 /// @param type matrix subtype
405 /// @return the tint AST type for a 2x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000406 sem::Matrix* mat2x3(sem::Type* type) const {
407 return builder->create<sem::Matrix>(type, 3u, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000408 }
409
410 /// @param type matrix subtype
411 /// @return the tint AST type for a 2x4 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000412 sem::Matrix* mat2x4(sem::Type* type) const {
413 return builder->create<sem::Matrix>(type, 4u, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000414 }
415
416 /// @param type matrix subtype
417 /// @return the tint AST type for a 3x2 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000418 sem::Matrix* mat3x2(sem::Type* type) const {
419 return builder->create<sem::Matrix>(type, 2u, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000420 }
421
422 /// @param type matrix subtype
423 /// @return the tint AST type for a 3x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000424 sem::Matrix* mat3x3(sem::Type* type) const {
425 return builder->create<sem::Matrix>(type, 3u, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000426 }
427
428 /// @param type matrix subtype
429 /// @return the tint AST type for a 3x4 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000430 sem::Matrix* mat3x4(sem::Type* type) const {
431 return builder->create<sem::Matrix>(type, 4u, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000432 }
433
434 /// @param type matrix subtype
435 /// @return the tint AST type for a 4x2 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000436 sem::Matrix* mat4x2(sem::Type* type) const {
437 return builder->create<sem::Matrix>(type, 2u, 4u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000438 }
439
440 /// @param type matrix subtype
441 /// @return the tint AST type for a 4x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000442 sem::Matrix* mat4x3(sem::Type* type) const {
443 return builder->create<sem::Matrix>(type, 3u, 4u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000444 }
445
446 /// @param type matrix subtype
447 /// @return the tint AST type for a 4x4 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000448 sem::Matrix* mat4x4(sem::Type* type) const {
449 return builder->create<sem::Matrix>(type, 4u, 4u);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000450 }
451
452 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
453 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000454 sem::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000455 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000456 }
457
458 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
459 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000460 sem::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000461 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000462 }
463
464 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
465 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000466 sem::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000467 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000468 }
469
470 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
471 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000472 sem::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000473 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000474 }
475
476 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
477 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000478 sem::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000479 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000480 }
481
482 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
483 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000484 sem::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000485 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000486 }
487
488 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
489 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000490 sem::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000491 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000492 }
493
494 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
495 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000496 sem::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000497 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000498 }
499
500 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
501 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000502 sem::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000503 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000504 }
505
506 /// @param subtype the array element type
507 /// @param n the array size. 0 represents a runtime-array.
508 /// @return the tint AST type for a array of size `n` of type `T`
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000509 sem::ArrayType* array(sem::Type* subtype, uint32_t n = 0) const {
510 return builder->create<sem::ArrayType>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000511 }
512
Ben Claytonbab31972021-02-08 21:16:21 +0000513 /// @param subtype the array element type
514 /// @param n the array size. 0 represents a runtime-array.
515 /// @param stride the array stride.
516 /// @return the tint AST type for a array of size `n` of type `T`
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000517 sem::ArrayType* array(sem::Type* subtype,
518 uint32_t n,
519 uint32_t stride) const {
520 return builder->create<sem::ArrayType>(
Ben Claytonbab31972021-02-08 21:16:21 +0000521 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000522 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000523 builder->create<ast::StrideDecoration>(stride),
524 });
525 }
526
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000527 /// @return the tint AST type for an array of size `N` of type `T`
528 template <typename T, int N = 0>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000529 sem::ArrayType* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000530 return array(Of<T>(), N);
531 }
532
Ben Claytonbab31972021-02-08 21:16:21 +0000533 /// @param stride the array stride
534 /// @return the tint AST type for an array of size `N` of type `T`
535 template <typename T, int N = 0>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000536 sem::ArrayType* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000537 return array(Of<T>(), N, stride);
538 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000539
Ben Clayton42d1e092021-02-02 14:29:15 +0000540 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000541 /// @param name the alias name
542 /// @param type the alias type
543 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000544 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000545 sem::Alias* alias(NAME&& name, sem::Type* type) const {
546 return builder->create<sem::Alias>(builder->Sym(std::forward<NAME>(name)),
547 type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000548 }
549
Ben Clayton4db10dd2021-04-16 08:47:14 +0000550 /// Creates an access control qualifier type
551 /// @param access the access control
552 /// @param type the inner type
553 /// @returns the access control qualifier type
Ben Clayton8a8d26b2021-04-20 15:04:21 +0000554 sem::AccessControl* access(ast::AccessControl::Access access,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000555 sem::Type* type) const {
556 return builder->create<sem::AccessControl>(access, type);
Ben Clayton4db10dd2021-04-16 08:47:14 +0000557 }
558
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000559 /// @return the tint AST pointer to `type` with the given ast::StorageClass
560 /// @param type the type of the pointer
561 /// @param storage_class the storage class of the pointer
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000562 sem::Pointer* pointer(sem::Type* type,
563 ast::StorageClass storage_class) const {
564 return builder->create<sem::Pointer>(type, storage_class);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000565 }
566
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 /// @return the tint AST pointer to type `T` with the given
568 /// ast::StorageClass.
569 /// @param storage_class the storage class of the pointer
570 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000571 sem::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000572 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000575 /// @param impl the struct implementation
576 /// @returns a struct pointer
Ben Clayton913a2f42021-04-20 15:21:21 +0000577 sem::StructType* struct_(ast::Struct* impl) const {
578 return builder->create<sem::StructType>(impl);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 }
580
581 private:
582 /// CToAST<T> is specialized for various `T` types and each specialization
583 /// contains a single static `get()` method for obtaining the corresponding
584 /// AST type for the C type `T`.
585 /// `get()` has the signature:
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000586 /// `static sem::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000587 template <typename T>
588 struct CToAST {};
589
Ben Claytonc7ca7662021-02-17 16:23:52 +0000590 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000591 };
592
593 //////////////////////////////////////////////////////////////////////////////
594 // AST helper methods
595 //////////////////////////////////////////////////////////////////////////////
596
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000597 /// @param name the symbol string
598 /// @return a Symbol with the given name
599 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
600
601 /// @param sym the symbol
602 /// @return `sym`
603 Symbol Sym(Symbol sym) { return sym; }
604
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000605 /// @param expr the expression
606 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000607 template <typename T>
608 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
609 return expr;
610 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000611
Ben Claytonb8ea5912021-04-19 16:52:42 +0000612 /// Passthrough for nullptr
613 /// @return nullptr
614 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
615
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000616 /// @param name the identifier name
617 /// @return an ast::IdentifierExpression with the given name
618 ast::IdentifierExpression* Expr(const std::string& name) {
619 return create<ast::IdentifierExpression>(Symbols().Register(name));
620 }
621
Ben Clayton46d78d72021-02-10 21:34:26 +0000622 /// @param symbol the identifier symbol
623 /// @return an ast::IdentifierExpression with the given symbol
624 ast::IdentifierExpression* Expr(Symbol symbol) {
625 return create<ast::IdentifierExpression>(symbol);
626 }
627
Ben Claytonb8ea5912021-04-19 16:52:42 +0000628 /// @param variable the AST variable
629 /// @return an ast::IdentifierExpression with the variable's symbol
630 ast::IdentifierExpression* Expr(ast::Variable* variable) {
631 return create<ast::IdentifierExpression>(variable->symbol());
632 }
633
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000634 /// @param source the source information
635 /// @param name the identifier name
636 /// @return an ast::IdentifierExpression with the given name
637 ast::IdentifierExpression* Expr(const Source& source,
638 const std::string& name) {
639 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
640 }
641
642 /// @param name the identifier name
643 /// @return an ast::IdentifierExpression with the given name
644 ast::IdentifierExpression* Expr(const char* name) {
645 return create<ast::IdentifierExpression>(Symbols().Register(name));
646 }
647
648 /// @param value the boolean value
649 /// @return a Scalar constructor for the given value
650 ast::ScalarConstructorExpression* Expr(bool value) {
651 return create<ast::ScalarConstructorExpression>(Literal(value));
652 }
653
654 /// @param value the float value
655 /// @return a Scalar constructor for the given value
656 ast::ScalarConstructorExpression* Expr(f32 value) {
657 return create<ast::ScalarConstructorExpression>(Literal(value));
658 }
659
660 /// @param value the integer value
661 /// @return a Scalar constructor for the given value
662 ast::ScalarConstructorExpression* Expr(i32 value) {
663 return create<ast::ScalarConstructorExpression>(Literal(value));
664 }
665
666 /// @param value the unsigned int value
667 /// @return a Scalar constructor for the given value
668 ast::ScalarConstructorExpression* Expr(u32 value) {
669 return create<ast::ScalarConstructorExpression>(Literal(value));
670 }
671
672 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
673 /// `list`.
674 /// @param list the list to append too
675 /// @param arg the arg to create
676 template <typename ARG>
677 void Append(ast::ExpressionList& list, ARG&& arg) {
678 list.emplace_back(Expr(std::forward<ARG>(arg)));
679 }
680
681 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
682 /// then appends them to `list`.
683 /// @param list the list to append too
684 /// @param arg0 the first argument
685 /// @param args the rest of the arguments
686 template <typename ARG0, typename... ARGS>
687 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
688 Append(list, std::forward<ARG0>(arg0));
689 Append(list, std::forward<ARGS>(args)...);
690 }
691
692 /// @return an empty list of expressions
693 ast::ExpressionList ExprList() { return {}; }
694
695 /// @param args the list of expressions
696 /// @return the list of expressions converted to `ast::Expression`s using
697 /// `Expr()`,
698 template <typename... ARGS>
699 ast::ExpressionList ExprList(ARGS&&... args) {
700 ast::ExpressionList list;
701 list.reserve(sizeof...(args));
702 Append(list, std::forward<ARGS>(args)...);
703 return list;
704 }
705
706 /// @param list the list of expressions
707 /// @return `list`
708 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
709
710 /// @param val the boolan value
711 /// @return a boolean literal with the given value
712 ast::BoolLiteral* Literal(bool val) {
713 return create<ast::BoolLiteral>(ty.bool_(), val);
714 }
715
716 /// @param val the float value
717 /// @return a float literal with the given value
718 ast::FloatLiteral* Literal(f32 val) {
719 return create<ast::FloatLiteral>(ty.f32(), val);
720 }
721
722 /// @param val the unsigned int value
723 /// @return a ast::UintLiteral with the given value
724 ast::UintLiteral* Literal(u32 val) {
725 return create<ast::UintLiteral>(ty.u32(), val);
726 }
727
728 /// @param val the integer value
729 /// @return the ast::SintLiteral with the given value
730 ast::SintLiteral* Literal(i32 val) {
731 return create<ast::SintLiteral>(ty.i32(), val);
732 }
733
734 /// @param args the arguments for the type constructor
735 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
736 /// of `args` converted to `ast::Expression`s using `Expr()`
737 template <typename T, typename... ARGS>
738 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
739 return create<ast::TypeConstructorExpression>(
740 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
741 }
742
743 /// @param type the type to construct
744 /// @param args the arguments for the constructor
745 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
746 /// values `args`.
747 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000748 ast::TypeConstructorExpression* Construct(sem::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000749 return create<ast::TypeConstructorExpression>(
750 type, ExprList(std::forward<ARGS>(args)...));
751 }
752
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000753 /// Creates a constructor expression that constructs an object of
754 /// `type` filled with `elem_value`. For example,
755 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
756 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
757 /// @param type the type to construct
758 /// @param elem_value the initial or element value (for vec and mat) to
759 /// construct with
760 /// @return the constructor expression
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000761 ast::ConstructorExpression* ConstructValueFilledWith(sem::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000762 int elem_value = 0);
763
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000764 /// @param args the arguments for the vector constructor
765 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
766 /// `T`, constructed with the values `args`.
767 template <typename T, typename... ARGS>
768 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
769 return create<ast::TypeConstructorExpression>(
770 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
771 }
772
773 /// @param args the arguments for the vector constructor
774 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
775 /// `T`, constructed with the values `args`.
776 template <typename T, typename... ARGS>
777 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
778 return create<ast::TypeConstructorExpression>(
779 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
780 }
781
782 /// @param args the arguments for the vector constructor
783 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
784 /// `T`, constructed with the values `args`.
785 template <typename T, typename... ARGS>
786 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
787 return create<ast::TypeConstructorExpression>(
788 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
789 }
790
791 /// @param args the arguments for the matrix constructor
792 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
793 /// `T`, constructed with the values `args`.
794 template <typename T, typename... ARGS>
795 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
796 return create<ast::TypeConstructorExpression>(
797 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
798 }
799
800 /// @param args the arguments for the matrix constructor
801 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
802 /// `T`, constructed with the values `args`.
803 template <typename T, typename... ARGS>
804 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
805 return create<ast::TypeConstructorExpression>(
806 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
807 }
808
809 /// @param args the arguments for the matrix constructor
810 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
811 /// `T`, constructed with the values `args`.
812 template <typename T, typename... ARGS>
813 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
814 return create<ast::TypeConstructorExpression>(
815 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
816 }
817
818 /// @param args the arguments for the matrix constructor
819 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
820 /// `T`, constructed with the values `args`.
821 template <typename T, typename... ARGS>
822 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
823 return create<ast::TypeConstructorExpression>(
824 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
825 }
826
827 /// @param args the arguments for the matrix constructor
828 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
829 /// `T`, constructed with the values `args`.
830 template <typename T, typename... ARGS>
831 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
832 return create<ast::TypeConstructorExpression>(
833 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
834 }
835
836 /// @param args the arguments for the matrix constructor
837 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
838 /// `T`, constructed with the values `args`.
839 template <typename T, typename... ARGS>
840 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
841 return create<ast::TypeConstructorExpression>(
842 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
843 }
844
845 /// @param args the arguments for the matrix constructor
846 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
847 /// `T`, constructed with the values `args`.
848 template <typename T, typename... ARGS>
849 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
850 return create<ast::TypeConstructorExpression>(
851 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
852 }
853
854 /// @param args the arguments for the matrix constructor
855 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
856 /// `T`, constructed with the values `args`.
857 template <typename T, typename... ARGS>
858 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
859 return create<ast::TypeConstructorExpression>(
860 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
861 }
862
863 /// @param args the arguments for the matrix constructor
864 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
865 /// `T`, constructed with the values `args`.
866 template <typename T, typename... ARGS>
867 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
868 return create<ast::TypeConstructorExpression>(
869 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
870 }
871
872 /// @param args the arguments for the array constructor
873 /// @return an `ast::TypeConstructorExpression` of an array with element type
874 /// `T`, constructed with the values `args`.
875 template <typename T, int N = 0, typename... ARGS>
876 ast::TypeConstructorExpression* array(ARGS&&... args) {
877 return create<ast::TypeConstructorExpression>(
878 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
879 }
880
881 /// @param subtype the array element type
882 /// @param n the array size. 0 represents a runtime-array.
883 /// @param args the arguments for the array constructor
884 /// @return an `ast::TypeConstructorExpression` of an array with element type
885 /// `subtype`, constructed with the values `args`.
886 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000887 ast::TypeConstructorExpression* array(sem::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000888 uint32_t n,
889 ARGS&&... args) {
890 return create<ast::TypeConstructorExpression>(
891 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
892 }
893
894 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000895 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000896 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000897 /// @param constructor constructor expression
898 /// @param decorations variable decorations
899 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000900 template <typename NAME>
901 ast::Variable* Var(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000902 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000903 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000904 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000905 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000906 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
907 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000908 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000909
910 /// @param source the variable source
911 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000912 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000913 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000914 /// @param constructor constructor expression
915 /// @param decorations variable decorations
916 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000917 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000918 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000919 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000920 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000921 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000922 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000923 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000924 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000925 type, false, constructor, decorations);
926 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000927
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000928 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000929 /// @param type the variable type
930 /// @param constructor optional constructor expression
931 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000932 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000933 template <typename NAME>
934 ast::Variable* Const(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000935 sem::Type* type,
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>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000939 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000940 constructor, decorations);
941 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000942
943 /// @param source the variable source
944 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000945 /// @param type the variable type
946 /// @param constructor optional constructor expression
947 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000948 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000949 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000950 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000951 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000952 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000953 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000954 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000955 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000956 ast::StorageClass::kNone, type, true,
957 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000958 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000959
James Pricedaf1f1c2021-04-08 22:15:48 +0000960 /// @param name the parameter name
961 /// @param type the parameter type
962 /// @param decorations optional parameter decorations
963 /// @returns a constant `ast::Variable` with the given name and type
964 template <typename NAME>
965 ast::Variable* Param(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000966 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000967 ast::DecorationList decorations = {}) {
968 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
969 ast::StorageClass::kNone, type, true, nullptr,
970 decorations);
971 }
972
973 /// @param source the parameter source
974 /// @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(const Source& source,
980 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000981 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000982 ast::DecorationList decorations = {}) {
983 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
984 ast::StorageClass::kNone, type, true, nullptr,
985 decorations);
986 }
987
Ben Clayton401b96b2021-02-03 17:19:59 +0000988 /// @param args the arguments to pass to Var()
989 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
990 /// of `args`, which is automatically registered as a global variable with the
991 /// ast::Module.
992 template <typename... ARGS>
993 ast::Variable* Global(ARGS&&... args) {
994 auto* var = Var(std::forward<ARGS>(args)...);
995 AST().AddGlobalVariable(var);
996 return var;
997 }
998
999 /// @param args the arguments to pass to Const()
1000 /// @returns a const `ast::Variable` constructed by calling Var() with the
1001 /// arguments of `args`, which is automatically registered as a global
1002 /// variable with the ast::Module.
1003 template <typename... ARGS>
1004 ast::Variable* GlobalConst(ARGS&&... args) {
1005 auto* var = Const(std::forward<ARGS>(args)...);
1006 AST().AddGlobalVariable(var);
1007 return var;
1008 }
1009
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001010 /// @param func the function name
1011 /// @param args the function call arguments
1012 /// @returns a `ast::CallExpression` to the function `func`, with the
1013 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1014 template <typename NAME, typename... ARGS>
1015 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1016 return create<ast::CallExpression>(Expr(func),
1017 ExprList(std::forward<ARGS>(args)...));
1018 }
1019
1020 /// @param lhs the left hand argument to the addition operation
1021 /// @param rhs the right hand argument to the addition operation
1022 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1023 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001024 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001025 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1026 Expr(std::forward<LHS>(lhs)),
1027 Expr(std::forward<RHS>(rhs)));
1028 }
1029
1030 /// @param lhs the left hand argument to the subtraction operation
1031 /// @param rhs the right hand argument to the subtraction operation
1032 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1033 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001034 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001035 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1036 Expr(std::forward<LHS>(lhs)),
1037 Expr(std::forward<RHS>(rhs)));
1038 }
1039
1040 /// @param lhs the left hand argument to the multiplication operation
1041 /// @param rhs the right hand argument to the multiplication operation
1042 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1043 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001044 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001045 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1046 Expr(std::forward<LHS>(lhs)),
1047 Expr(std::forward<RHS>(rhs)));
1048 }
1049
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001050 /// @param source the source information
1051 /// @param lhs the left hand argument to the multiplication operation
1052 /// @param rhs the right hand argument to the multiplication operation
1053 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1054 template <typename LHS, typename RHS>
1055 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1056 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1057 Expr(std::forward<LHS>(lhs)),
1058 Expr(std::forward<RHS>(rhs)));
1059 }
1060
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001061 /// @param lhs the left hand argument to the division operation
1062 /// @param rhs the right hand argument to the division operation
1063 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1064 template <typename LHS, typename RHS>
1065 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1066 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1067 Expr(std::forward<LHS>(lhs)),
1068 Expr(std::forward<RHS>(rhs)));
1069 }
1070
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001071 /// @param arr the array argument for the array accessor expression
1072 /// @param idx the index argument for the array accessor expression
1073 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1074 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001075 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001076 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1077 Expr(std::forward<IDX>(idx)));
1078 }
1079
1080 /// @param obj the object for the member accessor expression
1081 /// @param idx the index argument for the array accessor expression
1082 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1083 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001084 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001085 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1086 Expr(std::forward<IDX>(idx)));
1087 }
1088
Ben Clayton42d1e092021-02-02 14:29:15 +00001089 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001090 /// @param val the offset value
1091 /// @returns the offset decoration pointer
1092 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1093 return create<ast::StructMemberOffsetDecoration>(source_, val);
1094 }
1095
Ben Claytond614dd52021-03-15 10:43:11 +00001096 /// Creates a ast::StructMemberSizeDecoration
1097 /// @param source the source information
1098 /// @param val the size value
1099 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001100 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1101 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001102 return create<ast::StructMemberSizeDecoration>(source, val);
1103 }
1104
1105 /// Creates a ast::StructMemberSizeDecoration
1106 /// @param val the size value
1107 /// @returns the size decoration pointer
1108 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1109 return create<ast::StructMemberSizeDecoration>(source_, val);
1110 }
1111
1112 /// Creates a ast::StructMemberAlignDecoration
1113 /// @param source the source information
1114 /// @param val the align value
1115 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001116 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1117 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001118 return create<ast::StructMemberAlignDecoration>(source, val);
1119 }
1120
1121 /// Creates a ast::StructMemberAlignDecoration
1122 /// @param val the align value
1123 /// @returns the align decoration pointer
1124 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1125 return create<ast::StructMemberAlignDecoration>(source_, val);
1126 }
1127
Ben Clayton42d1e092021-02-02 14:29:15 +00001128 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001129 /// @param source the source information
1130 /// @param name the function name
1131 /// @param params the function parameters
1132 /// @param type the function return type
1133 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001134 /// @param decorations the optional function decorations
1135 /// @param return_type_decorations the optional function return type
1136 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001138 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001139 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001140 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001141 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001142 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001143 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001144 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001145 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001146 auto* func =
1147 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1148 type, create<ast::BlockStatement>(body),
1149 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001150 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001151 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001152 }
1153
Ben Clayton42d1e092021-02-02 14:29:15 +00001154 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001155 /// @param name the function name
1156 /// @param params the function parameters
1157 /// @param type the function return type
1158 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001159 /// @param decorations the optional function decorations
1160 /// @param return_type_decorations the optional function return type
1161 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001162 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001163 template <typename NAME>
1164 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001165 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001166 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001167 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001168 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001169 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001170 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1171 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001172 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001173 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001174 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001175 }
1176
Ben Clayton49668bb2021-04-17 05:32:01 +00001177 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001178 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001179 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1180
1181 /// Creates an ast::ReturnStatement with the given return value
1182 /// @param val the return value
1183 /// @returns the return statement pointer
1184 template <typename EXPR>
1185 ast::ReturnStatement* Return(EXPR&& val) {
1186 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001187 }
1188
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001189 /// Creates a ast::Struct and sem::StructType, registering the
1190 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001191 /// @param source the source information
1192 /// @param name the struct name
1193 /// @param members the struct members
1194 /// @param decorations the optional struct decorations
1195 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001196 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001197 sem::StructType* Structure(const Source& source,
1198 NAME&& name,
1199 ast::StructMemberList members,
1200 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001201 auto sym = Sym(std::forward<NAME>(name));
1202 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1203 std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001204 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001205 AST().AddConstructedType(type);
1206 return type;
1207 }
1208
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001209 /// Creates a ast::Struct and sem::StructType, registering the
1210 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001211 /// @param name the struct name
1212 /// @param members the struct members
1213 /// @param decorations the optional struct decorations
1214 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001215 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001216 sem::StructType* Structure(NAME&& name,
1217 ast::StructMemberList members,
1218 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001219 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001220 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001221 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001222 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001223 AST().AddConstructedType(type);
1224 return type;
1225 }
1226
Ben Clayton42d1e092021-02-02 14:29:15 +00001227 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228 /// @param source the source information
1229 /// @param name the struct member name
1230 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001231 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001232 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001233 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001234 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001235 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001236 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001237 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001238 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1239 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001240 }
1241
Ben Clayton42d1e092021-02-02 14:29:15 +00001242 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001243 /// @param name the struct member name
1244 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001245 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001246 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001247 template <typename NAME>
1248 ast::StructMember* Member(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001249 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001250 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001251 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1252 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253 }
1254
Ben Claytonbab31972021-02-08 21:16:21 +00001255 /// Creates a ast::StructMember with the given byte offset
1256 /// @param offset the offset to use in the StructMemberOffsetDecoration
1257 /// @param name the struct member name
1258 /// @param type the struct member type
1259 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001260 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001261 ast::StructMember* Member(uint32_t offset, NAME&& name, sem::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001262 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001263 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001264 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001265 create<ast::StructMemberOffsetDecoration>(offset),
1266 });
1267 }
1268
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001269 /// Creates a ast::BlockStatement with input statements
1270 /// @param statements statements of block
1271 /// @returns the block statement pointer
1272 template <typename... Statements>
1273 ast::BlockStatement* Block(Statements&&... statements) {
1274 return create<ast::BlockStatement>(
1275 ast::StatementList{std::forward<Statements>(statements)...});
1276 }
1277
1278 /// Creates a ast::ElseStatement with input condition and body
1279 /// @param condition the else condition expression
1280 /// @param body the else body
1281 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001282 template <typename CONDITION>
1283 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1284 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1285 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001286 }
1287
1288 /// Creates a ast::IfStatement with input condition, body, and optional
1289 /// variadic else statements
1290 /// @param condition the if statement condition expression
1291 /// @param body the if statement body
1292 /// @param elseStatements optional variadic else statements
1293 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001294 template <typename CONDITION, typename... ELSE_STATEMENTS>
1295 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001296 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001297 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001298 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001299 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001300 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001301 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001302 }
1303
1304 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001305 /// @param lhs the left hand side expression initializer
1306 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001307 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001308 template <typename LhsExpressionInit, typename RhsExpressionInit>
1309 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1310 RhsExpressionInit&& rhs) {
1311 return create<ast::AssignmentStatement>(
1312 Expr(std::forward<LhsExpressionInit>(lhs)),
1313 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001314 }
1315
1316 /// Creates a ast::LoopStatement with input body and optional continuing
1317 /// @param body the loop body
1318 /// @param continuing the optional continuing block
1319 /// @returns the loop statement pointer
1320 ast::LoopStatement* Loop(ast::BlockStatement* body,
1321 ast::BlockStatement* continuing = nullptr) {
1322 return create<ast::LoopStatement>(body, continuing);
1323 }
1324
1325 /// Creates a ast::VariableDeclStatement for the input variable
1326 /// @param var the variable to wrap in a decl statement
1327 /// @returns the variable decl statement pointer
1328 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1329 return create<ast::VariableDeclStatement>(var);
1330 }
1331
Antonio Maioranocea744d2021-03-25 12:55:27 +00001332 /// Creates a ast::SwitchStatement with input expression and cases
1333 /// @param condition the condition expression initializer
1334 /// @param cases case statements
1335 /// @returns the switch statement pointer
1336 template <typename ExpressionInit, typename... Cases>
1337 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1338 return create<ast::SwitchStatement>(
1339 Expr(std::forward<ExpressionInit>(condition)),
1340 ast::CaseStatementList{std::forward<Cases>(cases)...});
1341 }
1342
1343 /// Creates a ast::CaseStatement with input list of selectors, and body
1344 /// @param selectors list of selectors
1345 /// @param body the case body
1346 /// @returns the case statement pointer
1347 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1348 ast::BlockStatement* body = nullptr) {
1349 return create<ast::CaseStatement>(std::move(selectors),
1350 body ? body : Block());
1351 }
1352
1353 /// Convenient overload that takes a single selector
1354 /// @param selector a single case selector
1355 /// @param body the case body
1356 /// @returns the case statement pointer
1357 ast::CaseStatement* Case(ast::IntLiteral* selector,
1358 ast::BlockStatement* body = nullptr) {
1359 return Case(ast::CaseSelectorList{selector}, body);
1360 }
1361
1362 /// Convenience function that creates a 'default' ast::CaseStatement
1363 /// @param body the case body
1364 /// @returns the case statement pointer
1365 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1366 return Case(ast::CaseSelectorList{}, body);
1367 }
1368
James Price68f558f2021-04-06 15:51:47 +00001369 /// Creates an ast::BuiltinDecoration
1370 /// @param source the source information
1371 /// @param builtin the builtin value
1372 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001373 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001374 return create<ast::BuiltinDecoration>(source, builtin);
1375 }
1376
1377 /// Creates an ast::BuiltinDecoration
1378 /// @param builtin the builtin value
1379 /// @returns the builtin decoration pointer
1380 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1381 return create<ast::BuiltinDecoration>(source_, builtin);
1382 }
1383
1384 /// Creates an ast::LocationDecoration
1385 /// @param source the source information
1386 /// @param location the location value
1387 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001388 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001389 return create<ast::LocationDecoration>(source, location);
1390 }
1391
1392 /// Creates an ast::LocationDecoration
1393 /// @param location the location value
1394 /// @returns the location decoration pointer
1395 ast::LocationDecoration* Location(uint32_t location) {
1396 return create<ast::LocationDecoration>(source_, location);
1397 }
1398
1399 /// Creates an ast::StageDecoration
1400 /// @param source the source information
1401 /// @param stage the pipeline stage
1402 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001403 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001404 return create<ast::StageDecoration>(source, stage);
1405 }
1406
1407 /// Creates an ast::StageDecoration
1408 /// @param stage the pipeline stage
1409 /// @returns the stage decoration pointer
1410 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1411 return create<ast::StageDecoration>(source_, stage);
1412 }
1413
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001414 /// Sets the current builder source to `src`
1415 /// @param src the Source used for future create() calls
1416 void SetSource(const Source& src) {
1417 AssertNotMoved();
1418 source_ = src;
1419 }
1420
1421 /// Sets the current builder source to `loc`
1422 /// @param loc the Source used for future create() calls
1423 void SetSource(const Source::Location& loc) {
1424 AssertNotMoved();
1425 source_ = Source(loc);
1426 }
1427
Ben Clayton33352542021-01-29 16:43:41 +00001428 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001429 /// @note As the Resolver is run when the Program is built, this will only be
1430 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001431 /// @param expr the AST expression
1432 /// @return the resolved semantic type for the expression, or nullptr if the
1433 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001434 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001435
Ben Clayton169512e2021-04-17 05:52:11 +00001436 /// Wraps the ast::Literal in a statement. This is used by tests that
1437 /// construct a partial AST and require the Resolver to reach these
1438 /// nodes.
1439 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1440 /// @return the ast::Statement that wraps the ast::Statement
1441 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001442 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001443 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001444 /// nodes.
1445 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1446 /// @return the ast::Statement that wraps the ast::Expression
1447 ast::Statement* WrapInStatement(ast::Expression* expr);
1448 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001449 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001450 /// these nodes.
1451 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1452 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1453 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1454 /// Returns the statement argument. Used as a passthrough-overload by
1455 /// WrapInFunction().
1456 /// @param stmt the ast::Statement
1457 /// @return `stmt`
1458 ast::Statement* WrapInStatement(ast::Statement* stmt);
1459 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001460 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001461 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001462 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001463 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001464 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001465 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001466 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001467 }
1468 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001469 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001470 /// @returns the function
1471 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001472
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001473 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001474 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001475
1476 protected:
1477 /// Asserts that the builder has not been moved.
1478 void AssertNotMoved() const;
1479
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001480 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001481 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001482 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001483 ASTNodeAllocator ast_nodes_;
1484 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001485 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001486 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001487 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001488 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001489
1490 /// The source to use when creating AST nodes without providing a Source as
1491 /// the first argument.
1492 Source source_;
1493
Ben Clayton5f0ea112021-03-09 10:54:37 +00001494 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001495 /// program when built.
1496 bool resolve_on_build_ = true;
1497
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001498 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1499 bool moved_ = false;
1500};
1501
1502//! @cond Doxygen_Suppress
1503// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1504template <>
1505struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001506 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001507 return t->i32();
1508 }
1509};
1510template <>
1511struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001512 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001513 return t->u32();
1514 }
1515};
1516template <>
1517struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001518 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001519 return t->f32();
1520 }
1521};
1522template <>
1523struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001524 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001525 return t->bool_();
1526 }
1527};
1528template <>
1529struct ProgramBuilder::TypesBuilder::CToAST<void> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001530 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001531 return t->void_();
1532 }
1533};
1534//! @endcond
1535
Ben Clayton917b14b2021-04-19 16:50:23 +00001536/// @param builder the ProgramBuilder
1537/// @returns the ProgramID of the ProgramBuilder
1538inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1539 return builder->ID();
1540}
1541
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001542} // namespace tint
1543
1544#endif // SRC_PROGRAM_BUILDER_H_