blob: 1ddc1355e9b7eadda6174d49226f99290543a72c [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 Claytonc03d3bd2021-05-20 14:22:28 +0000358 typ::Bool bool_() const { return {builder->create<ast::Bool>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000359
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000360 /// @param source the Source of the node
361 /// @returns a boolean type
362 typ::Bool bool_(const Source& source) const {
Ben Claytonc03d3bd2021-05-20 14:22:28 +0000363 return {builder->create<ast::Bool>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000364 }
365
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000366 /// @returns a f32 type
Ben Clayton512d60c2021-05-19 19:51:22 +0000367 typ::F32 f32() const { return {builder->create<ast::F32>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000369 /// @param source the Source of the node
370 /// @returns a f32 type
371 typ::F32 f32(const Source& source) const {
Ben Clayton512d60c2021-05-19 19:51:22 +0000372 return {builder->create<ast::F32>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000373 }
374
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000375 /// @returns a i32 type
Ben Claytoncf421bb2021-05-19 17:47:11 +0000376 typ::I32 i32() const { return {builder->create<ast::I32>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000377
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000378 /// @param source the Source of the node
379 /// @returns a i32 type
380 typ::I32 i32(const Source& source) const {
Ben Claytoncf421bb2021-05-19 17:47:11 +0000381 return {builder->create<ast::I32>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000382 }
383
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000384 /// @returns a u32 type
Ben Clayton58dec172021-05-19 17:47:11 +0000385 typ::U32 u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000386
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000387 /// @param source the Source of the node
388 /// @returns a u32 type
389 typ::U32 u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000390 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000391 }
392
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000393 /// @returns a void type
Ben Claytoncc98b4f2021-05-20 14:31:48 +0000394 typ::Void void_() const { return {builder->create<ast::Void>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000395
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000396 /// @param source the Source of the node
397 /// @returns a void type
398 typ::Void void_(const Source& source) const {
Ben Claytoncc98b4f2021-05-20 14:31:48 +0000399 return {builder->create<ast::Void>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000400 }
401
Antonio Maiorano25436862021-04-13 13:32:33 +0000402 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000403 /// @param n vector width in elements
404 /// @return the tint AST type for a `n`-element vector of `type`.
405 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000406 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000407 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
408 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000409 }
410
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000411 /// @param source the Source of the node
412 /// @param type vector subtype
413 /// @param n vector width in elements
414 /// @return the tint AST type for a `n`-element vector of `type`.
415 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000416 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000417 return {
418 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
419 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 }
421
Antonio Maiorano25436862021-04-13 13:32:33 +0000422 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000423 /// @return the tint AST type for a 2-element vector of `type`.
424 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
425
426 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000427 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000428 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000429
430 /// @param type vector subtype
431 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000432 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
433
434 /// @param n vector width in elements
435 /// @return the tint AST type for a `n`-element vector of `type`.
436 template <typename T>
437 typ::Vector vec(uint32_t n) const {
438 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000439 }
440
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000441 /// @return the tint AST type for a 2-element vector of the C type `T`.
442 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000443 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000444 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000445 }
446
447 /// @return the tint AST type for a 3-element vector of the C type `T`.
448 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000449 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000450 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000451 }
452
453 /// @return the tint AST type for a 4-element vector of the C type `T`.
454 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000455 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000456 return vec4(Of<T>());
457 }
458
459 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000460 /// @param columns number of columns for the matrix
461 /// @param rows number of rows for the matrix
462 /// @return the tint AST type for a matrix of `type`
463 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000464 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000465 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
466 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000467 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000468 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000469 }
470
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000471 /// @param source the Source of the node
472 /// @param type matrix subtype
473 /// @param columns number of columns for the matrix
474 /// @param rows number of rows for the matrix
475 /// @return the tint AST type for a matrix of `type`
476 typ::Matrix mat(const Source& source,
477 typ::Type type,
478 uint32_t columns,
479 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000480 return {type.ast
481 ? builder->create<ast::Matrix>(source, type, rows, columns)
482 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000483 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000484 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000485 }
486
Ben Claytone204f272021-04-22 14:40:23 +0000487 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000488 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000489 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000490
491 /// @param type matrix subtype
492 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000493 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000494
495 /// @param type matrix subtype
496 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000497 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000498
499 /// @param type matrix subtype
500 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000501 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000502
503 /// @param type matrix subtype
504 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000505 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000506
507 /// @param type matrix subtype
508 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000509 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000510
511 /// @param type matrix subtype
512 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000513 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000514
515 /// @param type matrix subtype
516 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000517 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000518
519 /// @param type matrix subtype
520 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000521 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000522
Ben Claytone204f272021-04-22 14:40:23 +0000523 /// @param columns number of columns for the matrix
524 /// @param rows number of rows for the matrix
525 /// @return the tint AST type for a matrix of `type`
526 template <typename T>
527 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
528 return mat(Of<T>(), columns, rows);
529 }
530
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000531 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
532 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000533 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000534 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000535 }
536
537 /// @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 mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000540 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000541 }
542
543 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
544 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000545 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000546 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000547 }
548
549 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
550 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000551 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000552 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000553 }
554
555 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
556 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000557 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000558 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559 }
560
561 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
562 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000563 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000564 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000565 }
566
567 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
568 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000569 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000570 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 }
572
573 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
574 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000575 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000576 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 }
578
579 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
580 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000581 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000582 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000583 }
584
585 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000586 /// @param n the array size. 0 represents a runtime-array
587 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000588 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000589 ast::Array* array(typ::Type subtype,
590 uint32_t n = 0,
591 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000592 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000593 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000594 }
595
596 /// @param source the Source of the node
597 /// @param subtype the array element type
598 /// @param n the array size. 0 represents a runtime-array
599 /// @param decos the optional decorations for the array
600 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000601 ast::Array* array(const Source& source,
602 typ::Type subtype,
603 uint32_t n = 0,
604 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000605 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000606 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000607 }
608
Ben Claytonbab31972021-02-08 21:16:21 +0000609 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000610 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000611 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000612 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000613 ast::Array* array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000614 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000615 ast::DecorationList decos;
616 if (stride) {
617 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
618 }
619 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000620 }
621
Ben Clayton0f88b312021-05-04 17:36:31 +0000622 /// @param source the Source of the node
623 /// @param subtype the array element type
624 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000625 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000626 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000627 ast::Array* array(const Source& source,
628 typ::Type subtype,
629 uint32_t n,
630 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000631 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000632 ast::DecorationList decos;
633 if (stride) {
634 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
635 }
636 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000637 }
638
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000639 /// @return the tint AST type for an array of size `N` of type `T`
640 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000641 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000642 return array(Of<T>(), N);
643 }
644
Ben Claytonbab31972021-02-08 21:16:21 +0000645 /// @param stride the array stride
646 /// @return the tint AST type for an array of size `N` of type `T`
647 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000648 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000649 return array(Of<T>(), N, stride);
650 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000651
Ben Clayton0f88b312021-05-04 17:36:31 +0000652 /// Creates a type name
653 /// @param name the name
654 /// @returns the type name
655 template <typename NAME>
656 ast::TypeName* type_name(NAME&& name) const {
657 return builder->create<ast::TypeName>(
658 builder->Sym(std::forward<NAME>(name)));
659 }
660
661 /// Creates a type name
662 /// @param source the Source of the node
663 /// @param name the name
664 /// @returns the type name
665 template <typename NAME>
666 ast::TypeName* type_name(const Source& source, NAME&& name) const {
667 return builder->create<ast::TypeName>(
668 source, builder->Sym(std::forward<NAME>(name)));
669 }
670
Ben Clayton42d1e092021-02-02 14:29:15 +0000671 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000672 /// @param name the alias name
673 /// @param type the alias type
674 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000675 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000676 ast::Alias* alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000677 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000678 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000679 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000680 }
681
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000682 /// Creates an alias type
683 /// @param source the Source of the node
684 /// @param name the alias name
685 /// @param type the alias type
686 /// @returns the alias pointer
687 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000688 ast::Alias* alias(const Source& source, NAME&& name, typ::Type type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000689 type = MaybeCreateTypename(type);
690 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000691 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000692 }
693
Ben Clayton4db10dd2021-04-16 08:47:14 +0000694 /// Creates an access control qualifier type
695 /// @param access the access control
696 /// @param type the inner type
697 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000698 ast::AccessControl* access(ast::AccessControl::Access access,
699 const ast::Type* type) const {
700 type = MaybeCreateTypename(type).ast;
701 return type ? builder->create<ast::AccessControl>(access, type) : nullptr;
Ben Clayton4db10dd2021-04-16 08:47:14 +0000702 }
703
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000704 /// Creates an access control qualifier type
705 /// @param source the Source of the node
706 /// @param access the access control
707 /// @param type the inner type
708 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000709 ast::AccessControl* access(const Source& source,
710 ast::AccessControl::Access access,
711 const ast::Type* type) const {
712 type = MaybeCreateTypename(type).ast;
713 return type ? builder->create<ast::AccessControl>(source, access, type)
714 : nullptr;
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000715 }
716
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000717 /// @param type the type of the pointer
718 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000719 /// @return the pointer to `type` with the given ast::StorageClass
720 typ::Pointer pointer(typ::Type type,
721 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000722 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000723 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
724 : nullptr,
725 type.sem ? builder->create<sem::Pointer>(type, storage_class)
726 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000727 }
728
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000729 /// @param source the Source of the node
730 /// @param type the type of the pointer
731 /// @param storage_class the storage class of the pointer
732 /// @return the pointer to `type` with the given ast::StorageClass
733 typ::Pointer pointer(const Source& source,
734 typ::Type type,
735 ast::StorageClass storage_class) const {
736 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000737 return {type.ast
738 ? builder->create<ast::Pointer>(source, type, storage_class)
739 : nullptr,
740 type.sem ? builder->create<sem::Pointer>(type, storage_class)
741 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000742 }
743
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000744 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000745 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000746 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000747 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000748 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000749 }
750
Ben Claytone204f272021-04-22 14:40:23 +0000751 /// @param kind the kind of sampler
752 /// @returns the sampler
753 typ::Sampler sampler(ast::SamplerKind kind) const {
754 return {builder->create<ast::Sampler>(kind),
755 builder->create<sem::Sampler>(kind)};
756 }
757
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000758 /// @param source the Source of the node
759 /// @param kind the kind of sampler
760 /// @returns the sampler
761 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
762 return {builder->create<ast::Sampler>(source, kind),
763 builder->create<sem::Sampler>(kind)};
764 }
765
Ben Claytone204f272021-04-22 14:40:23 +0000766 /// @param dims the dimensionality of the texture
767 /// @returns the depth texture
768 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
769 return {builder->create<ast::DepthTexture>(dims),
770 builder->create<sem::DepthTexture>(dims)};
771 }
772
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000773 /// @param source the Source of the node
774 /// @param dims the dimensionality of the texture
775 /// @returns the depth texture
776 typ::DepthTexture depth_texture(const Source& source,
777 ast::TextureDimension dims) const {
778 return {builder->create<ast::DepthTexture>(source, dims),
779 builder->create<sem::DepthTexture>(dims)};
780 }
781
Ben Claytone204f272021-04-22 14:40:23 +0000782 /// @param dims the dimensionality of the texture
783 /// @param subtype the texture subtype.
784 /// @returns the sampled texture
785 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
786 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000787 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
788 : nullptr,
789 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
790 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000791 }
792
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000793 /// @param source the Source of the node
794 /// @param dims the dimensionality of the texture
795 /// @param subtype the texture subtype.
796 /// @returns the sampled texture
797 typ::SampledTexture sampled_texture(const Source& source,
798 ast::TextureDimension dims,
799 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000800 return {subtype.ast
801 ? builder->create<ast::SampledTexture>(source, dims, subtype)
802 : nullptr,
803 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
804 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000805 }
806
Ben Claytone204f272021-04-22 14:40:23 +0000807 /// @param dims the dimensionality of the texture
808 /// @param subtype the texture subtype.
809 /// @returns the multisampled texture
810 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
811 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000812 return {
813 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
814 : nullptr,
815 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
816 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000817 }
818
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000819 /// @param source the Source of the node
820 /// @param dims the dimensionality of the texture
821 /// @param subtype the texture subtype.
822 /// @returns the multisampled texture
823 typ::MultisampledTexture multisampled_texture(const Source& source,
824 ast::TextureDimension dims,
825 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000826 return {
827 subtype.ast
828 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
829 : nullptr,
830 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
831 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000832 }
833
Ben Claytone204f272021-04-22 14:40:23 +0000834 /// @param dims the dimensionality of the texture
835 /// @param format the image format of the texture
836 /// @returns the storage texture
837 typ::StorageTexture storage_texture(ast::TextureDimension dims,
838 ast::ImageFormat format) const {
839 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
840 auto* sem_subtype =
841 sem::StorageTexture::SubtypeFor(format, builder->Types());
842 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000843 builder->create<sem::StorageTexture>(
844 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000845 }
846
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000847 /// @param source the Source of the node
848 /// @param dims the dimensionality of the texture
849 /// @param format the image format of the texture
850 /// @returns the storage texture
851 typ::StorageTexture storage_texture(const Source& source,
852 ast::TextureDimension dims,
853 ast::ImageFormat format) const {
854 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
855 auto* sem_subtype =
856 sem::StorageTexture::SubtypeFor(format, builder->Types());
857 return {builder->create<ast::StorageTexture>(source, dims, format,
858 ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000859 builder->create<sem::StorageTexture>(
860 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000861 }
862
Brandon Jones7b257692021-05-17 17:40:17 +0000863 /// @returns the external texture
864 typ::ExternalTexture external_texture() const {
865 return {builder->create<ast::ExternalTexture>(),
866 builder->create<sem::ExternalTexture>()};
867 }
868
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000869 /// @param source the Source of the node
870 /// @returns the external texture
871 typ::ExternalTexture external_texture(const Source& source) const {
872 return {builder->create<ast::ExternalTexture>(source),
873 builder->create<sem::ExternalTexture>()};
874 }
875
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000876 /// If ty is a ast::Struct or ast::Alias, the returned type is an
877 /// ast::TypeName of the given type's name, otherwise type is returned.
878 /// @param type the type
879 /// @return either type or a pointer to a new ast::TypeName
880 typ::Type MaybeCreateTypename(typ::Type type) const;
881
Ben Claytonf5f311e2021-04-28 14:31:23 +0000882 /// The ProgramBuilder
883 ProgramBuilder* const builder;
884
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000885 private:
886 /// CToAST<T> is specialized for various `T` types and each specialization
887 /// contains a single static `get()` method for obtaining the corresponding
888 /// AST type for the C type `T`.
889 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000890 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000891 template <typename T>
892 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000893 };
894
895 //////////////////////////////////////////////////////////////////////////////
896 // AST helper methods
897 //////////////////////////////////////////////////////////////////////////////
898
James Price65ae64d2021-04-29 12:59:14 +0000899 /// @return a new unnamed symbol
900 Symbol Sym() { return Symbols().New(); }
901
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000902 /// @param name the symbol string
903 /// @return a Symbol with the given name
904 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
905
906 /// @param sym the symbol
907 /// @return `sym`
908 Symbol Sym(Symbol sym) { return sym; }
909
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000910 /// @param expr the expression
911 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000912 template <typename T>
913 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
914 return expr;
915 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000916
Ben Claytonb8ea5912021-04-19 16:52:42 +0000917 /// Passthrough for nullptr
918 /// @return nullptr
919 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
920
Ben Claytonfe0910f2021-05-17 15:51:47 +0000921 /// @param source the source information
922 /// @param symbol the identifier symbol
923 /// @return an ast::IdentifierExpression with the given symbol
924 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
925 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000926 }
927
Ben Clayton46d78d72021-02-10 21:34:26 +0000928 /// @param symbol the identifier symbol
929 /// @return an ast::IdentifierExpression with the given symbol
930 ast::IdentifierExpression* Expr(Symbol symbol) {
931 return create<ast::IdentifierExpression>(symbol);
932 }
933
Ben Claytonfe0910f2021-05-17 15:51:47 +0000934 /// @param source the source information
935 /// @param variable the AST variable
936 /// @return an ast::IdentifierExpression with the variable's symbol
937 ast::IdentifierExpression* Expr(const Source& source,
938 ast::Variable* variable) {
939 return create<ast::IdentifierExpression>(source, variable->symbol());
940 }
941
Ben Claytonb8ea5912021-04-19 16:52:42 +0000942 /// @param variable the AST variable
943 /// @return an ast::IdentifierExpression with the variable's symbol
944 ast::IdentifierExpression* Expr(ast::Variable* variable) {
945 return create<ast::IdentifierExpression>(variable->symbol());
946 }
947
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000948 /// @param source the source information
949 /// @param name the identifier name
950 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000951 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
952 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
953 }
954
955 /// @param name the identifier name
956 /// @return an ast::IdentifierExpression with the given name
957 ast::IdentifierExpression* Expr(const char* name) {
958 return create<ast::IdentifierExpression>(Symbols().Register(name));
959 }
960
961 /// @param source the source information
962 /// @param name the identifier name
963 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000964 ast::IdentifierExpression* Expr(const Source& source,
965 const std::string& name) {
966 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
967 }
968
969 /// @param name the identifier name
970 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000971 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000972 return create<ast::IdentifierExpression>(Symbols().Register(name));
973 }
974
Ben Claytonfe0910f2021-05-17 15:51:47 +0000975 /// @param source the source information
976 /// @param value the boolean value
977 /// @return a Scalar constructor for the given value
978 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
979 return create<ast::ScalarConstructorExpression>(source, Literal(value));
980 }
981
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000982 /// @param value the boolean value
983 /// @return a Scalar constructor for the given value
984 ast::ScalarConstructorExpression* Expr(bool value) {
985 return create<ast::ScalarConstructorExpression>(Literal(value));
986 }
987
Ben Claytonfe0910f2021-05-17 15:51:47 +0000988 /// @param source the source information
989 /// @param value the float value
990 /// @return a Scalar constructor for the given value
991 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
992 return create<ast::ScalarConstructorExpression>(source, Literal(value));
993 }
994
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000995 /// @param value the float value
996 /// @return a Scalar constructor for the given value
997 ast::ScalarConstructorExpression* Expr(f32 value) {
998 return create<ast::ScalarConstructorExpression>(Literal(value));
999 }
1000
Ben Claytonfe0910f2021-05-17 15:51:47 +00001001 /// @param source the source information
1002 /// @param value the integer value
1003 /// @return a Scalar constructor for the given value
1004 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1005 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1006 }
1007
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001008 /// @param value the integer value
1009 /// @return a Scalar constructor for the given value
1010 ast::ScalarConstructorExpression* Expr(i32 value) {
1011 return create<ast::ScalarConstructorExpression>(Literal(value));
1012 }
1013
Ben Claytonfe0910f2021-05-17 15:51:47 +00001014 /// @param source the source information
1015 /// @param value the unsigned int value
1016 /// @return a Scalar constructor for the given value
1017 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1018 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1019 }
1020
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001021 /// @param value the unsigned int value
1022 /// @return a Scalar constructor for the given value
1023 ast::ScalarConstructorExpression* Expr(u32 value) {
1024 return create<ast::ScalarConstructorExpression>(Literal(value));
1025 }
1026
1027 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1028 /// `list`.
1029 /// @param list the list to append too
1030 /// @param arg the arg to create
1031 template <typename ARG>
1032 void Append(ast::ExpressionList& list, ARG&& arg) {
1033 list.emplace_back(Expr(std::forward<ARG>(arg)));
1034 }
1035
1036 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1037 /// then appends them to `list`.
1038 /// @param list the list to append too
1039 /// @param arg0 the first argument
1040 /// @param args the rest of the arguments
1041 template <typename ARG0, typename... ARGS>
1042 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1043 Append(list, std::forward<ARG0>(arg0));
1044 Append(list, std::forward<ARGS>(args)...);
1045 }
1046
1047 /// @return an empty list of expressions
1048 ast::ExpressionList ExprList() { return {}; }
1049
1050 /// @param args the list of expressions
1051 /// @return the list of expressions converted to `ast::Expression`s using
1052 /// `Expr()`,
1053 template <typename... ARGS>
1054 ast::ExpressionList ExprList(ARGS&&... args) {
1055 ast::ExpressionList list;
1056 list.reserve(sizeof...(args));
1057 Append(list, std::forward<ARGS>(args)...);
1058 return list;
1059 }
1060
1061 /// @param list the list of expressions
1062 /// @return `list`
1063 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1064
1065 /// @param val the boolan value
1066 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001067 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001068
1069 /// @param val the float value
1070 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001071 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001072
1073 /// @param val the unsigned int value
1074 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001075 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001076
1077 /// @param val the integer value
1078 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001079 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001080
1081 /// @param args the arguments for the type constructor
1082 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1083 /// of `args` converted to `ast::Expression`s using `Expr()`
1084 template <typename T, typename... ARGS>
1085 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001086 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001087 }
1088
1089 /// @param type the type to construct
1090 /// @param args the arguments for the constructor
1091 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1092 /// values `args`.
1093 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001094 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001095 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001096 return create<ast::TypeConstructorExpression>(
1097 type, ExprList(std::forward<ARGS>(args)...));
1098 }
1099
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001100 /// Creates a constructor expression that constructs an object of
1101 /// `type` filled with `elem_value`. For example,
1102 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1103 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1104 /// @param type the type to construct
1105 /// @param elem_value the initial or element value (for vec and mat) to
1106 /// construct with
1107 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001108 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001109 int elem_value = 0);
1110
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001111 /// @param args the arguments for the vector constructor
1112 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1113 /// `T`, constructed with the values `args`.
1114 template <typename T, typename... ARGS>
1115 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001116 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001117 }
1118
1119 /// @param args the arguments for the vector constructor
1120 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1121 /// `T`, constructed with the values `args`.
1122 template <typename T, typename... ARGS>
1123 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001124 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001125 }
1126
1127 /// @param args the arguments for the vector constructor
1128 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1129 /// `T`, constructed with the values `args`.
1130 template <typename T, typename... ARGS>
1131 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001132 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001133 }
1134
1135 /// @param args the arguments for the matrix constructor
1136 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1137 /// `T`, constructed with the values `args`.
1138 template <typename T, typename... ARGS>
1139 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001140 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001141 }
1142
1143 /// @param args the arguments for the matrix constructor
1144 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1145 /// `T`, constructed with the values `args`.
1146 template <typename T, typename... ARGS>
1147 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001148 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001149 }
1150
1151 /// @param args the arguments for the matrix constructor
1152 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1153 /// `T`, constructed with the values `args`.
1154 template <typename T, typename... ARGS>
1155 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001156 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001157 }
1158
1159 /// @param args the arguments for the matrix constructor
1160 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1161 /// `T`, constructed with the values `args`.
1162 template <typename T, typename... ARGS>
1163 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001164 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001165 }
1166
1167 /// @param args the arguments for the matrix constructor
1168 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1169 /// `T`, constructed with the values `args`.
1170 template <typename T, typename... ARGS>
1171 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001172 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001173 }
1174
1175 /// @param args the arguments for the matrix constructor
1176 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1177 /// `T`, constructed with the values `args`.
1178 template <typename T, typename... ARGS>
1179 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001180 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181 }
1182
1183 /// @param args the arguments for the matrix constructor
1184 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1185 /// `T`, constructed with the values `args`.
1186 template <typename T, typename... ARGS>
1187 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001188 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001189 }
1190
1191 /// @param args the arguments for the matrix constructor
1192 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1193 /// `T`, constructed with the values `args`.
1194 template <typename T, typename... ARGS>
1195 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001196 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001197 }
1198
1199 /// @param args the arguments for the matrix constructor
1200 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1201 /// `T`, constructed with the values `args`.
1202 template <typename T, typename... ARGS>
1203 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001204 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001205 }
1206
1207 /// @param args the arguments for the array constructor
1208 /// @return an `ast::TypeConstructorExpression` of an array with element type
1209 /// `T`, constructed with the values `args`.
1210 template <typename T, int N = 0, typename... ARGS>
1211 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001212 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001213 }
1214
1215 /// @param subtype the array element type
1216 /// @param n the array size. 0 represents a runtime-array.
1217 /// @param args the arguments for the array constructor
1218 /// @return an `ast::TypeConstructorExpression` of an array with element type
1219 /// `subtype`, constructed with the values `args`.
1220 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001221 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001222 uint32_t n,
1223 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001224 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001225 }
1226
1227 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001229 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230 /// @param constructor constructor expression
1231 /// @param decorations variable decorations
1232 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001233 template <typename NAME>
1234 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001235 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001236 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001237 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001238 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001239 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001240 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1241 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001242 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001243
1244 /// @param source the variable source
1245 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001246 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001247 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001248 /// @param constructor constructor expression
1249 /// @param decorations variable decorations
1250 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001251 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001252 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001253 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001254 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001255 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001256 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001257 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001258 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001259 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001260 type, false, constructor, decorations);
1261 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001263 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001264 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001265 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001266 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001267 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001268 template <typename NAME>
1269 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001270 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001271 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001272 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001273 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001274 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001275 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001276 constructor, decorations);
1277 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001278
1279 /// @param source the variable source
1280 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001281 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001282 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001283 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001284 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001285 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001286 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001287 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001288 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001289 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001290 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001291 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001292 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001293 ast::StorageClass::kNone, type, true,
1294 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001295 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001296
James Pricedaf1f1c2021-04-08 22:15:48 +00001297 /// @param name the parameter name
1298 /// @param type the parameter type
1299 /// @param decorations optional parameter decorations
1300 /// @returns a constant `ast::Variable` with the given name and type
1301 template <typename NAME>
1302 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001303 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001304 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001305 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001306 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1307 ast::StorageClass::kNone, type, true, nullptr,
1308 decorations);
1309 }
1310
1311 /// @param source the parameter source
1312 /// @param name the parameter name
1313 /// @param type the parameter type
1314 /// @param decorations optional parameter decorations
1315 /// @returns a constant `ast::Variable` with the given name and type
1316 template <typename NAME>
1317 ast::Variable* Param(const Source& source,
1318 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001319 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001320 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001321 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001322 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1323 ast::StorageClass::kNone, type, true, nullptr,
1324 decorations);
1325 }
1326
Ben Clayton42708342021-04-21 17:55:12 +00001327 /// @param name the variable name
1328 /// @param type the variable type
1329 /// @param storage the variable storage class
1330 /// @param constructor constructor expression
1331 /// @param decorations variable decorations
1332 /// @returns a new `ast::Variable`, which is automatically registered as a
1333 /// global variable with the ast::Module.
1334 template <typename NAME>
1335 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001336 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001337 ast::StorageClass storage,
1338 ast::Expression* constructor = nullptr,
1339 ast::DecorationList decorations = {}) {
1340 auto* var =
1341 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1342 AST().AddGlobalVariable(var);
1343 return var;
1344 }
1345
1346 /// @param source the variable source
1347 /// @param name the variable name
1348 /// @param type the variable type
1349 /// @param storage the variable storage class
1350 /// @param constructor constructor expression
1351 /// @param decorations variable decorations
1352 /// @returns a new `ast::Variable`, which is automatically registered as a
1353 /// global variable with the ast::Module.
1354 template <typename NAME>
1355 ast::Variable* Global(const Source& source,
1356 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001357 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001358 ast::StorageClass storage,
1359 ast::Expression* constructor = nullptr,
1360 ast::DecorationList decorations = {}) {
1361 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1362 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001363 AST().AddGlobalVariable(var);
1364 return var;
1365 }
1366
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001367 /// @param name the variable name
1368 /// @param type the variable type
1369 /// @param constructor constructor expression
1370 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001371 /// @returns a const `ast::Variable` constructed by calling Var() with the
1372 /// arguments of `args`, which is automatically registered as a global
1373 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001374 template <typename NAME>
1375 ast::Variable* GlobalConst(NAME&& name,
1376 typ::Type type,
1377 ast::Expression* constructor,
1378 ast::DecorationList decorations = {}) {
1379 auto* var = Const(std::forward<NAME>(name), type, constructor,
1380 std::move(decorations));
1381 AST().AddGlobalVariable(var);
1382 return var;
1383 }
1384
1385 /// @param source the variable source
1386 /// @param name the variable name
1387 /// @param type the variable type
1388 /// @param constructor constructor expression
1389 /// @param decorations optional variable decorations
1390 /// @returns a const `ast::Variable` constructed by calling Var() with the
1391 /// arguments of `args`, which is automatically registered as a global
1392 /// variable with the ast::Module.
1393 template <typename NAME>
1394 ast::Variable* GlobalConst(const Source& source,
1395 NAME&& name,
1396 typ::Type type,
1397 ast::Expression* constructor,
1398 ast::DecorationList decorations = {}) {
1399 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1400 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001401 AST().AddGlobalVariable(var);
1402 return var;
1403 }
1404
Ben Claytonfe0910f2021-05-17 15:51:47 +00001405 /// @param source the source information
1406 /// @param expr the expression to take the address of
1407 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1408 template <typename EXPR>
1409 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1410 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1411 Expr(std::forward<EXPR>(expr)));
1412 }
1413
1414 /// @param expr the expression to take the address of
1415 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1416 template <typename EXPR>
1417 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1418 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1419 Expr(std::forward<EXPR>(expr)));
1420 }
1421
1422 /// @param source the source information
1423 /// @param expr the expression to perform an indirection on
1424 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1425 template <typename EXPR>
1426 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1427 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1428 Expr(std::forward<EXPR>(expr)));
1429 }
1430
1431 /// @param expr the expression to perform an indirection on
1432 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1433 template <typename EXPR>
1434 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1435 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1436 Expr(std::forward<EXPR>(expr)));
1437 }
1438
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001439 /// @param func the function name
1440 /// @param args the function call arguments
1441 /// @returns a `ast::CallExpression` to the function `func`, with the
1442 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1443 template <typename NAME, typename... ARGS>
1444 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1445 return create<ast::CallExpression>(Expr(func),
1446 ExprList(std::forward<ARGS>(args)...));
1447 }
1448
1449 /// @param lhs the left hand argument to the addition operation
1450 /// @param rhs the right hand argument to the addition operation
1451 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1452 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001453 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001454 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1455 Expr(std::forward<LHS>(lhs)),
1456 Expr(std::forward<RHS>(rhs)));
1457 }
1458
1459 /// @param lhs the left hand argument to the subtraction operation
1460 /// @param rhs the right hand argument to the subtraction operation
1461 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1462 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001463 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001464 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1465 Expr(std::forward<LHS>(lhs)),
1466 Expr(std::forward<RHS>(rhs)));
1467 }
1468
1469 /// @param lhs the left hand argument to the multiplication operation
1470 /// @param rhs the right hand argument to the multiplication operation
1471 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1472 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001473 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001474 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1475 Expr(std::forward<LHS>(lhs)),
1476 Expr(std::forward<RHS>(rhs)));
1477 }
1478
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001479 /// @param source the source information
1480 /// @param lhs the left hand argument to the multiplication operation
1481 /// @param rhs the right hand argument to the multiplication operation
1482 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1483 template <typename LHS, typename RHS>
1484 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1485 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1486 Expr(std::forward<LHS>(lhs)),
1487 Expr(std::forward<RHS>(rhs)));
1488 }
1489
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001490 /// @param lhs the left hand argument to the division operation
1491 /// @param rhs the right hand argument to the division operation
1492 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1493 template <typename LHS, typename RHS>
1494 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1495 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1496 Expr(std::forward<LHS>(lhs)),
1497 Expr(std::forward<RHS>(rhs)));
1498 }
1499
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001500 /// @param arr the array argument for the array accessor expression
1501 /// @param idx the index argument for the array accessor expression
1502 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1503 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001504 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001505 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1506 Expr(std::forward<IDX>(idx)));
1507 }
1508
1509 /// @param obj the object for the member accessor expression
1510 /// @param idx the index argument for the array accessor expression
1511 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1512 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001513 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001514 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1515 Expr(std::forward<IDX>(idx)));
1516 }
1517
Ben Clayton42d1e092021-02-02 14:29:15 +00001518 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001519 /// @param val the offset value
1520 /// @returns the offset decoration pointer
1521 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1522 return create<ast::StructMemberOffsetDecoration>(source_, val);
1523 }
1524
Ben Claytond614dd52021-03-15 10:43:11 +00001525 /// Creates a ast::StructMemberSizeDecoration
1526 /// @param source the source information
1527 /// @param val the size value
1528 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001529 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1530 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001531 return create<ast::StructMemberSizeDecoration>(source, val);
1532 }
1533
1534 /// Creates a ast::StructMemberSizeDecoration
1535 /// @param val the size value
1536 /// @returns the size decoration pointer
1537 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1538 return create<ast::StructMemberSizeDecoration>(source_, val);
1539 }
1540
1541 /// Creates a ast::StructMemberAlignDecoration
1542 /// @param source the source information
1543 /// @param val the align value
1544 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001545 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1546 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001547 return create<ast::StructMemberAlignDecoration>(source, val);
1548 }
1549
1550 /// Creates a ast::StructMemberAlignDecoration
1551 /// @param val the align value
1552 /// @returns the align decoration pointer
1553 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1554 return create<ast::StructMemberAlignDecoration>(source_, val);
1555 }
1556
Ben Clayton42d1e092021-02-02 14:29:15 +00001557 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001558 /// @param source the source information
1559 /// @param name the function name
1560 /// @param params the function parameters
1561 /// @param type the function return type
1562 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001563 /// @param decorations the optional function decorations
1564 /// @param return_type_decorations the optional function return type
1565 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001566 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001567 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001568 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001569 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001570 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001571 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001572 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001573 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001574 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001575 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001576 auto* func =
1577 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1578 type, create<ast::BlockStatement>(body),
1579 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001580 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001581 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001582 }
1583
Ben Clayton42d1e092021-02-02 14:29:15 +00001584 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001585 /// @param name the function name
1586 /// @param params the function parameters
1587 /// @param type the function return type
1588 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001589 /// @param decorations the optional function decorations
1590 /// @param return_type_decorations the optional function return type
1591 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001592 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001593 template <typename NAME>
1594 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001595 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001596 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001597 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001598 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001599 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001600 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001601 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1602 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001603 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001604 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001605 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001606 }
1607
Ben Clayton49668bb2021-04-17 05:32:01 +00001608 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001609 /// @param source the source information
1610 /// @returns the return statement pointer
1611 ast::ReturnStatement* Return(const Source& source) {
1612 return create<ast::ReturnStatement>(source);
1613 }
1614
1615 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001616 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001617 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1618
1619 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001620 /// @param source the source information
1621 /// @param val the return value
1622 /// @returns the return statement pointer
1623 template <typename EXPR>
1624 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1625 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1626 }
1627
1628 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001629 /// @param val the return value
1630 /// @returns the return statement pointer
1631 template <typename EXPR>
1632 ast::ReturnStatement* Return(EXPR&& val) {
1633 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001634 }
1635
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001636 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001637 /// @param source the source information
1638 /// @param name the struct name
1639 /// @param members the struct members
1640 /// @param decorations the optional struct decorations
1641 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001642 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001643 ast::Struct* Structure(const Source& source,
1644 NAME&& name,
1645 ast::StructMemberList members,
1646 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001647 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001648 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001649 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001650 AST().AddConstructedType(type);
1651 return type;
1652 }
1653
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001654 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001655 /// @param name the struct name
1656 /// @param members the struct members
1657 /// @param decorations the optional struct decorations
1658 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001659 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001660 ast::Struct* Structure(NAME&& name,
1661 ast::StructMemberList members,
1662 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001663 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001664 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001665 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001666 AST().AddConstructedType(type);
1667 return type;
1668 }
1669
Ben Clayton42d1e092021-02-02 14:29:15 +00001670 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001671 /// @param source the source information
1672 /// @param name the struct member name
1673 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001674 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001675 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001676 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001677 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001678 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001679 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001680 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001681 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001682 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1683 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001684 }
1685
Ben Clayton42d1e092021-02-02 14:29:15 +00001686 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001687 /// @param name the struct member name
1688 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001689 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001690 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001691 template <typename NAME>
1692 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001693 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001694 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001695 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001696 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1697 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001698 }
1699
Ben Claytonbab31972021-02-08 21:16:21 +00001700 /// Creates a ast::StructMember with the given byte offset
1701 /// @param offset the offset to use in the StructMemberOffsetDecoration
1702 /// @param name the struct member name
1703 /// @param type the struct member type
1704 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001705 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001706 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001707 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001708 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001709 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001710 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001711 create<ast::StructMemberOffsetDecoration>(offset),
1712 });
1713 }
1714
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001715 /// Creates a ast::BlockStatement with input statements
1716 /// @param statements statements of block
1717 /// @returns the block statement pointer
1718 template <typename... Statements>
1719 ast::BlockStatement* Block(Statements&&... statements) {
1720 return create<ast::BlockStatement>(
1721 ast::StatementList{std::forward<Statements>(statements)...});
1722 }
1723
1724 /// Creates a ast::ElseStatement with input condition and body
1725 /// @param condition the else condition expression
1726 /// @param body the else body
1727 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001728 template <typename CONDITION>
1729 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1730 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1731 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001732 }
1733
1734 /// Creates a ast::IfStatement with input condition, body, and optional
1735 /// variadic else statements
1736 /// @param condition the if statement condition expression
1737 /// @param body the if statement body
1738 /// @param elseStatements optional variadic else statements
1739 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001740 template <typename CONDITION, typename... ELSE_STATEMENTS>
1741 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001742 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001743 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001744 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001745 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001746 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001747 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001748 }
1749
1750 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001751 /// @param source the source information
1752 /// @param lhs the left hand side expression initializer
1753 /// @param rhs the right hand side expression initializer
1754 /// @returns the assignment statement pointer
1755 template <typename LhsExpressionInit, typename RhsExpressionInit>
1756 ast::AssignmentStatement* Assign(const Source& source,
1757 LhsExpressionInit&& lhs,
1758 RhsExpressionInit&& rhs) {
1759 return create<ast::AssignmentStatement>(
1760 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1761 Expr(std::forward<RhsExpressionInit>(rhs)));
1762 }
1763
1764 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001765 /// @param lhs the left hand side expression initializer
1766 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001767 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001768 template <typename LhsExpressionInit, typename RhsExpressionInit>
1769 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1770 RhsExpressionInit&& rhs) {
1771 return create<ast::AssignmentStatement>(
1772 Expr(std::forward<LhsExpressionInit>(lhs)),
1773 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001774 }
1775
1776 /// Creates a ast::LoopStatement with input body and optional continuing
1777 /// @param body the loop body
1778 /// @param continuing the optional continuing block
1779 /// @returns the loop statement pointer
1780 ast::LoopStatement* Loop(ast::BlockStatement* body,
1781 ast::BlockStatement* continuing = nullptr) {
1782 return create<ast::LoopStatement>(body, continuing);
1783 }
1784
1785 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001786 /// @param source the source information
1787 /// @param var the variable to wrap in a decl statement
1788 /// @returns the variable decl statement pointer
1789 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1790 return create<ast::VariableDeclStatement>(source, var);
1791 }
1792
1793 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001794 /// @param var the variable to wrap in a decl statement
1795 /// @returns the variable decl statement pointer
1796 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1797 return create<ast::VariableDeclStatement>(var);
1798 }
1799
Antonio Maioranocea744d2021-03-25 12:55:27 +00001800 /// Creates a ast::SwitchStatement with input expression and cases
1801 /// @param condition the condition expression initializer
1802 /// @param cases case statements
1803 /// @returns the switch statement pointer
1804 template <typename ExpressionInit, typename... Cases>
1805 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1806 return create<ast::SwitchStatement>(
1807 Expr(std::forward<ExpressionInit>(condition)),
1808 ast::CaseStatementList{std::forward<Cases>(cases)...});
1809 }
1810
1811 /// Creates a ast::CaseStatement with input list of selectors, and body
1812 /// @param selectors list of selectors
1813 /// @param body the case body
1814 /// @returns the case statement pointer
1815 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1816 ast::BlockStatement* body = nullptr) {
1817 return create<ast::CaseStatement>(std::move(selectors),
1818 body ? body : Block());
1819 }
1820
1821 /// Convenient overload that takes a single selector
1822 /// @param selector a single case selector
1823 /// @param body the case body
1824 /// @returns the case statement pointer
1825 ast::CaseStatement* Case(ast::IntLiteral* selector,
1826 ast::BlockStatement* body = nullptr) {
1827 return Case(ast::CaseSelectorList{selector}, body);
1828 }
1829
1830 /// Convenience function that creates a 'default' ast::CaseStatement
1831 /// @param body the case body
1832 /// @returns the case statement pointer
1833 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1834 return Case(ast::CaseSelectorList{}, body);
1835 }
1836
James Price68f558f2021-04-06 15:51:47 +00001837 /// Creates an ast::BuiltinDecoration
1838 /// @param source the source information
1839 /// @param builtin the builtin value
1840 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001841 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001842 return create<ast::BuiltinDecoration>(source, builtin);
1843 }
1844
1845 /// Creates an ast::BuiltinDecoration
1846 /// @param builtin the builtin value
1847 /// @returns the builtin decoration pointer
1848 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1849 return create<ast::BuiltinDecoration>(source_, builtin);
1850 }
1851
1852 /// Creates an ast::LocationDecoration
1853 /// @param source the source information
1854 /// @param location the location value
1855 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001856 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001857 return create<ast::LocationDecoration>(source, location);
1858 }
1859
1860 /// Creates an ast::LocationDecoration
1861 /// @param location the location value
1862 /// @returns the location decoration pointer
1863 ast::LocationDecoration* Location(uint32_t location) {
1864 return create<ast::LocationDecoration>(source_, location);
1865 }
1866
James Pricef2f3bfc2021-05-13 20:32:32 +00001867 /// Creates an ast::OverrideDecoration with a specific constant ID
1868 /// @param source the source information
1869 /// @param id the id value
1870 /// @returns the override decoration pointer
1871 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1872 return create<ast::OverrideDecoration>(source, id);
1873 }
1874
1875 /// Creates an ast::OverrideDecoration with a specific constant ID
1876 /// @param id the optional id value
1877 /// @returns the override decoration pointer
1878 ast::OverrideDecoration* Override(uint32_t id) {
1879 return Override(source_, id);
1880 }
1881
1882 /// Creates an ast::OverrideDecoration without a constant ID
1883 /// @param source the source information
1884 /// @returns the override decoration pointer
1885 ast::OverrideDecoration* Override(const Source& source) {
1886 return create<ast::OverrideDecoration>(source);
1887 }
1888
1889 /// Creates an ast::OverrideDecoration without a constant ID
1890 /// @returns the override decoration pointer
1891 ast::OverrideDecoration* Override() { return Override(source_); }
1892
James Price68f558f2021-04-06 15:51:47 +00001893 /// Creates an ast::StageDecoration
1894 /// @param source the source information
1895 /// @param stage the pipeline stage
1896 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001897 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001898 return create<ast::StageDecoration>(source, stage);
1899 }
1900
1901 /// Creates an ast::StageDecoration
1902 /// @param stage the pipeline stage
1903 /// @returns the stage decoration pointer
1904 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1905 return create<ast::StageDecoration>(source_, stage);
1906 }
1907
James Price70f80bb2021-05-19 13:40:08 +00001908 /// Creates an ast::WorkgroupDecoration
1909 /// @param x the x dimension expression
1910 /// @returns the workgroup decoration pointer
1911 template <typename EXPR_X>
1912 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1913 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1914 }
1915
1916 /// Creates an ast::WorkgroupDecoration
1917 /// @param x the x dimension expression
1918 /// @param y the y dimension expression
1919 /// @returns the workgroup decoration pointer
1920 template <typename EXPR_X, typename EXPR_Y>
1921 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1922 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1923 nullptr);
1924 }
1925
1926 /// Creates an ast::WorkgroupDecoration
1927 /// @param x the x dimension expression
1928 /// @param y the y dimension expression
1929 /// @param z the z dimension expression
1930 /// @returns the workgroup decoration pointer
1931 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1932 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1933 return create<ast::WorkgroupDecoration>(
1934 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1935 Expr(std::forward<EXPR_Z>(z)));
1936 }
1937
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001938 /// Sets the current builder source to `src`
1939 /// @param src the Source used for future create() calls
1940 void SetSource(const Source& src) {
1941 AssertNotMoved();
1942 source_ = src;
1943 }
1944
1945 /// Sets the current builder source to `loc`
1946 /// @param loc the Source used for future create() calls
1947 void SetSource(const Source::Location& loc) {
1948 AssertNotMoved();
1949 source_ = Source(loc);
1950 }
1951
Ben Clayton33352542021-01-29 16:43:41 +00001952 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001953 /// @note As the Resolver is run when the Program is built, this will only be
1954 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001955 /// @param expr the AST expression
1956 /// @return the resolved semantic type for the expression, or nullptr if the
1957 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001958 sem::Type* TypeOf(const ast::Expression* expr) const;
1959
Ben Claytonfe0910f2021-05-17 15:51:47 +00001960 /// Helper for returning the resolved semantic type of the variable `var`.
1961 /// @note As the Resolver is run when the Program is built, this will only be
1962 /// useful for the Resolver itself and tests that use their own Resolver.
1963 /// @param var the AST variable
1964 /// @return the resolved semantic type for the variable, or nullptr if the
1965 /// variable has no resolved type.
1966 sem::Type* TypeOf(const ast::Variable* var) const;
1967
Ben Claytonfbec46f2021-04-30 20:20:19 +00001968 /// Helper for returning the resolved semantic type of the AST type `type`.
1969 /// @note As the Resolver is run when the Program is built, this will only be
1970 /// useful for the Resolver itself and tests that use their own Resolver.
1971 /// @param expr the AST type
1972 /// @return the resolved semantic type for the type, or nullptr if the type
1973 /// has no resolved type.
1974 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001975
Ben Clayton169512e2021-04-17 05:52:11 +00001976 /// Wraps the ast::Literal in a statement. This is used by tests that
1977 /// construct a partial AST and require the Resolver to reach these
1978 /// nodes.
1979 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1980 /// @return the ast::Statement that wraps the ast::Statement
1981 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001982 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001983 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001984 /// nodes.
1985 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1986 /// @return the ast::Statement that wraps the ast::Expression
1987 ast::Statement* WrapInStatement(ast::Expression* expr);
1988 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001989 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001990 /// these nodes.
1991 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1992 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1993 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1994 /// Returns the statement argument. Used as a passthrough-overload by
1995 /// WrapInFunction().
1996 /// @param stmt the ast::Statement
1997 /// @return `stmt`
1998 ast::Statement* WrapInStatement(ast::Statement* stmt);
1999 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002000 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002001 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002002 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002003 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002004 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002005 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002006 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002007 }
2008 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002009 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002010 /// @returns the function
2011 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002012
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002013 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002014 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002015
2016 protected:
2017 /// Asserts that the builder has not been moved.
2018 void AssertNotMoved() const;
2019
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002020 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002021 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002022 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002023 ASTNodeAllocator ast_nodes_;
2024 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002025 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002026 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002027 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002028 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002029
2030 /// The source to use when creating AST nodes without providing a Source as
2031 /// the first argument.
2032 Source source_;
2033
Ben Clayton5f0ea112021-03-09 10:54:37 +00002034 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002035 /// program when built.
2036 bool resolve_on_build_ = true;
2037
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002038 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2039 bool moved_ = false;
2040};
2041
2042//! @cond Doxygen_Suppress
2043// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2044template <>
2045struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002046 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002047 return t->i32();
2048 }
2049};
2050template <>
2051struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
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->u32();
2054 }
2055};
2056template <>
2057struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
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->f32();
2060 }
2061};
2062template <>
2063struct ProgramBuilder::TypesBuilder::CToAST<bool> {
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->bool_();
2066 }
2067};
2068template <>
2069struct ProgramBuilder::TypesBuilder::CToAST<void> {
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->void_();
2072 }
2073};
2074//! @endcond
2075
Ben Clayton917b14b2021-04-19 16:50:23 +00002076/// @param builder the ProgramBuilder
2077/// @returns the ProgramID of the ProgramBuilder
2078inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2079 return builder->ID();
2080}
2081
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002082} // namespace tint
2083
2084#endif // SRC_PROGRAM_BUILDER_H_