blob: 186d6f952592303a45ed41ded08aac96ad51b830 [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 Claytona922e652021-04-21 13:37:22 +0000394 typ::Void void_() const {
395 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
396 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000397
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000398 /// @param source the Source of the node
399 /// @returns a void type
400 typ::Void void_(const Source& source) const {
401 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
402 }
403
Antonio Maiorano25436862021-04-13 13:32:33 +0000404 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000405 /// @param n vector width in elements
406 /// @return the tint AST type for a `n`-element vector of `type`.
407 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000408 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000409 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
410 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000411 }
412
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000413 /// @param source the Source of the node
414 /// @param type vector subtype
415 /// @param n vector width in elements
416 /// @return the tint AST type for a `n`-element vector of `type`.
417 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000418 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000419 return {
420 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
421 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000422 }
423
Antonio Maiorano25436862021-04-13 13:32:33 +0000424 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000425 /// @return the tint AST type for a 2-element vector of `type`.
426 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
427
428 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000429 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000430 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000431
432 /// @param type vector subtype
433 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000434 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
435
436 /// @param n vector width in elements
437 /// @return the tint AST type for a `n`-element vector of `type`.
438 template <typename T>
439 typ::Vector vec(uint32_t n) const {
440 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000441 }
442
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000443 /// @return the tint AST type for a 2-element vector of the C type `T`.
444 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000445 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000446 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000447 }
448
449 /// @return the tint AST type for a 3-element vector of the C type `T`.
450 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000451 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000452 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000453 }
454
455 /// @return the tint AST type for a 4-element vector of the C type `T`.
456 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000457 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000458 return vec4(Of<T>());
459 }
460
461 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000462 /// @param columns number of columns for the matrix
463 /// @param rows number of rows for the matrix
464 /// @return the tint AST type for a matrix of `type`
465 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000466 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000467 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
468 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000469 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000470 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000471 }
472
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000473 /// @param source the Source of the node
474 /// @param type matrix subtype
475 /// @param columns number of columns for the matrix
476 /// @param rows number of rows for the matrix
477 /// @return the tint AST type for a matrix of `type`
478 typ::Matrix mat(const Source& source,
479 typ::Type type,
480 uint32_t columns,
481 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000482 return {type.ast
483 ? builder->create<ast::Matrix>(source, type, rows, columns)
484 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000485 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000486 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000487 }
488
Ben Claytone204f272021-04-22 14:40:23 +0000489 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000490 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000491 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000492
493 /// @param type matrix subtype
494 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000495 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000496
497 /// @param type matrix subtype
498 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000499 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000500
501 /// @param type matrix subtype
502 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000503 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000504
505 /// @param type matrix subtype
506 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000507 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000508
509 /// @param type matrix subtype
510 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000511 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000512
513 /// @param type matrix subtype
514 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000515 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000516
517 /// @param type matrix subtype
518 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000519 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000520
521 /// @param type matrix subtype
522 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000523 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000524
Ben Claytone204f272021-04-22 14:40:23 +0000525 /// @param columns number of columns for the matrix
526 /// @param rows number of rows for the matrix
527 /// @return the tint AST type for a matrix of `type`
528 template <typename T>
529 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
530 return mat(Of<T>(), columns, rows);
531 }
532
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000533 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
534 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000535 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000536 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000537 }
538
539 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
540 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000541 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000542 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000543 }
544
545 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
546 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000547 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000548 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000549 }
550
551 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
552 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000553 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000554 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 }
556
557 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
558 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000559 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000560 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 }
562
563 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
564 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000565 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000566 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 }
568
569 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
570 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000571 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000572 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
575 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
576 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000577 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000578 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 }
580
581 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
582 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000583 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000584 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000585 }
586
587 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000588 /// @param n the array size. 0 represents a runtime-array
589 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000590 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000591 ast::Array* array(typ::Type subtype,
592 uint32_t n = 0,
593 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000594 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000595 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000596 }
597
598 /// @param source the Source of the node
599 /// @param subtype the array element type
600 /// @param n the array size. 0 represents a runtime-array
601 /// @param decos the optional decorations for the array
602 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000603 ast::Array* array(const Source& source,
604 typ::Type subtype,
605 uint32_t n = 0,
606 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000607 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000608 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000609 }
610
Ben Claytonbab31972021-02-08 21:16:21 +0000611 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000612 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000613 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000614 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000615 ast::Array* array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000616 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000617 ast::DecorationList decos;
618 if (stride) {
619 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
620 }
621 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000622 }
623
Ben Clayton0f88b312021-05-04 17:36:31 +0000624 /// @param source the Source of the node
625 /// @param subtype the array element type
626 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000627 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000628 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000629 ast::Array* array(const Source& source,
630 typ::Type subtype,
631 uint32_t n,
632 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000633 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000634 ast::DecorationList decos;
635 if (stride) {
636 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
637 }
638 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000639 }
640
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000641 /// @return the tint AST type for an array of size `N` of type `T`
642 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000643 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000644 return array(Of<T>(), N);
645 }
646
Ben Claytonbab31972021-02-08 21:16:21 +0000647 /// @param stride the array stride
648 /// @return the tint AST type for an array of size `N` of type `T`
649 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000650 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000651 return array(Of<T>(), N, stride);
652 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000653
Ben Clayton0f88b312021-05-04 17:36:31 +0000654 /// Creates a type name
655 /// @param name the name
656 /// @returns the type name
657 template <typename NAME>
658 ast::TypeName* type_name(NAME&& name) const {
659 return builder->create<ast::TypeName>(
660 builder->Sym(std::forward<NAME>(name)));
661 }
662
663 /// Creates a type name
664 /// @param source the Source of the node
665 /// @param name the name
666 /// @returns the type name
667 template <typename NAME>
668 ast::TypeName* type_name(const Source& source, NAME&& name) const {
669 return builder->create<ast::TypeName>(
670 source, builder->Sym(std::forward<NAME>(name)));
671 }
672
Ben Clayton42d1e092021-02-02 14:29:15 +0000673 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000674 /// @param name the alias name
675 /// @param type the alias type
676 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000677 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000678 ast::Alias* alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000679 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000680 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000681 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000682 }
683
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000684 /// Creates an alias type
685 /// @param source the Source of the node
686 /// @param name the alias name
687 /// @param type the alias type
688 /// @returns the alias pointer
689 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000690 ast::Alias* alias(const Source& source, NAME&& name, typ::Type type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000691 type = MaybeCreateTypename(type);
692 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000693 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000694 }
695
Ben Clayton4db10dd2021-04-16 08:47:14 +0000696 /// Creates an access control qualifier type
697 /// @param access the access control
698 /// @param type the inner type
699 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000700 ast::AccessControl* access(ast::AccessControl::Access access,
701 const ast::Type* type) const {
702 type = MaybeCreateTypename(type).ast;
703 return type ? builder->create<ast::AccessControl>(access, type) : nullptr;
Ben Clayton4db10dd2021-04-16 08:47:14 +0000704 }
705
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000706 /// Creates an access control qualifier type
707 /// @param source the Source of the node
708 /// @param access the access control
709 /// @param type the inner type
710 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000711 ast::AccessControl* access(const Source& source,
712 ast::AccessControl::Access access,
713 const ast::Type* type) const {
714 type = MaybeCreateTypename(type).ast;
715 return type ? builder->create<ast::AccessControl>(source, access, type)
716 : nullptr;
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000717 }
718
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000719 /// @param type the type of the pointer
720 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000721 /// @return the pointer to `type` with the given ast::StorageClass
722 typ::Pointer pointer(typ::Type type,
723 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000724 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000725 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
726 : nullptr,
727 type.sem ? builder->create<sem::Pointer>(type, storage_class)
728 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000729 }
730
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000731 /// @param source the Source of the node
732 /// @param type the type of the pointer
733 /// @param storage_class the storage class of the pointer
734 /// @return the pointer to `type` with the given ast::StorageClass
735 typ::Pointer pointer(const Source& source,
736 typ::Type type,
737 ast::StorageClass storage_class) const {
738 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000739 return {type.ast
740 ? builder->create<ast::Pointer>(source, type, storage_class)
741 : nullptr,
742 type.sem ? builder->create<sem::Pointer>(type, storage_class)
743 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000744 }
745
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000746 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000747 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000748 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000749 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000750 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000751 }
752
Ben Claytone204f272021-04-22 14:40:23 +0000753 /// @param kind the kind of sampler
754 /// @returns the sampler
755 typ::Sampler sampler(ast::SamplerKind kind) const {
756 return {builder->create<ast::Sampler>(kind),
757 builder->create<sem::Sampler>(kind)};
758 }
759
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000760 /// @param source the Source of the node
761 /// @param kind the kind of sampler
762 /// @returns the sampler
763 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
764 return {builder->create<ast::Sampler>(source, kind),
765 builder->create<sem::Sampler>(kind)};
766 }
767
Ben Claytone204f272021-04-22 14:40:23 +0000768 /// @param dims the dimensionality of the texture
769 /// @returns the depth texture
770 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
771 return {builder->create<ast::DepthTexture>(dims),
772 builder->create<sem::DepthTexture>(dims)};
773 }
774
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000775 /// @param source the Source of the node
776 /// @param dims the dimensionality of the texture
777 /// @returns the depth texture
778 typ::DepthTexture depth_texture(const Source& source,
779 ast::TextureDimension dims) const {
780 return {builder->create<ast::DepthTexture>(source, dims),
781 builder->create<sem::DepthTexture>(dims)};
782 }
783
Ben Claytone204f272021-04-22 14:40:23 +0000784 /// @param dims the dimensionality of the texture
785 /// @param subtype the texture subtype.
786 /// @returns the sampled texture
787 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
788 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000789 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
790 : nullptr,
791 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
792 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000793 }
794
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000795 /// @param source the Source of the node
796 /// @param dims the dimensionality of the texture
797 /// @param subtype the texture subtype.
798 /// @returns the sampled texture
799 typ::SampledTexture sampled_texture(const Source& source,
800 ast::TextureDimension dims,
801 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000802 return {subtype.ast
803 ? builder->create<ast::SampledTexture>(source, dims, subtype)
804 : nullptr,
805 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
806 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000807 }
808
Ben Claytone204f272021-04-22 14:40:23 +0000809 /// @param dims the dimensionality of the texture
810 /// @param subtype the texture subtype.
811 /// @returns the multisampled texture
812 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
813 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000814 return {
815 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
816 : nullptr,
817 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
818 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000819 }
820
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000821 /// @param source the Source of the node
822 /// @param dims the dimensionality of the texture
823 /// @param subtype the texture subtype.
824 /// @returns the multisampled texture
825 typ::MultisampledTexture multisampled_texture(const Source& source,
826 ast::TextureDimension dims,
827 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000828 return {
829 subtype.ast
830 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
831 : nullptr,
832 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
833 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000834 }
835
Ben Claytone204f272021-04-22 14:40:23 +0000836 /// @param dims the dimensionality of the texture
837 /// @param format the image format of the texture
838 /// @returns the storage texture
839 typ::StorageTexture storage_texture(ast::TextureDimension dims,
840 ast::ImageFormat format) const {
841 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
842 auto* sem_subtype =
843 sem::StorageTexture::SubtypeFor(format, builder->Types());
844 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000845 builder->create<sem::StorageTexture>(
846 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000847 }
848
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000849 /// @param source the Source of the node
850 /// @param dims the dimensionality of the texture
851 /// @param format the image format of the texture
852 /// @returns the storage texture
853 typ::StorageTexture storage_texture(const Source& source,
854 ast::TextureDimension dims,
855 ast::ImageFormat format) const {
856 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
857 auto* sem_subtype =
858 sem::StorageTexture::SubtypeFor(format, builder->Types());
859 return {builder->create<ast::StorageTexture>(source, dims, format,
860 ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000861 builder->create<sem::StorageTexture>(
862 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000863 }
864
Brandon Jones7b257692021-05-17 17:40:17 +0000865 /// @returns the external texture
866 typ::ExternalTexture external_texture() const {
867 return {builder->create<ast::ExternalTexture>(),
868 builder->create<sem::ExternalTexture>()};
869 }
870
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000871 /// @param source the Source of the node
872 /// @returns the external texture
873 typ::ExternalTexture external_texture(const Source& source) const {
874 return {builder->create<ast::ExternalTexture>(source),
875 builder->create<sem::ExternalTexture>()};
876 }
877
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000878 /// If ty is a ast::Struct or ast::Alias, the returned type is an
879 /// ast::TypeName of the given type's name, otherwise type is returned.
880 /// @param type the type
881 /// @return either type or a pointer to a new ast::TypeName
882 typ::Type MaybeCreateTypename(typ::Type type) const;
883
Ben Claytonf5f311e2021-04-28 14:31:23 +0000884 /// The ProgramBuilder
885 ProgramBuilder* const builder;
886
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000887 private:
888 /// CToAST<T> is specialized for various `T` types and each specialization
889 /// contains a single static `get()` method for obtaining the corresponding
890 /// AST type for the C type `T`.
891 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000892 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000893 template <typename T>
894 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000895 };
896
897 //////////////////////////////////////////////////////////////////////////////
898 // AST helper methods
899 //////////////////////////////////////////////////////////////////////////////
900
James Price65ae64d2021-04-29 12:59:14 +0000901 /// @return a new unnamed symbol
902 Symbol Sym() { return Symbols().New(); }
903
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000904 /// @param name the symbol string
905 /// @return a Symbol with the given name
906 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
907
908 /// @param sym the symbol
909 /// @return `sym`
910 Symbol Sym(Symbol sym) { return sym; }
911
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000912 /// @param expr the expression
913 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000914 template <typename T>
915 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
916 return expr;
917 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000918
Ben Claytonb8ea5912021-04-19 16:52:42 +0000919 /// Passthrough for nullptr
920 /// @return nullptr
921 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
922
Ben Claytonfe0910f2021-05-17 15:51:47 +0000923 /// @param source the source information
924 /// @param symbol the identifier symbol
925 /// @return an ast::IdentifierExpression with the given symbol
926 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
927 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000928 }
929
Ben Clayton46d78d72021-02-10 21:34:26 +0000930 /// @param symbol the identifier symbol
931 /// @return an ast::IdentifierExpression with the given symbol
932 ast::IdentifierExpression* Expr(Symbol symbol) {
933 return create<ast::IdentifierExpression>(symbol);
934 }
935
Ben Claytonfe0910f2021-05-17 15:51:47 +0000936 /// @param source the source information
937 /// @param variable the AST variable
938 /// @return an ast::IdentifierExpression with the variable's symbol
939 ast::IdentifierExpression* Expr(const Source& source,
940 ast::Variable* variable) {
941 return create<ast::IdentifierExpression>(source, variable->symbol());
942 }
943
Ben Claytonb8ea5912021-04-19 16:52:42 +0000944 /// @param variable the AST variable
945 /// @return an ast::IdentifierExpression with the variable's symbol
946 ast::IdentifierExpression* Expr(ast::Variable* variable) {
947 return create<ast::IdentifierExpression>(variable->symbol());
948 }
949
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000950 /// @param source the source information
951 /// @param name the identifier name
952 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000953 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
954 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
955 }
956
957 /// @param name the identifier name
958 /// @return an ast::IdentifierExpression with the given name
959 ast::IdentifierExpression* Expr(const char* name) {
960 return create<ast::IdentifierExpression>(Symbols().Register(name));
961 }
962
963 /// @param source the source information
964 /// @param name the identifier name
965 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000966 ast::IdentifierExpression* Expr(const Source& source,
967 const std::string& name) {
968 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
969 }
970
971 /// @param name the identifier name
972 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000973 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000974 return create<ast::IdentifierExpression>(Symbols().Register(name));
975 }
976
Ben Claytonfe0910f2021-05-17 15:51:47 +0000977 /// @param source the source information
978 /// @param value the boolean value
979 /// @return a Scalar constructor for the given value
980 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
981 return create<ast::ScalarConstructorExpression>(source, Literal(value));
982 }
983
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000984 /// @param value the boolean value
985 /// @return a Scalar constructor for the given value
986 ast::ScalarConstructorExpression* Expr(bool value) {
987 return create<ast::ScalarConstructorExpression>(Literal(value));
988 }
989
Ben Claytonfe0910f2021-05-17 15:51:47 +0000990 /// @param source the source information
991 /// @param value the float value
992 /// @return a Scalar constructor for the given value
993 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
994 return create<ast::ScalarConstructorExpression>(source, Literal(value));
995 }
996
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000997 /// @param value the float value
998 /// @return a Scalar constructor for the given value
999 ast::ScalarConstructorExpression* Expr(f32 value) {
1000 return create<ast::ScalarConstructorExpression>(Literal(value));
1001 }
1002
Ben Claytonfe0910f2021-05-17 15:51:47 +00001003 /// @param source the source information
1004 /// @param value the integer value
1005 /// @return a Scalar constructor for the given value
1006 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1007 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1008 }
1009
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001010 /// @param value the integer value
1011 /// @return a Scalar constructor for the given value
1012 ast::ScalarConstructorExpression* Expr(i32 value) {
1013 return create<ast::ScalarConstructorExpression>(Literal(value));
1014 }
1015
Ben Claytonfe0910f2021-05-17 15:51:47 +00001016 /// @param source the source information
1017 /// @param value the unsigned int value
1018 /// @return a Scalar constructor for the given value
1019 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1020 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1021 }
1022
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001023 /// @param value the unsigned int value
1024 /// @return a Scalar constructor for the given value
1025 ast::ScalarConstructorExpression* Expr(u32 value) {
1026 return create<ast::ScalarConstructorExpression>(Literal(value));
1027 }
1028
1029 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1030 /// `list`.
1031 /// @param list the list to append too
1032 /// @param arg the arg to create
1033 template <typename ARG>
1034 void Append(ast::ExpressionList& list, ARG&& arg) {
1035 list.emplace_back(Expr(std::forward<ARG>(arg)));
1036 }
1037
1038 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1039 /// then appends them to `list`.
1040 /// @param list the list to append too
1041 /// @param arg0 the first argument
1042 /// @param args the rest of the arguments
1043 template <typename ARG0, typename... ARGS>
1044 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1045 Append(list, std::forward<ARG0>(arg0));
1046 Append(list, std::forward<ARGS>(args)...);
1047 }
1048
1049 /// @return an empty list of expressions
1050 ast::ExpressionList ExprList() { return {}; }
1051
1052 /// @param args the list of expressions
1053 /// @return the list of expressions converted to `ast::Expression`s using
1054 /// `Expr()`,
1055 template <typename... ARGS>
1056 ast::ExpressionList ExprList(ARGS&&... args) {
1057 ast::ExpressionList list;
1058 list.reserve(sizeof...(args));
1059 Append(list, std::forward<ARGS>(args)...);
1060 return list;
1061 }
1062
1063 /// @param list the list of expressions
1064 /// @return `list`
1065 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1066
1067 /// @param val the boolan value
1068 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001069 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001070
1071 /// @param val the float value
1072 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001073 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074
1075 /// @param val the unsigned int value
1076 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001077 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078
1079 /// @param val the integer value
1080 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001081 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001082
1083 /// @param args the arguments for the type constructor
1084 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1085 /// of `args` converted to `ast::Expression`s using `Expr()`
1086 template <typename T, typename... ARGS>
1087 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001088 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 }
1090
1091 /// @param type the type to construct
1092 /// @param args the arguments for the constructor
1093 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1094 /// values `args`.
1095 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001096 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001097 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001098 return create<ast::TypeConstructorExpression>(
1099 type, ExprList(std::forward<ARGS>(args)...));
1100 }
1101
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001102 /// Creates a constructor expression that constructs an object of
1103 /// `type` filled with `elem_value`. For example,
1104 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1105 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1106 /// @param type the type to construct
1107 /// @param elem_value the initial or element value (for vec and mat) to
1108 /// construct with
1109 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001110 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001111 int elem_value = 0);
1112
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001113 /// @param args the arguments for the vector constructor
1114 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1115 /// `T`, constructed with the values `args`.
1116 template <typename T, typename... ARGS>
1117 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001118 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 }
1120
1121 /// @param args the arguments for the vector constructor
1122 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1123 /// `T`, constructed with the values `args`.
1124 template <typename T, typename... ARGS>
1125 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001126 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 }
1128
1129 /// @param args the arguments for the vector constructor
1130 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1131 /// `T`, constructed with the values `args`.
1132 template <typename T, typename... ARGS>
1133 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001134 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001135 }
1136
1137 /// @param args the arguments for the matrix constructor
1138 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1139 /// `T`, constructed with the values `args`.
1140 template <typename T, typename... ARGS>
1141 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001142 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001143 }
1144
1145 /// @param args the arguments for the matrix constructor
1146 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1147 /// `T`, constructed with the values `args`.
1148 template <typename T, typename... ARGS>
1149 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001150 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001151 }
1152
1153 /// @param args the arguments for the matrix constructor
1154 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1155 /// `T`, constructed with the values `args`.
1156 template <typename T, typename... ARGS>
1157 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001158 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001159 }
1160
1161 /// @param args the arguments for the matrix constructor
1162 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1163 /// `T`, constructed with the values `args`.
1164 template <typename T, typename... ARGS>
1165 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001166 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001167 }
1168
1169 /// @param args the arguments for the matrix constructor
1170 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1171 /// `T`, constructed with the values `args`.
1172 template <typename T, typename... ARGS>
1173 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001174 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001175 }
1176
1177 /// @param args the arguments for the matrix constructor
1178 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1179 /// `T`, constructed with the values `args`.
1180 template <typename T, typename... ARGS>
1181 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001182 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001183 }
1184
1185 /// @param args the arguments for the matrix constructor
1186 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1187 /// `T`, constructed with the values `args`.
1188 template <typename T, typename... ARGS>
1189 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001190 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001191 }
1192
1193 /// @param args the arguments for the matrix constructor
1194 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1195 /// `T`, constructed with the values `args`.
1196 template <typename T, typename... ARGS>
1197 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001198 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 }
1200
1201 /// @param args the arguments for the matrix constructor
1202 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1203 /// `T`, constructed with the values `args`.
1204 template <typename T, typename... ARGS>
1205 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001206 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 }
1208
1209 /// @param args the arguments for the array constructor
1210 /// @return an `ast::TypeConstructorExpression` of an array with element type
1211 /// `T`, constructed with the values `args`.
1212 template <typename T, int N = 0, typename... ARGS>
1213 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001214 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 }
1216
1217 /// @param subtype the array element type
1218 /// @param n the array size. 0 represents a runtime-array.
1219 /// @param args the arguments for the array constructor
1220 /// @return an `ast::TypeConstructorExpression` of an array with element type
1221 /// `subtype`, constructed with the values `args`.
1222 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001223 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001224 uint32_t n,
1225 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001226 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227 }
1228
1229 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001231 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001232 /// @param constructor constructor expression
1233 /// @param decorations variable decorations
1234 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001235 template <typename NAME>
1236 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001237 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001238 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001239 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001240 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001241 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001242 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1243 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001244 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001245
1246 /// @param source the variable source
1247 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001248 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001249 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250 /// @param constructor constructor expression
1251 /// @param decorations variable decorations
1252 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001253 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001254 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001255 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001256 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001257 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001258 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001259 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001260 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001261 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001262 type, false, constructor, decorations);
1263 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001264
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001266 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001267 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001268 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001269 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001270 template <typename NAME>
1271 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001272 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001273 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001274 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001275 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001276 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001277 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001278 constructor, decorations);
1279 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001280
1281 /// @param source the variable source
1282 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001283 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001284 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001285 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001286 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001287 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001288 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001289 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001290 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001291 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001292 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001293 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001294 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001295 ast::StorageClass::kNone, type, true,
1296 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001297 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001298
James Pricedaf1f1c2021-04-08 22:15:48 +00001299 /// @param name the parameter name
1300 /// @param type the parameter type
1301 /// @param decorations optional parameter decorations
1302 /// @returns a constant `ast::Variable` with the given name and type
1303 template <typename NAME>
1304 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001305 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001306 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001307 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001308 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1309 ast::StorageClass::kNone, type, true, nullptr,
1310 decorations);
1311 }
1312
1313 /// @param source the parameter source
1314 /// @param name the parameter name
1315 /// @param type the parameter type
1316 /// @param decorations optional parameter decorations
1317 /// @returns a constant `ast::Variable` with the given name and type
1318 template <typename NAME>
1319 ast::Variable* Param(const Source& source,
1320 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001321 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001322 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001323 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001324 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1325 ast::StorageClass::kNone, type, true, nullptr,
1326 decorations);
1327 }
1328
Ben Clayton42708342021-04-21 17:55:12 +00001329 /// @param name the variable name
1330 /// @param type the variable type
1331 /// @param storage the variable storage class
1332 /// @param constructor constructor expression
1333 /// @param decorations variable decorations
1334 /// @returns a new `ast::Variable`, which is automatically registered as a
1335 /// global variable with the ast::Module.
1336 template <typename NAME>
1337 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001338 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001339 ast::StorageClass storage,
1340 ast::Expression* constructor = nullptr,
1341 ast::DecorationList decorations = {}) {
1342 auto* var =
1343 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1344 AST().AddGlobalVariable(var);
1345 return var;
1346 }
1347
1348 /// @param source the variable source
1349 /// @param name the variable name
1350 /// @param type the variable type
1351 /// @param storage the variable storage class
1352 /// @param constructor constructor expression
1353 /// @param decorations variable decorations
1354 /// @returns a new `ast::Variable`, which is automatically registered as a
1355 /// global variable with the ast::Module.
1356 template <typename NAME>
1357 ast::Variable* Global(const Source& source,
1358 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001359 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001360 ast::StorageClass storage,
1361 ast::Expression* constructor = nullptr,
1362 ast::DecorationList decorations = {}) {
1363 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1364 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001365 AST().AddGlobalVariable(var);
1366 return var;
1367 }
1368
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001369 /// @param name the variable name
1370 /// @param type the variable type
1371 /// @param constructor constructor expression
1372 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001373 /// @returns a const `ast::Variable` constructed by calling Var() with the
1374 /// arguments of `args`, which is automatically registered as a global
1375 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001376 template <typename NAME>
1377 ast::Variable* GlobalConst(NAME&& name,
1378 typ::Type type,
1379 ast::Expression* constructor,
1380 ast::DecorationList decorations = {}) {
1381 auto* var = Const(std::forward<NAME>(name), type, constructor,
1382 std::move(decorations));
1383 AST().AddGlobalVariable(var);
1384 return var;
1385 }
1386
1387 /// @param source the variable source
1388 /// @param name the variable name
1389 /// @param type the variable type
1390 /// @param constructor constructor expression
1391 /// @param decorations optional variable decorations
1392 /// @returns a const `ast::Variable` constructed by calling Var() with the
1393 /// arguments of `args`, which is automatically registered as a global
1394 /// variable with the ast::Module.
1395 template <typename NAME>
1396 ast::Variable* GlobalConst(const Source& source,
1397 NAME&& name,
1398 typ::Type type,
1399 ast::Expression* constructor,
1400 ast::DecorationList decorations = {}) {
1401 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1402 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001403 AST().AddGlobalVariable(var);
1404 return var;
1405 }
1406
Ben Claytonfe0910f2021-05-17 15:51:47 +00001407 /// @param source the source information
1408 /// @param expr the expression to take the address of
1409 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1410 template <typename EXPR>
1411 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1412 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1413 Expr(std::forward<EXPR>(expr)));
1414 }
1415
1416 /// @param expr the expression to take the address of
1417 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1418 template <typename EXPR>
1419 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1420 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1421 Expr(std::forward<EXPR>(expr)));
1422 }
1423
1424 /// @param source the source information
1425 /// @param expr the expression to perform an indirection on
1426 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1427 template <typename EXPR>
1428 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1429 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1430 Expr(std::forward<EXPR>(expr)));
1431 }
1432
1433 /// @param expr the expression to perform an indirection on
1434 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1435 template <typename EXPR>
1436 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1437 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1438 Expr(std::forward<EXPR>(expr)));
1439 }
1440
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001441 /// @param func the function name
1442 /// @param args the function call arguments
1443 /// @returns a `ast::CallExpression` to the function `func`, with the
1444 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1445 template <typename NAME, typename... ARGS>
1446 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1447 return create<ast::CallExpression>(Expr(func),
1448 ExprList(std::forward<ARGS>(args)...));
1449 }
1450
1451 /// @param lhs the left hand argument to the addition operation
1452 /// @param rhs the right hand argument to the addition operation
1453 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1454 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001455 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001456 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1457 Expr(std::forward<LHS>(lhs)),
1458 Expr(std::forward<RHS>(rhs)));
1459 }
1460
1461 /// @param lhs the left hand argument to the subtraction operation
1462 /// @param rhs the right hand argument to the subtraction operation
1463 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1464 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001465 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001466 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1467 Expr(std::forward<LHS>(lhs)),
1468 Expr(std::forward<RHS>(rhs)));
1469 }
1470
1471 /// @param lhs the left hand argument to the multiplication operation
1472 /// @param rhs the right hand argument to the multiplication operation
1473 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1474 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001475 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001476 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1477 Expr(std::forward<LHS>(lhs)),
1478 Expr(std::forward<RHS>(rhs)));
1479 }
1480
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001481 /// @param source the source information
1482 /// @param lhs the left hand argument to the multiplication operation
1483 /// @param rhs the right hand argument to the multiplication operation
1484 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1485 template <typename LHS, typename RHS>
1486 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1487 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1488 Expr(std::forward<LHS>(lhs)),
1489 Expr(std::forward<RHS>(rhs)));
1490 }
1491
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001492 /// @param lhs the left hand argument to the division operation
1493 /// @param rhs the right hand argument to the division operation
1494 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1495 template <typename LHS, typename RHS>
1496 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1497 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1498 Expr(std::forward<LHS>(lhs)),
1499 Expr(std::forward<RHS>(rhs)));
1500 }
1501
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001502 /// @param arr the array argument for the array accessor expression
1503 /// @param idx the index argument for the array accessor expression
1504 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1505 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001506 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001507 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1508 Expr(std::forward<IDX>(idx)));
1509 }
1510
1511 /// @param obj the object for the member accessor expression
1512 /// @param idx the index argument for the array accessor expression
1513 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1514 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001515 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001516 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1517 Expr(std::forward<IDX>(idx)));
1518 }
1519
Ben Clayton42d1e092021-02-02 14:29:15 +00001520 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001521 /// @param val the offset value
1522 /// @returns the offset decoration pointer
1523 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1524 return create<ast::StructMemberOffsetDecoration>(source_, val);
1525 }
1526
Ben Claytond614dd52021-03-15 10:43:11 +00001527 /// Creates a ast::StructMemberSizeDecoration
1528 /// @param source the source information
1529 /// @param val the size value
1530 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001531 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1532 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001533 return create<ast::StructMemberSizeDecoration>(source, val);
1534 }
1535
1536 /// Creates a ast::StructMemberSizeDecoration
1537 /// @param val the size value
1538 /// @returns the size decoration pointer
1539 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1540 return create<ast::StructMemberSizeDecoration>(source_, val);
1541 }
1542
1543 /// Creates a ast::StructMemberAlignDecoration
1544 /// @param source the source information
1545 /// @param val the align value
1546 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001547 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1548 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001549 return create<ast::StructMemberAlignDecoration>(source, val);
1550 }
1551
1552 /// Creates a ast::StructMemberAlignDecoration
1553 /// @param val the align value
1554 /// @returns the align decoration pointer
1555 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1556 return create<ast::StructMemberAlignDecoration>(source_, val);
1557 }
1558
Ben Clayton42d1e092021-02-02 14:29:15 +00001559 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001560 /// @param source the source information
1561 /// @param name the function name
1562 /// @param params the function parameters
1563 /// @param type the function return type
1564 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001565 /// @param decorations the optional function decorations
1566 /// @param return_type_decorations the optional function return type
1567 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001568 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001569 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001570 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001571 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001572 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001573 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001574 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001575 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001576 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001577 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001578 auto* func =
1579 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1580 type, create<ast::BlockStatement>(body),
1581 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001582 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001583 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001584 }
1585
Ben Clayton42d1e092021-02-02 14:29:15 +00001586 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001587 /// @param name the function name
1588 /// @param params the function parameters
1589 /// @param type the function return type
1590 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001591 /// @param decorations the optional function decorations
1592 /// @param return_type_decorations the optional function return type
1593 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001594 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001595 template <typename NAME>
1596 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001597 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001598 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001599 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001600 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001601 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001602 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001603 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1604 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001605 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001606 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001607 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001608 }
1609
Ben Clayton49668bb2021-04-17 05:32:01 +00001610 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001611 /// @param source the source information
1612 /// @returns the return statement pointer
1613 ast::ReturnStatement* Return(const Source& source) {
1614 return create<ast::ReturnStatement>(source);
1615 }
1616
1617 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001618 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001619 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1620
1621 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001622 /// @param source the source information
1623 /// @param val the return value
1624 /// @returns the return statement pointer
1625 template <typename EXPR>
1626 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1627 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1628 }
1629
1630 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001631 /// @param val the return value
1632 /// @returns the return statement pointer
1633 template <typename EXPR>
1634 ast::ReturnStatement* Return(EXPR&& val) {
1635 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001636 }
1637
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001638 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001639 /// @param source the source information
1640 /// @param name the struct name
1641 /// @param members the struct members
1642 /// @param decorations the optional struct decorations
1643 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001644 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001645 ast::Struct* Structure(const Source& source,
1646 NAME&& name,
1647 ast::StructMemberList members,
1648 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001649 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001650 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001651 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001652 AST().AddConstructedType(type);
1653 return type;
1654 }
1655
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001656 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001657 /// @param name the struct name
1658 /// @param members the struct members
1659 /// @param decorations the optional struct decorations
1660 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001661 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001662 ast::Struct* Structure(NAME&& name,
1663 ast::StructMemberList members,
1664 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001665 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001666 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001667 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001668 AST().AddConstructedType(type);
1669 return type;
1670 }
1671
Ben Clayton42d1e092021-02-02 14:29:15 +00001672 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001673 /// @param source the source information
1674 /// @param name the struct member name
1675 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001676 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001677 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001678 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001679 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001680 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001681 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001682 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001683 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001684 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1685 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001686 }
1687
Ben Clayton42d1e092021-02-02 14:29:15 +00001688 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001689 /// @param name the struct member name
1690 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001691 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001692 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001693 template <typename NAME>
1694 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001695 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001696 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001697 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001698 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1699 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001700 }
1701
Ben Claytonbab31972021-02-08 21:16:21 +00001702 /// Creates a ast::StructMember with the given byte offset
1703 /// @param offset the offset to use in the StructMemberOffsetDecoration
1704 /// @param name the struct member name
1705 /// @param type the struct member type
1706 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001707 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001708 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001709 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001710 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001711 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001712 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001713 create<ast::StructMemberOffsetDecoration>(offset),
1714 });
1715 }
1716
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001717 /// Creates a ast::BlockStatement with input statements
1718 /// @param statements statements of block
1719 /// @returns the block statement pointer
1720 template <typename... Statements>
1721 ast::BlockStatement* Block(Statements&&... statements) {
1722 return create<ast::BlockStatement>(
1723 ast::StatementList{std::forward<Statements>(statements)...});
1724 }
1725
1726 /// Creates a ast::ElseStatement with input condition and body
1727 /// @param condition the else condition expression
1728 /// @param body the else body
1729 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001730 template <typename CONDITION>
1731 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1732 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1733 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001734 }
1735
1736 /// Creates a ast::IfStatement with input condition, body, and optional
1737 /// variadic else statements
1738 /// @param condition the if statement condition expression
1739 /// @param body the if statement body
1740 /// @param elseStatements optional variadic else statements
1741 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001742 template <typename CONDITION, typename... ELSE_STATEMENTS>
1743 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001744 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001745 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001746 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001747 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001748 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001749 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001750 }
1751
1752 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001753 /// @param source the source information
1754 /// @param lhs the left hand side expression initializer
1755 /// @param rhs the right hand side expression initializer
1756 /// @returns the assignment statement pointer
1757 template <typename LhsExpressionInit, typename RhsExpressionInit>
1758 ast::AssignmentStatement* Assign(const Source& source,
1759 LhsExpressionInit&& lhs,
1760 RhsExpressionInit&& rhs) {
1761 return create<ast::AssignmentStatement>(
1762 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1763 Expr(std::forward<RhsExpressionInit>(rhs)));
1764 }
1765
1766 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001767 /// @param lhs the left hand side expression initializer
1768 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001769 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001770 template <typename LhsExpressionInit, typename RhsExpressionInit>
1771 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1772 RhsExpressionInit&& rhs) {
1773 return create<ast::AssignmentStatement>(
1774 Expr(std::forward<LhsExpressionInit>(lhs)),
1775 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001776 }
1777
1778 /// Creates a ast::LoopStatement with input body and optional continuing
1779 /// @param body the loop body
1780 /// @param continuing the optional continuing block
1781 /// @returns the loop statement pointer
1782 ast::LoopStatement* Loop(ast::BlockStatement* body,
1783 ast::BlockStatement* continuing = nullptr) {
1784 return create<ast::LoopStatement>(body, continuing);
1785 }
1786
1787 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001788 /// @param source the source information
1789 /// @param var the variable to wrap in a decl statement
1790 /// @returns the variable decl statement pointer
1791 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1792 return create<ast::VariableDeclStatement>(source, var);
1793 }
1794
1795 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001796 /// @param var the variable to wrap in a decl statement
1797 /// @returns the variable decl statement pointer
1798 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1799 return create<ast::VariableDeclStatement>(var);
1800 }
1801
Antonio Maioranocea744d2021-03-25 12:55:27 +00001802 /// Creates a ast::SwitchStatement with input expression and cases
1803 /// @param condition the condition expression initializer
1804 /// @param cases case statements
1805 /// @returns the switch statement pointer
1806 template <typename ExpressionInit, typename... Cases>
1807 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1808 return create<ast::SwitchStatement>(
1809 Expr(std::forward<ExpressionInit>(condition)),
1810 ast::CaseStatementList{std::forward<Cases>(cases)...});
1811 }
1812
1813 /// Creates a ast::CaseStatement with input list of selectors, and body
1814 /// @param selectors list of selectors
1815 /// @param body the case body
1816 /// @returns the case statement pointer
1817 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1818 ast::BlockStatement* body = nullptr) {
1819 return create<ast::CaseStatement>(std::move(selectors),
1820 body ? body : Block());
1821 }
1822
1823 /// Convenient overload that takes a single selector
1824 /// @param selector a single case selector
1825 /// @param body the case body
1826 /// @returns the case statement pointer
1827 ast::CaseStatement* Case(ast::IntLiteral* selector,
1828 ast::BlockStatement* body = nullptr) {
1829 return Case(ast::CaseSelectorList{selector}, body);
1830 }
1831
1832 /// Convenience function that creates a 'default' ast::CaseStatement
1833 /// @param body the case body
1834 /// @returns the case statement pointer
1835 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1836 return Case(ast::CaseSelectorList{}, body);
1837 }
1838
James Price68f558f2021-04-06 15:51:47 +00001839 /// Creates an ast::BuiltinDecoration
1840 /// @param source the source information
1841 /// @param builtin the builtin value
1842 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001843 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001844 return create<ast::BuiltinDecoration>(source, builtin);
1845 }
1846
1847 /// Creates an ast::BuiltinDecoration
1848 /// @param builtin the builtin value
1849 /// @returns the builtin decoration pointer
1850 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1851 return create<ast::BuiltinDecoration>(source_, builtin);
1852 }
1853
1854 /// Creates an ast::LocationDecoration
1855 /// @param source the source information
1856 /// @param location the location value
1857 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001858 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001859 return create<ast::LocationDecoration>(source, location);
1860 }
1861
1862 /// Creates an ast::LocationDecoration
1863 /// @param location the location value
1864 /// @returns the location decoration pointer
1865 ast::LocationDecoration* Location(uint32_t location) {
1866 return create<ast::LocationDecoration>(source_, location);
1867 }
1868
James Pricef2f3bfc2021-05-13 20:32:32 +00001869 /// Creates an ast::OverrideDecoration with a specific constant ID
1870 /// @param source the source information
1871 /// @param id the id value
1872 /// @returns the override decoration pointer
1873 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1874 return create<ast::OverrideDecoration>(source, id);
1875 }
1876
1877 /// Creates an ast::OverrideDecoration with a specific constant ID
1878 /// @param id the optional id value
1879 /// @returns the override decoration pointer
1880 ast::OverrideDecoration* Override(uint32_t id) {
1881 return Override(source_, id);
1882 }
1883
1884 /// Creates an ast::OverrideDecoration without a constant ID
1885 /// @param source the source information
1886 /// @returns the override decoration pointer
1887 ast::OverrideDecoration* Override(const Source& source) {
1888 return create<ast::OverrideDecoration>(source);
1889 }
1890
1891 /// Creates an ast::OverrideDecoration without a constant ID
1892 /// @returns the override decoration pointer
1893 ast::OverrideDecoration* Override() { return Override(source_); }
1894
James Price68f558f2021-04-06 15:51:47 +00001895 /// Creates an ast::StageDecoration
1896 /// @param source the source information
1897 /// @param stage the pipeline stage
1898 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001899 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001900 return create<ast::StageDecoration>(source, stage);
1901 }
1902
1903 /// Creates an ast::StageDecoration
1904 /// @param stage the pipeline stage
1905 /// @returns the stage decoration pointer
1906 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1907 return create<ast::StageDecoration>(source_, stage);
1908 }
1909
James Price70f80bb2021-05-19 13:40:08 +00001910 /// Creates an ast::WorkgroupDecoration
1911 /// @param x the x dimension expression
1912 /// @returns the workgroup decoration pointer
1913 template <typename EXPR_X>
1914 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1915 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1916 }
1917
1918 /// Creates an ast::WorkgroupDecoration
1919 /// @param x the x dimension expression
1920 /// @param y the y dimension expression
1921 /// @returns the workgroup decoration pointer
1922 template <typename EXPR_X, typename EXPR_Y>
1923 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1924 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1925 nullptr);
1926 }
1927
1928 /// Creates an ast::WorkgroupDecoration
1929 /// @param x the x dimension expression
1930 /// @param y the y dimension expression
1931 /// @param z the z dimension expression
1932 /// @returns the workgroup decoration pointer
1933 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1934 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1935 return create<ast::WorkgroupDecoration>(
1936 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1937 Expr(std::forward<EXPR_Z>(z)));
1938 }
1939
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001940 /// Sets the current builder source to `src`
1941 /// @param src the Source used for future create() calls
1942 void SetSource(const Source& src) {
1943 AssertNotMoved();
1944 source_ = src;
1945 }
1946
1947 /// Sets the current builder source to `loc`
1948 /// @param loc the Source used for future create() calls
1949 void SetSource(const Source::Location& loc) {
1950 AssertNotMoved();
1951 source_ = Source(loc);
1952 }
1953
Ben Clayton33352542021-01-29 16:43:41 +00001954 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001955 /// @note As the Resolver is run when the Program is built, this will only be
1956 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001957 /// @param expr the AST expression
1958 /// @return the resolved semantic type for the expression, or nullptr if the
1959 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001960 sem::Type* TypeOf(const ast::Expression* expr) const;
1961
Ben Claytonfe0910f2021-05-17 15:51:47 +00001962 /// Helper for returning the resolved semantic type of the variable `var`.
1963 /// @note As the Resolver is run when the Program is built, this will only be
1964 /// useful for the Resolver itself and tests that use their own Resolver.
1965 /// @param var the AST variable
1966 /// @return the resolved semantic type for the variable, or nullptr if the
1967 /// variable has no resolved type.
1968 sem::Type* TypeOf(const ast::Variable* var) const;
1969
Ben Claytonfbec46f2021-04-30 20:20:19 +00001970 /// Helper for returning the resolved semantic type of the AST type `type`.
1971 /// @note As the Resolver is run when the Program is built, this will only be
1972 /// useful for the Resolver itself and tests that use their own Resolver.
1973 /// @param expr the AST type
1974 /// @return the resolved semantic type for the type, or nullptr if the type
1975 /// has no resolved type.
1976 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001977
Ben Clayton169512e2021-04-17 05:52:11 +00001978 /// Wraps the ast::Literal in a statement. This is used by tests that
1979 /// construct a partial AST and require the Resolver to reach these
1980 /// nodes.
1981 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1982 /// @return the ast::Statement that wraps the ast::Statement
1983 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001984 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001985 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001986 /// nodes.
1987 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1988 /// @return the ast::Statement that wraps the ast::Expression
1989 ast::Statement* WrapInStatement(ast::Expression* expr);
1990 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001991 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001992 /// these nodes.
1993 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1994 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1995 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1996 /// Returns the statement argument. Used as a passthrough-overload by
1997 /// WrapInFunction().
1998 /// @param stmt the ast::Statement
1999 /// @return `stmt`
2000 ast::Statement* WrapInStatement(ast::Statement* stmt);
2001 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002002 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002003 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002004 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002005 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002006 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002007 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002008 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002009 }
2010 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002011 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002012 /// @returns the function
2013 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002014
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002015 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002016 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002017
2018 protected:
2019 /// Asserts that the builder has not been moved.
2020 void AssertNotMoved() const;
2021
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002022 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002023 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002024 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002025 ASTNodeAllocator ast_nodes_;
2026 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002027 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002028 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002029 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002030 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002031
2032 /// The source to use when creating AST nodes without providing a Source as
2033 /// the first argument.
2034 Source source_;
2035
Ben Clayton5f0ea112021-03-09 10:54:37 +00002036 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002037 /// program when built.
2038 bool resolve_on_build_ = true;
2039
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002040 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2041 bool moved_ = false;
2042};
2043
2044//! @cond Doxygen_Suppress
2045// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2046template <>
2047struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002048 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002049 return t->i32();
2050 }
2051};
2052template <>
2053struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002054 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002055 return t->u32();
2056 }
2057};
2058template <>
2059struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002060 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002061 return t->f32();
2062 }
2063};
2064template <>
2065struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002066 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002067 return t->bool_();
2068 }
2069};
2070template <>
2071struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002072 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002073 return t->void_();
2074 }
2075};
2076//! @endcond
2077
Ben Clayton917b14b2021-04-19 16:50:23 +00002078/// @param builder the ProgramBuilder
2079/// @returns the ProgramID of the ProgramBuilder
2080inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2081 return builder->ID();
2082}
2083
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002084} // namespace tint
2085
2086#endif // SRC_PROGRAM_BUILDER_H_