blob: c006792e7b48705f1c1b528024c4bc7703776e7b [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 Clayton42708342021-04-21 17:55:12 +0000988 /// @param name the variable name
989 /// @param type the variable type
990 /// @param storage the variable storage class
991 /// @param constructor constructor expression
992 /// @param decorations variable decorations
993 /// @returns a new `ast::Variable`, which is automatically registered as a
994 /// global variable with the ast::Module.
995 template <typename NAME>
996 ast::Variable* Global(NAME&& name,
997 sem::Type* type,
998 ast::StorageClass storage,
999 ast::Expression* constructor = nullptr,
1000 ast::DecorationList decorations = {}) {
1001 auto* var =
1002 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1003 AST().AddGlobalVariable(var);
1004 return var;
1005 }
1006
1007 /// @param source the variable source
1008 /// @param name the variable name
1009 /// @param type the variable type
1010 /// @param storage the variable storage class
1011 /// @param constructor constructor expression
1012 /// @param decorations variable decorations
1013 /// @returns a new `ast::Variable`, which is automatically registered as a
1014 /// global variable with the ast::Module.
1015 template <typename NAME>
1016 ast::Variable* Global(const Source& source,
1017 NAME&& name,
1018 sem::Type* type,
1019 ast::StorageClass storage,
1020 ast::Expression* constructor = nullptr,
1021 ast::DecorationList decorations = {}) {
1022 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1023 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001024 AST().AddGlobalVariable(var);
1025 return var;
1026 }
1027
1028 /// @param args the arguments to pass to Const()
1029 /// @returns a const `ast::Variable` constructed by calling Var() with the
1030 /// arguments of `args`, which is automatically registered as a global
1031 /// variable with the ast::Module.
1032 template <typename... ARGS>
1033 ast::Variable* GlobalConst(ARGS&&... args) {
1034 auto* var = Const(std::forward<ARGS>(args)...);
1035 AST().AddGlobalVariable(var);
1036 return var;
1037 }
1038
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001039 /// @param func the function name
1040 /// @param args the function call arguments
1041 /// @returns a `ast::CallExpression` to the function `func`, with the
1042 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1043 template <typename NAME, typename... ARGS>
1044 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1045 return create<ast::CallExpression>(Expr(func),
1046 ExprList(std::forward<ARGS>(args)...));
1047 }
1048
1049 /// @param lhs the left hand argument to the addition operation
1050 /// @param rhs the right hand argument to the addition operation
1051 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1052 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001053 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001054 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1055 Expr(std::forward<LHS>(lhs)),
1056 Expr(std::forward<RHS>(rhs)));
1057 }
1058
1059 /// @param lhs the left hand argument to the subtraction operation
1060 /// @param rhs the right hand argument to the subtraction operation
1061 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1062 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001063 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001064 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1065 Expr(std::forward<LHS>(lhs)),
1066 Expr(std::forward<RHS>(rhs)));
1067 }
1068
1069 /// @param lhs the left hand argument to the multiplication operation
1070 /// @param rhs the right hand argument to the multiplication operation
1071 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1072 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001073 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1075 Expr(std::forward<LHS>(lhs)),
1076 Expr(std::forward<RHS>(rhs)));
1077 }
1078
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001079 /// @param source the source information
1080 /// @param lhs the left hand argument to the multiplication operation
1081 /// @param rhs the right hand argument to the multiplication operation
1082 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1083 template <typename LHS, typename RHS>
1084 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1085 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1086 Expr(std::forward<LHS>(lhs)),
1087 Expr(std::forward<RHS>(rhs)));
1088 }
1089
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001090 /// @param lhs the left hand argument to the division operation
1091 /// @param rhs the right hand argument to the division operation
1092 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1093 template <typename LHS, typename RHS>
1094 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1095 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1096 Expr(std::forward<LHS>(lhs)),
1097 Expr(std::forward<RHS>(rhs)));
1098 }
1099
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001100 /// @param arr the array argument for the array accessor expression
1101 /// @param idx the index argument for the array accessor expression
1102 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1103 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001104 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001105 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1106 Expr(std::forward<IDX>(idx)));
1107 }
1108
1109 /// @param obj the object for the member accessor expression
1110 /// @param idx the index argument for the array accessor expression
1111 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1112 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001113 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1115 Expr(std::forward<IDX>(idx)));
1116 }
1117
Ben Clayton42d1e092021-02-02 14:29:15 +00001118 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 /// @param val the offset value
1120 /// @returns the offset decoration pointer
1121 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1122 return create<ast::StructMemberOffsetDecoration>(source_, val);
1123 }
1124
Ben Claytond614dd52021-03-15 10:43:11 +00001125 /// Creates a ast::StructMemberSizeDecoration
1126 /// @param source the source information
1127 /// @param val the size value
1128 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001129 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1130 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001131 return create<ast::StructMemberSizeDecoration>(source, val);
1132 }
1133
1134 /// Creates a ast::StructMemberSizeDecoration
1135 /// @param val the size value
1136 /// @returns the size decoration pointer
1137 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1138 return create<ast::StructMemberSizeDecoration>(source_, val);
1139 }
1140
1141 /// Creates a ast::StructMemberAlignDecoration
1142 /// @param source the source information
1143 /// @param val the align value
1144 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001145 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1146 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001147 return create<ast::StructMemberAlignDecoration>(source, val);
1148 }
1149
1150 /// Creates a ast::StructMemberAlignDecoration
1151 /// @param val the align value
1152 /// @returns the align decoration pointer
1153 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1154 return create<ast::StructMemberAlignDecoration>(source_, val);
1155 }
1156
Ben Clayton42d1e092021-02-02 14:29:15 +00001157 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001158 /// @param source the source information
1159 /// @param name the function name
1160 /// @param params the function parameters
1161 /// @param type the function return type
1162 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001163 /// @param decorations the optional function decorations
1164 /// @param return_type_decorations the optional function return type
1165 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001166 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001167 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001168 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001169 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001170 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001171 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001172 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001173 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001174 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001175 auto* func =
1176 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1177 type, create<ast::BlockStatement>(body),
1178 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001179 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001180 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181 }
1182
Ben Clayton42d1e092021-02-02 14:29:15 +00001183 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001184 /// @param name the function name
1185 /// @param params the function parameters
1186 /// @param type the function return type
1187 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001188 /// @param decorations the optional function decorations
1189 /// @param return_type_decorations the optional function return type
1190 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001191 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001192 template <typename NAME>
1193 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001194 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001195 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001196 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001197 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001198 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001199 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1200 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001201 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001202 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001203 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001204 }
1205
Ben Clayton49668bb2021-04-17 05:32:01 +00001206 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001207 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001208 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1209
1210 /// Creates an ast::ReturnStatement with the given return value
1211 /// @param val the return value
1212 /// @returns the return statement pointer
1213 template <typename EXPR>
1214 ast::ReturnStatement* Return(EXPR&& val) {
1215 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001216 }
1217
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001218 /// Creates a ast::Struct and sem::StructType, registering the
1219 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001220 /// @param source the source information
1221 /// @param name the struct name
1222 /// @param members the struct members
1223 /// @param decorations the optional struct decorations
1224 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001225 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001226 sem::StructType* Structure(const Source& source,
1227 NAME&& name,
1228 ast::StructMemberList members,
1229 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001230 auto sym = Sym(std::forward<NAME>(name));
1231 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1232 std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001233 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001234 AST().AddConstructedType(type);
1235 return type;
1236 }
1237
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001238 /// Creates a ast::Struct and sem::StructType, registering the
1239 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001240 /// @param name the struct name
1241 /// @param members the struct members
1242 /// @param decorations the optional struct decorations
1243 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001244 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001245 sem::StructType* Structure(NAME&& name,
1246 ast::StructMemberList members,
1247 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001248 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001249 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001250 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001251 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001252 AST().AddConstructedType(type);
1253 return type;
1254 }
1255
Ben Clayton42d1e092021-02-02 14:29:15 +00001256 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001257 /// @param source the source information
1258 /// @param name the struct member name
1259 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001260 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001261 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001262 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001263 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001264 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001265 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001266 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001267 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1268 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001269 }
1270
Ben Clayton42d1e092021-02-02 14:29:15 +00001271 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001272 /// @param name the struct member name
1273 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001274 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001275 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001276 template <typename NAME>
1277 ast::StructMember* Member(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001278 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001279 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001280 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1281 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001282 }
1283
Ben Claytonbab31972021-02-08 21:16:21 +00001284 /// Creates a ast::StructMember with the given byte offset
1285 /// @param offset the offset to use in the StructMemberOffsetDecoration
1286 /// @param name the struct member name
1287 /// @param type the struct member type
1288 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001289 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001290 ast::StructMember* Member(uint32_t offset, NAME&& name, sem::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001291 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001292 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001293 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001294 create<ast::StructMemberOffsetDecoration>(offset),
1295 });
1296 }
1297
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001298 /// Creates a ast::BlockStatement with input statements
1299 /// @param statements statements of block
1300 /// @returns the block statement pointer
1301 template <typename... Statements>
1302 ast::BlockStatement* Block(Statements&&... statements) {
1303 return create<ast::BlockStatement>(
1304 ast::StatementList{std::forward<Statements>(statements)...});
1305 }
1306
1307 /// Creates a ast::ElseStatement with input condition and body
1308 /// @param condition the else condition expression
1309 /// @param body the else body
1310 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001311 template <typename CONDITION>
1312 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1313 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1314 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001315 }
1316
1317 /// Creates a ast::IfStatement with input condition, body, and optional
1318 /// variadic else statements
1319 /// @param condition the if statement condition expression
1320 /// @param body the if statement body
1321 /// @param elseStatements optional variadic else statements
1322 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001323 template <typename CONDITION, typename... ELSE_STATEMENTS>
1324 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001325 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001326 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001327 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001328 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001329 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001330 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001331 }
1332
1333 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001334 /// @param lhs the left hand side expression initializer
1335 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001336 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001337 template <typename LhsExpressionInit, typename RhsExpressionInit>
1338 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1339 RhsExpressionInit&& rhs) {
1340 return create<ast::AssignmentStatement>(
1341 Expr(std::forward<LhsExpressionInit>(lhs)),
1342 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001343 }
1344
1345 /// Creates a ast::LoopStatement with input body and optional continuing
1346 /// @param body the loop body
1347 /// @param continuing the optional continuing block
1348 /// @returns the loop statement pointer
1349 ast::LoopStatement* Loop(ast::BlockStatement* body,
1350 ast::BlockStatement* continuing = nullptr) {
1351 return create<ast::LoopStatement>(body, continuing);
1352 }
1353
1354 /// Creates a ast::VariableDeclStatement for the input variable
1355 /// @param var the variable to wrap in a decl statement
1356 /// @returns the variable decl statement pointer
1357 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1358 return create<ast::VariableDeclStatement>(var);
1359 }
1360
Antonio Maioranocea744d2021-03-25 12:55:27 +00001361 /// Creates a ast::SwitchStatement with input expression and cases
1362 /// @param condition the condition expression initializer
1363 /// @param cases case statements
1364 /// @returns the switch statement pointer
1365 template <typename ExpressionInit, typename... Cases>
1366 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1367 return create<ast::SwitchStatement>(
1368 Expr(std::forward<ExpressionInit>(condition)),
1369 ast::CaseStatementList{std::forward<Cases>(cases)...});
1370 }
1371
1372 /// Creates a ast::CaseStatement with input list of selectors, and body
1373 /// @param selectors list of selectors
1374 /// @param body the case body
1375 /// @returns the case statement pointer
1376 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1377 ast::BlockStatement* body = nullptr) {
1378 return create<ast::CaseStatement>(std::move(selectors),
1379 body ? body : Block());
1380 }
1381
1382 /// Convenient overload that takes a single selector
1383 /// @param selector a single case selector
1384 /// @param body the case body
1385 /// @returns the case statement pointer
1386 ast::CaseStatement* Case(ast::IntLiteral* selector,
1387 ast::BlockStatement* body = nullptr) {
1388 return Case(ast::CaseSelectorList{selector}, body);
1389 }
1390
1391 /// Convenience function that creates a 'default' ast::CaseStatement
1392 /// @param body the case body
1393 /// @returns the case statement pointer
1394 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1395 return Case(ast::CaseSelectorList{}, body);
1396 }
1397
James Price68f558f2021-04-06 15:51:47 +00001398 /// Creates an ast::BuiltinDecoration
1399 /// @param source the source information
1400 /// @param builtin the builtin value
1401 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001402 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001403 return create<ast::BuiltinDecoration>(source, builtin);
1404 }
1405
1406 /// Creates an ast::BuiltinDecoration
1407 /// @param builtin the builtin value
1408 /// @returns the builtin decoration pointer
1409 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1410 return create<ast::BuiltinDecoration>(source_, builtin);
1411 }
1412
1413 /// Creates an ast::LocationDecoration
1414 /// @param source the source information
1415 /// @param location the location value
1416 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001417 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001418 return create<ast::LocationDecoration>(source, location);
1419 }
1420
1421 /// Creates an ast::LocationDecoration
1422 /// @param location the location value
1423 /// @returns the location decoration pointer
1424 ast::LocationDecoration* Location(uint32_t location) {
1425 return create<ast::LocationDecoration>(source_, location);
1426 }
1427
1428 /// Creates an ast::StageDecoration
1429 /// @param source the source information
1430 /// @param stage the pipeline stage
1431 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001432 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001433 return create<ast::StageDecoration>(source, stage);
1434 }
1435
1436 /// Creates an ast::StageDecoration
1437 /// @param stage the pipeline stage
1438 /// @returns the stage decoration pointer
1439 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1440 return create<ast::StageDecoration>(source_, stage);
1441 }
1442
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001443 /// Sets the current builder source to `src`
1444 /// @param src the Source used for future create() calls
1445 void SetSource(const Source& src) {
1446 AssertNotMoved();
1447 source_ = src;
1448 }
1449
1450 /// Sets the current builder source to `loc`
1451 /// @param loc the Source used for future create() calls
1452 void SetSource(const Source::Location& loc) {
1453 AssertNotMoved();
1454 source_ = Source(loc);
1455 }
1456
Ben Clayton33352542021-01-29 16:43:41 +00001457 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001458 /// @note As the Resolver is run when the Program is built, this will only be
1459 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001460 /// @param expr the AST expression
1461 /// @return the resolved semantic type for the expression, or nullptr if the
1462 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001463 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001464
Ben Clayton169512e2021-04-17 05:52:11 +00001465 /// Wraps the ast::Literal in a statement. This is used by tests that
1466 /// construct a partial AST and require the Resolver to reach these
1467 /// nodes.
1468 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1469 /// @return the ast::Statement that wraps the ast::Statement
1470 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001471 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001472 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001473 /// nodes.
1474 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1475 /// @return the ast::Statement that wraps the ast::Expression
1476 ast::Statement* WrapInStatement(ast::Expression* expr);
1477 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001478 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001479 /// these nodes.
1480 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1481 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1482 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1483 /// Returns the statement argument. Used as a passthrough-overload by
1484 /// WrapInFunction().
1485 /// @param stmt the ast::Statement
1486 /// @return `stmt`
1487 ast::Statement* WrapInStatement(ast::Statement* stmt);
1488 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001489 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001490 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001491 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001492 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001493 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001494 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001495 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001496 }
1497 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001498 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001499 /// @returns the function
1500 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001501
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001502 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001503 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001504
1505 protected:
1506 /// Asserts that the builder has not been moved.
1507 void AssertNotMoved() const;
1508
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001509 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001510 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001511 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001512 ASTNodeAllocator ast_nodes_;
1513 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001514 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001515 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001516 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001517 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001518
1519 /// The source to use when creating AST nodes without providing a Source as
1520 /// the first argument.
1521 Source source_;
1522
Ben Clayton5f0ea112021-03-09 10:54:37 +00001523 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001524 /// program when built.
1525 bool resolve_on_build_ = true;
1526
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001527 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1528 bool moved_ = false;
1529};
1530
1531//! @cond Doxygen_Suppress
1532// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1533template <>
1534struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001535 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001536 return t->i32();
1537 }
1538};
1539template <>
1540struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001541 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001542 return t->u32();
1543 }
1544};
1545template <>
1546struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001547 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001548 return t->f32();
1549 }
1550};
1551template <>
1552struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001553 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001554 return t->bool_();
1555 }
1556};
1557template <>
1558struct ProgramBuilder::TypesBuilder::CToAST<void> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001559 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001560 return t->void_();
1561 }
1562};
1563//! @endcond
1564
Ben Clayton917b14b2021-04-19 16:50:23 +00001565/// @param builder the ProgramBuilder
1566/// @returns the ProgramID of the ProgramBuilder
1567inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1568 return builder->ID();
1569}
1570
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001571} // namespace tint
1572
1573#endif // SRC_PROGRAM_BUILDER_H_