blob: f0975f9d0b67455b96ccd1bb982c4531f393fdcb [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"
James Pricef2f3bfc2021-05-13 20:32:32 +000041#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000043#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/scalar_constructor_expression.h"
46#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000047#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000049#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000050#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000053#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000055#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000056#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000058#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000059#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000060#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000061#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000062#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000064#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000065#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000066#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000067#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000068#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000069#include "src/sem/f32_type.h"
70#include "src/sem/i32_type.h"
71#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000073#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/sampled_texture_type.h"
75#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000076#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000077#include "src/sem/u32_type.h"
78#include "src/sem/vector_type.h"
79#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000080#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000081
82namespace tint {
83
Ben Clayton401b96b2021-02-03 17:19:59 +000084// Forward declarations
85namespace ast {
86class VariableDeclStatement;
87} // namespace ast
88
Ben Claytona6b9a8e2021-01-26 16:57:10 +000089class CloneContext;
90
91/// ProgramBuilder is a mutable builder for a Program.
92/// To construct a Program, populate the builder and then `std::move` it to a
93/// Program.
94class ProgramBuilder {
95 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000096 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
97 using ASTNodeAllocator = BlockAllocator<ast::Node>;
98
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000099 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
100 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000101
102 /// `i32` is a type alias to `int`.
103 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
104 /// WGSL syntax.
105 /// Note: this is intentionally not aliased to uint32_t as we want integer
106 /// literals passed to the builder to match WGSL's integer literal types.
107 using i32 = decltype(1);
108 /// `u32` is a type alias to `unsigned int`.
109 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
110 /// WGSL syntax.
111 /// Note: this is intentionally not aliased to uint32_t as we want integer
112 /// literals passed to the builder to match WGSL's integer literal types.
113 using u32 = decltype(1u);
114 /// `f32` is a type alias to `float`
115 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
116 /// WGSL syntax.
117 using f32 = float;
118
119 /// Constructor
120 ProgramBuilder();
121
122 /// Move constructor
123 /// @param rhs the builder to move
124 ProgramBuilder(ProgramBuilder&& rhs);
125
126 /// Destructor
127 virtual ~ProgramBuilder();
128
129 /// Move assignment operator
130 /// @param rhs the builder to move
131 /// @return this builder
132 ProgramBuilder& operator=(ProgramBuilder&& rhs);
133
Ben Claytone43c8302021-01-29 11:59:32 +0000134 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
135 /// making a deep clone of the Program contents.
136 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
137 /// existing immutable program.
138 /// As the returned ProgramBuilder wraps `program`, `program` must not be
139 /// destructed or assigned while using the returned ProgramBuilder.
140 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
141 /// function. See crbug.com/tint/460.
142 /// @param program the immutable Program to wrap
143 /// @return the ProgramBuilder that wraps `program`
144 static ProgramBuilder Wrap(const Program* program);
145
Ben Claytone6995de2021-04-13 23:27:27 +0000146 /// @returns the unique identifier for this program
147 ProgramID ID() const { return id_; }
148
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000149 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000150 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000151 AssertNotMoved();
152 return types_;
153 }
154
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000156 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000157 AssertNotMoved();
158 return types_;
159 }
160
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000162 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000163 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000164 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000165 }
166
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000168 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000169 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000170 return ast_nodes_;
171 }
172
173 /// @returns a reference to the program's semantic nodes storage
174 SemNodeAllocator& SemNodes() {
175 AssertNotMoved();
176 return sem_nodes_;
177 }
178
179 /// @returns a reference to the program's semantic nodes storage
180 const SemNodeAllocator& SemNodes() const {
181 AssertNotMoved();
182 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000183 }
184
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000185 /// @returns a reference to the program's AST root Module
186 ast::Module& AST() {
187 AssertNotMoved();
188 return *ast_;
189 }
190
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000191 /// @returns a reference to the program's AST root Module
192 const ast::Module& AST() const {
193 AssertNotMoved();
194 return *ast_;
195 }
196
197 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000198 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000199 AssertNotMoved();
200 return sem_;
201 }
202
203 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000204 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000205 AssertNotMoved();
206 return sem_;
207 }
208
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000209 /// @returns a reference to the program's SymbolTable
210 SymbolTable& Symbols() {
211 AssertNotMoved();
212 return symbols_;
213 }
214
Ben Clayton708dc2d2021-01-29 11:22:40 +0000215 /// @returns a reference to the program's SymbolTable
216 const SymbolTable& Symbols() const {
217 AssertNotMoved();
218 return symbols_;
219 }
220
Ben Clayton844217f2021-01-27 18:49:05 +0000221 /// @returns a reference to the program's diagnostics
222 diag::List& Diagnostics() {
223 AssertNotMoved();
224 return diagnostics_;
225 }
226
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000227 /// @returns a reference to the program's diagnostics
228 const diag::List& Diagnostics() const {
229 AssertNotMoved();
230 return diagnostics_;
231 }
232
Ben Clayton5f0ea112021-03-09 10:54:37 +0000233 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000234 /// @param enable the new flag value (defaults to true)
235 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
236
Ben Clayton5f0ea112021-03-09 10:54:37 +0000237 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000238 /// built.
239 bool ResolveOnBuild() const { return resolve_on_build_; }
240
Ben Clayton844217f2021-01-27 18:49:05 +0000241 /// @returns true if the program has no error diagnostics and is not missing
242 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000243 bool IsValid() const;
244
Ben Clayton708dc2d2021-01-29 11:22:40 +0000245 /// Writes a representation of the node to the output stream
246 /// @note unlike str(), to_str() does not automatically demangle the string.
247 /// @param node the AST node
248 /// @param out the stream to write to
249 /// @param indent number of spaces to indent the node when writing
250 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
251 node->to_str(Sem(), out, indent);
252 }
253
254 /// Returns a demangled, string representation of `node`.
255 /// @param node the AST node
256 /// @returns a string representation of the node
257 std::string str(const ast::Node* node) const;
258
Ben Clayton7fdfff12021-01-29 15:17:30 +0000259 /// Creates a new ast::Node owned by the ProgramBuilder. When the
260 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000261 /// @param source the Source of the node
262 /// @param args the arguments to pass to the type constructor
263 /// @returns the node pointer
264 template <typename T, typename... ARGS>
265 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
266 ARGS&&... args) {
267 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000268 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000269 }
270
Ben Clayton7fdfff12021-01-29 15:17:30 +0000271 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
272 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000273 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000274 /// When the ProgramBuilder is destructed, the ast::Node will also be
275 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000276 /// @returns the node pointer
277 template <typename T>
278 traits::EnableIfIsType<T, ast::Node>* create() {
279 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000280 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000281 }
282
Ben Clayton7fdfff12021-01-29 15:17:30 +0000283 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
284 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000285 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000286 /// When the ProgramBuilder is destructed, the ast::Node will also be
287 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000288 /// @param arg0 the first arguments to pass to the type constructor
289 /// @param args the remaining arguments to pass to the type constructor
290 /// @returns the node pointer
291 template <typename T, typename ARG0, typename... ARGS>
292 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
293 traits::IsTypeOrDerived<T, ast::Node>::value &&
294 !traits::IsTypeOrDerived<ARG0, Source>::value,
295 T>*
296 create(ARG0&& arg0, ARGS&&... args) {
297 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000298 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000299 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000300 }
301
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000302 /// Creates a new sem::Node owned by the ProgramBuilder.
303 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000304 /// destructed.
305 /// @param args the arguments to pass to the type constructor
306 /// @returns the node pointer
307 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000308 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
309 !traits::IsTypeOrDerived<T, sem::Type>::value,
310 T>*
311 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000312 AssertNotMoved();
313 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
314 }
315
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000316 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000317 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
318 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000319 /// Types are unique (de-aliased), and so calling create() for the same `T`
320 /// and arguments will return the same pointer.
321 /// @warning Use this method to acquire a type only if all of its type
322 /// information is provided in the constructor arguments `args`.<br>
323 /// If the type requires additional configuration after construction that
324 /// affect its fundamental type, build the type with `std::make_unique`, make
325 /// any necessary alterations and then call unique_type() instead.
326 /// @param args the arguments to pass to the type constructor
327 /// @returns the de-aliased type pointer
328 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000329 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
330 static_assert(std::is_base_of<sem::Type, T>::value,
331 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000332 AssertNotMoved();
333 return types_.Get<T>(std::forward<ARGS>(args)...);
334 }
335
336 /// Marks this builder as moved, preventing any further use of the builder.
337 void MarkAsMoved();
338
339 //////////////////////////////////////////////////////////////////////////////
340 // TypesBuilder
341 //////////////////////////////////////////////////////////////////////////////
342
343 /// TypesBuilder holds basic `tint` types and methods for constructing
344 /// complex types.
345 class TypesBuilder {
346 public:
347 /// Constructor
348 /// @param builder the program builder
349 explicit TypesBuilder(ProgramBuilder* builder);
350
351 /// @return the tint AST type for the C type `T`.
352 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000353 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000354 return CToAST<T>::get(this);
355 }
356
357 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000358 typ::Bool bool_() const {
359 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
360 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000361
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000362 /// @param source the Source of the node
363 /// @returns a boolean type
364 typ::Bool bool_(const Source& source) const {
365 return {builder->create<ast::Bool>(source), builder->create<sem::Bool>()};
366 }
367
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000369 typ::F32 f32() const {
370 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
371 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000372
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000373 /// @param source the Source of the node
374 /// @returns a f32 type
375 typ::F32 f32(const Source& source) const {
376 return {builder->create<ast::F32>(source), builder->create<sem::F32>()};
377 }
378
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000379 /// @returns a i32 type
Ben Claytoncf421bb2021-05-19 17:47:11 +0000380 typ::I32 i32() const { return {builder->create<ast::I32>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000381
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000382 /// @param source the Source of the node
383 /// @returns a i32 type
384 typ::I32 i32(const Source& source) const {
Ben Claytoncf421bb2021-05-19 17:47:11 +0000385 return {builder->create<ast::I32>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000386 }
387
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000388 /// @returns a u32 type
Ben Clayton58dec172021-05-19 17:47:11 +0000389 typ::U32 u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000390
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000391 /// @param source the Source of the node
392 /// @returns a u32 type
393 typ::U32 u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000394 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000395 }
396
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000397 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000398 typ::Void void_() const {
399 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
400 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000401
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000402 /// @param source the Source of the node
403 /// @returns a void type
404 typ::Void void_(const Source& source) const {
405 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
406 }
407
Antonio Maiorano25436862021-04-13 13:32:33 +0000408 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000409 /// @param n vector width in elements
410 /// @return the tint AST type for a `n`-element vector of `type`.
411 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000412 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000413 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
414 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000415 }
416
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000417 /// @param source the Source of the node
418 /// @param type vector subtype
419 /// @param n vector width in elements
420 /// @return the tint AST type for a `n`-element vector of `type`.
421 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000422 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000423 return {
424 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
425 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000426 }
427
Antonio Maiorano25436862021-04-13 13:32:33 +0000428 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000429 /// @return the tint AST type for a 2-element vector of `type`.
430 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
431
432 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000433 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000434 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000435
436 /// @param type vector subtype
437 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000438 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
439
440 /// @param n vector width in elements
441 /// @return the tint AST type for a `n`-element vector of `type`.
442 template <typename T>
443 typ::Vector vec(uint32_t n) const {
444 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000445 }
446
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000447 /// @return the tint AST type for a 2-element vector of the C type `T`.
448 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000449 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000450 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000451 }
452
453 /// @return the tint AST type for a 3-element vector of the C type `T`.
454 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000455 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000456 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000457 }
458
459 /// @return the tint AST type for a 4-element vector of the C type `T`.
460 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000461 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000462 return vec4(Of<T>());
463 }
464
465 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000466 /// @param columns number of columns for the matrix
467 /// @param rows number of rows for the matrix
468 /// @return the tint AST type for a matrix of `type`
469 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000470 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000471 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
472 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000473 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000474 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000475 }
476
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000477 /// @param source the Source of the node
478 /// @param type matrix subtype
479 /// @param columns number of columns for the matrix
480 /// @param rows number of rows for the matrix
481 /// @return the tint AST type for a matrix of `type`
482 typ::Matrix mat(const Source& source,
483 typ::Type type,
484 uint32_t columns,
485 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000486 return {type.ast
487 ? builder->create<ast::Matrix>(source, type, rows, columns)
488 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000489 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000490 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000491 }
492
Ben Claytone204f272021-04-22 14:40:23 +0000493 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000494 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000495 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000496
497 /// @param type matrix subtype
498 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000499 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000500
501 /// @param type matrix subtype
502 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000503 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000504
505 /// @param type matrix subtype
506 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000507 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000508
509 /// @param type matrix subtype
510 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000511 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000512
513 /// @param type matrix subtype
514 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000515 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000516
517 /// @param type matrix subtype
518 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000519 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000520
521 /// @param type matrix subtype
522 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000523 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000524
525 /// @param type matrix subtype
526 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000527 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000528
Ben Claytone204f272021-04-22 14:40:23 +0000529 /// @param columns number of columns for the matrix
530 /// @param rows number of rows for the matrix
531 /// @return the tint AST type for a matrix of `type`
532 template <typename T>
533 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
534 return mat(Of<T>(), columns, rows);
535 }
536
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000537 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
538 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000539 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000540 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000541 }
542
543 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
544 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000545 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000546 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000547 }
548
549 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
550 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000551 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000552 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000553 }
554
555 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
556 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000557 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000558 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559 }
560
561 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
562 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000563 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000564 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000565 }
566
567 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
568 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000569 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000570 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 }
572
573 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
574 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000575 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000576 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 }
578
579 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
580 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000581 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000582 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000583 }
584
585 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
586 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000587 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000588 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000589 }
590
591 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000592 /// @param n the array size. 0 represents a runtime-array
593 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000594 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000595 ast::Array* array(typ::Type subtype,
596 uint32_t n = 0,
597 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000598 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000599 return builder->create<ast::Array>(subtype, n, decos);
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`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000607 ast::Array* array(const Source& source,
608 typ::Type subtype,
609 uint32_t n = 0,
610 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000611 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000612 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000613 }
614
Ben Claytonbab31972021-02-08 21:16:21 +0000615 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000616 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000617 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000618 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000619 ast::Array* array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000620 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000621 ast::DecorationList decos;
622 if (stride) {
623 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
624 }
625 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000626 }
627
Ben Clayton0f88b312021-05-04 17:36:31 +0000628 /// @param source the Source of the node
629 /// @param subtype the array element type
630 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000631 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000632 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000633 ast::Array* array(const Source& source,
634 typ::Type subtype,
635 uint32_t n,
636 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000637 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000638 ast::DecorationList decos;
639 if (stride) {
640 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
641 }
642 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000643 }
644
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000645 /// @return the tint AST type for an array of size `N` of type `T`
646 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000647 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000648 return array(Of<T>(), N);
649 }
650
Ben Claytonbab31972021-02-08 21:16:21 +0000651 /// @param stride the array stride
652 /// @return the tint AST type for an array of size `N` of type `T`
653 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000654 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000655 return array(Of<T>(), N, stride);
656 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000657
Ben Clayton0f88b312021-05-04 17:36:31 +0000658 /// Creates a type name
659 /// @param name the name
660 /// @returns the type name
661 template <typename NAME>
662 ast::TypeName* type_name(NAME&& name) const {
663 return builder->create<ast::TypeName>(
664 builder->Sym(std::forward<NAME>(name)));
665 }
666
667 /// Creates a type name
668 /// @param source the Source of the node
669 /// @param name the name
670 /// @returns the type name
671 template <typename NAME>
672 ast::TypeName* type_name(const Source& source, NAME&& name) const {
673 return builder->create<ast::TypeName>(
674 source, builder->Sym(std::forward<NAME>(name)));
675 }
676
Ben Clayton42d1e092021-02-02 14:29:15 +0000677 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000678 /// @param name the alias name
679 /// @param type the alias type
680 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000681 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000682 ast::Alias* alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000683 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000684 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000685 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000686 }
687
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000688 /// Creates an alias type
689 /// @param source the Source of the node
690 /// @param name the alias name
691 /// @param type the alias type
692 /// @returns the alias pointer
693 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000694 ast::Alias* alias(const Source& source, NAME&& name, typ::Type type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000695 type = MaybeCreateTypename(type);
696 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000697 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000698 }
699
Ben Clayton4db10dd2021-04-16 08:47:14 +0000700 /// Creates an access control qualifier type
701 /// @param access the access control
702 /// @param type the inner type
703 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000704 ast::AccessControl* access(ast::AccessControl::Access access,
705 const ast::Type* type) const {
706 type = MaybeCreateTypename(type).ast;
707 return type ? builder->create<ast::AccessControl>(access, type) : nullptr;
Ben Clayton4db10dd2021-04-16 08:47:14 +0000708 }
709
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000710 /// Creates an access control qualifier type
711 /// @param source the Source of the node
712 /// @param access the access control
713 /// @param type the inner type
714 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000715 ast::AccessControl* access(const Source& source,
716 ast::AccessControl::Access access,
717 const ast::Type* type) const {
718 type = MaybeCreateTypename(type).ast;
719 return type ? builder->create<ast::AccessControl>(source, access, type)
720 : nullptr;
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000721 }
722
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000723 /// @param type the type of the pointer
724 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000725 /// @return the pointer to `type` with the given ast::StorageClass
726 typ::Pointer pointer(typ::Type type,
727 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000728 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000729 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
730 : nullptr,
731 type.sem ? builder->create<sem::Pointer>(type, storage_class)
732 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000733 }
734
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000735 /// @param source the Source of the node
736 /// @param type the type of the pointer
737 /// @param storage_class the storage class of the pointer
738 /// @return the pointer to `type` with the given ast::StorageClass
739 typ::Pointer pointer(const Source& source,
740 typ::Type type,
741 ast::StorageClass storage_class) const {
742 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000743 return {type.ast
744 ? builder->create<ast::Pointer>(source, type, storage_class)
745 : nullptr,
746 type.sem ? builder->create<sem::Pointer>(type, storage_class)
747 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000748 }
749
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000750 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000751 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000752 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000753 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000754 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000755 }
756
Ben Claytone204f272021-04-22 14:40:23 +0000757 /// @param kind the kind of sampler
758 /// @returns the sampler
759 typ::Sampler sampler(ast::SamplerKind kind) const {
760 return {builder->create<ast::Sampler>(kind),
761 builder->create<sem::Sampler>(kind)};
762 }
763
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000764 /// @param source the Source of the node
765 /// @param kind the kind of sampler
766 /// @returns the sampler
767 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
768 return {builder->create<ast::Sampler>(source, kind),
769 builder->create<sem::Sampler>(kind)};
770 }
771
Ben Claytone204f272021-04-22 14:40:23 +0000772 /// @param dims the dimensionality of the texture
773 /// @returns the depth texture
774 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
775 return {builder->create<ast::DepthTexture>(dims),
776 builder->create<sem::DepthTexture>(dims)};
777 }
778
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000779 /// @param source the Source of the node
780 /// @param dims the dimensionality of the texture
781 /// @returns the depth texture
782 typ::DepthTexture depth_texture(const Source& source,
783 ast::TextureDimension dims) const {
784 return {builder->create<ast::DepthTexture>(source, dims),
785 builder->create<sem::DepthTexture>(dims)};
786 }
787
Ben Claytone204f272021-04-22 14:40:23 +0000788 /// @param dims the dimensionality of the texture
789 /// @param subtype the texture subtype.
790 /// @returns the sampled texture
791 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
792 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000793 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
794 : nullptr,
795 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
796 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000797 }
798
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000799 /// @param source the Source of the node
800 /// @param dims the dimensionality of the texture
801 /// @param subtype the texture subtype.
802 /// @returns the sampled texture
803 typ::SampledTexture sampled_texture(const Source& source,
804 ast::TextureDimension dims,
805 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000806 return {subtype.ast
807 ? builder->create<ast::SampledTexture>(source, dims, subtype)
808 : nullptr,
809 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
810 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000811 }
812
Ben Claytone204f272021-04-22 14:40:23 +0000813 /// @param dims the dimensionality of the texture
814 /// @param subtype the texture subtype.
815 /// @returns the multisampled texture
816 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
817 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000818 return {
819 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
820 : nullptr,
821 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
822 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000823 }
824
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000825 /// @param source the Source of the node
826 /// @param dims the dimensionality of the texture
827 /// @param subtype the texture subtype.
828 /// @returns the multisampled texture
829 typ::MultisampledTexture multisampled_texture(const Source& source,
830 ast::TextureDimension dims,
831 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000832 return {
833 subtype.ast
834 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
835 : nullptr,
836 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
837 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000838 }
839
Ben Claytone204f272021-04-22 14:40:23 +0000840 /// @param dims the dimensionality of the texture
841 /// @param format the image format of the texture
842 /// @returns the storage texture
843 typ::StorageTexture storage_texture(ast::TextureDimension dims,
844 ast::ImageFormat format) const {
845 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
846 auto* sem_subtype =
847 sem::StorageTexture::SubtypeFor(format, builder->Types());
848 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000849 builder->create<sem::StorageTexture>(
850 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000851 }
852
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000853 /// @param source the Source of the node
854 /// @param dims the dimensionality of the texture
855 /// @param format the image format of the texture
856 /// @returns the storage texture
857 typ::StorageTexture storage_texture(const Source& source,
858 ast::TextureDimension dims,
859 ast::ImageFormat format) const {
860 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
861 auto* sem_subtype =
862 sem::StorageTexture::SubtypeFor(format, builder->Types());
863 return {builder->create<ast::StorageTexture>(source, dims, format,
864 ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000865 builder->create<sem::StorageTexture>(
866 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000867 }
868
Brandon Jones7b257692021-05-17 17:40:17 +0000869 /// @returns the external texture
870 typ::ExternalTexture external_texture() const {
871 return {builder->create<ast::ExternalTexture>(),
872 builder->create<sem::ExternalTexture>()};
873 }
874
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000875 /// @param source the Source of the node
876 /// @returns the external texture
877 typ::ExternalTexture external_texture(const Source& source) const {
878 return {builder->create<ast::ExternalTexture>(source),
879 builder->create<sem::ExternalTexture>()};
880 }
881
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000882 /// If ty is a ast::Struct or ast::Alias, the returned type is an
883 /// ast::TypeName of the given type's name, otherwise type is returned.
884 /// @param type the type
885 /// @return either type or a pointer to a new ast::TypeName
886 typ::Type MaybeCreateTypename(typ::Type type) const;
887
Ben Claytonf5f311e2021-04-28 14:31:23 +0000888 /// The ProgramBuilder
889 ProgramBuilder* const builder;
890
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000891 private:
892 /// CToAST<T> is specialized for various `T` types and each specialization
893 /// contains a single static `get()` method for obtaining the corresponding
894 /// AST type for the C type `T`.
895 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000896 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000897 template <typename T>
898 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 };
900
901 //////////////////////////////////////////////////////////////////////////////
902 // AST helper methods
903 //////////////////////////////////////////////////////////////////////////////
904
James Price65ae64d2021-04-29 12:59:14 +0000905 /// @return a new unnamed symbol
906 Symbol Sym() { return Symbols().New(); }
907
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000908 /// @param name the symbol string
909 /// @return a Symbol with the given name
910 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
911
912 /// @param sym the symbol
913 /// @return `sym`
914 Symbol Sym(Symbol sym) { return sym; }
915
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000916 /// @param expr the expression
917 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000918 template <typename T>
919 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
920 return expr;
921 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000922
Ben Claytonb8ea5912021-04-19 16:52:42 +0000923 /// Passthrough for nullptr
924 /// @return nullptr
925 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
926
Ben Claytonfe0910f2021-05-17 15:51:47 +0000927 /// @param source the source information
928 /// @param symbol the identifier symbol
929 /// @return an ast::IdentifierExpression with the given symbol
930 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
931 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 }
933
Ben Clayton46d78d72021-02-10 21:34:26 +0000934 /// @param symbol the identifier symbol
935 /// @return an ast::IdentifierExpression with the given symbol
936 ast::IdentifierExpression* Expr(Symbol symbol) {
937 return create<ast::IdentifierExpression>(symbol);
938 }
939
Ben Claytonfe0910f2021-05-17 15:51:47 +0000940 /// @param source the source information
941 /// @param variable the AST variable
942 /// @return an ast::IdentifierExpression with the variable's symbol
943 ast::IdentifierExpression* Expr(const Source& source,
944 ast::Variable* variable) {
945 return create<ast::IdentifierExpression>(source, variable->symbol());
946 }
947
Ben Claytonb8ea5912021-04-19 16:52:42 +0000948 /// @param variable the AST variable
949 /// @return an ast::IdentifierExpression with the variable's symbol
950 ast::IdentifierExpression* Expr(ast::Variable* variable) {
951 return create<ast::IdentifierExpression>(variable->symbol());
952 }
953
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000954 /// @param source the source information
955 /// @param name the identifier name
956 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000957 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
958 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
959 }
960
961 /// @param name the identifier name
962 /// @return an ast::IdentifierExpression with the given name
963 ast::IdentifierExpression* Expr(const char* name) {
964 return create<ast::IdentifierExpression>(Symbols().Register(name));
965 }
966
967 /// @param source the source information
968 /// @param name the identifier name
969 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000970 ast::IdentifierExpression* Expr(const Source& source,
971 const std::string& name) {
972 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
973 }
974
975 /// @param name the identifier name
976 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000977 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000978 return create<ast::IdentifierExpression>(Symbols().Register(name));
979 }
980
Ben Claytonfe0910f2021-05-17 15:51:47 +0000981 /// @param source the source information
982 /// @param value the boolean value
983 /// @return a Scalar constructor for the given value
984 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
985 return create<ast::ScalarConstructorExpression>(source, Literal(value));
986 }
987
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000988 /// @param value the boolean value
989 /// @return a Scalar constructor for the given value
990 ast::ScalarConstructorExpression* Expr(bool value) {
991 return create<ast::ScalarConstructorExpression>(Literal(value));
992 }
993
Ben Claytonfe0910f2021-05-17 15:51:47 +0000994 /// @param source the source information
995 /// @param value the float value
996 /// @return a Scalar constructor for the given value
997 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
998 return create<ast::ScalarConstructorExpression>(source, Literal(value));
999 }
1000
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001001 /// @param value the float value
1002 /// @return a Scalar constructor for the given value
1003 ast::ScalarConstructorExpression* Expr(f32 value) {
1004 return create<ast::ScalarConstructorExpression>(Literal(value));
1005 }
1006
Ben Claytonfe0910f2021-05-17 15:51:47 +00001007 /// @param source the source information
1008 /// @param value the integer value
1009 /// @return a Scalar constructor for the given value
1010 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1011 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1012 }
1013
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001014 /// @param value the integer value
1015 /// @return a Scalar constructor for the given value
1016 ast::ScalarConstructorExpression* Expr(i32 value) {
1017 return create<ast::ScalarConstructorExpression>(Literal(value));
1018 }
1019
Ben Claytonfe0910f2021-05-17 15:51:47 +00001020 /// @param source the source information
1021 /// @param value the unsigned int value
1022 /// @return a Scalar constructor for the given value
1023 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1024 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1025 }
1026
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001027 /// @param value the unsigned int value
1028 /// @return a Scalar constructor for the given value
1029 ast::ScalarConstructorExpression* Expr(u32 value) {
1030 return create<ast::ScalarConstructorExpression>(Literal(value));
1031 }
1032
1033 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1034 /// `list`.
1035 /// @param list the list to append too
1036 /// @param arg the arg to create
1037 template <typename ARG>
1038 void Append(ast::ExpressionList& list, ARG&& arg) {
1039 list.emplace_back(Expr(std::forward<ARG>(arg)));
1040 }
1041
1042 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1043 /// then appends them to `list`.
1044 /// @param list the list to append too
1045 /// @param arg0 the first argument
1046 /// @param args the rest of the arguments
1047 template <typename ARG0, typename... ARGS>
1048 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1049 Append(list, std::forward<ARG0>(arg0));
1050 Append(list, std::forward<ARGS>(args)...);
1051 }
1052
1053 /// @return an empty list of expressions
1054 ast::ExpressionList ExprList() { return {}; }
1055
1056 /// @param args the list of expressions
1057 /// @return the list of expressions converted to `ast::Expression`s using
1058 /// `Expr()`,
1059 template <typename... ARGS>
1060 ast::ExpressionList ExprList(ARGS&&... args) {
1061 ast::ExpressionList list;
1062 list.reserve(sizeof...(args));
1063 Append(list, std::forward<ARGS>(args)...);
1064 return list;
1065 }
1066
1067 /// @param list the list of expressions
1068 /// @return `list`
1069 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1070
1071 /// @param val the boolan value
1072 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001073 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074
1075 /// @param val the float value
1076 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001077 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078
1079 /// @param val the unsigned int value
1080 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001081 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001082
1083 /// @param val the integer value
1084 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001085 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001086
1087 /// @param args the arguments for the type constructor
1088 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1089 /// of `args` converted to `ast::Expression`s using `Expr()`
1090 template <typename T, typename... ARGS>
1091 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001092 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001093 }
1094
1095 /// @param type the type to construct
1096 /// @param args the arguments for the constructor
1097 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1098 /// values `args`.
1099 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001100 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001101 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001102 return create<ast::TypeConstructorExpression>(
1103 type, ExprList(std::forward<ARGS>(args)...));
1104 }
1105
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001106 /// Creates a constructor expression that constructs an object of
1107 /// `type` filled with `elem_value`. For example,
1108 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1109 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1110 /// @param type the type to construct
1111 /// @param elem_value the initial or element value (for vec and mat) to
1112 /// construct with
1113 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001114 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001115 int elem_value = 0);
1116
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001117 /// @param args the arguments for the vector constructor
1118 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1119 /// `T`, constructed with the values `args`.
1120 template <typename T, typename... ARGS>
1121 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001122 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001123 }
1124
1125 /// @param args the arguments for the vector constructor
1126 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1127 /// `T`, constructed with the values `args`.
1128 template <typename T, typename... ARGS>
1129 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001130 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001131 }
1132
1133 /// @param args the arguments for the vector constructor
1134 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1135 /// `T`, constructed with the values `args`.
1136 template <typename T, typename... ARGS>
1137 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001138 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001139 }
1140
1141 /// @param args the arguments for the matrix constructor
1142 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1143 /// `T`, constructed with the values `args`.
1144 template <typename T, typename... ARGS>
1145 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001146 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001147 }
1148
1149 /// @param args the arguments for the matrix constructor
1150 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1151 /// `T`, constructed with the values `args`.
1152 template <typename T, typename... ARGS>
1153 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001154 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001155 }
1156
1157 /// @param args the arguments for the matrix constructor
1158 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1159 /// `T`, constructed with the values `args`.
1160 template <typename T, typename... ARGS>
1161 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001162 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001163 }
1164
1165 /// @param args the arguments for the matrix constructor
1166 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1167 /// `T`, constructed with the values `args`.
1168 template <typename T, typename... ARGS>
1169 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001170 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001171 }
1172
1173 /// @param args the arguments for the matrix constructor
1174 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1175 /// `T`, constructed with the values `args`.
1176 template <typename T, typename... ARGS>
1177 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001178 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001179 }
1180
1181 /// @param args the arguments for the matrix constructor
1182 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1183 /// `T`, constructed with the values `args`.
1184 template <typename T, typename... ARGS>
1185 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001186 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001187 }
1188
1189 /// @param args the arguments for the matrix constructor
1190 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1191 /// `T`, constructed with the values `args`.
1192 template <typename T, typename... ARGS>
1193 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001194 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001195 }
1196
1197 /// @param args the arguments for the matrix constructor
1198 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1199 /// `T`, constructed with the values `args`.
1200 template <typename T, typename... ARGS>
1201 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001202 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001203 }
1204
1205 /// @param args the arguments for the matrix constructor
1206 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1207 /// `T`, constructed with the values `args`.
1208 template <typename T, typename... ARGS>
1209 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001210 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001211 }
1212
1213 /// @param args the arguments for the array constructor
1214 /// @return an `ast::TypeConstructorExpression` of an array with element type
1215 /// `T`, constructed with the values `args`.
1216 template <typename T, int N = 0, typename... ARGS>
1217 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001218 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001219 }
1220
1221 /// @param subtype the array element type
1222 /// @param n the array size. 0 represents a runtime-array.
1223 /// @param args the arguments for the array constructor
1224 /// @return an `ast::TypeConstructorExpression` of an array with element type
1225 /// `subtype`, constructed with the values `args`.
1226 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001227 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228 uint32_t n,
1229 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001230 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231 }
1232
1233 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001234 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001235 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001236 /// @param constructor constructor expression
1237 /// @param decorations variable decorations
1238 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001239 template <typename NAME>
1240 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001241 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001242 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001243 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001244 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001245 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001246 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1247 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001248 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001249
1250 /// @param source the variable source
1251 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001252 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001253 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001254 /// @param constructor constructor expression
1255 /// @param decorations variable decorations
1256 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001257 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001258 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001259 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001260 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001261 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001262 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001263 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001264 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001265 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001266 type, false, constructor, decorations);
1267 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001268
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001269 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001270 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001271 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001272 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001273 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001274 template <typename NAME>
1275 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001276 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001277 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001278 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001279 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001280 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001281 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001282 constructor, decorations);
1283 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001284
1285 /// @param source the variable source
1286 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001287 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001288 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001289 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001290 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001291 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001292 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001293 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001294 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001295 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001296 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001297 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001298 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001299 ast::StorageClass::kNone, type, true,
1300 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001301 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001302
James Pricedaf1f1c2021-04-08 22:15:48 +00001303 /// @param name the parameter name
1304 /// @param type the parameter type
1305 /// @param decorations optional parameter decorations
1306 /// @returns a constant `ast::Variable` with the given name and type
1307 template <typename NAME>
1308 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001309 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001310 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001311 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001312 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1313 ast::StorageClass::kNone, type, true, nullptr,
1314 decorations);
1315 }
1316
1317 /// @param source the parameter source
1318 /// @param name the parameter name
1319 /// @param type the parameter type
1320 /// @param decorations optional parameter decorations
1321 /// @returns a constant `ast::Variable` with the given name and type
1322 template <typename NAME>
1323 ast::Variable* Param(const Source& source,
1324 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001325 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001326 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001327 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001328 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1329 ast::StorageClass::kNone, type, true, nullptr,
1330 decorations);
1331 }
1332
Ben Clayton42708342021-04-21 17:55:12 +00001333 /// @param name the variable name
1334 /// @param type the variable type
1335 /// @param storage the variable storage class
1336 /// @param constructor constructor expression
1337 /// @param decorations variable decorations
1338 /// @returns a new `ast::Variable`, which is automatically registered as a
1339 /// global variable with the ast::Module.
1340 template <typename NAME>
1341 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001342 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001343 ast::StorageClass storage,
1344 ast::Expression* constructor = nullptr,
1345 ast::DecorationList decorations = {}) {
1346 auto* var =
1347 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1348 AST().AddGlobalVariable(var);
1349 return var;
1350 }
1351
1352 /// @param source the variable source
1353 /// @param name the variable name
1354 /// @param type the variable type
1355 /// @param storage the variable storage class
1356 /// @param constructor constructor expression
1357 /// @param decorations variable decorations
1358 /// @returns a new `ast::Variable`, which is automatically registered as a
1359 /// global variable with the ast::Module.
1360 template <typename NAME>
1361 ast::Variable* Global(const Source& source,
1362 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001363 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001364 ast::StorageClass storage,
1365 ast::Expression* constructor = nullptr,
1366 ast::DecorationList decorations = {}) {
1367 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1368 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001369 AST().AddGlobalVariable(var);
1370 return var;
1371 }
1372
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001373 /// @param name the variable name
1374 /// @param type the variable type
1375 /// @param constructor constructor expression
1376 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001377 /// @returns a const `ast::Variable` constructed by calling Var() with the
1378 /// arguments of `args`, which is automatically registered as a global
1379 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001380 template <typename NAME>
1381 ast::Variable* GlobalConst(NAME&& name,
1382 typ::Type type,
1383 ast::Expression* constructor,
1384 ast::DecorationList decorations = {}) {
1385 auto* var = Const(std::forward<NAME>(name), type, constructor,
1386 std::move(decorations));
1387 AST().AddGlobalVariable(var);
1388 return var;
1389 }
1390
1391 /// @param source the variable source
1392 /// @param name the variable name
1393 /// @param type the variable type
1394 /// @param constructor constructor expression
1395 /// @param decorations optional variable decorations
1396 /// @returns a const `ast::Variable` constructed by calling Var() with the
1397 /// arguments of `args`, which is automatically registered as a global
1398 /// variable with the ast::Module.
1399 template <typename NAME>
1400 ast::Variable* GlobalConst(const Source& source,
1401 NAME&& name,
1402 typ::Type type,
1403 ast::Expression* constructor,
1404 ast::DecorationList decorations = {}) {
1405 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1406 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001407 AST().AddGlobalVariable(var);
1408 return var;
1409 }
1410
Ben Claytonfe0910f2021-05-17 15:51:47 +00001411 /// @param source the source information
1412 /// @param expr the expression to take the address of
1413 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1414 template <typename EXPR>
1415 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1416 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1417 Expr(std::forward<EXPR>(expr)));
1418 }
1419
1420 /// @param expr the expression to take the address of
1421 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1422 template <typename EXPR>
1423 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1424 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1425 Expr(std::forward<EXPR>(expr)));
1426 }
1427
1428 /// @param source the source information
1429 /// @param expr the expression to perform an indirection on
1430 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1431 template <typename EXPR>
1432 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1433 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1434 Expr(std::forward<EXPR>(expr)));
1435 }
1436
1437 /// @param expr the expression to perform an indirection on
1438 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1439 template <typename EXPR>
1440 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1441 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1442 Expr(std::forward<EXPR>(expr)));
1443 }
1444
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001445 /// @param func the function name
1446 /// @param args the function call arguments
1447 /// @returns a `ast::CallExpression` to the function `func`, with the
1448 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1449 template <typename NAME, typename... ARGS>
1450 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1451 return create<ast::CallExpression>(Expr(func),
1452 ExprList(std::forward<ARGS>(args)...));
1453 }
1454
1455 /// @param lhs the left hand argument to the addition operation
1456 /// @param rhs the right hand argument to the addition operation
1457 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1458 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001459 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001460 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1461 Expr(std::forward<LHS>(lhs)),
1462 Expr(std::forward<RHS>(rhs)));
1463 }
1464
1465 /// @param lhs the left hand argument to the subtraction operation
1466 /// @param rhs the right hand argument to the subtraction operation
1467 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1468 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001469 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001470 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1471 Expr(std::forward<LHS>(lhs)),
1472 Expr(std::forward<RHS>(rhs)));
1473 }
1474
1475 /// @param lhs the left hand argument to the multiplication operation
1476 /// @param rhs the right hand argument to the multiplication operation
1477 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1478 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001479 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001480 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1481 Expr(std::forward<LHS>(lhs)),
1482 Expr(std::forward<RHS>(rhs)));
1483 }
1484
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001485 /// @param source the source information
1486 /// @param lhs the left hand argument to the multiplication operation
1487 /// @param rhs the right hand argument to the multiplication operation
1488 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1489 template <typename LHS, typename RHS>
1490 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1491 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1492 Expr(std::forward<LHS>(lhs)),
1493 Expr(std::forward<RHS>(rhs)));
1494 }
1495
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001496 /// @param lhs the left hand argument to the division operation
1497 /// @param rhs the right hand argument to the division operation
1498 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1499 template <typename LHS, typename RHS>
1500 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1501 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1502 Expr(std::forward<LHS>(lhs)),
1503 Expr(std::forward<RHS>(rhs)));
1504 }
1505
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001506 /// @param arr the array argument for the array accessor expression
1507 /// @param idx the index argument for the array accessor expression
1508 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1509 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001510 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001511 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1512 Expr(std::forward<IDX>(idx)));
1513 }
1514
1515 /// @param obj the object for the member accessor expression
1516 /// @param idx the index argument for the array accessor expression
1517 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1518 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001519 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001520 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1521 Expr(std::forward<IDX>(idx)));
1522 }
1523
Ben Clayton42d1e092021-02-02 14:29:15 +00001524 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001525 /// @param val the offset value
1526 /// @returns the offset decoration pointer
1527 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1528 return create<ast::StructMemberOffsetDecoration>(source_, val);
1529 }
1530
Ben Claytond614dd52021-03-15 10:43:11 +00001531 /// Creates a ast::StructMemberSizeDecoration
1532 /// @param source the source information
1533 /// @param val the size value
1534 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001535 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1536 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001537 return create<ast::StructMemberSizeDecoration>(source, val);
1538 }
1539
1540 /// Creates a ast::StructMemberSizeDecoration
1541 /// @param val the size value
1542 /// @returns the size decoration pointer
1543 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1544 return create<ast::StructMemberSizeDecoration>(source_, val);
1545 }
1546
1547 /// Creates a ast::StructMemberAlignDecoration
1548 /// @param source the source information
1549 /// @param val the align value
1550 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001551 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1552 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001553 return create<ast::StructMemberAlignDecoration>(source, val);
1554 }
1555
1556 /// Creates a ast::StructMemberAlignDecoration
1557 /// @param val the align value
1558 /// @returns the align decoration pointer
1559 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1560 return create<ast::StructMemberAlignDecoration>(source_, val);
1561 }
1562
Ben Clayton42d1e092021-02-02 14:29:15 +00001563 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001564 /// @param source the source information
1565 /// @param name the function name
1566 /// @param params the function parameters
1567 /// @param type the function return type
1568 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001569 /// @param decorations the optional function decorations
1570 /// @param return_type_decorations the optional function return type
1571 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001572 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001573 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001574 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001575 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001576 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001577 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001578 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001579 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001580 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001581 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001582 auto* func =
1583 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1584 type, create<ast::BlockStatement>(body),
1585 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001586 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001587 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001588 }
1589
Ben Clayton42d1e092021-02-02 14:29:15 +00001590 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001591 /// @param name the function name
1592 /// @param params the function parameters
1593 /// @param type the function return type
1594 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001595 /// @param decorations the optional function decorations
1596 /// @param return_type_decorations the optional function return type
1597 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001598 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001599 template <typename NAME>
1600 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001601 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001602 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001603 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001604 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001605 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001606 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001607 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1608 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001609 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001610 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001611 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001612 }
1613
Ben Clayton49668bb2021-04-17 05:32:01 +00001614 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001615 /// @param source the source information
1616 /// @returns the return statement pointer
1617 ast::ReturnStatement* Return(const Source& source) {
1618 return create<ast::ReturnStatement>(source);
1619 }
1620
1621 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001622 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001623 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1624
1625 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001626 /// @param source the source information
1627 /// @param val the return value
1628 /// @returns the return statement pointer
1629 template <typename EXPR>
1630 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1631 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1632 }
1633
1634 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001635 /// @param val the return value
1636 /// @returns the return statement pointer
1637 template <typename EXPR>
1638 ast::ReturnStatement* Return(EXPR&& val) {
1639 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001640 }
1641
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001642 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001643 /// @param source the source information
1644 /// @param name the struct name
1645 /// @param members the struct members
1646 /// @param decorations the optional struct decorations
1647 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001648 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001649 ast::Struct* Structure(const Source& source,
1650 NAME&& name,
1651 ast::StructMemberList members,
1652 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001653 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001654 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001655 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001656 AST().AddConstructedType(type);
1657 return type;
1658 }
1659
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001660 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001661 /// @param name the struct name
1662 /// @param members the struct members
1663 /// @param decorations the optional struct decorations
1664 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001665 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001666 ast::Struct* Structure(NAME&& name,
1667 ast::StructMemberList members,
1668 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001669 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001670 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001671 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001672 AST().AddConstructedType(type);
1673 return type;
1674 }
1675
Ben Clayton42d1e092021-02-02 14:29:15 +00001676 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001677 /// @param source the source information
1678 /// @param name the struct member name
1679 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001680 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001681 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001682 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001683 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001684 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001685 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001686 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001687 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001688 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1689 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001690 }
1691
Ben Clayton42d1e092021-02-02 14:29:15 +00001692 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001693 /// @param name the struct member name
1694 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001695 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001696 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001697 template <typename NAME>
1698 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001699 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001700 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001701 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001702 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1703 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001704 }
1705
Ben Claytonbab31972021-02-08 21:16:21 +00001706 /// Creates a ast::StructMember with the given byte offset
1707 /// @param offset the offset to use in the StructMemberOffsetDecoration
1708 /// @param name the struct member name
1709 /// @param type the struct member type
1710 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001711 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001712 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001713 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001714 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001715 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001716 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001717 create<ast::StructMemberOffsetDecoration>(offset),
1718 });
1719 }
1720
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001721 /// Creates a ast::BlockStatement with input statements
1722 /// @param statements statements of block
1723 /// @returns the block statement pointer
1724 template <typename... Statements>
1725 ast::BlockStatement* Block(Statements&&... statements) {
1726 return create<ast::BlockStatement>(
1727 ast::StatementList{std::forward<Statements>(statements)...});
1728 }
1729
1730 /// Creates a ast::ElseStatement with input condition and body
1731 /// @param condition the else condition expression
1732 /// @param body the else body
1733 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001734 template <typename CONDITION>
1735 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1736 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1737 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001738 }
1739
1740 /// Creates a ast::IfStatement with input condition, body, and optional
1741 /// variadic else statements
1742 /// @param condition the if statement condition expression
1743 /// @param body the if statement body
1744 /// @param elseStatements optional variadic else statements
1745 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001746 template <typename CONDITION, typename... ELSE_STATEMENTS>
1747 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001748 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001749 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001750 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001751 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001752 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001753 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001754 }
1755
1756 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001757 /// @param source the source information
1758 /// @param lhs the left hand side expression initializer
1759 /// @param rhs the right hand side expression initializer
1760 /// @returns the assignment statement pointer
1761 template <typename LhsExpressionInit, typename RhsExpressionInit>
1762 ast::AssignmentStatement* Assign(const Source& source,
1763 LhsExpressionInit&& lhs,
1764 RhsExpressionInit&& rhs) {
1765 return create<ast::AssignmentStatement>(
1766 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1767 Expr(std::forward<RhsExpressionInit>(rhs)));
1768 }
1769
1770 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001771 /// @param lhs the left hand side expression initializer
1772 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001773 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001774 template <typename LhsExpressionInit, typename RhsExpressionInit>
1775 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1776 RhsExpressionInit&& rhs) {
1777 return create<ast::AssignmentStatement>(
1778 Expr(std::forward<LhsExpressionInit>(lhs)),
1779 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001780 }
1781
1782 /// Creates a ast::LoopStatement with input body and optional continuing
1783 /// @param body the loop body
1784 /// @param continuing the optional continuing block
1785 /// @returns the loop statement pointer
1786 ast::LoopStatement* Loop(ast::BlockStatement* body,
1787 ast::BlockStatement* continuing = nullptr) {
1788 return create<ast::LoopStatement>(body, continuing);
1789 }
1790
1791 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001792 /// @param source the source information
1793 /// @param var the variable to wrap in a decl statement
1794 /// @returns the variable decl statement pointer
1795 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1796 return create<ast::VariableDeclStatement>(source, var);
1797 }
1798
1799 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001800 /// @param var the variable to wrap in a decl statement
1801 /// @returns the variable decl statement pointer
1802 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1803 return create<ast::VariableDeclStatement>(var);
1804 }
1805
Antonio Maioranocea744d2021-03-25 12:55:27 +00001806 /// Creates a ast::SwitchStatement with input expression and cases
1807 /// @param condition the condition expression initializer
1808 /// @param cases case statements
1809 /// @returns the switch statement pointer
1810 template <typename ExpressionInit, typename... Cases>
1811 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1812 return create<ast::SwitchStatement>(
1813 Expr(std::forward<ExpressionInit>(condition)),
1814 ast::CaseStatementList{std::forward<Cases>(cases)...});
1815 }
1816
1817 /// Creates a ast::CaseStatement with input list of selectors, and body
1818 /// @param selectors list of selectors
1819 /// @param body the case body
1820 /// @returns the case statement pointer
1821 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1822 ast::BlockStatement* body = nullptr) {
1823 return create<ast::CaseStatement>(std::move(selectors),
1824 body ? body : Block());
1825 }
1826
1827 /// Convenient overload that takes a single selector
1828 /// @param selector a single case selector
1829 /// @param body the case body
1830 /// @returns the case statement pointer
1831 ast::CaseStatement* Case(ast::IntLiteral* selector,
1832 ast::BlockStatement* body = nullptr) {
1833 return Case(ast::CaseSelectorList{selector}, body);
1834 }
1835
1836 /// Convenience function that creates a 'default' ast::CaseStatement
1837 /// @param body the case body
1838 /// @returns the case statement pointer
1839 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1840 return Case(ast::CaseSelectorList{}, body);
1841 }
1842
James Price68f558f2021-04-06 15:51:47 +00001843 /// Creates an ast::BuiltinDecoration
1844 /// @param source the source information
1845 /// @param builtin the builtin value
1846 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001847 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001848 return create<ast::BuiltinDecoration>(source, builtin);
1849 }
1850
1851 /// Creates an ast::BuiltinDecoration
1852 /// @param builtin the builtin value
1853 /// @returns the builtin decoration pointer
1854 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1855 return create<ast::BuiltinDecoration>(source_, builtin);
1856 }
1857
1858 /// Creates an ast::LocationDecoration
1859 /// @param source the source information
1860 /// @param location the location value
1861 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001862 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001863 return create<ast::LocationDecoration>(source, location);
1864 }
1865
1866 /// Creates an ast::LocationDecoration
1867 /// @param location the location value
1868 /// @returns the location decoration pointer
1869 ast::LocationDecoration* Location(uint32_t location) {
1870 return create<ast::LocationDecoration>(source_, location);
1871 }
1872
James Pricef2f3bfc2021-05-13 20:32:32 +00001873 /// Creates an ast::OverrideDecoration with a specific constant ID
1874 /// @param source the source information
1875 /// @param id the id value
1876 /// @returns the override decoration pointer
1877 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1878 return create<ast::OverrideDecoration>(source, id);
1879 }
1880
1881 /// Creates an ast::OverrideDecoration with a specific constant ID
1882 /// @param id the optional id value
1883 /// @returns the override decoration pointer
1884 ast::OverrideDecoration* Override(uint32_t id) {
1885 return Override(source_, id);
1886 }
1887
1888 /// Creates an ast::OverrideDecoration without a constant ID
1889 /// @param source the source information
1890 /// @returns the override decoration pointer
1891 ast::OverrideDecoration* Override(const Source& source) {
1892 return create<ast::OverrideDecoration>(source);
1893 }
1894
1895 /// Creates an ast::OverrideDecoration without a constant ID
1896 /// @returns the override decoration pointer
1897 ast::OverrideDecoration* Override() { return Override(source_); }
1898
James Price68f558f2021-04-06 15:51:47 +00001899 /// Creates an ast::StageDecoration
1900 /// @param source the source information
1901 /// @param stage the pipeline stage
1902 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001903 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001904 return create<ast::StageDecoration>(source, stage);
1905 }
1906
1907 /// Creates an ast::StageDecoration
1908 /// @param stage the pipeline stage
1909 /// @returns the stage decoration pointer
1910 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1911 return create<ast::StageDecoration>(source_, stage);
1912 }
1913
James Price70f80bb2021-05-19 13:40:08 +00001914 /// Creates an ast::WorkgroupDecoration
1915 /// @param x the x dimension expression
1916 /// @returns the workgroup decoration pointer
1917 template <typename EXPR_X>
1918 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1919 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1920 }
1921
1922 /// Creates an ast::WorkgroupDecoration
1923 /// @param x the x dimension expression
1924 /// @param y the y dimension expression
1925 /// @returns the workgroup decoration pointer
1926 template <typename EXPR_X, typename EXPR_Y>
1927 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1928 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1929 nullptr);
1930 }
1931
1932 /// Creates an ast::WorkgroupDecoration
1933 /// @param x the x dimension expression
1934 /// @param y the y dimension expression
1935 /// @param z the z dimension expression
1936 /// @returns the workgroup decoration pointer
1937 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1938 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1939 return create<ast::WorkgroupDecoration>(
1940 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1941 Expr(std::forward<EXPR_Z>(z)));
1942 }
1943
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001944 /// Sets the current builder source to `src`
1945 /// @param src the Source used for future create() calls
1946 void SetSource(const Source& src) {
1947 AssertNotMoved();
1948 source_ = src;
1949 }
1950
1951 /// Sets the current builder source to `loc`
1952 /// @param loc the Source used for future create() calls
1953 void SetSource(const Source::Location& loc) {
1954 AssertNotMoved();
1955 source_ = Source(loc);
1956 }
1957
Ben Clayton33352542021-01-29 16:43:41 +00001958 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001959 /// @note As the Resolver is run when the Program is built, this will only be
1960 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001961 /// @param expr the AST expression
1962 /// @return the resolved semantic type for the expression, or nullptr if the
1963 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001964 sem::Type* TypeOf(const ast::Expression* expr) const;
1965
Ben Claytonfe0910f2021-05-17 15:51:47 +00001966 /// Helper for returning the resolved semantic type of the variable `var`.
1967 /// @note As the Resolver is run when the Program is built, this will only be
1968 /// useful for the Resolver itself and tests that use their own Resolver.
1969 /// @param var the AST variable
1970 /// @return the resolved semantic type for the variable, or nullptr if the
1971 /// variable has no resolved type.
1972 sem::Type* TypeOf(const ast::Variable* var) const;
1973
Ben Claytonfbec46f2021-04-30 20:20:19 +00001974 /// Helper for returning the resolved semantic type of the AST type `type`.
1975 /// @note As the Resolver is run when the Program is built, this will only be
1976 /// useful for the Resolver itself and tests that use their own Resolver.
1977 /// @param expr the AST type
1978 /// @return the resolved semantic type for the type, or nullptr if the type
1979 /// has no resolved type.
1980 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001981
Ben Clayton169512e2021-04-17 05:52:11 +00001982 /// Wraps the ast::Literal in a statement. This is used by tests that
1983 /// construct a partial AST and require the Resolver to reach these
1984 /// nodes.
1985 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1986 /// @return the ast::Statement that wraps the ast::Statement
1987 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001988 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001989 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001990 /// nodes.
1991 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1992 /// @return the ast::Statement that wraps the ast::Expression
1993 ast::Statement* WrapInStatement(ast::Expression* expr);
1994 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001995 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001996 /// these nodes.
1997 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1998 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1999 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2000 /// Returns the statement argument. Used as a passthrough-overload by
2001 /// WrapInFunction().
2002 /// @param stmt the ast::Statement
2003 /// @return `stmt`
2004 ast::Statement* WrapInStatement(ast::Statement* stmt);
2005 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002006 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002007 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002008 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002009 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002010 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002011 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002012 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002013 }
2014 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002015 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002016 /// @returns the function
2017 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002018
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002019 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002020 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002021
2022 protected:
2023 /// Asserts that the builder has not been moved.
2024 void AssertNotMoved() const;
2025
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002026 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002027 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002028 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002029 ASTNodeAllocator ast_nodes_;
2030 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002031 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002032 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002033 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002034 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002035
2036 /// The source to use when creating AST nodes without providing a Source as
2037 /// the first argument.
2038 Source source_;
2039
Ben Clayton5f0ea112021-03-09 10:54:37 +00002040 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002041 /// program when built.
2042 bool resolve_on_build_ = true;
2043
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002044 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2045 bool moved_ = false;
2046};
2047
2048//! @cond Doxygen_Suppress
2049// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2050template <>
2051struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002052 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002053 return t->i32();
2054 }
2055};
2056template <>
2057struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002058 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002059 return t->u32();
2060 }
2061};
2062template <>
2063struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002064 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002065 return t->f32();
2066 }
2067};
2068template <>
2069struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002070 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002071 return t->bool_();
2072 }
2073};
2074template <>
2075struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002076 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002077 return t->void_();
2078 }
2079};
2080//! @endcond
2081
Ben Clayton917b14b2021-04-19 16:50:23 +00002082/// @param builder the ProgramBuilder
2083/// @returns the ProgramID of the ProgramBuilder
2084inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2085 return builder->ID();
2086}
2087
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002088} // namespace tint
2089
2090#endif // SRC_PROGRAM_BUILDER_H_