blob: 805dba38edcd396b742b90235f52be1e66375496 [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 Claytone30ffeb2021-04-22 14:24:43 +000048#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000049#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000050#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000051#include "src/program_id.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000052#include "src/sem/access_control_type.h"
53#include "src/sem/alias_type.h"
54#include "src/sem/array_type.h"
55#include "src/sem/bool_type.h"
56#include "src/sem/f32_type.h"
57#include "src/sem/i32_type.h"
58#include "src/sem/matrix_type.h"
59#include "src/sem/pointer_type.h"
60#include "src/sem/struct_type.h"
61#include "src/sem/u32_type.h"
62#include "src/sem/vector_type.h"
63#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000064#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000065
66namespace tint {
67
Ben Clayton401b96b2021-02-03 17:19:59 +000068// Forward declarations
69namespace ast {
70class VariableDeclStatement;
71} // namespace ast
72
Ben Claytona6b9a8e2021-01-26 16:57:10 +000073class CloneContext;
74
75/// ProgramBuilder is a mutable builder for a Program.
76/// To construct a Program, populate the builder and then `std::move` it to a
77/// Program.
78class ProgramBuilder {
79 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000080 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
81 using ASTNodeAllocator = BlockAllocator<ast::Node>;
82
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000083 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
84 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000085
86 /// `i32` is a type alias to `int`.
87 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
88 /// WGSL syntax.
89 /// Note: this is intentionally not aliased to uint32_t as we want integer
90 /// literals passed to the builder to match WGSL's integer literal types.
91 using i32 = decltype(1);
92 /// `u32` is a type alias to `unsigned int`.
93 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
94 /// WGSL syntax.
95 /// Note: this is intentionally not aliased to uint32_t as we want integer
96 /// literals passed to the builder to match WGSL's integer literal types.
97 using u32 = decltype(1u);
98 /// `f32` is a type alias to `float`
99 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
100 /// WGSL syntax.
101 using f32 = float;
102
103 /// Constructor
104 ProgramBuilder();
105
106 /// Move constructor
107 /// @param rhs the builder to move
108 ProgramBuilder(ProgramBuilder&& rhs);
109
110 /// Destructor
111 virtual ~ProgramBuilder();
112
113 /// Move assignment operator
114 /// @param rhs the builder to move
115 /// @return this builder
116 ProgramBuilder& operator=(ProgramBuilder&& rhs);
117
Ben Claytone43c8302021-01-29 11:59:32 +0000118 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
119 /// making a deep clone of the Program contents.
120 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
121 /// existing immutable program.
122 /// As the returned ProgramBuilder wraps `program`, `program` must not be
123 /// destructed or assigned while using the returned ProgramBuilder.
124 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
125 /// function. See crbug.com/tint/460.
126 /// @param program the immutable Program to wrap
127 /// @return the ProgramBuilder that wraps `program`
128 static ProgramBuilder Wrap(const Program* program);
129
Ben Claytone6995de2021-04-13 23:27:27 +0000130 /// @returns the unique identifier for this program
131 ProgramID ID() const { return id_; }
132
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000133 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000134 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000135 AssertNotMoved();
136 return types_;
137 }
138
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000139 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000140 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000141 AssertNotMoved();
142 return types_;
143 }
144
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000145 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000146 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000147 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000148 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000149 }
150
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000151 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000152 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000153 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000154 return ast_nodes_;
155 }
156
157 /// @returns a reference to the program's semantic nodes storage
158 SemNodeAllocator& SemNodes() {
159 AssertNotMoved();
160 return sem_nodes_;
161 }
162
163 /// @returns a reference to the program's semantic nodes storage
164 const SemNodeAllocator& SemNodes() const {
165 AssertNotMoved();
166 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 }
168
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000169 /// @returns a reference to the program's AST root Module
170 ast::Module& AST() {
171 AssertNotMoved();
172 return *ast_;
173 }
174
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000175 /// @returns a reference to the program's AST root Module
176 const ast::Module& AST() const {
177 AssertNotMoved();
178 return *ast_;
179 }
180
181 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000182 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000183 AssertNotMoved();
184 return sem_;
185 }
186
187 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000188 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000189 AssertNotMoved();
190 return sem_;
191 }
192
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000193 /// @returns a reference to the program's SymbolTable
194 SymbolTable& Symbols() {
195 AssertNotMoved();
196 return symbols_;
197 }
198
Ben Clayton708dc2d2021-01-29 11:22:40 +0000199 /// @returns a reference to the program's SymbolTable
200 const SymbolTable& Symbols() const {
201 AssertNotMoved();
202 return symbols_;
203 }
204
Ben Clayton844217f2021-01-27 18:49:05 +0000205 /// @returns a reference to the program's diagnostics
206 diag::List& Diagnostics() {
207 AssertNotMoved();
208 return diagnostics_;
209 }
210
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000211 /// @returns a reference to the program's diagnostics
212 const diag::List& Diagnostics() const {
213 AssertNotMoved();
214 return diagnostics_;
215 }
216
Ben Clayton5f0ea112021-03-09 10:54:37 +0000217 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000218 /// @param enable the new flag value (defaults to true)
219 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
220
Ben Clayton5f0ea112021-03-09 10:54:37 +0000221 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000222 /// built.
223 bool ResolveOnBuild() const { return resolve_on_build_; }
224
Ben Clayton844217f2021-01-27 18:49:05 +0000225 /// @returns true if the program has no error diagnostics and is not missing
226 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000227 bool IsValid() const;
228
Ben Clayton708dc2d2021-01-29 11:22:40 +0000229 /// Writes a representation of the node to the output stream
230 /// @note unlike str(), to_str() does not automatically demangle the string.
231 /// @param node the AST node
232 /// @param out the stream to write to
233 /// @param indent number of spaces to indent the node when writing
234 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
235 node->to_str(Sem(), out, indent);
236 }
237
238 /// Returns a demangled, string representation of `node`.
239 /// @param node the AST node
240 /// @returns a string representation of the node
241 std::string str(const ast::Node* node) const;
242
Ben Clayton7fdfff12021-01-29 15:17:30 +0000243 /// Creates a new ast::Node owned by the ProgramBuilder. When the
244 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000245 /// @param source the Source of the node
246 /// @param args the arguments to pass to the type constructor
247 /// @returns the node pointer
248 template <typename T, typename... ARGS>
249 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
250 ARGS&&... args) {
251 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000252 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000253 }
254
Ben Clayton7fdfff12021-01-29 15:17:30 +0000255 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
256 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000257 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000258 /// When the ProgramBuilder is destructed, the ast::Node will also be
259 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000260 /// @returns the node pointer
261 template <typename T>
262 traits::EnableIfIsType<T, ast::Node>* create() {
263 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000264 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000265 }
266
Ben Clayton7fdfff12021-01-29 15:17:30 +0000267 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
268 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000269 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000270 /// When the ProgramBuilder is destructed, the ast::Node will also be
271 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000272 /// @param arg0 the first arguments to pass to the type constructor
273 /// @param args the remaining arguments to pass to the type constructor
274 /// @returns the node pointer
275 template <typename T, typename ARG0, typename... ARGS>
276 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
277 traits::IsTypeOrDerived<T, ast::Node>::value &&
278 !traits::IsTypeOrDerived<ARG0, Source>::value,
279 T>*
280 create(ARG0&& arg0, ARGS&&... args) {
281 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000282 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000283 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000284 }
285
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000286 /// Creates a new sem::Node owned by the ProgramBuilder.
287 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000288 /// destructed.
289 /// @param args the arguments to pass to the type constructor
290 /// @returns the node pointer
291 template <typename T, typename... ARGS>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000292 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000293 AssertNotMoved();
294 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
295 }
296
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000297 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000298 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
299 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000300 /// Types are unique (de-aliased), and so calling create() for the same `T`
301 /// and arguments will return the same pointer.
302 /// @warning Use this method to acquire a type only if all of its type
303 /// information is provided in the constructor arguments `args`.<br>
304 /// If the type requires additional configuration after construction that
305 /// affect its fundamental type, build the type with `std::make_unique`, make
306 /// any necessary alterations and then call unique_type() instead.
307 /// @param args the arguments to pass to the type constructor
308 /// @returns the de-aliased type pointer
309 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000310 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
311 static_assert(std::is_base_of<sem::Type, T>::value,
312 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000313 AssertNotMoved();
314 return types_.Get<T>(std::forward<ARGS>(args)...);
315 }
316
317 /// Marks this builder as moved, preventing any further use of the builder.
318 void MarkAsMoved();
319
320 //////////////////////////////////////////////////////////////////////////////
321 // TypesBuilder
322 //////////////////////////////////////////////////////////////////////////////
323
324 /// TypesBuilder holds basic `tint` types and methods for constructing
325 /// complex types.
326 class TypesBuilder {
327 public:
328 /// Constructor
329 /// @param builder the program builder
330 explicit TypesBuilder(ProgramBuilder* builder);
331
332 /// @return the tint AST type for the C type `T`.
333 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000334 sem::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000335 return CToAST<T>::get(this);
336 }
337
338 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000339 typ::Bool bool_() const {
340 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
341 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000342
343 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000344 typ::F32 f32() const {
345 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
346 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000347
348 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000349 typ::I32 i32() const {
350 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
351 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000352
353 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000354 typ::U32 u32() const {
355 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
356 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000357
358 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000359 typ::Void void_() const {
360 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
361 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000362
Antonio Maiorano25436862021-04-13 13:32:33 +0000363 /// @param type vector subtype
364 /// @return the tint AST type for a 2-element vector of `type`.
Ben Claytone30ffeb2021-04-22 14:24:43 +0000365 typ::Vector vec2(typ::Type type) const {
366 return {builder->create<ast::Vector>(type, 2u),
367 builder->create<sem::Vector>(type, 2u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000368 }
369
370 /// @param type vector subtype
371 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone30ffeb2021-04-22 14:24:43 +0000372 typ::Vector vec3(typ::Type type) const {
373 return {builder->create<ast::Vector>(type, 3u),
374 builder->create<sem::Vector>(type, 3u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000375 }
376
377 /// @param type vector subtype
378 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone30ffeb2021-04-22 14:24:43 +0000379 typ::Vector vec4(typ::Type type) const {
380 return {builder->create<ast::Vector>(type, 4u),
381 builder->create<sem::Vector>(type, 4u)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000382 }
383
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000384 /// @return the tint AST type for a 2-element vector of the C type `T`.
385 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000386 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000387 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000388 }
389
390 /// @return the tint AST type for a 3-element vector of the C type `T`.
391 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000392 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000393 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000394 }
395
396 /// @return the tint AST type for a 4-element vector of the C type `T`.
397 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000398 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000399 return vec4(Of<T>());
400 }
401
402 /// @param type matrix subtype
403 /// @return the tint AST type for a 2x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000404 sem::Matrix* mat2x2(sem::Type* type) const {
405 return builder->create<sem::Matrix>(type, 2u, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000406 }
407
408 /// @param type matrix subtype
409 /// @return the tint AST type for a 2x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000410 sem::Matrix* mat2x3(sem::Type* type) const {
411 return builder->create<sem::Matrix>(type, 3u, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000412 }
413
414 /// @param type matrix subtype
415 /// @return the tint AST type for a 2x4 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000416 sem::Matrix* mat2x4(sem::Type* type) const {
417 return builder->create<sem::Matrix>(type, 4u, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000418 }
419
420 /// @param type matrix subtype
421 /// @return the tint AST type for a 3x2 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000422 sem::Matrix* mat3x2(sem::Type* type) const {
423 return builder->create<sem::Matrix>(type, 2u, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000424 }
425
426 /// @param type matrix subtype
427 /// @return the tint AST type for a 3x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000428 sem::Matrix* mat3x3(sem::Type* type) const {
429 return builder->create<sem::Matrix>(type, 3u, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000430 }
431
432 /// @param type matrix subtype
433 /// @return the tint AST type for a 3x4 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000434 sem::Matrix* mat3x4(sem::Type* type) const {
435 return builder->create<sem::Matrix>(type, 4u, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000436 }
437
438 /// @param type matrix subtype
439 /// @return the tint AST type for a 4x2 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000440 sem::Matrix* mat4x2(sem::Type* type) const {
441 return builder->create<sem::Matrix>(type, 2u, 4u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000442 }
443
444 /// @param type matrix subtype
445 /// @return the tint AST type for a 4x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000446 sem::Matrix* mat4x3(sem::Type* type) const {
447 return builder->create<sem::Matrix>(type, 3u, 4u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000448 }
449
450 /// @param type matrix subtype
451 /// @return the tint AST type for a 4x4 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000452 sem::Matrix* mat4x4(sem::Type* type) const {
453 return builder->create<sem::Matrix>(type, 4u, 4u);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000454 }
455
456 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
457 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000458 sem::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000459 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000460 }
461
462 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
463 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000464 sem::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000465 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000466 }
467
468 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
469 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000470 sem::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000471 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000472 }
473
474 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
475 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000476 sem::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000477 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000478 }
479
480 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
481 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000482 sem::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000483 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000484 }
485
486 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
487 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000488 sem::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000489 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000490 }
491
492 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
493 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000494 sem::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000495 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000496 }
497
498 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
499 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000500 sem::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000501 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000502 }
503
504 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
505 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000506 sem::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000507 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000508 }
509
510 /// @param subtype the array element type
511 /// @param n the array size. 0 represents a runtime-array.
512 /// @return the tint AST type for a array of size `n` of type `T`
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000513 sem::ArrayType* array(sem::Type* subtype, uint32_t n = 0) const {
514 return builder->create<sem::ArrayType>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000515 }
516
Ben Claytonbab31972021-02-08 21:16:21 +0000517 /// @param subtype the array element type
518 /// @param n the array size. 0 represents a runtime-array.
519 /// @param stride the array stride.
520 /// @return the tint AST type for a array of size `n` of type `T`
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000521 sem::ArrayType* array(sem::Type* subtype,
522 uint32_t n,
523 uint32_t stride) const {
524 return builder->create<sem::ArrayType>(
Ben Claytonbab31972021-02-08 21:16:21 +0000525 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000526 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000527 builder->create<ast::StrideDecoration>(stride),
528 });
529 }
530
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000531 /// @return the tint AST type for an array of size `N` of type `T`
532 template <typename T, int N = 0>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000533 sem::ArrayType* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000534 return array(Of<T>(), N);
535 }
536
Ben Claytonbab31972021-02-08 21:16:21 +0000537 /// @param stride the array stride
538 /// @return the tint AST type for an array of size `N` of type `T`
539 template <typename T, int N = 0>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000540 sem::ArrayType* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000541 return array(Of<T>(), N, stride);
542 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000543
Ben Clayton42d1e092021-02-02 14:29:15 +0000544 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000545 /// @param name the alias name
546 /// @param type the alias type
547 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000548 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000549 sem::Alias* alias(NAME&& name, sem::Type* type) const {
550 return builder->create<sem::Alias>(builder->Sym(std::forward<NAME>(name)),
551 type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000552 }
553
Ben Clayton4db10dd2021-04-16 08:47:14 +0000554 /// Creates an access control qualifier type
555 /// @param access the access control
556 /// @param type the inner type
557 /// @returns the access control qualifier type
Ben Clayton8a8d26b2021-04-20 15:04:21 +0000558 sem::AccessControl* access(ast::AccessControl::Access access,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000559 sem::Type* type) const {
560 return builder->create<sem::AccessControl>(access, type);
Ben Clayton4db10dd2021-04-16 08:47:14 +0000561 }
562
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000563 /// @return the tint AST pointer to `type` with the given ast::StorageClass
564 /// @param type the type of the pointer
565 /// @param storage_class the storage class of the pointer
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000566 sem::Pointer* pointer(sem::Type* type,
567 ast::StorageClass storage_class) const {
568 return builder->create<sem::Pointer>(type, storage_class);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000569 }
570
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 /// @return the tint AST pointer to type `T` with the given
572 /// ast::StorageClass.
573 /// @param storage_class the storage class of the pointer
574 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000575 sem::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000576 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 }
578
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 /// @param impl the struct implementation
580 /// @returns a struct pointer
Ben Clayton913a2f42021-04-20 15:21:21 +0000581 sem::StructType* struct_(ast::Struct* impl) const {
582 return builder->create<sem::StructType>(impl);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000583 }
584
585 private:
586 /// CToAST<T> is specialized for various `T` types and each specialization
587 /// contains a single static `get()` method for obtaining the corresponding
588 /// AST type for the C type `T`.
589 /// `get()` has the signature:
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000590 /// `static sem::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000591 template <typename T>
592 struct CToAST {};
593
Ben Claytonc7ca7662021-02-17 16:23:52 +0000594 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000595 };
596
597 //////////////////////////////////////////////////////////////////////////////
598 // AST helper methods
599 //////////////////////////////////////////////////////////////////////////////
600
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000601 /// @param name the symbol string
602 /// @return a Symbol with the given name
603 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
604
605 /// @param sym the symbol
606 /// @return `sym`
607 Symbol Sym(Symbol sym) { return sym; }
608
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000609 /// @param expr the expression
610 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000611 template <typename T>
612 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
613 return expr;
614 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000615
Ben Claytonb8ea5912021-04-19 16:52:42 +0000616 /// Passthrough for nullptr
617 /// @return nullptr
618 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
619
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000620 /// @param name the identifier name
621 /// @return an ast::IdentifierExpression with the given name
622 ast::IdentifierExpression* Expr(const std::string& name) {
623 return create<ast::IdentifierExpression>(Symbols().Register(name));
624 }
625
Ben Clayton46d78d72021-02-10 21:34:26 +0000626 /// @param symbol the identifier symbol
627 /// @return an ast::IdentifierExpression with the given symbol
628 ast::IdentifierExpression* Expr(Symbol symbol) {
629 return create<ast::IdentifierExpression>(symbol);
630 }
631
Ben Claytonb8ea5912021-04-19 16:52:42 +0000632 /// @param variable the AST variable
633 /// @return an ast::IdentifierExpression with the variable's symbol
634 ast::IdentifierExpression* Expr(ast::Variable* variable) {
635 return create<ast::IdentifierExpression>(variable->symbol());
636 }
637
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000638 /// @param source the source information
639 /// @param name the identifier name
640 /// @return an ast::IdentifierExpression with the given name
641 ast::IdentifierExpression* Expr(const Source& source,
642 const std::string& name) {
643 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
644 }
645
646 /// @param name the identifier name
647 /// @return an ast::IdentifierExpression with the given name
648 ast::IdentifierExpression* Expr(const char* name) {
649 return create<ast::IdentifierExpression>(Symbols().Register(name));
650 }
651
652 /// @param value the boolean value
653 /// @return a Scalar constructor for the given value
654 ast::ScalarConstructorExpression* Expr(bool value) {
655 return create<ast::ScalarConstructorExpression>(Literal(value));
656 }
657
658 /// @param value the float value
659 /// @return a Scalar constructor for the given value
660 ast::ScalarConstructorExpression* Expr(f32 value) {
661 return create<ast::ScalarConstructorExpression>(Literal(value));
662 }
663
664 /// @param value the integer value
665 /// @return a Scalar constructor for the given value
666 ast::ScalarConstructorExpression* Expr(i32 value) {
667 return create<ast::ScalarConstructorExpression>(Literal(value));
668 }
669
670 /// @param value the unsigned int value
671 /// @return a Scalar constructor for the given value
672 ast::ScalarConstructorExpression* Expr(u32 value) {
673 return create<ast::ScalarConstructorExpression>(Literal(value));
674 }
675
676 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
677 /// `list`.
678 /// @param list the list to append too
679 /// @param arg the arg to create
680 template <typename ARG>
681 void Append(ast::ExpressionList& list, ARG&& arg) {
682 list.emplace_back(Expr(std::forward<ARG>(arg)));
683 }
684
685 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
686 /// then appends them to `list`.
687 /// @param list the list to append too
688 /// @param arg0 the first argument
689 /// @param args the rest of the arguments
690 template <typename ARG0, typename... ARGS>
691 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
692 Append(list, std::forward<ARG0>(arg0));
693 Append(list, std::forward<ARGS>(args)...);
694 }
695
696 /// @return an empty list of expressions
697 ast::ExpressionList ExprList() { return {}; }
698
699 /// @param args the list of expressions
700 /// @return the list of expressions converted to `ast::Expression`s using
701 /// `Expr()`,
702 template <typename... ARGS>
703 ast::ExpressionList ExprList(ARGS&&... args) {
704 ast::ExpressionList list;
705 list.reserve(sizeof...(args));
706 Append(list, std::forward<ARGS>(args)...);
707 return list;
708 }
709
710 /// @param list the list of expressions
711 /// @return `list`
712 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
713
714 /// @param val the boolan value
715 /// @return a boolean literal with the given value
716 ast::BoolLiteral* Literal(bool val) {
717 return create<ast::BoolLiteral>(ty.bool_(), val);
718 }
719
720 /// @param val the float value
721 /// @return a float literal with the given value
722 ast::FloatLiteral* Literal(f32 val) {
723 return create<ast::FloatLiteral>(ty.f32(), val);
724 }
725
726 /// @param val the unsigned int value
727 /// @return a ast::UintLiteral with the given value
728 ast::UintLiteral* Literal(u32 val) {
729 return create<ast::UintLiteral>(ty.u32(), val);
730 }
731
732 /// @param val the integer value
733 /// @return the ast::SintLiteral with the given value
734 ast::SintLiteral* Literal(i32 val) {
735 return create<ast::SintLiteral>(ty.i32(), val);
736 }
737
738 /// @param args the arguments for the type constructor
739 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
740 /// of `args` converted to `ast::Expression`s using `Expr()`
741 template <typename T, typename... ARGS>
742 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
743 return create<ast::TypeConstructorExpression>(
744 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
745 }
746
747 /// @param type the type to construct
748 /// @param args the arguments for the constructor
749 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
750 /// values `args`.
751 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000752 ast::TypeConstructorExpression* Construct(sem::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000753 return create<ast::TypeConstructorExpression>(
754 type, ExprList(std::forward<ARGS>(args)...));
755 }
756
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000757 /// Creates a constructor expression that constructs an object of
758 /// `type` filled with `elem_value`. For example,
759 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
760 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
761 /// @param type the type to construct
762 /// @param elem_value the initial or element value (for vec and mat) to
763 /// construct with
764 /// @return the constructor expression
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000765 ast::ConstructorExpression* ConstructValueFilledWith(sem::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000766 int elem_value = 0);
767
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000768 /// @param args the arguments for the vector constructor
769 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
770 /// `T`, constructed with the values `args`.
771 template <typename T, typename... ARGS>
772 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
773 return create<ast::TypeConstructorExpression>(
774 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
775 }
776
777 /// @param args the arguments for the vector constructor
778 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
779 /// `T`, constructed with the values `args`.
780 template <typename T, typename... ARGS>
781 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
782 return create<ast::TypeConstructorExpression>(
783 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
784 }
785
786 /// @param args the arguments for the vector constructor
787 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
788 /// `T`, constructed with the values `args`.
789 template <typename T, typename... ARGS>
790 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
791 return create<ast::TypeConstructorExpression>(
792 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
793 }
794
795 /// @param args the arguments for the matrix constructor
796 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
797 /// `T`, constructed with the values `args`.
798 template <typename T, typename... ARGS>
799 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
800 return create<ast::TypeConstructorExpression>(
801 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
802 }
803
804 /// @param args the arguments for the matrix constructor
805 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
806 /// `T`, constructed with the values `args`.
807 template <typename T, typename... ARGS>
808 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
809 return create<ast::TypeConstructorExpression>(
810 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
811 }
812
813 /// @param args the arguments for the matrix constructor
814 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
815 /// `T`, constructed with the values `args`.
816 template <typename T, typename... ARGS>
817 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
818 return create<ast::TypeConstructorExpression>(
819 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
820 }
821
822 /// @param args the arguments for the matrix constructor
823 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
824 /// `T`, constructed with the values `args`.
825 template <typename T, typename... ARGS>
826 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
827 return create<ast::TypeConstructorExpression>(
828 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
829 }
830
831 /// @param args the arguments for the matrix constructor
832 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
833 /// `T`, constructed with the values `args`.
834 template <typename T, typename... ARGS>
835 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
836 return create<ast::TypeConstructorExpression>(
837 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
838 }
839
840 /// @param args the arguments for the matrix constructor
841 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
842 /// `T`, constructed with the values `args`.
843 template <typename T, typename... ARGS>
844 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
845 return create<ast::TypeConstructorExpression>(
846 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
847 }
848
849 /// @param args the arguments for the matrix constructor
850 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
851 /// `T`, constructed with the values `args`.
852 template <typename T, typename... ARGS>
853 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
854 return create<ast::TypeConstructorExpression>(
855 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
856 }
857
858 /// @param args the arguments for the matrix constructor
859 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
860 /// `T`, constructed with the values `args`.
861 template <typename T, typename... ARGS>
862 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
863 return create<ast::TypeConstructorExpression>(
864 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
865 }
866
867 /// @param args the arguments for the matrix constructor
868 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
869 /// `T`, constructed with the values `args`.
870 template <typename T, typename... ARGS>
871 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
872 return create<ast::TypeConstructorExpression>(
873 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
874 }
875
876 /// @param args the arguments for the array constructor
877 /// @return an `ast::TypeConstructorExpression` of an array with element type
878 /// `T`, constructed with the values `args`.
879 template <typename T, int N = 0, typename... ARGS>
880 ast::TypeConstructorExpression* array(ARGS&&... args) {
881 return create<ast::TypeConstructorExpression>(
882 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
883 }
884
885 /// @param subtype the array element type
886 /// @param n the array size. 0 represents a runtime-array.
887 /// @param args the arguments for the array constructor
888 /// @return an `ast::TypeConstructorExpression` of an array with element type
889 /// `subtype`, constructed with the values `args`.
890 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000891 ast::TypeConstructorExpression* array(sem::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000892 uint32_t n,
893 ARGS&&... args) {
894 return create<ast::TypeConstructorExpression>(
895 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
896 }
897
898 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000900 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000901 /// @param constructor constructor expression
902 /// @param decorations variable decorations
903 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000904 template <typename NAME>
905 ast::Variable* Var(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000906 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000907 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000908 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000909 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000910 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
911 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000912 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000913
914 /// @param source the variable source
915 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000916 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000917 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000918 /// @param constructor constructor expression
919 /// @param decorations variable decorations
920 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000921 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000922 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000923 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000924 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000925 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000926 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000927 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000928 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000929 type, false, constructor, decorations);
930 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000931
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000933 /// @param type the variable type
934 /// @param constructor optional constructor expression
935 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000936 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000937 template <typename NAME>
938 ast::Variable* Const(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000939 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000940 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000941 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000942 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000943 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000944 constructor, decorations);
945 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000946
947 /// @param source the variable source
948 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000949 /// @param type the variable type
950 /// @param constructor optional constructor expression
951 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000952 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000953 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000954 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000955 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000956 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000957 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000958 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000959 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000960 ast::StorageClass::kNone, type, true,
961 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000962 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000963
James Pricedaf1f1c2021-04-08 22:15:48 +0000964 /// @param name the parameter name
965 /// @param type the parameter type
966 /// @param decorations optional parameter decorations
967 /// @returns a constant `ast::Variable` with the given name and type
968 template <typename NAME>
969 ast::Variable* Param(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000970 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000971 ast::DecorationList decorations = {}) {
972 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
973 ast::StorageClass::kNone, type, true, nullptr,
974 decorations);
975 }
976
977 /// @param source the parameter source
978 /// @param name the parameter name
979 /// @param type the parameter type
980 /// @param decorations optional parameter decorations
981 /// @returns a constant `ast::Variable` with the given name and type
982 template <typename NAME>
983 ast::Variable* Param(const Source& source,
984 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000985 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000986 ast::DecorationList decorations = {}) {
987 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
988 ast::StorageClass::kNone, type, true, nullptr,
989 decorations);
990 }
991
Ben Clayton42708342021-04-21 17:55:12 +0000992 /// @param name the variable name
993 /// @param type the variable type
994 /// @param storage the variable storage class
995 /// @param constructor constructor expression
996 /// @param decorations variable decorations
997 /// @returns a new `ast::Variable`, which is automatically registered as a
998 /// global variable with the ast::Module.
999 template <typename NAME>
1000 ast::Variable* Global(NAME&& name,
1001 sem::Type* type,
1002 ast::StorageClass storage,
1003 ast::Expression* constructor = nullptr,
1004 ast::DecorationList decorations = {}) {
1005 auto* var =
1006 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1007 AST().AddGlobalVariable(var);
1008 return var;
1009 }
1010
1011 /// @param source the variable source
1012 /// @param name the variable name
1013 /// @param type the variable type
1014 /// @param storage the variable storage class
1015 /// @param constructor constructor expression
1016 /// @param decorations variable decorations
1017 /// @returns a new `ast::Variable`, which is automatically registered as a
1018 /// global variable with the ast::Module.
1019 template <typename NAME>
1020 ast::Variable* Global(const Source& source,
1021 NAME&& name,
1022 sem::Type* type,
1023 ast::StorageClass storage,
1024 ast::Expression* constructor = nullptr,
1025 ast::DecorationList decorations = {}) {
1026 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1027 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001028 AST().AddGlobalVariable(var);
1029 return var;
1030 }
1031
1032 /// @param args the arguments to pass to Const()
1033 /// @returns a const `ast::Variable` constructed by calling Var() with the
1034 /// arguments of `args`, which is automatically registered as a global
1035 /// variable with the ast::Module.
1036 template <typename... ARGS>
1037 ast::Variable* GlobalConst(ARGS&&... args) {
1038 auto* var = Const(std::forward<ARGS>(args)...);
1039 AST().AddGlobalVariable(var);
1040 return var;
1041 }
1042
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001043 /// @param func the function name
1044 /// @param args the function call arguments
1045 /// @returns a `ast::CallExpression` to the function `func`, with the
1046 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1047 template <typename NAME, typename... ARGS>
1048 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1049 return create<ast::CallExpression>(Expr(func),
1050 ExprList(std::forward<ARGS>(args)...));
1051 }
1052
1053 /// @param lhs the left hand argument to the addition operation
1054 /// @param rhs the right hand argument to the addition operation
1055 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1056 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001057 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001058 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1059 Expr(std::forward<LHS>(lhs)),
1060 Expr(std::forward<RHS>(rhs)));
1061 }
1062
1063 /// @param lhs the left hand argument to the subtraction operation
1064 /// @param rhs the right hand argument to the subtraction operation
1065 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1066 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001067 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001068 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1069 Expr(std::forward<LHS>(lhs)),
1070 Expr(std::forward<RHS>(rhs)));
1071 }
1072
1073 /// @param lhs the left hand argument to the multiplication operation
1074 /// @param rhs the right hand argument to the multiplication operation
1075 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1076 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001077 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1079 Expr(std::forward<LHS>(lhs)),
1080 Expr(std::forward<RHS>(rhs)));
1081 }
1082
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001083 /// @param source the source information
1084 /// @param lhs the left hand argument to the multiplication operation
1085 /// @param rhs the right hand argument to the multiplication operation
1086 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1087 template <typename LHS, typename RHS>
1088 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1089 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1090 Expr(std::forward<LHS>(lhs)),
1091 Expr(std::forward<RHS>(rhs)));
1092 }
1093
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001094 /// @param lhs the left hand argument to the division operation
1095 /// @param rhs the right hand argument to the division operation
1096 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1097 template <typename LHS, typename RHS>
1098 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1099 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1100 Expr(std::forward<LHS>(lhs)),
1101 Expr(std::forward<RHS>(rhs)));
1102 }
1103
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001104 /// @param arr the array argument for the array accessor expression
1105 /// @param idx the index argument for the array accessor expression
1106 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1107 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001108 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001109 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1110 Expr(std::forward<IDX>(idx)));
1111 }
1112
1113 /// @param obj the object for the member accessor expression
1114 /// @param idx the index argument for the array accessor expression
1115 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1116 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001117 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001118 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1119 Expr(std::forward<IDX>(idx)));
1120 }
1121
Ben Clayton42d1e092021-02-02 14:29:15 +00001122 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001123 /// @param val the offset value
1124 /// @returns the offset decoration pointer
1125 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1126 return create<ast::StructMemberOffsetDecoration>(source_, val);
1127 }
1128
Ben Claytond614dd52021-03-15 10:43:11 +00001129 /// Creates a ast::StructMemberSizeDecoration
1130 /// @param source the source information
1131 /// @param val the size value
1132 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001133 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1134 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001135 return create<ast::StructMemberSizeDecoration>(source, val);
1136 }
1137
1138 /// Creates a ast::StructMemberSizeDecoration
1139 /// @param val the size value
1140 /// @returns the size decoration pointer
1141 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1142 return create<ast::StructMemberSizeDecoration>(source_, val);
1143 }
1144
1145 /// Creates a ast::StructMemberAlignDecoration
1146 /// @param source the source information
1147 /// @param val the align value
1148 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001149 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1150 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001151 return create<ast::StructMemberAlignDecoration>(source, val);
1152 }
1153
1154 /// Creates a ast::StructMemberAlignDecoration
1155 /// @param val the align value
1156 /// @returns the align decoration pointer
1157 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1158 return create<ast::StructMemberAlignDecoration>(source_, val);
1159 }
1160
Ben Clayton42d1e092021-02-02 14:29:15 +00001161 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001162 /// @param source the source information
1163 /// @param name the function name
1164 /// @param params the function parameters
1165 /// @param type the function return type
1166 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001167 /// @param decorations the optional function decorations
1168 /// @param return_type_decorations the optional function return type
1169 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001170 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001171 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001172 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001173 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001174 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001175 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001176 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001177 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001178 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001179 auto* func =
1180 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1181 type, create<ast::BlockStatement>(body),
1182 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001183 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001184 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001185 }
1186
Ben Clayton42d1e092021-02-02 14:29:15 +00001187 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001188 /// @param name the function name
1189 /// @param params the function parameters
1190 /// @param type the function return type
1191 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001192 /// @param decorations the optional function decorations
1193 /// @param return_type_decorations the optional function return type
1194 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001195 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001196 template <typename NAME>
1197 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001198 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001199 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001200 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001201 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001202 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001203 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1204 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001205 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001206 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001207 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001208 }
1209
Ben Clayton49668bb2021-04-17 05:32:01 +00001210 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001211 /// @param source the source information
1212 /// @returns the return statement pointer
1213 ast::ReturnStatement* Return(const Source& source) {
1214 return create<ast::ReturnStatement>(source);
1215 }
1216
1217 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001218 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001219 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1220
1221 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001222 /// @param source the source information
1223 /// @param val the return value
1224 /// @returns the return statement pointer
1225 template <typename EXPR>
1226 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1227 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1228 }
1229
1230 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001231 /// @param val the return value
1232 /// @returns the return statement pointer
1233 template <typename EXPR>
1234 ast::ReturnStatement* Return(EXPR&& val) {
1235 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001236 }
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 source the source information
1241 /// @param name the struct name
1242 /// @param members the struct members
1243 /// @param decorations the optional struct decorations
1244 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001245 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001246 sem::StructType* Structure(const Source& source,
1247 NAME&& name,
1248 ast::StructMemberList members,
1249 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001250 auto sym = Sym(std::forward<NAME>(name));
1251 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1252 std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001253 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001254 AST().AddConstructedType(type);
1255 return type;
1256 }
1257
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001258 /// Creates a ast::Struct and sem::StructType, registering the
1259 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001260 /// @param name the struct name
1261 /// @param members the struct members
1262 /// @param decorations the optional struct decorations
1263 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001264 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001265 sem::StructType* Structure(NAME&& name,
1266 ast::StructMemberList members,
1267 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001268 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001269 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001270 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001271 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001272 AST().AddConstructedType(type);
1273 return type;
1274 }
1275
Ben Clayton42d1e092021-02-02 14:29:15 +00001276 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001277 /// @param source the source information
1278 /// @param name the struct member name
1279 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001280 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001281 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001282 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001283 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001284 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001285 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001286 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001287 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1288 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001289 }
1290
Ben Clayton42d1e092021-02-02 14:29:15 +00001291 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001292 /// @param name the struct member name
1293 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001294 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001295 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001296 template <typename NAME>
1297 ast::StructMember* Member(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001298 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001299 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001300 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1301 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001302 }
1303
Ben Claytonbab31972021-02-08 21:16:21 +00001304 /// Creates a ast::StructMember with the given byte offset
1305 /// @param offset the offset to use in the StructMemberOffsetDecoration
1306 /// @param name the struct member name
1307 /// @param type the struct member type
1308 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001309 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001310 ast::StructMember* Member(uint32_t offset, NAME&& name, sem::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001311 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001312 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001313 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001314 create<ast::StructMemberOffsetDecoration>(offset),
1315 });
1316 }
1317
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001318 /// Creates a ast::BlockStatement with input statements
1319 /// @param statements statements of block
1320 /// @returns the block statement pointer
1321 template <typename... Statements>
1322 ast::BlockStatement* Block(Statements&&... statements) {
1323 return create<ast::BlockStatement>(
1324 ast::StatementList{std::forward<Statements>(statements)...});
1325 }
1326
1327 /// Creates a ast::ElseStatement with input condition and body
1328 /// @param condition the else condition expression
1329 /// @param body the else body
1330 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001331 template <typename CONDITION>
1332 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1333 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1334 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001335 }
1336
1337 /// Creates a ast::IfStatement with input condition, body, and optional
1338 /// variadic else statements
1339 /// @param condition the if statement condition expression
1340 /// @param body the if statement body
1341 /// @param elseStatements optional variadic else statements
1342 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001343 template <typename CONDITION, typename... ELSE_STATEMENTS>
1344 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001345 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001346 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001347 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001348 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001349 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001350 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001351 }
1352
1353 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001354 /// @param source the source information
1355 /// @param lhs the left hand side expression initializer
1356 /// @param rhs the right hand side expression initializer
1357 /// @returns the assignment statement pointer
1358 template <typename LhsExpressionInit, typename RhsExpressionInit>
1359 ast::AssignmentStatement* Assign(const Source& source,
1360 LhsExpressionInit&& lhs,
1361 RhsExpressionInit&& rhs) {
1362 return create<ast::AssignmentStatement>(
1363 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1364 Expr(std::forward<RhsExpressionInit>(rhs)));
1365 }
1366
1367 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001368 /// @param lhs the left hand side expression initializer
1369 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001370 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001371 template <typename LhsExpressionInit, typename RhsExpressionInit>
1372 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1373 RhsExpressionInit&& rhs) {
1374 return create<ast::AssignmentStatement>(
1375 Expr(std::forward<LhsExpressionInit>(lhs)),
1376 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001377 }
1378
1379 /// Creates a ast::LoopStatement with input body and optional continuing
1380 /// @param body the loop body
1381 /// @param continuing the optional continuing block
1382 /// @returns the loop statement pointer
1383 ast::LoopStatement* Loop(ast::BlockStatement* body,
1384 ast::BlockStatement* continuing = nullptr) {
1385 return create<ast::LoopStatement>(body, continuing);
1386 }
1387
1388 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001389 /// @param source the source information
1390 /// @param var the variable to wrap in a decl statement
1391 /// @returns the variable decl statement pointer
1392 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1393 return create<ast::VariableDeclStatement>(source, var);
1394 }
1395
1396 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001397 /// @param var the variable to wrap in a decl statement
1398 /// @returns the variable decl statement pointer
1399 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1400 return create<ast::VariableDeclStatement>(var);
1401 }
1402
Antonio Maioranocea744d2021-03-25 12:55:27 +00001403 /// Creates a ast::SwitchStatement with input expression and cases
1404 /// @param condition the condition expression initializer
1405 /// @param cases case statements
1406 /// @returns the switch statement pointer
1407 template <typename ExpressionInit, typename... Cases>
1408 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1409 return create<ast::SwitchStatement>(
1410 Expr(std::forward<ExpressionInit>(condition)),
1411 ast::CaseStatementList{std::forward<Cases>(cases)...});
1412 }
1413
1414 /// Creates a ast::CaseStatement with input list of selectors, and body
1415 /// @param selectors list of selectors
1416 /// @param body the case body
1417 /// @returns the case statement pointer
1418 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1419 ast::BlockStatement* body = nullptr) {
1420 return create<ast::CaseStatement>(std::move(selectors),
1421 body ? body : Block());
1422 }
1423
1424 /// Convenient overload that takes a single selector
1425 /// @param selector a single case selector
1426 /// @param body the case body
1427 /// @returns the case statement pointer
1428 ast::CaseStatement* Case(ast::IntLiteral* selector,
1429 ast::BlockStatement* body = nullptr) {
1430 return Case(ast::CaseSelectorList{selector}, body);
1431 }
1432
1433 /// Convenience function that creates a 'default' ast::CaseStatement
1434 /// @param body the case body
1435 /// @returns the case statement pointer
1436 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1437 return Case(ast::CaseSelectorList{}, body);
1438 }
1439
James Price68f558f2021-04-06 15:51:47 +00001440 /// Creates an ast::BuiltinDecoration
1441 /// @param source the source information
1442 /// @param builtin the builtin value
1443 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001444 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001445 return create<ast::BuiltinDecoration>(source, builtin);
1446 }
1447
1448 /// Creates an ast::BuiltinDecoration
1449 /// @param builtin the builtin value
1450 /// @returns the builtin decoration pointer
1451 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1452 return create<ast::BuiltinDecoration>(source_, builtin);
1453 }
1454
1455 /// Creates an ast::LocationDecoration
1456 /// @param source the source information
1457 /// @param location the location value
1458 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001459 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001460 return create<ast::LocationDecoration>(source, location);
1461 }
1462
1463 /// Creates an ast::LocationDecoration
1464 /// @param location the location value
1465 /// @returns the location decoration pointer
1466 ast::LocationDecoration* Location(uint32_t location) {
1467 return create<ast::LocationDecoration>(source_, location);
1468 }
1469
1470 /// Creates an ast::StageDecoration
1471 /// @param source the source information
1472 /// @param stage the pipeline stage
1473 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001474 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001475 return create<ast::StageDecoration>(source, stage);
1476 }
1477
1478 /// Creates an ast::StageDecoration
1479 /// @param stage the pipeline stage
1480 /// @returns the stage decoration pointer
1481 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1482 return create<ast::StageDecoration>(source_, stage);
1483 }
1484
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001485 /// Sets the current builder source to `src`
1486 /// @param src the Source used for future create() calls
1487 void SetSource(const Source& src) {
1488 AssertNotMoved();
1489 source_ = src;
1490 }
1491
1492 /// Sets the current builder source to `loc`
1493 /// @param loc the Source used for future create() calls
1494 void SetSource(const Source::Location& loc) {
1495 AssertNotMoved();
1496 source_ = Source(loc);
1497 }
1498
Ben Clayton33352542021-01-29 16:43:41 +00001499 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001500 /// @note As the Resolver is run when the Program is built, this will only be
1501 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001502 /// @param expr the AST expression
1503 /// @return the resolved semantic type for the expression, or nullptr if the
1504 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001505 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001506
Ben Clayton169512e2021-04-17 05:52:11 +00001507 /// Wraps the ast::Literal in a statement. This is used by tests that
1508 /// construct a partial AST and require the Resolver to reach these
1509 /// nodes.
1510 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1511 /// @return the ast::Statement that wraps the ast::Statement
1512 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001513 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001514 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001515 /// nodes.
1516 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1517 /// @return the ast::Statement that wraps the ast::Expression
1518 ast::Statement* WrapInStatement(ast::Expression* expr);
1519 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001520 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001521 /// these nodes.
1522 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1523 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1524 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1525 /// Returns the statement argument. Used as a passthrough-overload by
1526 /// WrapInFunction().
1527 /// @param stmt the ast::Statement
1528 /// @return `stmt`
1529 ast::Statement* WrapInStatement(ast::Statement* stmt);
1530 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001531 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001532 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001533 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001534 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001535 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001536 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001537 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001538 }
1539 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001540 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001541 /// @returns the function
1542 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001543
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001544 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001545 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001546
1547 protected:
1548 /// Asserts that the builder has not been moved.
1549 void AssertNotMoved() const;
1550
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001551 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001552 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001553 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001554 ASTNodeAllocator ast_nodes_;
1555 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001556 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001557 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001558 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001559 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001560
1561 /// The source to use when creating AST nodes without providing a Source as
1562 /// the first argument.
1563 Source source_;
1564
Ben Clayton5f0ea112021-03-09 10:54:37 +00001565 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001566 /// program when built.
1567 bool resolve_on_build_ = true;
1568
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001569 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1570 bool moved_ = false;
1571};
1572
1573//! @cond Doxygen_Suppress
1574// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1575template <>
1576struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001577 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001578 return t->i32();
1579 }
1580};
1581template <>
1582struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001583 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001584 return t->u32();
1585 }
1586};
1587template <>
1588struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001589 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001590 return t->f32();
1591 }
1592};
1593template <>
1594struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001595 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001596 return t->bool_();
1597 }
1598};
1599template <>
1600struct ProgramBuilder::TypesBuilder::CToAST<void> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001601 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001602 return t->void_();
1603 }
1604};
1605//! @endcond
1606
Ben Clayton917b14b2021-04-19 16:50:23 +00001607/// @param builder the ProgramBuilder
1608/// @returns the ProgramID of the ProgramBuilder
1609inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1610 return builder->ID();
1611}
1612
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001613} // namespace tint
1614
1615#endif // SRC_PROGRAM_BUILDER_H_