blob: 4d6f52a13bc88551a28348c99b3ba6d4612fd1da [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
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000026#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/bool_literal.h"
28#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000029#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000030#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000031#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000032#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000035#include "src/ast/if_statement.h"
36#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000037#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/member_accessor_expression.h"
39#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000040#include "src/ast/multisampled_texture.h"
41#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000042#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000043#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/ast/scalar_constructor_expression.h"
45#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000046#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000047#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000048#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000049#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000050#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000051#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000052#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053#include "src/ast/type_constructor_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000054#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000055#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000056#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000057#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000058#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000059#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000060#include "src/program_id.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000061#include "src/sem/access_control_type.h"
62#include "src/sem/alias_type.h"
63#include "src/sem/array_type.h"
64#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000065#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000066#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000067#include "src/sem/f32_type.h"
68#include "src/sem/i32_type.h"
69#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000070#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000071#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/sampled_texture_type.h"
73#include "src/sem/storage_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000074#include "src/sem/struct_type.h"
75#include "src/sem/u32_type.h"
76#include "src/sem/vector_type.h"
77#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000078#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000079
80namespace tint {
81
Ben Clayton401b96b2021-02-03 17:19:59 +000082// Forward declarations
83namespace ast {
84class VariableDeclStatement;
85} // namespace ast
86
Ben Claytona6b9a8e2021-01-26 16:57:10 +000087class CloneContext;
88
89/// ProgramBuilder is a mutable builder for a Program.
90/// To construct a Program, populate the builder and then `std::move` it to a
91/// Program.
92class ProgramBuilder {
93 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000094 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
95 using ASTNodeAllocator = BlockAllocator<ast::Node>;
96
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000097 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
98 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000099
100 /// `i32` is a type alias to `int`.
101 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
102 /// WGSL syntax.
103 /// Note: this is intentionally not aliased to uint32_t as we want integer
104 /// literals passed to the builder to match WGSL's integer literal types.
105 using i32 = decltype(1);
106 /// `u32` is a type alias to `unsigned int`.
107 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
108 /// WGSL syntax.
109 /// Note: this is intentionally not aliased to uint32_t as we want integer
110 /// literals passed to the builder to match WGSL's integer literal types.
111 using u32 = decltype(1u);
112 /// `f32` is a type alias to `float`
113 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
114 /// WGSL syntax.
115 using f32 = float;
116
117 /// Constructor
118 ProgramBuilder();
119
120 /// Move constructor
121 /// @param rhs the builder to move
122 ProgramBuilder(ProgramBuilder&& rhs);
123
124 /// Destructor
125 virtual ~ProgramBuilder();
126
127 /// Move assignment operator
128 /// @param rhs the builder to move
129 /// @return this builder
130 ProgramBuilder& operator=(ProgramBuilder&& rhs);
131
Ben Claytone43c8302021-01-29 11:59:32 +0000132 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
133 /// making a deep clone of the Program contents.
134 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
135 /// existing immutable program.
136 /// As the returned ProgramBuilder wraps `program`, `program` must not be
137 /// destructed or assigned while using the returned ProgramBuilder.
138 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
139 /// function. See crbug.com/tint/460.
140 /// @param program the immutable Program to wrap
141 /// @return the ProgramBuilder that wraps `program`
142 static ProgramBuilder Wrap(const Program* program);
143
Ben Claytone6995de2021-04-13 23:27:27 +0000144 /// @returns the unique identifier for this program
145 ProgramID ID() const { return id_; }
146
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000147 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000148 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000149 AssertNotMoved();
150 return types_;
151 }
152
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000153 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000154 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 AssertNotMoved();
156 return types_;
157 }
158
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000159 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000160 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000162 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000163 }
164
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000165 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000166 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000168 return ast_nodes_;
169 }
170
171 /// @returns a reference to the program's semantic nodes storage
172 SemNodeAllocator& SemNodes() {
173 AssertNotMoved();
174 return sem_nodes_;
175 }
176
177 /// @returns a reference to the program's semantic nodes storage
178 const SemNodeAllocator& SemNodes() const {
179 AssertNotMoved();
180 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000181 }
182
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000183 /// @returns a reference to the program's AST root Module
184 ast::Module& AST() {
185 AssertNotMoved();
186 return *ast_;
187 }
188
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000189 /// @returns a reference to the program's AST root Module
190 const ast::Module& AST() const {
191 AssertNotMoved();
192 return *ast_;
193 }
194
195 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000196 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000197 AssertNotMoved();
198 return sem_;
199 }
200
201 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000202 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000203 AssertNotMoved();
204 return sem_;
205 }
206
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000207 /// @returns a reference to the program's SymbolTable
208 SymbolTable& Symbols() {
209 AssertNotMoved();
210 return symbols_;
211 }
212
Ben Clayton708dc2d2021-01-29 11:22:40 +0000213 /// @returns a reference to the program's SymbolTable
214 const SymbolTable& Symbols() const {
215 AssertNotMoved();
216 return symbols_;
217 }
218
Ben Clayton844217f2021-01-27 18:49:05 +0000219 /// @returns a reference to the program's diagnostics
220 diag::List& Diagnostics() {
221 AssertNotMoved();
222 return diagnostics_;
223 }
224
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000225 /// @returns a reference to the program's diagnostics
226 const diag::List& Diagnostics() const {
227 AssertNotMoved();
228 return diagnostics_;
229 }
230
Ben Clayton5f0ea112021-03-09 10:54:37 +0000231 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000232 /// @param enable the new flag value (defaults to true)
233 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
234
Ben Clayton5f0ea112021-03-09 10:54:37 +0000235 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000236 /// built.
237 bool ResolveOnBuild() const { return resolve_on_build_; }
238
Ben Clayton844217f2021-01-27 18:49:05 +0000239 /// @returns true if the program has no error diagnostics and is not missing
240 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000241 bool IsValid() const;
242
Ben Clayton708dc2d2021-01-29 11:22:40 +0000243 /// Writes a representation of the node to the output stream
244 /// @note unlike str(), to_str() does not automatically demangle the string.
245 /// @param node the AST node
246 /// @param out the stream to write to
247 /// @param indent number of spaces to indent the node when writing
248 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
249 node->to_str(Sem(), out, indent);
250 }
251
252 /// Returns a demangled, string representation of `node`.
253 /// @param node the AST node
254 /// @returns a string representation of the node
255 std::string str(const ast::Node* node) const;
256
Ben Clayton7fdfff12021-01-29 15:17:30 +0000257 /// Creates a new ast::Node owned by the ProgramBuilder. When the
258 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000259 /// @param source the Source of the node
260 /// @param args the arguments to pass to the type constructor
261 /// @returns the node pointer
262 template <typename T, typename... ARGS>
263 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
264 ARGS&&... args) {
265 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000266 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000267 }
268
Ben Clayton7fdfff12021-01-29 15:17:30 +0000269 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
270 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000271 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000272 /// When the ProgramBuilder is destructed, the ast::Node will also be
273 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000274 /// @returns the node pointer
275 template <typename T>
276 traits::EnableIfIsType<T, ast::Node>* create() {
277 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000278 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000279 }
280
Ben Clayton7fdfff12021-01-29 15:17:30 +0000281 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
282 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000283 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000284 /// When the ProgramBuilder is destructed, the ast::Node will also be
285 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000286 /// @param arg0 the first arguments to pass to the type constructor
287 /// @param args the remaining arguments to pass to the type constructor
288 /// @returns the node pointer
289 template <typename T, typename ARG0, typename... ARGS>
290 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
291 traits::IsTypeOrDerived<T, ast::Node>::value &&
292 !traits::IsTypeOrDerived<ARG0, Source>::value,
293 T>*
294 create(ARG0&& arg0, ARGS&&... args) {
295 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000296 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000297 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000298 }
299
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000300 /// Creates a new sem::Node owned by the ProgramBuilder.
301 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000302 /// destructed.
303 /// @param args the arguments to pass to the type constructor
304 /// @returns the node pointer
305 template <typename T, typename... ARGS>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000306 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000307 AssertNotMoved();
308 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
309 }
310
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000311 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000312 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
313 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000314 /// Types are unique (de-aliased), and so calling create() for the same `T`
315 /// and arguments will return the same pointer.
316 /// @warning Use this method to acquire a type only if all of its type
317 /// information is provided in the constructor arguments `args`.<br>
318 /// If the type requires additional configuration after construction that
319 /// affect its fundamental type, build the type with `std::make_unique`, make
320 /// any necessary alterations and then call unique_type() instead.
321 /// @param args the arguments to pass to the type constructor
322 /// @returns the de-aliased type pointer
323 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000324 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
325 static_assert(std::is_base_of<sem::Type, T>::value,
326 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000327 AssertNotMoved();
328 return types_.Get<T>(std::forward<ARGS>(args)...);
329 }
330
331 /// Marks this builder as moved, preventing any further use of the builder.
332 void MarkAsMoved();
333
334 //////////////////////////////////////////////////////////////////////////////
335 // TypesBuilder
336 //////////////////////////////////////////////////////////////////////////////
337
338 /// TypesBuilder holds basic `tint` types and methods for constructing
339 /// complex types.
340 class TypesBuilder {
341 public:
342 /// Constructor
343 /// @param builder the program builder
344 explicit TypesBuilder(ProgramBuilder* builder);
345
346 /// @return the tint AST type for the C type `T`.
347 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000348 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000349 return CToAST<T>::get(this);
350 }
351
352 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000353 typ::Bool bool_() const {
354 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
355 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000356
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000357 /// @param source the Source of the node
358 /// @returns a boolean type
359 typ::Bool bool_(const Source& source) const {
360 return {builder->create<ast::Bool>(source), builder->create<sem::Bool>()};
361 }
362
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000363 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000364 typ::F32 f32() const {
365 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
366 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000367
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000368 /// @param source the Source of the node
369 /// @returns a f32 type
370 typ::F32 f32(const Source& source) const {
371 return {builder->create<ast::F32>(source), builder->create<sem::F32>()};
372 }
373
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000374 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000375 typ::I32 i32() const {
376 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
377 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000378
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000379 /// @param source the Source of the node
380 /// @returns a i32 type
381 typ::I32 i32(const Source& source) const {
382 return {builder->create<ast::I32>(source), builder->create<sem::I32>()};
383 }
384
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000385 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000386 typ::U32 u32() const {
387 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
388 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000389
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000390 /// @param source the Source of the node
391 /// @returns a u32 type
392 typ::U32 u32(const Source& source) const {
393 return {builder->create<ast::U32>(source), builder->create<sem::U32>()};
394 }
395
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000396 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000397 typ::Void void_() const {
398 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
399 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000400
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000401 /// @param source the Source of the node
402 /// @returns a void type
403 typ::Void void_(const Source& source) const {
404 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
405 }
406
Antonio Maiorano25436862021-04-13 13:32:33 +0000407 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000408 /// @param n vector width in elements
409 /// @return the tint AST type for a `n`-element vector of `type`.
410 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000411 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
412 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000413 }
414
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000415 /// @param source the Source of the node
416 /// @param type vector subtype
417 /// @param n vector width in elements
418 /// @return the tint AST type for a `n`-element vector of `type`.
419 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000420 return {
421 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
422 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000423 }
424
Antonio Maiorano25436862021-04-13 13:32:33 +0000425 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000426 /// @return the tint AST type for a 2-element vector of `type`.
427 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
428
429 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000430 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000431 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000432
433 /// @param type vector subtype
434 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000435 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
436
437 /// @param n vector width in elements
438 /// @return the tint AST type for a `n`-element vector of `type`.
439 template <typename T>
440 typ::Vector vec(uint32_t n) const {
441 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000442 }
443
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000444 /// @return the tint AST type for a 2-element vector of the C type `T`.
445 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000446 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000447 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000448 }
449
450 /// @return the tint AST type for a 3-element vector of the C type `T`.
451 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000452 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000453 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000454 }
455
456 /// @return the tint AST type for a 4-element vector of the C type `T`.
457 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000458 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000459 return vec4(Of<T>());
460 }
461
462 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000463 /// @param columns number of columns for the matrix
464 /// @param rows number of rows for the matrix
465 /// @return the tint AST type for a matrix of `type`
466 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000467 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
468 : nullptr,
469 type.sem ? builder->create<sem::Matrix>(type, rows, columns)
470 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000471 }
472
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000473 /// @param source the Source of the node
474 /// @param type matrix subtype
475 /// @param columns number of columns for the matrix
476 /// @param rows number of rows for the matrix
477 /// @return the tint AST type for a matrix of `type`
478 typ::Matrix mat(const Source& source,
479 typ::Type type,
480 uint32_t columns,
481 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000482 return {type.ast
483 ? builder->create<ast::Matrix>(source, type, rows, columns)
484 : nullptr,
485 type.sem ? builder->create<sem::Matrix>(type, rows, columns)
486 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000487 }
488
Ben Claytone204f272021-04-22 14:40:23 +0000489 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000490 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000491 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000492
493 /// @param type matrix subtype
494 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000495 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000496
497 /// @param type matrix subtype
498 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000499 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000500
501 /// @param type matrix subtype
502 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000503 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000504
505 /// @param type matrix subtype
506 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000507 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000508
509 /// @param type matrix subtype
510 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000511 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000512
513 /// @param type matrix subtype
514 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000515 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000516
517 /// @param type matrix subtype
518 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000519 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000520
521 /// @param type matrix subtype
522 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000523 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000524
Ben Claytone204f272021-04-22 14:40:23 +0000525 /// @param columns number of columns for the matrix
526 /// @param rows number of rows for the matrix
527 /// @return the tint AST type for a matrix of `type`
528 template <typename T>
529 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
530 return mat(Of<T>(), columns, rows);
531 }
532
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000533 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
534 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000535 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000536 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000537 }
538
539 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
540 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000541 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000542 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000543 }
544
545 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
546 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000547 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000548 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000549 }
550
551 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
552 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000553 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000554 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 }
556
557 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
558 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000559 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000560 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 }
562
563 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
564 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000565 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000566 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 }
568
569 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
570 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000571 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000572 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
575 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
576 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000577 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000578 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 }
580
581 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
582 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000583 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000584 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000585 }
586
587 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000588 /// @param n the array size. 0 represents a runtime-array
589 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000590 /// @return the tint AST type for a array of size `n` of type `T`
Ben Claytone204f272021-04-22 14:40:23 +0000591 typ::Array array(typ::Type subtype,
592 uint32_t n = 0,
593 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000594 subtype = MaybeCreateTypename(subtype);
Ben Clayton95c2e952021-04-28 12:58:13 +0000595 return {subtype.ast ? builder->create<ast::Array>(subtype, n, decos)
596 : nullptr,
597 subtype.sem ? builder->create<sem::ArrayType>(subtype, n,
598 std::move(decos))
599 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000600 }
601
602 /// @param source the Source of the node
603 /// @param subtype the array element type
604 /// @param n the array size. 0 represents a runtime-array
605 /// @param decos the optional decorations for the array
606 /// @return the tint AST type for a array of size `n` of type `T`
607 typ::Array array(const Source& source,
608 typ::Type subtype,
609 uint32_t n = 0,
610 ast::DecorationList decos = {}) const {
611 subtype = MaybeCreateTypename(subtype);
Ben Clayton95c2e952021-04-28 12:58:13 +0000612 return {
613 subtype.ast ? builder->create<ast::Array>(source, subtype, n, decos)
614 : nullptr,
615 subtype.sem
616 ? builder->create<sem::ArrayType>(subtype, n, std::move(decos))
617 : nullptr};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000618 }
619
Ben Claytonbab31972021-02-08 21:16:21 +0000620 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000621 /// @param n the array size. 0 represents a runtime-array
622 /// @param stride the array stride
Ben Claytonbab31972021-02-08 21:16:21 +0000623 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton7241a502021-04-22 14:32:53 +0000624 typ::Array array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000625 subtype = MaybeCreateTypename(subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000626 return array(subtype, n,
627 {builder->create<ast::StrideDecoration>(stride)});
Ben Claytonbab31972021-02-08 21:16:21 +0000628 }
629
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000630 /// @return the tint AST type for an array of size `N` of type `T`
631 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000632 typ::Array array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000633 return array(Of<T>(), N);
634 }
635
Ben Claytonbab31972021-02-08 21:16:21 +0000636 /// @param stride the array stride
637 /// @return the tint AST type for an array of size `N` of type `T`
638 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000639 typ::Array array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000640 return array(Of<T>(), N, stride);
641 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000642
Ben Clayton42d1e092021-02-02 14:29:15 +0000643 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000644 /// @param name the alias name
645 /// @param type the alias type
646 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000647 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +0000648 typ::Alias alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000649 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000650 auto sym = builder->Sym(std::forward<NAME>(name));
651 return {
652 builder->create<ast::Alias>(sym, type),
653 builder->create<sem::Alias>(sym, type),
654 };
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000655 }
656
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000657 /// Creates an alias type
658 /// @param source the Source of the node
659 /// @param name the alias name
660 /// @param type the alias type
661 /// @returns the alias pointer
662 template <typename NAME>
663 typ::Alias alias(const Source& source, NAME&& name, typ::Type type) const {
664 type = MaybeCreateTypename(type);
665 auto sym = builder->Sym(std::forward<NAME>(name));
666 return {
Ben Clayton95c2e952021-04-28 12:58:13 +0000667 type.ast ? builder->create<ast::Alias>(source, sym, type) : nullptr,
668 type.sem ? builder->create<sem::Alias>(sym, type) : nullptr,
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000669 };
670 }
671
Ben Clayton4db10dd2021-04-16 08:47:14 +0000672 /// Creates an access control qualifier type
673 /// @param access the access control
674 /// @param type the inner type
675 /// @returns the access control qualifier type
Ben Claytone204f272021-04-22 14:40:23 +0000676 typ::AccessControl access(ast::AccessControl::Access access,
677 typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000678 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000679 return {type.ast ? builder->create<ast::AccessControl>(access, type)
680 : nullptr,
681 type.sem ? builder->create<sem::AccessControl>(access, type)
682 : nullptr};
Ben Clayton4db10dd2021-04-16 08:47:14 +0000683 }
684
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000685 /// Creates an access control qualifier type
686 /// @param source the Source of the node
687 /// @param access the access control
688 /// @param type the inner type
689 /// @returns the access control qualifier type
690 typ::AccessControl access(const Source& source,
691 ast::AccessControl::Access access,
692 typ::Type type) const {
693 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000694 return {type.ast
695 ? builder->create<ast::AccessControl>(source, access, type)
696 : nullptr,
697 type.sem ? builder->create<sem::AccessControl>(access, type)
698 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000699 }
700
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000701 /// @param type the type of the pointer
702 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000703 /// @return the pointer to `type` with the given ast::StorageClass
704 typ::Pointer pointer(typ::Type type,
705 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000706 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000707 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
708 : nullptr,
709 type.sem ? builder->create<sem::Pointer>(type, storage_class)
710 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000711 }
712
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000713 /// @param source the Source of the node
714 /// @param type the type of the pointer
715 /// @param storage_class the storage class of the pointer
716 /// @return the pointer to `type` with the given ast::StorageClass
717 typ::Pointer pointer(const Source& source,
718 typ::Type type,
719 ast::StorageClass storage_class) const {
720 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000721 return {type.ast
722 ? builder->create<ast::Pointer>(source, type, storage_class)
723 : nullptr,
724 type.sem ? builder->create<sem::Pointer>(type, storage_class)
725 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000726 }
727
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000728 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000729 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000730 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000731 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000732 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000733 }
734
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000735 /// @param impl the struct implementation
736 /// @returns a struct pointer
Ben Claytone204f272021-04-22 14:40:23 +0000737 typ::Struct struct_(ast::Struct* impl) const {
738 return {impl, builder->create<sem::StructType>(impl)};
739 }
740
741 /// @param kind the kind of sampler
742 /// @returns the sampler
743 typ::Sampler sampler(ast::SamplerKind kind) const {
744 return {builder->create<ast::Sampler>(kind),
745 builder->create<sem::Sampler>(kind)};
746 }
747
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000748 /// @param source the Source of the node
749 /// @param kind the kind of sampler
750 /// @returns the sampler
751 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
752 return {builder->create<ast::Sampler>(source, kind),
753 builder->create<sem::Sampler>(kind)};
754 }
755
Ben Claytone204f272021-04-22 14:40:23 +0000756 /// @param dims the dimensionality of the texture
757 /// @returns the depth texture
758 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
759 return {builder->create<ast::DepthTexture>(dims),
760 builder->create<sem::DepthTexture>(dims)};
761 }
762
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000763 /// @param source the Source of the node
764 /// @param dims the dimensionality of the texture
765 /// @returns the depth texture
766 typ::DepthTexture depth_texture(const Source& source,
767 ast::TextureDimension dims) const {
768 return {builder->create<ast::DepthTexture>(source, dims),
769 builder->create<sem::DepthTexture>(dims)};
770 }
771
Ben Claytone204f272021-04-22 14:40:23 +0000772 /// @param dims the dimensionality of the texture
773 /// @param subtype the texture subtype.
774 /// @returns the sampled texture
775 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
776 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000777 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
778 : nullptr,
779 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
780 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000781 }
782
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000783 /// @param source the Source of the node
784 /// @param dims the dimensionality of the texture
785 /// @param subtype the texture subtype.
786 /// @returns the sampled texture
787 typ::SampledTexture sampled_texture(const Source& source,
788 ast::TextureDimension dims,
789 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000790 return {subtype.ast
791 ? builder->create<ast::SampledTexture>(source, dims, subtype)
792 : nullptr,
793 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
794 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000795 }
796
Ben Claytone204f272021-04-22 14:40:23 +0000797 /// @param dims the dimensionality of the texture
798 /// @param subtype the texture subtype.
799 /// @returns the multisampled texture
800 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
801 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000802 return {
803 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
804 : nullptr,
805 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
806 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000807 }
808
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000809 /// @param source the Source of the node
810 /// @param dims the dimensionality of the texture
811 /// @param subtype the texture subtype.
812 /// @returns the multisampled texture
813 typ::MultisampledTexture multisampled_texture(const Source& source,
814 ast::TextureDimension dims,
815 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000816 return {
817 subtype.ast
818 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
819 : nullptr,
820 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
821 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000822 }
823
Ben Claytone204f272021-04-22 14:40:23 +0000824 /// @param dims the dimensionality of the texture
825 /// @param format the image format of the texture
826 /// @returns the storage texture
827 typ::StorageTexture storage_texture(ast::TextureDimension dims,
828 ast::ImageFormat format) const {
829 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
830 auto* sem_subtype =
831 sem::StorageTexture::SubtypeFor(format, builder->Types());
832 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
833 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000834 }
835
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000836 /// @param source the Source of the node
837 /// @param dims the dimensionality of the texture
838 /// @param format the image format of the texture
839 /// @returns the storage texture
840 typ::StorageTexture storage_texture(const Source& source,
841 ast::TextureDimension dims,
842 ast::ImageFormat format) const {
843 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
844 auto* sem_subtype =
845 sem::StorageTexture::SubtypeFor(format, builder->Types());
846 return {builder->create<ast::StorageTexture>(source, dims, format,
847 ast_subtype),
848 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
849 }
850
851 /// @param source the Source of the node
852 /// @returns the external texture
853 typ::ExternalTexture external_texture(const Source& source) const {
854 return {builder->create<ast::ExternalTexture>(source),
855 builder->create<sem::ExternalTexture>()};
856 }
857
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000858 /// If ty is a ast::Struct or ast::Alias, the returned type is an
859 /// ast::TypeName of the given type's name, otherwise type is returned.
860 /// @param type the type
861 /// @return either type or a pointer to a new ast::TypeName
862 typ::Type MaybeCreateTypename(typ::Type type) const;
863
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000864 private:
865 /// CToAST<T> is specialized for various `T` types and each specialization
866 /// contains a single static `get()` method for obtaining the corresponding
867 /// AST type for the C type `T`.
868 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000869 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000870 template <typename T>
871 struct CToAST {};
872
Ben Claytonc7ca7662021-02-17 16:23:52 +0000873 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000874 };
875
876 //////////////////////////////////////////////////////////////////////////////
877 // AST helper methods
878 //////////////////////////////////////////////////////////////////////////////
879
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000880 /// @param name the symbol string
881 /// @return a Symbol with the given name
882 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
883
884 /// @param sym the symbol
885 /// @return `sym`
886 Symbol Sym(Symbol sym) { return sym; }
887
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000888 /// @param expr the expression
889 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000890 template <typename T>
891 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
892 return expr;
893 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000894
Ben Claytonb8ea5912021-04-19 16:52:42 +0000895 /// Passthrough for nullptr
896 /// @return nullptr
897 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
898
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 /// @param name the identifier name
900 /// @return an ast::IdentifierExpression with the given name
901 ast::IdentifierExpression* Expr(const std::string& name) {
902 return create<ast::IdentifierExpression>(Symbols().Register(name));
903 }
904
Ben Clayton46d78d72021-02-10 21:34:26 +0000905 /// @param symbol the identifier symbol
906 /// @return an ast::IdentifierExpression with the given symbol
907 ast::IdentifierExpression* Expr(Symbol symbol) {
908 return create<ast::IdentifierExpression>(symbol);
909 }
910
Ben Claytonb8ea5912021-04-19 16:52:42 +0000911 /// @param variable the AST variable
912 /// @return an ast::IdentifierExpression with the variable's symbol
913 ast::IdentifierExpression* Expr(ast::Variable* variable) {
914 return create<ast::IdentifierExpression>(variable->symbol());
915 }
916
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000917 /// @param source the source information
918 /// @param name the identifier name
919 /// @return an ast::IdentifierExpression with the given name
920 ast::IdentifierExpression* Expr(const Source& source,
921 const std::string& name) {
922 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
923 }
924
925 /// @param name the identifier name
926 /// @return an ast::IdentifierExpression with the given name
927 ast::IdentifierExpression* Expr(const char* name) {
928 return create<ast::IdentifierExpression>(Symbols().Register(name));
929 }
930
931 /// @param value the boolean value
932 /// @return a Scalar constructor for the given value
933 ast::ScalarConstructorExpression* Expr(bool value) {
934 return create<ast::ScalarConstructorExpression>(Literal(value));
935 }
936
937 /// @param value the float value
938 /// @return a Scalar constructor for the given value
939 ast::ScalarConstructorExpression* Expr(f32 value) {
940 return create<ast::ScalarConstructorExpression>(Literal(value));
941 }
942
943 /// @param value the integer value
944 /// @return a Scalar constructor for the given value
945 ast::ScalarConstructorExpression* Expr(i32 value) {
946 return create<ast::ScalarConstructorExpression>(Literal(value));
947 }
948
949 /// @param value the unsigned int value
950 /// @return a Scalar constructor for the given value
951 ast::ScalarConstructorExpression* Expr(u32 value) {
952 return create<ast::ScalarConstructorExpression>(Literal(value));
953 }
954
955 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
956 /// `list`.
957 /// @param list the list to append too
958 /// @param arg the arg to create
959 template <typename ARG>
960 void Append(ast::ExpressionList& list, ARG&& arg) {
961 list.emplace_back(Expr(std::forward<ARG>(arg)));
962 }
963
964 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
965 /// then appends them to `list`.
966 /// @param list the list to append too
967 /// @param arg0 the first argument
968 /// @param args the rest of the arguments
969 template <typename ARG0, typename... ARGS>
970 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
971 Append(list, std::forward<ARG0>(arg0));
972 Append(list, std::forward<ARGS>(args)...);
973 }
974
975 /// @return an empty list of expressions
976 ast::ExpressionList ExprList() { return {}; }
977
978 /// @param args the list of expressions
979 /// @return the list of expressions converted to `ast::Expression`s using
980 /// `Expr()`,
981 template <typename... ARGS>
982 ast::ExpressionList ExprList(ARGS&&... args) {
983 ast::ExpressionList list;
984 list.reserve(sizeof...(args));
985 Append(list, std::forward<ARGS>(args)...);
986 return list;
987 }
988
989 /// @param list the list of expressions
990 /// @return `list`
991 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
992
993 /// @param val the boolan value
994 /// @return a boolean literal with the given value
995 ast::BoolLiteral* Literal(bool val) {
996 return create<ast::BoolLiteral>(ty.bool_(), val);
997 }
998
999 /// @param val the float value
1000 /// @return a float literal with the given value
1001 ast::FloatLiteral* Literal(f32 val) {
1002 return create<ast::FloatLiteral>(ty.f32(), val);
1003 }
1004
1005 /// @param val the unsigned int value
1006 /// @return a ast::UintLiteral with the given value
1007 ast::UintLiteral* Literal(u32 val) {
1008 return create<ast::UintLiteral>(ty.u32(), val);
1009 }
1010
1011 /// @param val the integer value
1012 /// @return the ast::SintLiteral with the given value
1013 ast::SintLiteral* Literal(i32 val) {
1014 return create<ast::SintLiteral>(ty.i32(), val);
1015 }
1016
1017 /// @param args the arguments for the type constructor
1018 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1019 /// of `args` converted to `ast::Expression`s using `Expr()`
1020 template <typename T, typename... ARGS>
1021 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
1022 return create<ast::TypeConstructorExpression>(
1023 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
1024 }
1025
1026 /// @param type the type to construct
1027 /// @param args the arguments for the constructor
1028 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1029 /// values `args`.
1030 template <typename... ARGS>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001031 ast::TypeConstructorExpression* Construct(typ::Type type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001032 return create<ast::TypeConstructorExpression>(
1033 type, ExprList(std::forward<ARGS>(args)...));
1034 }
1035
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001036 /// Creates a constructor expression that constructs an object of
1037 /// `type` filled with `elem_value`. For example,
1038 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1039 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1040 /// @param type the type to construct
1041 /// @param elem_value the initial or element value (for vec and mat) to
1042 /// construct with
1043 /// @return the constructor expression
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001044 ast::ConstructorExpression* ConstructValueFilledWith(typ::Type type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001045 int elem_value = 0);
1046
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001047 /// @param args the arguments for the vector constructor
1048 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1049 /// `T`, constructed with the values `args`.
1050 template <typename T, typename... ARGS>
1051 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
1052 return create<ast::TypeConstructorExpression>(
1053 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
1054 }
1055
1056 /// @param args the arguments for the vector constructor
1057 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1058 /// `T`, constructed with the values `args`.
1059 template <typename T, typename... ARGS>
1060 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
1061 return create<ast::TypeConstructorExpression>(
1062 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
1063 }
1064
1065 /// @param args the arguments for the vector constructor
1066 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1067 /// `T`, constructed with the values `args`.
1068 template <typename T, typename... ARGS>
1069 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
1070 return create<ast::TypeConstructorExpression>(
1071 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
1072 }
1073
1074 /// @param args the arguments for the matrix constructor
1075 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1076 /// `T`, constructed with the values `args`.
1077 template <typename T, typename... ARGS>
1078 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
1079 return create<ast::TypeConstructorExpression>(
1080 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
1081 }
1082
1083 /// @param args the arguments for the matrix constructor
1084 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1085 /// `T`, constructed with the values `args`.
1086 template <typename T, typename... ARGS>
1087 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
1088 return create<ast::TypeConstructorExpression>(
1089 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
1090 }
1091
1092 /// @param args the arguments for the matrix constructor
1093 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1094 /// `T`, constructed with the values `args`.
1095 template <typename T, typename... ARGS>
1096 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
1097 return create<ast::TypeConstructorExpression>(
1098 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
1099 }
1100
1101 /// @param args the arguments for the matrix constructor
1102 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1103 /// `T`, constructed with the values `args`.
1104 template <typename T, typename... ARGS>
1105 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
1106 return create<ast::TypeConstructorExpression>(
1107 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
1108 }
1109
1110 /// @param args the arguments for the matrix constructor
1111 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1112 /// `T`, constructed with the values `args`.
1113 template <typename T, typename... ARGS>
1114 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
1115 return create<ast::TypeConstructorExpression>(
1116 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
1117 }
1118
1119 /// @param args the arguments for the matrix constructor
1120 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1121 /// `T`, constructed with the values `args`.
1122 template <typename T, typename... ARGS>
1123 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
1124 return create<ast::TypeConstructorExpression>(
1125 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
1126 }
1127
1128 /// @param args the arguments for the matrix constructor
1129 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1130 /// `T`, constructed with the values `args`.
1131 template <typename T, typename... ARGS>
1132 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
1133 return create<ast::TypeConstructorExpression>(
1134 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
1135 }
1136
1137 /// @param args the arguments for the matrix constructor
1138 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1139 /// `T`, constructed with the values `args`.
1140 template <typename T, typename... ARGS>
1141 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
1142 return create<ast::TypeConstructorExpression>(
1143 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
1144 }
1145
1146 /// @param args the arguments for the matrix constructor
1147 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1148 /// `T`, constructed with the values `args`.
1149 template <typename T, typename... ARGS>
1150 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
1151 return create<ast::TypeConstructorExpression>(
1152 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
1153 }
1154
1155 /// @param args the arguments for the array constructor
1156 /// @return an `ast::TypeConstructorExpression` of an array with element type
1157 /// `T`, constructed with the values `args`.
1158 template <typename T, int N = 0, typename... ARGS>
1159 ast::TypeConstructorExpression* array(ARGS&&... args) {
1160 return create<ast::TypeConstructorExpression>(
1161 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
1162 }
1163
1164 /// @param subtype the array element type
1165 /// @param n the array size. 0 represents a runtime-array.
1166 /// @param args the arguments for the array constructor
1167 /// @return an `ast::TypeConstructorExpression` of an array with element type
1168 /// `subtype`, constructed with the values `args`.
1169 template <typename... ARGS>
Ben Claytone204f272021-04-22 14:40:23 +00001170 ast::TypeConstructorExpression* array(typ::Type subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001171 uint32_t n,
1172 ARGS&&... args) {
1173 return create<ast::TypeConstructorExpression>(
1174 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
1175 }
1176
1177 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001178 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001179 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001180 /// @param constructor constructor expression
1181 /// @param decorations variable decorations
1182 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001183 template <typename NAME>
1184 ast::Variable* Var(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001185 typ::Type type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001186 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001187 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001188 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001189 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001190 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1191 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001192 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001193
1194 /// @param source the variable source
1195 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001196 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001197 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001198 /// @param constructor constructor expression
1199 /// @param decorations variable decorations
1200 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001201 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001202 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001203 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001204 typ::Type type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001205 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001206 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001207 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001208 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001209 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001210 type, false, constructor, decorations);
1211 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001212
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001213 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001214 /// @param type the variable type
1215 /// @param constructor optional constructor expression
1216 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001217 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001218 template <typename NAME>
1219 ast::Variable* Const(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001220 typ::Type type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001221 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001222 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001223 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001224 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001225 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001226 constructor, decorations);
1227 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228
1229 /// @param source the variable source
1230 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231 /// @param type the variable type
1232 /// @param constructor optional constructor expression
1233 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001234 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001235 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001236 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001237 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001238 typ::Type type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001239 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001240 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001241 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001242 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001243 ast::StorageClass::kNone, type, true,
1244 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001245 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001246
James Pricedaf1f1c2021-04-08 22:15:48 +00001247 /// @param name the parameter name
1248 /// @param type the parameter type
1249 /// @param decorations optional parameter decorations
1250 /// @returns a constant `ast::Variable` with the given name and type
1251 template <typename NAME>
1252 ast::Variable* Param(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001253 typ::Type type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001254 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001255 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001256 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1257 ast::StorageClass::kNone, type, true, nullptr,
1258 decorations);
1259 }
1260
1261 /// @param source the parameter source
1262 /// @param name the parameter name
1263 /// @param type the parameter type
1264 /// @param decorations optional parameter decorations
1265 /// @returns a constant `ast::Variable` with the given name and type
1266 template <typename NAME>
1267 ast::Variable* Param(const Source& source,
1268 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001269 typ::Type type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001270 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001271 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001272 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1273 ast::StorageClass::kNone, type, true, nullptr,
1274 decorations);
1275 }
1276
Ben Clayton42708342021-04-21 17:55:12 +00001277 /// @param name the variable name
1278 /// @param type the variable type
1279 /// @param storage the variable storage class
1280 /// @param constructor constructor expression
1281 /// @param decorations variable decorations
1282 /// @returns a new `ast::Variable`, which is automatically registered as a
1283 /// global variable with the ast::Module.
1284 template <typename NAME>
1285 ast::Variable* Global(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001286 typ::Type type,
Ben Clayton42708342021-04-21 17:55:12 +00001287 ast::StorageClass storage,
1288 ast::Expression* constructor = nullptr,
1289 ast::DecorationList decorations = {}) {
1290 auto* var =
1291 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1292 AST().AddGlobalVariable(var);
1293 return var;
1294 }
1295
1296 /// @param source the variable source
1297 /// @param name the variable name
1298 /// @param type the variable type
1299 /// @param storage the variable storage class
1300 /// @param constructor constructor expression
1301 /// @param decorations variable decorations
1302 /// @returns a new `ast::Variable`, which is automatically registered as a
1303 /// global variable with the ast::Module.
1304 template <typename NAME>
1305 ast::Variable* Global(const Source& source,
1306 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001307 typ::Type type,
Ben Clayton42708342021-04-21 17:55:12 +00001308 ast::StorageClass storage,
1309 ast::Expression* constructor = nullptr,
1310 ast::DecorationList decorations = {}) {
1311 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1312 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001313 AST().AddGlobalVariable(var);
1314 return var;
1315 }
1316
1317 /// @param args the arguments to pass to Const()
1318 /// @returns a const `ast::Variable` constructed by calling Var() with the
1319 /// arguments of `args`, which is automatically registered as a global
1320 /// variable with the ast::Module.
1321 template <typename... ARGS>
1322 ast::Variable* GlobalConst(ARGS&&... args) {
1323 auto* var = Const(std::forward<ARGS>(args)...);
1324 AST().AddGlobalVariable(var);
1325 return var;
1326 }
1327
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001328 /// @param func the function name
1329 /// @param args the function call arguments
1330 /// @returns a `ast::CallExpression` to the function `func`, with the
1331 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1332 template <typename NAME, typename... ARGS>
1333 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1334 return create<ast::CallExpression>(Expr(func),
1335 ExprList(std::forward<ARGS>(args)...));
1336 }
1337
1338 /// @param lhs the left hand argument to the addition operation
1339 /// @param rhs the right hand argument to the addition operation
1340 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1341 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001342 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001343 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1344 Expr(std::forward<LHS>(lhs)),
1345 Expr(std::forward<RHS>(rhs)));
1346 }
1347
1348 /// @param lhs the left hand argument to the subtraction operation
1349 /// @param rhs the right hand argument to the subtraction operation
1350 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1351 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001352 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001353 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1354 Expr(std::forward<LHS>(lhs)),
1355 Expr(std::forward<RHS>(rhs)));
1356 }
1357
1358 /// @param lhs the left hand argument to the multiplication operation
1359 /// @param rhs the right hand argument to the multiplication operation
1360 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1361 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001362 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001363 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1364 Expr(std::forward<LHS>(lhs)),
1365 Expr(std::forward<RHS>(rhs)));
1366 }
1367
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001368 /// @param source the source information
1369 /// @param lhs the left hand argument to the multiplication operation
1370 /// @param rhs the right hand argument to the multiplication operation
1371 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1372 template <typename LHS, typename RHS>
1373 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1374 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1375 Expr(std::forward<LHS>(lhs)),
1376 Expr(std::forward<RHS>(rhs)));
1377 }
1378
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001379 /// @param lhs the left hand argument to the division operation
1380 /// @param rhs the right hand argument to the division operation
1381 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1382 template <typename LHS, typename RHS>
1383 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1384 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1385 Expr(std::forward<LHS>(lhs)),
1386 Expr(std::forward<RHS>(rhs)));
1387 }
1388
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001389 /// @param arr the array argument for the array accessor expression
1390 /// @param idx the index argument for the array accessor expression
1391 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1392 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001393 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001394 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1395 Expr(std::forward<IDX>(idx)));
1396 }
1397
1398 /// @param obj the object for the member accessor expression
1399 /// @param idx the index argument for the array accessor expression
1400 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1401 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001402 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001403 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1404 Expr(std::forward<IDX>(idx)));
1405 }
1406
Ben Clayton42d1e092021-02-02 14:29:15 +00001407 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001408 /// @param val the offset value
1409 /// @returns the offset decoration pointer
1410 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1411 return create<ast::StructMemberOffsetDecoration>(source_, val);
1412 }
1413
Ben Claytond614dd52021-03-15 10:43:11 +00001414 /// Creates a ast::StructMemberSizeDecoration
1415 /// @param source the source information
1416 /// @param val the size value
1417 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001418 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1419 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001420 return create<ast::StructMemberSizeDecoration>(source, val);
1421 }
1422
1423 /// Creates a ast::StructMemberSizeDecoration
1424 /// @param val the size value
1425 /// @returns the size decoration pointer
1426 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1427 return create<ast::StructMemberSizeDecoration>(source_, val);
1428 }
1429
1430 /// Creates a ast::StructMemberAlignDecoration
1431 /// @param source the source information
1432 /// @param val the align value
1433 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001434 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1435 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001436 return create<ast::StructMemberAlignDecoration>(source, val);
1437 }
1438
1439 /// Creates a ast::StructMemberAlignDecoration
1440 /// @param val the align value
1441 /// @returns the align decoration pointer
1442 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1443 return create<ast::StructMemberAlignDecoration>(source_, val);
1444 }
1445
Ben Clayton42d1e092021-02-02 14:29:15 +00001446 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001447 /// @param source the source information
1448 /// @param name the function name
1449 /// @param params the function parameters
1450 /// @param type the function return type
1451 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001452 /// @param decorations the optional function decorations
1453 /// @param return_type_decorations the optional function return type
1454 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001455 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001456 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001457 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001458 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001459 ast::VariableList params,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001460 typ::Type type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001461 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001462 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001463 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001464 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001465 auto* func =
1466 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1467 type, create<ast::BlockStatement>(body),
1468 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001469 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001470 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001471 }
1472
Ben Clayton42d1e092021-02-02 14:29:15 +00001473 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001474 /// @param name the function name
1475 /// @param params the function parameters
1476 /// @param type the function return type
1477 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001478 /// @param decorations the optional function decorations
1479 /// @param return_type_decorations the optional function return type
1480 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001481 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001482 template <typename NAME>
1483 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001484 ast::VariableList params,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001485 typ::Type type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001486 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001487 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001488 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001489 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001490 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1491 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001492 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001493 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001494 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001495 }
1496
Ben Clayton49668bb2021-04-17 05:32:01 +00001497 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001498 /// @param source the source information
1499 /// @returns the return statement pointer
1500 ast::ReturnStatement* Return(const Source& source) {
1501 return create<ast::ReturnStatement>(source);
1502 }
1503
1504 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001505 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001506 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1507
1508 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001509 /// @param source the source information
1510 /// @param val the return value
1511 /// @returns the return statement pointer
1512 template <typename EXPR>
1513 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1514 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1515 }
1516
1517 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001518 /// @param val the return value
1519 /// @returns the return statement pointer
1520 template <typename EXPR>
1521 ast::ReturnStatement* Return(EXPR&& val) {
1522 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001523 }
1524
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001525 /// Creates a ast::Struct and sem::StructType, registering the
1526 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001527 /// @param source the source information
1528 /// @param name the struct name
1529 /// @param members the struct members
1530 /// @param decorations the optional struct decorations
1531 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001532 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001533 typ::Struct Structure(const Source& source,
1534 NAME&& name,
1535 ast::StructMemberList members,
1536 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001537 auto sym = Sym(std::forward<NAME>(name));
1538 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1539 std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001540 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001541 AST().AddConstructedType(type);
1542 return type;
1543 }
1544
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001545 /// Creates a ast::Struct and sem::StructType, registering the
1546 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001547 /// @param name the struct name
1548 /// @param members the struct members
1549 /// @param decorations the optional struct decorations
1550 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001551 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001552 typ::Struct Structure(NAME&& name,
1553 ast::StructMemberList members,
1554 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001555 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001556 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001557 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001558 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001559 AST().AddConstructedType(type);
1560 return type;
1561 }
1562
Ben Clayton42d1e092021-02-02 14:29:15 +00001563 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001564 /// @param source the source information
1565 /// @param name the struct member name
1566 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001567 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001568 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001569 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001570 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001571 NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001572 typ::Type type,
Ben Claytond614dd52021-03-15 10:43:11 +00001573 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001574 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001575 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1576 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001577 }
1578
Ben Clayton42d1e092021-02-02 14:29:15 +00001579 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001580 /// @param name the struct member name
1581 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001582 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001583 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001584 template <typename NAME>
1585 ast::StructMember* Member(NAME&& name,
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001586 typ::Type type,
Ben Claytond614dd52021-03-15 10:43:11 +00001587 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001588 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001589 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1590 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001591 }
1592
Ben Claytonbab31972021-02-08 21:16:21 +00001593 /// Creates a ast::StructMember with the given byte offset
1594 /// @param offset the offset to use in the StructMemberOffsetDecoration
1595 /// @param name the struct member name
1596 /// @param type the struct member type
1597 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001598 template <typename NAME>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001599 ast::StructMember* Member(uint32_t offset, NAME&& name, typ::Type type) {
1600 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001601 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001602 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001603 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001604 create<ast::StructMemberOffsetDecoration>(offset),
1605 });
1606 }
1607
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001608 /// Creates a ast::BlockStatement with input statements
1609 /// @param statements statements of block
1610 /// @returns the block statement pointer
1611 template <typename... Statements>
1612 ast::BlockStatement* Block(Statements&&... statements) {
1613 return create<ast::BlockStatement>(
1614 ast::StatementList{std::forward<Statements>(statements)...});
1615 }
1616
1617 /// Creates a ast::ElseStatement with input condition and body
1618 /// @param condition the else condition expression
1619 /// @param body the else body
1620 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001621 template <typename CONDITION>
1622 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1623 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1624 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001625 }
1626
1627 /// Creates a ast::IfStatement with input condition, body, and optional
1628 /// variadic else statements
1629 /// @param condition the if statement condition expression
1630 /// @param body the if statement body
1631 /// @param elseStatements optional variadic else statements
1632 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001633 template <typename CONDITION, typename... ELSE_STATEMENTS>
1634 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001635 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001636 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001637 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001638 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001639 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001640 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001641 }
1642
1643 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001644 /// @param source the source information
1645 /// @param lhs the left hand side expression initializer
1646 /// @param rhs the right hand side expression initializer
1647 /// @returns the assignment statement pointer
1648 template <typename LhsExpressionInit, typename RhsExpressionInit>
1649 ast::AssignmentStatement* Assign(const Source& source,
1650 LhsExpressionInit&& lhs,
1651 RhsExpressionInit&& rhs) {
1652 return create<ast::AssignmentStatement>(
1653 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1654 Expr(std::forward<RhsExpressionInit>(rhs)));
1655 }
1656
1657 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001658 /// @param lhs the left hand side expression initializer
1659 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001660 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001661 template <typename LhsExpressionInit, typename RhsExpressionInit>
1662 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1663 RhsExpressionInit&& rhs) {
1664 return create<ast::AssignmentStatement>(
1665 Expr(std::forward<LhsExpressionInit>(lhs)),
1666 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001667 }
1668
1669 /// Creates a ast::LoopStatement with input body and optional continuing
1670 /// @param body the loop body
1671 /// @param continuing the optional continuing block
1672 /// @returns the loop statement pointer
1673 ast::LoopStatement* Loop(ast::BlockStatement* body,
1674 ast::BlockStatement* continuing = nullptr) {
1675 return create<ast::LoopStatement>(body, continuing);
1676 }
1677
1678 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001679 /// @param source the source information
1680 /// @param var the variable to wrap in a decl statement
1681 /// @returns the variable decl statement pointer
1682 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1683 return create<ast::VariableDeclStatement>(source, var);
1684 }
1685
1686 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001687 /// @param var the variable to wrap in a decl statement
1688 /// @returns the variable decl statement pointer
1689 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1690 return create<ast::VariableDeclStatement>(var);
1691 }
1692
Antonio Maioranocea744d2021-03-25 12:55:27 +00001693 /// Creates a ast::SwitchStatement with input expression and cases
1694 /// @param condition the condition expression initializer
1695 /// @param cases case statements
1696 /// @returns the switch statement pointer
1697 template <typename ExpressionInit, typename... Cases>
1698 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1699 return create<ast::SwitchStatement>(
1700 Expr(std::forward<ExpressionInit>(condition)),
1701 ast::CaseStatementList{std::forward<Cases>(cases)...});
1702 }
1703
1704 /// Creates a ast::CaseStatement with input list of selectors, and body
1705 /// @param selectors list of selectors
1706 /// @param body the case body
1707 /// @returns the case statement pointer
1708 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1709 ast::BlockStatement* body = nullptr) {
1710 return create<ast::CaseStatement>(std::move(selectors),
1711 body ? body : Block());
1712 }
1713
1714 /// Convenient overload that takes a single selector
1715 /// @param selector a single case selector
1716 /// @param body the case body
1717 /// @returns the case statement pointer
1718 ast::CaseStatement* Case(ast::IntLiteral* selector,
1719 ast::BlockStatement* body = nullptr) {
1720 return Case(ast::CaseSelectorList{selector}, body);
1721 }
1722
1723 /// Convenience function that creates a 'default' ast::CaseStatement
1724 /// @param body the case body
1725 /// @returns the case statement pointer
1726 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1727 return Case(ast::CaseSelectorList{}, body);
1728 }
1729
James Price68f558f2021-04-06 15:51:47 +00001730 /// Creates an ast::BuiltinDecoration
1731 /// @param source the source information
1732 /// @param builtin the builtin value
1733 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001734 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001735 return create<ast::BuiltinDecoration>(source, builtin);
1736 }
1737
1738 /// Creates an ast::BuiltinDecoration
1739 /// @param builtin the builtin value
1740 /// @returns the builtin decoration pointer
1741 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1742 return create<ast::BuiltinDecoration>(source_, builtin);
1743 }
1744
1745 /// Creates an ast::LocationDecoration
1746 /// @param source the source information
1747 /// @param location the location value
1748 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001749 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001750 return create<ast::LocationDecoration>(source, location);
1751 }
1752
1753 /// Creates an ast::LocationDecoration
1754 /// @param location the location value
1755 /// @returns the location decoration pointer
1756 ast::LocationDecoration* Location(uint32_t location) {
1757 return create<ast::LocationDecoration>(source_, location);
1758 }
1759
1760 /// Creates an ast::StageDecoration
1761 /// @param source the source information
1762 /// @param stage the pipeline stage
1763 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001764 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001765 return create<ast::StageDecoration>(source, stage);
1766 }
1767
1768 /// Creates an ast::StageDecoration
1769 /// @param stage the pipeline stage
1770 /// @returns the stage decoration pointer
1771 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1772 return create<ast::StageDecoration>(source_, stage);
1773 }
1774
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001775 /// Sets the current builder source to `src`
1776 /// @param src the Source used for future create() calls
1777 void SetSource(const Source& src) {
1778 AssertNotMoved();
1779 source_ = src;
1780 }
1781
1782 /// Sets the current builder source to `loc`
1783 /// @param loc the Source used for future create() calls
1784 void SetSource(const Source::Location& loc) {
1785 AssertNotMoved();
1786 source_ = Source(loc);
1787 }
1788
Ben Clayton33352542021-01-29 16:43:41 +00001789 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001790 /// @note As the Resolver is run when the Program is built, this will only be
1791 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001792 /// @param expr the AST expression
1793 /// @return the resolved semantic type for the expression, or nullptr if the
1794 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001795 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001796
Ben Clayton169512e2021-04-17 05:52:11 +00001797 /// Wraps the ast::Literal in a statement. This is used by tests that
1798 /// construct a partial AST and require the Resolver to reach these
1799 /// nodes.
1800 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1801 /// @return the ast::Statement that wraps the ast::Statement
1802 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001803 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001804 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001805 /// nodes.
1806 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1807 /// @return the ast::Statement that wraps the ast::Expression
1808 ast::Statement* WrapInStatement(ast::Expression* expr);
1809 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001810 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001811 /// these nodes.
1812 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1813 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1814 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1815 /// Returns the statement argument. Used as a passthrough-overload by
1816 /// WrapInFunction().
1817 /// @param stmt the ast::Statement
1818 /// @return `stmt`
1819 ast::Statement* WrapInStatement(ast::Statement* stmt);
1820 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001821 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001822 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001823 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001824 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001825 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001826 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001827 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001828 }
1829 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001830 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001831 /// @returns the function
1832 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001833
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001834 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001835 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001836
1837 protected:
1838 /// Asserts that the builder has not been moved.
1839 void AssertNotMoved() const;
1840
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001841 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001842 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001843 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001844 ASTNodeAllocator ast_nodes_;
1845 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001846 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001847 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001848 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001849 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001850
1851 /// The source to use when creating AST nodes without providing a Source as
1852 /// the first argument.
1853 Source source_;
1854
Ben Clayton5f0ea112021-03-09 10:54:37 +00001855 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001856 /// program when built.
1857 bool resolve_on_build_ = true;
1858
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001859 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1860 bool moved_ = false;
1861};
1862
1863//! @cond Doxygen_Suppress
1864// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1865template <>
1866struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001867 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001868 return t->i32();
1869 }
1870};
1871template <>
1872struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001873 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001874 return t->u32();
1875 }
1876};
1877template <>
1878struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001879 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001880 return t->f32();
1881 }
1882};
1883template <>
1884struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001885 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001886 return t->bool_();
1887 }
1888};
1889template <>
1890struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001891 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001892 return t->void_();
1893 }
1894};
1895//! @endcond
1896
Ben Clayton917b14b2021-04-19 16:50:23 +00001897/// @param builder the ProgramBuilder
1898/// @returns the ProgramID of the ProgramBuilder
1899inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1900 return builder->ID();
1901}
1902
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001903} // namespace tint
1904
1905#endif // SRC_PROGRAM_BUILDER_H_