blob: f6e00ba4d054ac9d8a23b48b15c6d577424f0644 [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
Ben Clayton43073d82021-04-22 13:50:53 +00001207 /// @param source the source information
1208 /// @returns the return statement pointer
1209 ast::ReturnStatement* Return(const Source& source) {
1210 return create<ast::ReturnStatement>(source);
1211 }
1212
1213 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001214 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001215 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1216
1217 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001218 /// @param source the source information
1219 /// @param val the return value
1220 /// @returns the return statement pointer
1221 template <typename EXPR>
1222 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1223 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1224 }
1225
1226 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001227 /// @param val the return value
1228 /// @returns the return statement pointer
1229 template <typename EXPR>
1230 ast::ReturnStatement* Return(EXPR&& val) {
1231 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001232 }
1233
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001234 /// Creates a ast::Struct and sem::StructType, registering the
1235 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001236 /// @param source the source information
1237 /// @param name the struct name
1238 /// @param members the struct members
1239 /// @param decorations the optional struct decorations
1240 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001241 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001242 sem::StructType* Structure(const Source& source,
1243 NAME&& name,
1244 ast::StructMemberList members,
1245 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001246 auto sym = Sym(std::forward<NAME>(name));
1247 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1248 std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001249 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001250 AST().AddConstructedType(type);
1251 return type;
1252 }
1253
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001254 /// Creates a ast::Struct and sem::StructType, registering the
1255 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001256 /// @param name the struct name
1257 /// @param members the struct members
1258 /// @param decorations the optional struct decorations
1259 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001260 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001261 sem::StructType* Structure(NAME&& name,
1262 ast::StructMemberList members,
1263 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001264 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001265 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001266 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001267 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001268 AST().AddConstructedType(type);
1269 return type;
1270 }
1271
Ben Clayton42d1e092021-02-02 14:29:15 +00001272 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001273 /// @param source the source information
1274 /// @param name the struct member name
1275 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001276 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001277 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001278 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001279 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001280 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001281 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001282 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001283 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1284 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001285 }
1286
Ben Clayton42d1e092021-02-02 14:29:15 +00001287 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001288 /// @param name the struct member name
1289 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001290 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001292 template <typename NAME>
1293 ast::StructMember* Member(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001294 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001295 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001296 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1297 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001298 }
1299
Ben Claytonbab31972021-02-08 21:16:21 +00001300 /// Creates a ast::StructMember with the given byte offset
1301 /// @param offset the offset to use in the StructMemberOffsetDecoration
1302 /// @param name the struct member name
1303 /// @param type the struct member type
1304 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001305 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001306 ast::StructMember* Member(uint32_t offset, NAME&& name, sem::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001307 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001308 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001309 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001310 create<ast::StructMemberOffsetDecoration>(offset),
1311 });
1312 }
1313
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001314 /// Creates a ast::BlockStatement with input statements
1315 /// @param statements statements of block
1316 /// @returns the block statement pointer
1317 template <typename... Statements>
1318 ast::BlockStatement* Block(Statements&&... statements) {
1319 return create<ast::BlockStatement>(
1320 ast::StatementList{std::forward<Statements>(statements)...});
1321 }
1322
1323 /// Creates a ast::ElseStatement with input condition and body
1324 /// @param condition the else condition expression
1325 /// @param body the else body
1326 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001327 template <typename CONDITION>
1328 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1329 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1330 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001331 }
1332
1333 /// Creates a ast::IfStatement with input condition, body, and optional
1334 /// variadic else statements
1335 /// @param condition the if statement condition expression
1336 /// @param body the if statement body
1337 /// @param elseStatements optional variadic else statements
1338 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001339 template <typename CONDITION, typename... ELSE_STATEMENTS>
1340 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001341 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001342 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001343 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001344 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001345 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001346 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001347 }
1348
1349 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001350 /// @param source the source information
1351 /// @param lhs the left hand side expression initializer
1352 /// @param rhs the right hand side expression initializer
1353 /// @returns the assignment statement pointer
1354 template <typename LhsExpressionInit, typename RhsExpressionInit>
1355 ast::AssignmentStatement* Assign(const Source& source,
1356 LhsExpressionInit&& lhs,
1357 RhsExpressionInit&& rhs) {
1358 return create<ast::AssignmentStatement>(
1359 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1360 Expr(std::forward<RhsExpressionInit>(rhs)));
1361 }
1362
1363 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001364 /// @param lhs the left hand side expression initializer
1365 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001366 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001367 template <typename LhsExpressionInit, typename RhsExpressionInit>
1368 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1369 RhsExpressionInit&& rhs) {
1370 return create<ast::AssignmentStatement>(
1371 Expr(std::forward<LhsExpressionInit>(lhs)),
1372 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001373 }
1374
1375 /// Creates a ast::LoopStatement with input body and optional continuing
1376 /// @param body the loop body
1377 /// @param continuing the optional continuing block
1378 /// @returns the loop statement pointer
1379 ast::LoopStatement* Loop(ast::BlockStatement* body,
1380 ast::BlockStatement* continuing = nullptr) {
1381 return create<ast::LoopStatement>(body, continuing);
1382 }
1383
1384 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001385 /// @param source the source information
1386 /// @param var the variable to wrap in a decl statement
1387 /// @returns the variable decl statement pointer
1388 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1389 return create<ast::VariableDeclStatement>(source, var);
1390 }
1391
1392 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001393 /// @param var the variable to wrap in a decl statement
1394 /// @returns the variable decl statement pointer
1395 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1396 return create<ast::VariableDeclStatement>(var);
1397 }
1398
Antonio Maioranocea744d2021-03-25 12:55:27 +00001399 /// Creates a ast::SwitchStatement with input expression and cases
1400 /// @param condition the condition expression initializer
1401 /// @param cases case statements
1402 /// @returns the switch statement pointer
1403 template <typename ExpressionInit, typename... Cases>
1404 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1405 return create<ast::SwitchStatement>(
1406 Expr(std::forward<ExpressionInit>(condition)),
1407 ast::CaseStatementList{std::forward<Cases>(cases)...});
1408 }
1409
1410 /// Creates a ast::CaseStatement with input list of selectors, and body
1411 /// @param selectors list of selectors
1412 /// @param body the case body
1413 /// @returns the case statement pointer
1414 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1415 ast::BlockStatement* body = nullptr) {
1416 return create<ast::CaseStatement>(std::move(selectors),
1417 body ? body : Block());
1418 }
1419
1420 /// Convenient overload that takes a single selector
1421 /// @param selector a single case selector
1422 /// @param body the case body
1423 /// @returns the case statement pointer
1424 ast::CaseStatement* Case(ast::IntLiteral* selector,
1425 ast::BlockStatement* body = nullptr) {
1426 return Case(ast::CaseSelectorList{selector}, body);
1427 }
1428
1429 /// Convenience function that creates a 'default' ast::CaseStatement
1430 /// @param body the case body
1431 /// @returns the case statement pointer
1432 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1433 return Case(ast::CaseSelectorList{}, body);
1434 }
1435
James Price68f558f2021-04-06 15:51:47 +00001436 /// Creates an ast::BuiltinDecoration
1437 /// @param source the source information
1438 /// @param builtin the builtin value
1439 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001440 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001441 return create<ast::BuiltinDecoration>(source, builtin);
1442 }
1443
1444 /// Creates an ast::BuiltinDecoration
1445 /// @param builtin the builtin value
1446 /// @returns the builtin decoration pointer
1447 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1448 return create<ast::BuiltinDecoration>(source_, builtin);
1449 }
1450
1451 /// Creates an ast::LocationDecoration
1452 /// @param source the source information
1453 /// @param location the location value
1454 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001455 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001456 return create<ast::LocationDecoration>(source, location);
1457 }
1458
1459 /// Creates an ast::LocationDecoration
1460 /// @param location the location value
1461 /// @returns the location decoration pointer
1462 ast::LocationDecoration* Location(uint32_t location) {
1463 return create<ast::LocationDecoration>(source_, location);
1464 }
1465
1466 /// Creates an ast::StageDecoration
1467 /// @param source the source information
1468 /// @param stage the pipeline stage
1469 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001470 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001471 return create<ast::StageDecoration>(source, stage);
1472 }
1473
1474 /// Creates an ast::StageDecoration
1475 /// @param stage the pipeline stage
1476 /// @returns the stage decoration pointer
1477 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1478 return create<ast::StageDecoration>(source_, stage);
1479 }
1480
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001481 /// Sets the current builder source to `src`
1482 /// @param src the Source used for future create() calls
1483 void SetSource(const Source& src) {
1484 AssertNotMoved();
1485 source_ = src;
1486 }
1487
1488 /// Sets the current builder source to `loc`
1489 /// @param loc the Source used for future create() calls
1490 void SetSource(const Source::Location& loc) {
1491 AssertNotMoved();
1492 source_ = Source(loc);
1493 }
1494
Ben Clayton33352542021-01-29 16:43:41 +00001495 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001496 /// @note As the Resolver is run when the Program is built, this will only be
1497 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001498 /// @param expr the AST expression
1499 /// @return the resolved semantic type for the expression, or nullptr if the
1500 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001501 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001502
Ben Clayton169512e2021-04-17 05:52:11 +00001503 /// Wraps the ast::Literal in a statement. This is used by tests that
1504 /// construct a partial AST and require the Resolver to reach these
1505 /// nodes.
1506 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1507 /// @return the ast::Statement that wraps the ast::Statement
1508 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001509 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001510 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001511 /// nodes.
1512 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1513 /// @return the ast::Statement that wraps the ast::Expression
1514 ast::Statement* WrapInStatement(ast::Expression* expr);
1515 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001516 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001517 /// these nodes.
1518 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1519 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1520 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1521 /// Returns the statement argument. Used as a passthrough-overload by
1522 /// WrapInFunction().
1523 /// @param stmt the ast::Statement
1524 /// @return `stmt`
1525 ast::Statement* WrapInStatement(ast::Statement* stmt);
1526 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001527 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001528 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001529 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001530 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001531 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001532 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001533 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001534 }
1535 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001536 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001537 /// @returns the function
1538 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001539
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001540 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001541 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001542
1543 protected:
1544 /// Asserts that the builder has not been moved.
1545 void AssertNotMoved() const;
1546
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001547 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001548 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001549 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001550 ASTNodeAllocator ast_nodes_;
1551 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001552 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001553 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001554 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001555 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001556
1557 /// The source to use when creating AST nodes without providing a Source as
1558 /// the first argument.
1559 Source source_;
1560
Ben Clayton5f0ea112021-03-09 10:54:37 +00001561 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001562 /// program when built.
1563 bool resolve_on_build_ = true;
1564
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001565 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1566 bool moved_ = false;
1567};
1568
1569//! @cond Doxygen_Suppress
1570// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1571template <>
1572struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001573 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001574 return t->i32();
1575 }
1576};
1577template <>
1578struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001579 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001580 return t->u32();
1581 }
1582};
1583template <>
1584struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001585 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001586 return t->f32();
1587 }
1588};
1589template <>
1590struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001591 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001592 return t->bool_();
1593 }
1594};
1595template <>
1596struct ProgramBuilder::TypesBuilder::CToAST<void> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001597 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001598 return t->void_();
1599 }
1600};
1601//! @endcond
1602
Ben Clayton917b14b2021-04-19 16:50:23 +00001603/// @param builder the ProgramBuilder
1604/// @returns the ProgramID of the ProgramBuilder
1605inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1606 return builder->ID();
1607}
1608
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001609} // namespace tint
1610
1611#endif // SRC_PROGRAM_BUILDER_H_