blob: 36ae067122759bec8d12c551e876097eb2203613 [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000026#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/bool_literal.h"
28#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000029#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000030#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000031#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000032#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000035#include "src/ast/if_statement.h"
36#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000037#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/member_accessor_expression.h"
39#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000040#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000041#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000043#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/scalar_constructor_expression.h"
46#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000047#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000049#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000050#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000053#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000055#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000056#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000058#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000059#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000060#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000061#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000062#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000064#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000065#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000066#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000067#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000068#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000069#include "src/sem/f32_type.h"
70#include "src/sem/i32_type.h"
71#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000073#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/sampled_texture_type.h"
75#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000076#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000077#include "src/sem/u32_type.h"
78#include "src/sem/vector_type.h"
79#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000080#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000081
82namespace tint {
83
Ben Clayton401b96b2021-02-03 17:19:59 +000084// Forward declarations
85namespace ast {
86class VariableDeclStatement;
87} // namespace ast
88
Ben Claytona6b9a8e2021-01-26 16:57:10 +000089class CloneContext;
90
91/// ProgramBuilder is a mutable builder for a Program.
92/// To construct a Program, populate the builder and then `std::move` it to a
93/// Program.
94class ProgramBuilder {
95 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000096 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
97 using ASTNodeAllocator = BlockAllocator<ast::Node>;
98
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000099 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
100 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000101
102 /// `i32` is a type alias to `int`.
103 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
104 /// WGSL syntax.
105 /// Note: this is intentionally not aliased to uint32_t as we want integer
106 /// literals passed to the builder to match WGSL's integer literal types.
107 using i32 = decltype(1);
108 /// `u32` is a type alias to `unsigned int`.
109 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
110 /// WGSL syntax.
111 /// Note: this is intentionally not aliased to uint32_t as we want integer
112 /// literals passed to the builder to match WGSL's integer literal types.
113 using u32 = decltype(1u);
114 /// `f32` is a type alias to `float`
115 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
116 /// WGSL syntax.
117 using f32 = float;
118
119 /// Constructor
120 ProgramBuilder();
121
122 /// Move constructor
123 /// @param rhs the builder to move
124 ProgramBuilder(ProgramBuilder&& rhs);
125
126 /// Destructor
127 virtual ~ProgramBuilder();
128
129 /// Move assignment operator
130 /// @param rhs the builder to move
131 /// @return this builder
132 ProgramBuilder& operator=(ProgramBuilder&& rhs);
133
Ben Claytone43c8302021-01-29 11:59:32 +0000134 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
135 /// making a deep clone of the Program contents.
136 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
137 /// existing immutable program.
138 /// As the returned ProgramBuilder wraps `program`, `program` must not be
139 /// destructed or assigned while using the returned ProgramBuilder.
140 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
141 /// function. See crbug.com/tint/460.
142 /// @param program the immutable Program to wrap
143 /// @return the ProgramBuilder that wraps `program`
144 static ProgramBuilder Wrap(const Program* program);
145
Ben Claytone6995de2021-04-13 23:27:27 +0000146 /// @returns the unique identifier for this program
147 ProgramID ID() const { return id_; }
148
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000149 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000150 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000151 AssertNotMoved();
152 return types_;
153 }
154
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000156 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000157 AssertNotMoved();
158 return types_;
159 }
160
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000162 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000163 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000164 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000165 }
166
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000168 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000169 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000170 return ast_nodes_;
171 }
172
173 /// @returns a reference to the program's semantic nodes storage
174 SemNodeAllocator& SemNodes() {
175 AssertNotMoved();
176 return sem_nodes_;
177 }
178
179 /// @returns a reference to the program's semantic nodes storage
180 const SemNodeAllocator& SemNodes() const {
181 AssertNotMoved();
182 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000183 }
184
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000185 /// @returns a reference to the program's AST root Module
186 ast::Module& AST() {
187 AssertNotMoved();
188 return *ast_;
189 }
190
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000191 /// @returns a reference to the program's AST root Module
192 const ast::Module& AST() const {
193 AssertNotMoved();
194 return *ast_;
195 }
196
197 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000198 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000199 AssertNotMoved();
200 return sem_;
201 }
202
203 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000204 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000205 AssertNotMoved();
206 return sem_;
207 }
208
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000209 /// @returns a reference to the program's SymbolTable
210 SymbolTable& Symbols() {
211 AssertNotMoved();
212 return symbols_;
213 }
214
Ben Clayton708dc2d2021-01-29 11:22:40 +0000215 /// @returns a reference to the program's SymbolTable
216 const SymbolTable& Symbols() const {
217 AssertNotMoved();
218 return symbols_;
219 }
220
Ben Clayton844217f2021-01-27 18:49:05 +0000221 /// @returns a reference to the program's diagnostics
222 diag::List& Diagnostics() {
223 AssertNotMoved();
224 return diagnostics_;
225 }
226
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000227 /// @returns a reference to the program's diagnostics
228 const diag::List& Diagnostics() const {
229 AssertNotMoved();
230 return diagnostics_;
231 }
232
Ben Clayton5f0ea112021-03-09 10:54:37 +0000233 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000234 /// @param enable the new flag value (defaults to true)
235 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
236
Ben Clayton5f0ea112021-03-09 10:54:37 +0000237 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000238 /// built.
239 bool ResolveOnBuild() const { return resolve_on_build_; }
240
Ben Clayton844217f2021-01-27 18:49:05 +0000241 /// @returns true if the program has no error diagnostics and is not missing
242 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000243 bool IsValid() const;
244
Ben Clayton708dc2d2021-01-29 11:22:40 +0000245 /// Writes a representation of the node to the output stream
246 /// @note unlike str(), to_str() does not automatically demangle the string.
247 /// @param node the AST node
248 /// @param out the stream to write to
249 /// @param indent number of spaces to indent the node when writing
250 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
251 node->to_str(Sem(), out, indent);
252 }
253
254 /// Returns a demangled, string representation of `node`.
255 /// @param node the AST node
256 /// @returns a string representation of the node
257 std::string str(const ast::Node* node) const;
258
Ben Clayton7fdfff12021-01-29 15:17:30 +0000259 /// Creates a new ast::Node owned by the ProgramBuilder. When the
260 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000261 /// @param source the Source of the node
262 /// @param args the arguments to pass to the type constructor
263 /// @returns the node pointer
264 template <typename T, typename... ARGS>
265 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
266 ARGS&&... args) {
267 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000268 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000269 }
270
Ben Clayton7fdfff12021-01-29 15:17:30 +0000271 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
272 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000273 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000274 /// When the ProgramBuilder is destructed, the ast::Node will also be
275 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000276 /// @returns the node pointer
277 template <typename T>
278 traits::EnableIfIsType<T, ast::Node>* create() {
279 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000280 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000281 }
282
Ben Clayton7fdfff12021-01-29 15:17:30 +0000283 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
284 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000285 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000286 /// When the ProgramBuilder is destructed, the ast::Node will also be
287 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000288 /// @param arg0 the first arguments to pass to the type constructor
289 /// @param args the remaining arguments to pass to the type constructor
290 /// @returns the node pointer
291 template <typename T, typename ARG0, typename... ARGS>
292 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
293 traits::IsTypeOrDerived<T, ast::Node>::value &&
294 !traits::IsTypeOrDerived<ARG0, Source>::value,
295 T>*
296 create(ARG0&& arg0, ARGS&&... args) {
297 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000298 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000299 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000300 }
301
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000302 /// Creates a new sem::Node owned by the ProgramBuilder.
303 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000304 /// destructed.
305 /// @param args the arguments to pass to the type constructor
306 /// @returns the node pointer
307 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000308 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
309 !traits::IsTypeOrDerived<T, sem::Type>::value,
310 T>*
311 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000312 AssertNotMoved();
313 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
314 }
315
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000316 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000317 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
318 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000319 /// Types are unique (de-aliased), and so calling create() for the same `T`
320 /// and arguments will return the same pointer.
321 /// @warning Use this method to acquire a type only if all of its type
322 /// information is provided in the constructor arguments `args`.<br>
323 /// If the type requires additional configuration after construction that
324 /// affect its fundamental type, build the type with `std::make_unique`, make
325 /// any necessary alterations and then call unique_type() instead.
326 /// @param args the arguments to pass to the type constructor
327 /// @returns the de-aliased type pointer
328 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000329 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
330 static_assert(std::is_base_of<sem::Type, T>::value,
331 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000332 AssertNotMoved();
333 return types_.Get<T>(std::forward<ARGS>(args)...);
334 }
335
336 /// Marks this builder as moved, preventing any further use of the builder.
337 void MarkAsMoved();
338
339 //////////////////////////////////////////////////////////////////////////////
340 // TypesBuilder
341 //////////////////////////////////////////////////////////////////////////////
342
343 /// TypesBuilder holds basic `tint` types and methods for constructing
344 /// complex types.
345 class TypesBuilder {
346 public:
347 /// Constructor
348 /// @param builder the program builder
349 explicit TypesBuilder(ProgramBuilder* builder);
350
351 /// @return the tint AST type for the C type `T`.
352 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000353 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000354 return CToAST<T>::get(this);
355 }
356
357 /// @returns a boolean type
Ben Claytonc03d3bd2021-05-20 14:22:28 +0000358 typ::Bool bool_() const { return {builder->create<ast::Bool>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000359
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000360 /// @param source the Source of the node
361 /// @returns a boolean type
362 typ::Bool bool_(const Source& source) const {
Ben Claytonc03d3bd2021-05-20 14:22:28 +0000363 return {builder->create<ast::Bool>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000364 }
365
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000366 /// @returns a f32 type
Ben Clayton512d60c2021-05-19 19:51:22 +0000367 typ::F32 f32() const { return {builder->create<ast::F32>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000369 /// @param source the Source of the node
370 /// @returns a f32 type
371 typ::F32 f32(const Source& source) const {
Ben Clayton512d60c2021-05-19 19:51:22 +0000372 return {builder->create<ast::F32>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000373 }
374
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000375 /// @returns a i32 type
Ben Claytoncf421bb2021-05-19 17:47:11 +0000376 typ::I32 i32() const { return {builder->create<ast::I32>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000377
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000378 /// @param source the Source of the node
379 /// @returns a i32 type
380 typ::I32 i32(const Source& source) const {
Ben Claytoncf421bb2021-05-19 17:47:11 +0000381 return {builder->create<ast::I32>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000382 }
383
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000384 /// @returns a u32 type
Ben Clayton58dec172021-05-19 17:47:11 +0000385 typ::U32 u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000386
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000387 /// @param source the Source of the node
388 /// @returns a u32 type
389 typ::U32 u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000390 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000391 }
392
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000393 /// @returns a void type
Ben Claytoncc98b4f2021-05-20 14:31:48 +0000394 typ::Void void_() const { return {builder->create<ast::Void>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000395
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000396 /// @param source the Source of the node
397 /// @returns a void type
398 typ::Void void_(const Source& source) const {
Ben Claytoncc98b4f2021-05-20 14:31:48 +0000399 return {builder->create<ast::Void>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000400 }
401
Antonio Maiorano25436862021-04-13 13:32:33 +0000402 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000403 /// @param n vector width in elements
404 /// @return the tint AST type for a `n`-element vector of `type`.
405 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000406 type = MaybeCreateTypename(type);
Ben Claytond3e80fd2021-05-20 14:57:58 +0000407 return {builder->create<ast::Vector>(type, n)};
Antonio Maiorano25436862021-04-13 13:32:33 +0000408 }
409
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000410 /// @param source the Source of the node
411 /// @param type vector subtype
412 /// @param n vector width in elements
413 /// @return the tint AST type for a `n`-element vector of `type`.
414 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000415 type = MaybeCreateTypename(type);
Ben Claytond3e80fd2021-05-20 14:57:58 +0000416 return {builder->create<ast::Vector>(source, type, n)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000417 }
418
Antonio Maiorano25436862021-04-13 13:32:33 +0000419 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000420 /// @return the tint AST type for a 2-element vector of `type`.
421 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
422
423 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000424 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000425 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000426
427 /// @param type vector subtype
428 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000429 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
430
431 /// @param n vector width in elements
432 /// @return the tint AST type for a `n`-element vector of `type`.
433 template <typename T>
434 typ::Vector vec(uint32_t n) const {
435 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000436 }
437
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000438 /// @return the tint AST type for a 2-element vector of the C type `T`.
439 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000440 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000441 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000442 }
443
444 /// @return the tint AST type for a 3-element vector of the C type `T`.
445 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000446 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000447 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000448 }
449
450 /// @return the tint AST type for a 4-element vector of the C type `T`.
451 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000452 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000453 return vec4(Of<T>());
454 }
455
456 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000457 /// @param columns number of columns for the matrix
458 /// @param rows number of rows for the matrix
459 /// @return the tint AST type for a matrix of `type`
460 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000461 type = MaybeCreateTypename(type);
Ben Claytond3e80fd2021-05-20 14:57:58 +0000462 return {builder->create<ast::Matrix>(type, rows, columns)};
Ben Claytone204f272021-04-22 14:40:23 +0000463 }
464
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000465 /// @param source the Source of the node
466 /// @param type matrix subtype
467 /// @param columns number of columns for the matrix
468 /// @param rows number of rows for the matrix
469 /// @return the tint AST type for a matrix of `type`
470 typ::Matrix mat(const Source& source,
471 typ::Type type,
472 uint32_t columns,
473 uint32_t rows) const {
Ben Claytond3e80fd2021-05-20 14:57:58 +0000474 return {builder->create<ast::Matrix>(source, type, rows, columns)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000475 }
476
Ben Claytone204f272021-04-22 14:40:23 +0000477 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000478 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000479 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000480
481 /// @param type matrix subtype
482 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000483 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000484
485 /// @param type matrix subtype
486 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000487 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000488
489 /// @param type matrix subtype
490 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000491 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000492
493 /// @param type matrix subtype
494 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000495 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000496
497 /// @param type matrix subtype
498 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000499 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000500
501 /// @param type matrix subtype
502 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000503 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000504
505 /// @param type matrix subtype
506 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000507 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000508
509 /// @param type matrix subtype
510 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000511 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000512
Ben Claytone204f272021-04-22 14:40:23 +0000513 /// @param columns number of columns for the matrix
514 /// @param rows number of rows for the matrix
515 /// @return the tint AST type for a matrix of `type`
516 template <typename T>
517 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
518 return mat(Of<T>(), columns, rows);
519 }
520
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000521 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
522 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000523 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000524 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000525 }
526
527 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
528 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000529 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000530 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000531 }
532
533 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
534 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000535 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000536 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000537 }
538
539 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
540 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000541 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000542 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000543 }
544
545 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
546 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000547 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000548 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000549 }
550
551 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
552 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000553 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000554 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 }
556
557 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
558 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000559 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000560 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 }
562
563 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
564 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000565 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000566 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 }
568
569 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
570 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000571 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000572 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
575 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000576 /// @param n the array size. 0 represents a runtime-array
577 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000578 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000579 ast::Array* array(typ::Type subtype,
580 uint32_t n = 0,
581 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000582 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000583 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000584 }
585
586 /// @param source the Source of the node
587 /// @param subtype the array element type
588 /// @param n the array size. 0 represents a runtime-array
589 /// @param decos the optional decorations for the array
590 /// @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(const Source& source,
592 typ::Type subtype,
593 uint32_t n = 0,
594 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000595 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000596 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000597 }
598
Ben Claytonbab31972021-02-08 21:16:21 +0000599 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000600 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000601 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000602 /// @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(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000604 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000605 ast::DecorationList decos;
606 if (stride) {
607 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
608 }
609 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000610 }
611
Ben Clayton0f88b312021-05-04 17:36:31 +0000612 /// @param source the Source of the node
613 /// @param subtype the array element type
614 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000615 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000616 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000617 ast::Array* array(const Source& source,
618 typ::Type subtype,
619 uint32_t n,
620 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000621 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000622 ast::DecorationList decos;
623 if (stride) {
624 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
625 }
626 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000627 }
628
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000629 /// @return the tint AST type for an array of size `N` of type `T`
630 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000631 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000632 return array(Of<T>(), N);
633 }
634
Ben Claytonbab31972021-02-08 21:16:21 +0000635 /// @param stride the array stride
636 /// @return the tint AST type for an array of size `N` of type `T`
637 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000638 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000639 return array(Of<T>(), N, stride);
640 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000641
Ben Clayton0f88b312021-05-04 17:36:31 +0000642 /// Creates a type name
643 /// @param name the name
644 /// @returns the type name
645 template <typename NAME>
646 ast::TypeName* type_name(NAME&& name) const {
647 return builder->create<ast::TypeName>(
648 builder->Sym(std::forward<NAME>(name)));
649 }
650
651 /// Creates a type name
652 /// @param source the Source of the node
653 /// @param name the name
654 /// @returns the type name
655 template <typename NAME>
656 ast::TypeName* type_name(const Source& source, NAME&& name) const {
657 return builder->create<ast::TypeName>(
658 source, builder->Sym(std::forward<NAME>(name)));
659 }
660
Ben Clayton42d1e092021-02-02 14:29:15 +0000661 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000662 /// @param name the alias name
663 /// @param type the alias type
664 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000665 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000666 ast::Alias* alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000667 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000668 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000669 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000670 }
671
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000672 /// Creates an alias type
673 /// @param source the Source of the node
674 /// @param name the alias name
675 /// @param type the alias type
676 /// @returns the alias pointer
677 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000678 ast::Alias* alias(const Source& source, NAME&& name, typ::Type type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000679 type = MaybeCreateTypename(type);
680 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000681 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000682 }
683
Ben Clayton4db10dd2021-04-16 08:47:14 +0000684 /// Creates an access control qualifier type
685 /// @param access the access control
686 /// @param type the inner type
687 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000688 ast::AccessControl* access(ast::AccessControl::Access access,
689 const ast::Type* type) const {
690 type = MaybeCreateTypename(type).ast;
691 return type ? builder->create<ast::AccessControl>(access, type) : nullptr;
Ben Clayton4db10dd2021-04-16 08:47:14 +0000692 }
693
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000694 /// Creates an access control qualifier type
695 /// @param source the Source of the node
696 /// @param access the access control
697 /// @param type the inner type
698 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000699 ast::AccessControl* access(const Source& source,
700 ast::AccessControl::Access access,
701 const ast::Type* type) const {
702 type = MaybeCreateTypename(type).ast;
703 return type ? builder->create<ast::AccessControl>(source, access, type)
704 : nullptr;
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000705 }
706
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000707 /// @param type the type of the pointer
708 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000709 /// @return the pointer to `type` with the given ast::StorageClass
710 typ::Pointer pointer(typ::Type type,
711 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000712 type = MaybeCreateTypename(type);
Ben Clayton37c4e4a2021-05-20 15:02:38 +0000713 return {builder->create<ast::Pointer>(type, storage_class)};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000714 }
715
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000716 /// @param source the Source of the node
717 /// @param type the type of the pointer
718 /// @param storage_class the storage class of the pointer
719 /// @return the pointer to `type` with the given ast::StorageClass
720 typ::Pointer pointer(const Source& source,
721 typ::Type type,
722 ast::StorageClass storage_class) const {
723 type = MaybeCreateTypename(type);
Ben Clayton37c4e4a2021-05-20 15:02:38 +0000724 return {builder->create<ast::Pointer>(source, type, storage_class)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000725 }
726
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000727 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000728 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000729 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000730 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000731 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000732 }
733
Ben Claytone204f272021-04-22 14:40:23 +0000734 /// @param kind the kind of sampler
735 /// @returns the sampler
736 typ::Sampler sampler(ast::SamplerKind kind) const {
Ben Claytonb9662652021-05-20 14:54:28 +0000737 return {builder->create<ast::Sampler>(kind)};
Ben Claytone204f272021-04-22 14:40:23 +0000738 }
739
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000740 /// @param source the Source of the node
741 /// @param kind the kind of sampler
742 /// @returns the sampler
743 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
Ben Claytonb9662652021-05-20 14:54:28 +0000744 return {builder->create<ast::Sampler>(source, kind)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000745 }
746
Ben Claytone204f272021-04-22 14:40:23 +0000747 /// @param dims the dimensionality of the texture
748 /// @returns the depth texture
749 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000750 return {builder->create<ast::DepthTexture>(dims)};
Ben Claytone204f272021-04-22 14:40:23 +0000751 }
752
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000753 /// @param source the Source of the node
754 /// @param dims the dimensionality of the texture
755 /// @returns the depth texture
756 typ::DepthTexture depth_texture(const Source& source,
757 ast::TextureDimension dims) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000758 return {builder->create<ast::DepthTexture>(source, dims)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000759 }
760
Ben Claytone204f272021-04-22 14:40:23 +0000761 /// @param dims the dimensionality of the texture
762 /// @param subtype the texture subtype.
763 /// @returns the sampled texture
764 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
765 typ::Type subtype) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000766 return {builder->create<ast::SampledTexture>(dims, subtype)};
Ben Claytone204f272021-04-22 14:40:23 +0000767 }
768
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000769 /// @param source the Source of the node
770 /// @param dims the dimensionality of the texture
771 /// @param subtype the texture subtype.
772 /// @returns the sampled texture
773 typ::SampledTexture sampled_texture(const Source& source,
774 ast::TextureDimension dims,
775 typ::Type subtype) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000776 return {builder->create<ast::SampledTexture>(source, dims, subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000777 }
778
Ben Claytone204f272021-04-22 14:40:23 +0000779 /// @param dims the dimensionality of the texture
780 /// @param subtype the texture subtype.
781 /// @returns the multisampled texture
782 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
783 typ::Type subtype) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000784 return {builder->create<ast::MultisampledTexture>(dims, subtype)};
Ben Claytone204f272021-04-22 14:40:23 +0000785 }
786
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000787 /// @param source the Source of the node
788 /// @param dims the dimensionality of the texture
789 /// @param subtype the texture subtype.
790 /// @returns the multisampled texture
791 typ::MultisampledTexture multisampled_texture(const Source& source,
792 ast::TextureDimension dims,
793 typ::Type subtype) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000794 return {builder->create<ast::MultisampledTexture>(source, dims, subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000795 }
796
Ben Claytone204f272021-04-22 14:40:23 +0000797 /// @param dims the dimensionality of the texture
798 /// @param format the image format of the texture
799 /// @returns the storage texture
800 typ::StorageTexture storage_texture(ast::TextureDimension dims,
801 ast::ImageFormat format) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000802 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
803 return {builder->create<ast::StorageTexture>(dims, format, subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000804 }
805
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000806 /// @param source the Source of the node
807 /// @param dims the dimensionality of the texture
808 /// @param format the image format of the texture
809 /// @returns the storage texture
810 typ::StorageTexture storage_texture(const Source& source,
811 ast::TextureDimension dims,
812 ast::ImageFormat format) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000813 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
814 return {
815 builder->create<ast::StorageTexture>(source, dims, format, subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000816 }
817
Brandon Jones7b257692021-05-17 17:40:17 +0000818 /// @returns the external texture
819 typ::ExternalTexture external_texture() const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000820 return {builder->create<ast::ExternalTexture>()};
Brandon Jones7b257692021-05-17 17:40:17 +0000821 }
822
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000823 /// @param source the Source of the node
824 /// @returns the external texture
825 typ::ExternalTexture external_texture(const Source& source) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000826 return {builder->create<ast::ExternalTexture>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000827 }
828
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000829 /// If ty is a ast::Struct or ast::Alias, the returned type is an
830 /// ast::TypeName of the given type's name, otherwise type is returned.
831 /// @param type the type
832 /// @return either type or a pointer to a new ast::TypeName
833 typ::Type MaybeCreateTypename(typ::Type type) const;
834
Ben Claytonf5f311e2021-04-28 14:31:23 +0000835 /// The ProgramBuilder
836 ProgramBuilder* const builder;
837
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000838 private:
839 /// CToAST<T> is specialized for various `T` types and each specialization
840 /// contains a single static `get()` method for obtaining the corresponding
841 /// AST type for the C type `T`.
842 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000843 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000844 template <typename T>
845 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000846 };
847
848 //////////////////////////////////////////////////////////////////////////////
849 // AST helper methods
850 //////////////////////////////////////////////////////////////////////////////
851
James Price65ae64d2021-04-29 12:59:14 +0000852 /// @return a new unnamed symbol
853 Symbol Sym() { return Symbols().New(); }
854
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000855 /// @param name the symbol string
856 /// @return a Symbol with the given name
857 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
858
859 /// @param sym the symbol
860 /// @return `sym`
861 Symbol Sym(Symbol sym) { return sym; }
862
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000863 /// @param expr the expression
864 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000865 template <typename T>
866 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
867 return expr;
868 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000869
Ben Claytonb8ea5912021-04-19 16:52:42 +0000870 /// Passthrough for nullptr
871 /// @return nullptr
872 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
873
Ben Claytonfe0910f2021-05-17 15:51:47 +0000874 /// @param source the source information
875 /// @param symbol the identifier symbol
876 /// @return an ast::IdentifierExpression with the given symbol
877 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
878 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000879 }
880
Ben Clayton46d78d72021-02-10 21:34:26 +0000881 /// @param symbol the identifier symbol
882 /// @return an ast::IdentifierExpression with the given symbol
883 ast::IdentifierExpression* Expr(Symbol symbol) {
884 return create<ast::IdentifierExpression>(symbol);
885 }
886
Ben Claytonfe0910f2021-05-17 15:51:47 +0000887 /// @param source the source information
888 /// @param variable the AST variable
889 /// @return an ast::IdentifierExpression with the variable's symbol
890 ast::IdentifierExpression* Expr(const Source& source,
891 ast::Variable* variable) {
892 return create<ast::IdentifierExpression>(source, variable->symbol());
893 }
894
Ben Claytonb8ea5912021-04-19 16:52:42 +0000895 /// @param variable the AST variable
896 /// @return an ast::IdentifierExpression with the variable's symbol
897 ast::IdentifierExpression* Expr(ast::Variable* variable) {
898 return create<ast::IdentifierExpression>(variable->symbol());
899 }
900
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000901 /// @param source the source information
902 /// @param name the identifier name
903 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000904 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
905 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
906 }
907
908 /// @param name the identifier name
909 /// @return an ast::IdentifierExpression with the given name
910 ast::IdentifierExpression* Expr(const char* name) {
911 return create<ast::IdentifierExpression>(Symbols().Register(name));
912 }
913
914 /// @param source the source information
915 /// @param name the identifier name
916 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000917 ast::IdentifierExpression* Expr(const Source& source,
918 const std::string& name) {
919 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
920 }
921
922 /// @param name the identifier name
923 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000924 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000925 return create<ast::IdentifierExpression>(Symbols().Register(name));
926 }
927
Ben Claytonfe0910f2021-05-17 15:51:47 +0000928 /// @param source the source information
929 /// @param value the boolean value
930 /// @return a Scalar constructor for the given value
931 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
932 return create<ast::ScalarConstructorExpression>(source, Literal(value));
933 }
934
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000935 /// @param value the boolean value
936 /// @return a Scalar constructor for the given value
937 ast::ScalarConstructorExpression* Expr(bool value) {
938 return create<ast::ScalarConstructorExpression>(Literal(value));
939 }
940
Ben Claytonfe0910f2021-05-17 15:51:47 +0000941 /// @param source the source information
942 /// @param value the float value
943 /// @return a Scalar constructor for the given value
944 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
945 return create<ast::ScalarConstructorExpression>(source, Literal(value));
946 }
947
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000948 /// @param value the float value
949 /// @return a Scalar constructor for the given value
950 ast::ScalarConstructorExpression* Expr(f32 value) {
951 return create<ast::ScalarConstructorExpression>(Literal(value));
952 }
953
Ben Claytonfe0910f2021-05-17 15:51:47 +0000954 /// @param source the source information
955 /// @param value the integer value
956 /// @return a Scalar constructor for the given value
957 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
958 return create<ast::ScalarConstructorExpression>(source, Literal(value));
959 }
960
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000961 /// @param value the integer value
962 /// @return a Scalar constructor for the given value
963 ast::ScalarConstructorExpression* Expr(i32 value) {
964 return create<ast::ScalarConstructorExpression>(Literal(value));
965 }
966
Ben Claytonfe0910f2021-05-17 15:51:47 +0000967 /// @param source the source information
968 /// @param value the unsigned int value
969 /// @return a Scalar constructor for the given value
970 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
971 return create<ast::ScalarConstructorExpression>(source, Literal(value));
972 }
973
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000974 /// @param value the unsigned int value
975 /// @return a Scalar constructor for the given value
976 ast::ScalarConstructorExpression* Expr(u32 value) {
977 return create<ast::ScalarConstructorExpression>(Literal(value));
978 }
979
980 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
981 /// `list`.
982 /// @param list the list to append too
983 /// @param arg the arg to create
984 template <typename ARG>
985 void Append(ast::ExpressionList& list, ARG&& arg) {
986 list.emplace_back(Expr(std::forward<ARG>(arg)));
987 }
988
989 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
990 /// then appends them to `list`.
991 /// @param list the list to append too
992 /// @param arg0 the first argument
993 /// @param args the rest of the arguments
994 template <typename ARG0, typename... ARGS>
995 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
996 Append(list, std::forward<ARG0>(arg0));
997 Append(list, std::forward<ARGS>(args)...);
998 }
999
1000 /// @return an empty list of expressions
1001 ast::ExpressionList ExprList() { return {}; }
1002
1003 /// @param args the list of expressions
1004 /// @return the list of expressions converted to `ast::Expression`s using
1005 /// `Expr()`,
1006 template <typename... ARGS>
1007 ast::ExpressionList ExprList(ARGS&&... args) {
1008 ast::ExpressionList list;
1009 list.reserve(sizeof...(args));
1010 Append(list, std::forward<ARGS>(args)...);
1011 return list;
1012 }
1013
1014 /// @param list the list of expressions
1015 /// @return `list`
1016 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1017
1018 /// @param val the boolan value
1019 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001020 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001021
1022 /// @param val the float value
1023 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001024 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001025
1026 /// @param val the unsigned int value
1027 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001028 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001029
1030 /// @param val the integer value
1031 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001032 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001033
1034 /// @param args the arguments for the type constructor
1035 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1036 /// of `args` converted to `ast::Expression`s using `Expr()`
1037 template <typename T, typename... ARGS>
1038 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001039 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001040 }
1041
1042 /// @param type the type to construct
1043 /// @param args the arguments for the constructor
1044 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1045 /// values `args`.
1046 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001047 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001048 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001049 return create<ast::TypeConstructorExpression>(
1050 type, ExprList(std::forward<ARGS>(args)...));
1051 }
1052
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001053 /// Creates a constructor expression that constructs an object of
1054 /// `type` filled with `elem_value`. For example,
1055 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1056 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1057 /// @param type the type to construct
1058 /// @param elem_value the initial or element value (for vec and mat) to
1059 /// construct with
1060 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001061 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001062 int elem_value = 0);
1063
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001064 /// @param args the arguments for the vector constructor
1065 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1066 /// `T`, constructed with the values `args`.
1067 template <typename T, typename... ARGS>
1068 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001069 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001070 }
1071
1072 /// @param args the arguments for the vector constructor
1073 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1074 /// `T`, constructed with the values `args`.
1075 template <typename T, typename... ARGS>
1076 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001077 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078 }
1079
1080 /// @param args the arguments for the vector constructor
1081 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1082 /// `T`, constructed with the values `args`.
1083 template <typename T, typename... ARGS>
1084 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001085 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001086 }
1087
1088 /// @param args the arguments for the matrix constructor
1089 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1090 /// `T`, constructed with the values `args`.
1091 template <typename T, typename... ARGS>
1092 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001093 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001094 }
1095
1096 /// @param args the arguments for the matrix constructor
1097 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1098 /// `T`, constructed with the values `args`.
1099 template <typename T, typename... ARGS>
1100 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001101 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001102 }
1103
1104 /// @param args the arguments for the matrix constructor
1105 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1106 /// `T`, constructed with the values `args`.
1107 template <typename T, typename... ARGS>
1108 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001109 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001110 }
1111
1112 /// @param args the arguments for the matrix constructor
1113 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1114 /// `T`, constructed with the values `args`.
1115 template <typename T, typename... ARGS>
1116 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001117 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001118 }
1119
1120 /// @param args the arguments for the matrix constructor
1121 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1122 /// `T`, constructed with the values `args`.
1123 template <typename T, typename... ARGS>
1124 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001125 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001126 }
1127
1128 /// @param args the arguments for the matrix constructor
1129 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1130 /// `T`, constructed with the values `args`.
1131 template <typename T, typename... ARGS>
1132 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001133 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001134 }
1135
1136 /// @param args the arguments for the matrix constructor
1137 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1138 /// `T`, constructed with the values `args`.
1139 template <typename T, typename... ARGS>
1140 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001141 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001142 }
1143
1144 /// @param args the arguments for the matrix constructor
1145 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1146 /// `T`, constructed with the values `args`.
1147 template <typename T, typename... ARGS>
1148 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001149 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001150 }
1151
1152 /// @param args the arguments for the matrix constructor
1153 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1154 /// `T`, constructed with the values `args`.
1155 template <typename T, typename... ARGS>
1156 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001157 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001158 }
1159
1160 /// @param args the arguments for the array constructor
1161 /// @return an `ast::TypeConstructorExpression` of an array with element type
1162 /// `T`, constructed with the values `args`.
1163 template <typename T, int N = 0, typename... ARGS>
1164 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001165 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001166 }
1167
1168 /// @param subtype the array element type
1169 /// @param n the array size. 0 represents a runtime-array.
1170 /// @param args the arguments for the array constructor
1171 /// @return an `ast::TypeConstructorExpression` of an array with element type
1172 /// `subtype`, constructed with the values `args`.
1173 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001174 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001175 uint32_t n,
1176 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001177 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001178 }
1179
1180 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001182 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001183 /// @param constructor constructor expression
1184 /// @param decorations variable decorations
1185 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001186 template <typename NAME>
1187 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001188 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001189 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001190 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001191 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001192 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001193 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1194 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001195 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001196
1197 /// @param source the variable source
1198 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001200 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001201 /// @param constructor constructor expression
1202 /// @param decorations variable decorations
1203 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001204 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001205 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001206 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001207 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001208 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001209 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001210 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001211 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001212 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001213 type, false, constructor, decorations);
1214 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001217 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001218 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001219 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001220 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001221 template <typename NAME>
1222 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001223 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001224 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001225 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001226 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001227 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001228 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001229 constructor, decorations);
1230 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231
1232 /// @param source the variable source
1233 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001234 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001235 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001236 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001237 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001238 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001239 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001240 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001241 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001242 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001243 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001244 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001245 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001246 ast::StorageClass::kNone, type, true,
1247 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001248 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001249
James Pricedaf1f1c2021-04-08 22:15:48 +00001250 /// @param name the parameter name
1251 /// @param type the parameter type
1252 /// @param decorations optional parameter decorations
1253 /// @returns a constant `ast::Variable` with the given name and type
1254 template <typename NAME>
1255 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001256 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001257 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001258 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001259 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1260 ast::StorageClass::kNone, type, true, nullptr,
1261 decorations);
1262 }
1263
1264 /// @param source the parameter source
1265 /// @param name the parameter name
1266 /// @param type the parameter type
1267 /// @param decorations optional parameter decorations
1268 /// @returns a constant `ast::Variable` with the given name and type
1269 template <typename NAME>
1270 ast::Variable* Param(const Source& source,
1271 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001272 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001273 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001274 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001275 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1276 ast::StorageClass::kNone, type, true, nullptr,
1277 decorations);
1278 }
1279
Ben Clayton42708342021-04-21 17:55:12 +00001280 /// @param name the variable name
1281 /// @param type the variable type
1282 /// @param storage the variable storage class
1283 /// @param constructor constructor expression
1284 /// @param decorations variable decorations
1285 /// @returns a new `ast::Variable`, which is automatically registered as a
1286 /// global variable with the ast::Module.
1287 template <typename NAME>
1288 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001289 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001290 ast::StorageClass storage,
1291 ast::Expression* constructor = nullptr,
1292 ast::DecorationList decorations = {}) {
1293 auto* var =
1294 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1295 AST().AddGlobalVariable(var);
1296 return var;
1297 }
1298
1299 /// @param source the variable source
1300 /// @param name the variable name
1301 /// @param type the variable type
1302 /// @param storage the variable storage class
1303 /// @param constructor constructor expression
1304 /// @param decorations variable decorations
1305 /// @returns a new `ast::Variable`, which is automatically registered as a
1306 /// global variable with the ast::Module.
1307 template <typename NAME>
1308 ast::Variable* Global(const Source& source,
1309 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001310 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001311 ast::StorageClass storage,
1312 ast::Expression* constructor = nullptr,
1313 ast::DecorationList decorations = {}) {
1314 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1315 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001316 AST().AddGlobalVariable(var);
1317 return var;
1318 }
1319
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001320 /// @param name the variable name
1321 /// @param type the variable type
1322 /// @param constructor constructor expression
1323 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001324 /// @returns a const `ast::Variable` constructed by calling Var() with the
1325 /// arguments of `args`, which is automatically registered as a global
1326 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001327 template <typename NAME>
1328 ast::Variable* GlobalConst(NAME&& name,
1329 typ::Type type,
1330 ast::Expression* constructor,
1331 ast::DecorationList decorations = {}) {
1332 auto* var = Const(std::forward<NAME>(name), type, constructor,
1333 std::move(decorations));
1334 AST().AddGlobalVariable(var);
1335 return var;
1336 }
1337
1338 /// @param source the variable source
1339 /// @param name the variable name
1340 /// @param type the variable type
1341 /// @param constructor constructor expression
1342 /// @param decorations optional variable decorations
1343 /// @returns a const `ast::Variable` constructed by calling Var() with the
1344 /// arguments of `args`, which is automatically registered as a global
1345 /// variable with the ast::Module.
1346 template <typename NAME>
1347 ast::Variable* GlobalConst(const Source& source,
1348 NAME&& name,
1349 typ::Type type,
1350 ast::Expression* constructor,
1351 ast::DecorationList decorations = {}) {
1352 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1353 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001354 AST().AddGlobalVariable(var);
1355 return var;
1356 }
1357
Ben Claytonfe0910f2021-05-17 15:51:47 +00001358 /// @param source the source information
1359 /// @param expr the expression to take the address of
1360 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1361 template <typename EXPR>
1362 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1363 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1364 Expr(std::forward<EXPR>(expr)));
1365 }
1366
1367 /// @param expr the expression to take the address of
1368 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1369 template <typename EXPR>
1370 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1371 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1372 Expr(std::forward<EXPR>(expr)));
1373 }
1374
1375 /// @param source the source information
1376 /// @param expr the expression to perform an indirection on
1377 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1378 template <typename EXPR>
1379 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1380 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1381 Expr(std::forward<EXPR>(expr)));
1382 }
1383
1384 /// @param expr the expression to perform an indirection on
1385 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1386 template <typename EXPR>
1387 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1388 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1389 Expr(std::forward<EXPR>(expr)));
1390 }
1391
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001392 /// @param func the function name
1393 /// @param args the function call arguments
1394 /// @returns a `ast::CallExpression` to the function `func`, with the
1395 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1396 template <typename NAME, typename... ARGS>
1397 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1398 return create<ast::CallExpression>(Expr(func),
1399 ExprList(std::forward<ARGS>(args)...));
1400 }
1401
1402 /// @param lhs the left hand argument to the addition operation
1403 /// @param rhs the right hand argument to the addition operation
1404 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1405 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001406 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001407 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1408 Expr(std::forward<LHS>(lhs)),
1409 Expr(std::forward<RHS>(rhs)));
1410 }
1411
1412 /// @param lhs the left hand argument to the subtraction operation
1413 /// @param rhs the right hand argument to the subtraction operation
1414 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1415 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001416 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001417 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1418 Expr(std::forward<LHS>(lhs)),
1419 Expr(std::forward<RHS>(rhs)));
1420 }
1421
1422 /// @param lhs the left hand argument to the multiplication operation
1423 /// @param rhs the right hand argument to the multiplication operation
1424 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1425 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001426 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001427 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1428 Expr(std::forward<LHS>(lhs)),
1429 Expr(std::forward<RHS>(rhs)));
1430 }
1431
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001432 /// @param source the source information
1433 /// @param lhs the left hand argument to the multiplication operation
1434 /// @param rhs the right hand argument to the multiplication operation
1435 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1436 template <typename LHS, typename RHS>
1437 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1438 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1439 Expr(std::forward<LHS>(lhs)),
1440 Expr(std::forward<RHS>(rhs)));
1441 }
1442
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001443 /// @param lhs the left hand argument to the division operation
1444 /// @param rhs the right hand argument to the division operation
1445 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1446 template <typename LHS, typename RHS>
1447 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1448 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1449 Expr(std::forward<LHS>(lhs)),
1450 Expr(std::forward<RHS>(rhs)));
1451 }
1452
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001453 /// @param arr the array argument for the array accessor expression
1454 /// @param idx the index argument for the array accessor expression
1455 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1456 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001457 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001458 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1459 Expr(std::forward<IDX>(idx)));
1460 }
1461
1462 /// @param obj the object for the member accessor expression
1463 /// @param idx the index argument for the array accessor expression
1464 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1465 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001466 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001467 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1468 Expr(std::forward<IDX>(idx)));
1469 }
1470
Ben Clayton42d1e092021-02-02 14:29:15 +00001471 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001472 /// @param val the offset value
1473 /// @returns the offset decoration pointer
1474 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1475 return create<ast::StructMemberOffsetDecoration>(source_, val);
1476 }
1477
Ben Claytond614dd52021-03-15 10:43:11 +00001478 /// Creates a ast::StructMemberSizeDecoration
1479 /// @param source the source information
1480 /// @param val the size value
1481 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001482 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1483 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001484 return create<ast::StructMemberSizeDecoration>(source, val);
1485 }
1486
1487 /// Creates a ast::StructMemberSizeDecoration
1488 /// @param val the size value
1489 /// @returns the size decoration pointer
1490 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1491 return create<ast::StructMemberSizeDecoration>(source_, val);
1492 }
1493
1494 /// Creates a ast::StructMemberAlignDecoration
1495 /// @param source the source information
1496 /// @param val the align value
1497 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001498 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1499 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001500 return create<ast::StructMemberAlignDecoration>(source, val);
1501 }
1502
1503 /// Creates a ast::StructMemberAlignDecoration
1504 /// @param val the align value
1505 /// @returns the align decoration pointer
1506 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1507 return create<ast::StructMemberAlignDecoration>(source_, val);
1508 }
1509
Ben Clayton42d1e092021-02-02 14:29:15 +00001510 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001511 /// @param source the source information
1512 /// @param name the function name
1513 /// @param params the function parameters
1514 /// @param type the function return type
1515 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001516 /// @param decorations the optional function decorations
1517 /// @param return_type_decorations the optional function return type
1518 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001519 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001520 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001521 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001522 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001523 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001524 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001525 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001526 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001527 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001528 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001529 auto* func =
1530 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1531 type, create<ast::BlockStatement>(body),
1532 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001533 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001534 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001535 }
1536
Ben Clayton42d1e092021-02-02 14:29:15 +00001537 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001538 /// @param name the function name
1539 /// @param params the function parameters
1540 /// @param type the function return type
1541 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001542 /// @param decorations the optional function decorations
1543 /// @param return_type_decorations the optional function return type
1544 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001545 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001546 template <typename NAME>
1547 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001548 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001549 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001550 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001551 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001552 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001553 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001554 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1555 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001556 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001557 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001558 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001559 }
1560
Ben Clayton49668bb2021-04-17 05:32:01 +00001561 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001562 /// @param source the source information
1563 /// @returns the return statement pointer
1564 ast::ReturnStatement* Return(const Source& source) {
1565 return create<ast::ReturnStatement>(source);
1566 }
1567
1568 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001569 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001570 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1571
1572 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001573 /// @param source the source information
1574 /// @param val the return value
1575 /// @returns the return statement pointer
1576 template <typename EXPR>
1577 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1578 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1579 }
1580
1581 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001582 /// @param val the return value
1583 /// @returns the return statement pointer
1584 template <typename EXPR>
1585 ast::ReturnStatement* Return(EXPR&& val) {
1586 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001587 }
1588
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001589 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001590 /// @param source the source information
1591 /// @param name the struct name
1592 /// @param members the struct members
1593 /// @param decorations the optional struct decorations
1594 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001595 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001596 ast::Struct* Structure(const Source& source,
1597 NAME&& name,
1598 ast::StructMemberList members,
1599 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001600 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001601 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001602 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001603 AST().AddConstructedType(type);
1604 return type;
1605 }
1606
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001607 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001608 /// @param name the struct name
1609 /// @param members the struct members
1610 /// @param decorations the optional struct decorations
1611 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001612 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001613 ast::Struct* Structure(NAME&& name,
1614 ast::StructMemberList members,
1615 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001616 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001617 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001618 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001619 AST().AddConstructedType(type);
1620 return type;
1621 }
1622
Ben Clayton42d1e092021-02-02 14:29:15 +00001623 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001624 /// @param source the source information
1625 /// @param name the struct member name
1626 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001627 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001628 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001629 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001630 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001631 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001632 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001633 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001634 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001635 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1636 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001637 }
1638
Ben Clayton42d1e092021-02-02 14:29:15 +00001639 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001640 /// @param name the struct member name
1641 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001642 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001643 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001644 template <typename NAME>
1645 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001646 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001647 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001648 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001649 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1650 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001651 }
1652
Ben Claytonbab31972021-02-08 21:16:21 +00001653 /// Creates a ast::StructMember with the given byte offset
1654 /// @param offset the offset to use in the StructMemberOffsetDecoration
1655 /// @param name the struct member name
1656 /// @param type the struct member type
1657 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001658 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001659 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001660 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001661 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001662 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001663 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001664 create<ast::StructMemberOffsetDecoration>(offset),
1665 });
1666 }
1667
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001668 /// Creates a ast::BlockStatement with input statements
1669 /// @param statements statements of block
1670 /// @returns the block statement pointer
1671 template <typename... Statements>
1672 ast::BlockStatement* Block(Statements&&... statements) {
1673 return create<ast::BlockStatement>(
1674 ast::StatementList{std::forward<Statements>(statements)...});
1675 }
1676
1677 /// Creates a ast::ElseStatement with input condition and body
1678 /// @param condition the else condition expression
1679 /// @param body the else body
1680 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001681 template <typename CONDITION>
1682 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1683 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1684 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001685 }
1686
1687 /// Creates a ast::IfStatement with input condition, body, and optional
1688 /// variadic else statements
1689 /// @param condition the if statement condition expression
1690 /// @param body the if statement body
1691 /// @param elseStatements optional variadic else statements
1692 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001693 template <typename CONDITION, typename... ELSE_STATEMENTS>
1694 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001695 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001696 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001697 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001698 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001699 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001700 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001701 }
1702
1703 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001704 /// @param source the source information
1705 /// @param lhs the left hand side expression initializer
1706 /// @param rhs the right hand side expression initializer
1707 /// @returns the assignment statement pointer
1708 template <typename LhsExpressionInit, typename RhsExpressionInit>
1709 ast::AssignmentStatement* Assign(const Source& source,
1710 LhsExpressionInit&& lhs,
1711 RhsExpressionInit&& rhs) {
1712 return create<ast::AssignmentStatement>(
1713 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1714 Expr(std::forward<RhsExpressionInit>(rhs)));
1715 }
1716
1717 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001718 /// @param lhs the left hand side expression initializer
1719 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001720 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001721 template <typename LhsExpressionInit, typename RhsExpressionInit>
1722 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1723 RhsExpressionInit&& rhs) {
1724 return create<ast::AssignmentStatement>(
1725 Expr(std::forward<LhsExpressionInit>(lhs)),
1726 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001727 }
1728
1729 /// Creates a ast::LoopStatement with input body and optional continuing
1730 /// @param body the loop body
1731 /// @param continuing the optional continuing block
1732 /// @returns the loop statement pointer
1733 ast::LoopStatement* Loop(ast::BlockStatement* body,
1734 ast::BlockStatement* continuing = nullptr) {
1735 return create<ast::LoopStatement>(body, continuing);
1736 }
1737
1738 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001739 /// @param source the source information
1740 /// @param var the variable to wrap in a decl statement
1741 /// @returns the variable decl statement pointer
1742 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1743 return create<ast::VariableDeclStatement>(source, var);
1744 }
1745
1746 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001747 /// @param var the variable to wrap in a decl statement
1748 /// @returns the variable decl statement pointer
1749 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1750 return create<ast::VariableDeclStatement>(var);
1751 }
1752
Antonio Maioranocea744d2021-03-25 12:55:27 +00001753 /// Creates a ast::SwitchStatement with input expression and cases
1754 /// @param condition the condition expression initializer
1755 /// @param cases case statements
1756 /// @returns the switch statement pointer
1757 template <typename ExpressionInit, typename... Cases>
1758 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1759 return create<ast::SwitchStatement>(
1760 Expr(std::forward<ExpressionInit>(condition)),
1761 ast::CaseStatementList{std::forward<Cases>(cases)...});
1762 }
1763
1764 /// Creates a ast::CaseStatement with input list of selectors, and body
1765 /// @param selectors list of selectors
1766 /// @param body the case body
1767 /// @returns the case statement pointer
1768 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1769 ast::BlockStatement* body = nullptr) {
1770 return create<ast::CaseStatement>(std::move(selectors),
1771 body ? body : Block());
1772 }
1773
1774 /// Convenient overload that takes a single selector
1775 /// @param selector a single case selector
1776 /// @param body the case body
1777 /// @returns the case statement pointer
1778 ast::CaseStatement* Case(ast::IntLiteral* selector,
1779 ast::BlockStatement* body = nullptr) {
1780 return Case(ast::CaseSelectorList{selector}, body);
1781 }
1782
1783 /// Convenience function that creates a 'default' ast::CaseStatement
1784 /// @param body the case body
1785 /// @returns the case statement pointer
1786 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1787 return Case(ast::CaseSelectorList{}, body);
1788 }
1789
James Price68f558f2021-04-06 15:51:47 +00001790 /// Creates an ast::BuiltinDecoration
1791 /// @param source the source information
1792 /// @param builtin the builtin value
1793 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001794 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001795 return create<ast::BuiltinDecoration>(source, builtin);
1796 }
1797
1798 /// Creates an ast::BuiltinDecoration
1799 /// @param builtin the builtin value
1800 /// @returns the builtin decoration pointer
1801 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1802 return create<ast::BuiltinDecoration>(source_, builtin);
1803 }
1804
1805 /// Creates an ast::LocationDecoration
1806 /// @param source the source information
1807 /// @param location the location value
1808 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001809 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001810 return create<ast::LocationDecoration>(source, location);
1811 }
1812
1813 /// Creates an ast::LocationDecoration
1814 /// @param location the location value
1815 /// @returns the location decoration pointer
1816 ast::LocationDecoration* Location(uint32_t location) {
1817 return create<ast::LocationDecoration>(source_, location);
1818 }
1819
James Pricef2f3bfc2021-05-13 20:32:32 +00001820 /// Creates an ast::OverrideDecoration with a specific constant ID
1821 /// @param source the source information
1822 /// @param id the id value
1823 /// @returns the override decoration pointer
1824 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1825 return create<ast::OverrideDecoration>(source, id);
1826 }
1827
1828 /// Creates an ast::OverrideDecoration with a specific constant ID
1829 /// @param id the optional id value
1830 /// @returns the override decoration pointer
1831 ast::OverrideDecoration* Override(uint32_t id) {
1832 return Override(source_, id);
1833 }
1834
1835 /// Creates an ast::OverrideDecoration without a constant ID
1836 /// @param source the source information
1837 /// @returns the override decoration pointer
1838 ast::OverrideDecoration* Override(const Source& source) {
1839 return create<ast::OverrideDecoration>(source);
1840 }
1841
1842 /// Creates an ast::OverrideDecoration without a constant ID
1843 /// @returns the override decoration pointer
1844 ast::OverrideDecoration* Override() { return Override(source_); }
1845
James Price68f558f2021-04-06 15:51:47 +00001846 /// Creates an ast::StageDecoration
1847 /// @param source the source information
1848 /// @param stage the pipeline stage
1849 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001850 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001851 return create<ast::StageDecoration>(source, stage);
1852 }
1853
1854 /// Creates an ast::StageDecoration
1855 /// @param stage the pipeline stage
1856 /// @returns the stage decoration pointer
1857 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1858 return create<ast::StageDecoration>(source_, stage);
1859 }
1860
James Price70f80bb2021-05-19 13:40:08 +00001861 /// Creates an ast::WorkgroupDecoration
1862 /// @param x the x dimension expression
1863 /// @returns the workgroup decoration pointer
1864 template <typename EXPR_X>
1865 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1866 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1867 }
1868
1869 /// Creates an ast::WorkgroupDecoration
1870 /// @param x the x dimension expression
1871 /// @param y the y dimension expression
1872 /// @returns the workgroup decoration pointer
1873 template <typename EXPR_X, typename EXPR_Y>
1874 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1875 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1876 nullptr);
1877 }
1878
1879 /// Creates an ast::WorkgroupDecoration
1880 /// @param x the x dimension expression
1881 /// @param y the y dimension expression
1882 /// @param z the z dimension expression
1883 /// @returns the workgroup decoration pointer
1884 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1885 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1886 return create<ast::WorkgroupDecoration>(
1887 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1888 Expr(std::forward<EXPR_Z>(z)));
1889 }
1890
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001891 /// Sets the current builder source to `src`
1892 /// @param src the Source used for future create() calls
1893 void SetSource(const Source& src) {
1894 AssertNotMoved();
1895 source_ = src;
1896 }
1897
1898 /// Sets the current builder source to `loc`
1899 /// @param loc the Source used for future create() calls
1900 void SetSource(const Source::Location& loc) {
1901 AssertNotMoved();
1902 source_ = Source(loc);
1903 }
1904
Ben Clayton33352542021-01-29 16:43:41 +00001905 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001906 /// @note As the Resolver is run when the Program is built, this will only be
1907 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001908 /// @param expr the AST expression
1909 /// @return the resolved semantic type for the expression, or nullptr if the
1910 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001911 sem::Type* TypeOf(const ast::Expression* expr) const;
1912
Ben Claytonfe0910f2021-05-17 15:51:47 +00001913 /// Helper for returning the resolved semantic type of the variable `var`.
1914 /// @note As the Resolver is run when the Program is built, this will only be
1915 /// useful for the Resolver itself and tests that use their own Resolver.
1916 /// @param var the AST variable
1917 /// @return the resolved semantic type for the variable, or nullptr if the
1918 /// variable has no resolved type.
1919 sem::Type* TypeOf(const ast::Variable* var) const;
1920
Ben Claytonfbec46f2021-04-30 20:20:19 +00001921 /// Helper for returning the resolved semantic type of the AST type `type`.
1922 /// @note As the Resolver is run when the Program is built, this will only be
1923 /// useful for the Resolver itself and tests that use their own Resolver.
1924 /// @param expr the AST type
1925 /// @return the resolved semantic type for the type, or nullptr if the type
1926 /// has no resolved type.
1927 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001928
Ben Clayton169512e2021-04-17 05:52:11 +00001929 /// Wraps the ast::Literal in a statement. This is used by tests that
1930 /// construct a partial AST and require the Resolver to reach these
1931 /// nodes.
1932 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1933 /// @return the ast::Statement that wraps the ast::Statement
1934 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001935 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001936 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001937 /// nodes.
1938 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1939 /// @return the ast::Statement that wraps the ast::Expression
1940 ast::Statement* WrapInStatement(ast::Expression* expr);
1941 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001942 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001943 /// these nodes.
1944 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1945 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1946 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1947 /// Returns the statement argument. Used as a passthrough-overload by
1948 /// WrapInFunction().
1949 /// @param stmt the ast::Statement
1950 /// @return `stmt`
1951 ast::Statement* WrapInStatement(ast::Statement* stmt);
1952 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001953 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001954 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001955 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001956 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001957 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001958 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001959 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001960 }
1961 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001962 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001963 /// @returns the function
1964 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001965
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001966 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001967 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001968
1969 protected:
1970 /// Asserts that the builder has not been moved.
1971 void AssertNotMoved() const;
1972
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001973 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001974 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001975 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001976 ASTNodeAllocator ast_nodes_;
1977 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001978 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001979 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001980 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001981 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001982
1983 /// The source to use when creating AST nodes without providing a Source as
1984 /// the first argument.
1985 Source source_;
1986
Ben Clayton5f0ea112021-03-09 10:54:37 +00001987 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001988 /// program when built.
1989 bool resolve_on_build_ = true;
1990
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001991 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1992 bool moved_ = false;
1993};
1994
1995//! @cond Doxygen_Suppress
1996// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1997template <>
1998struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001999 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002000 return t->i32();
2001 }
2002};
2003template <>
2004struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002005 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002006 return t->u32();
2007 }
2008};
2009template <>
2010struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002011 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002012 return t->f32();
2013 }
2014};
2015template <>
2016struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002017 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002018 return t->bool_();
2019 }
2020};
2021template <>
2022struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002023 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002024 return t->void_();
2025 }
2026};
2027//! @endcond
2028
Ben Clayton917b14b2021-04-19 16:50:23 +00002029/// @param builder the ProgramBuilder
2030/// @returns the ProgramID of the ProgramBuilder
2031inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2032 return builder->ID();
2033}
2034
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002035} // namespace tint
2036
2037#endif // SRC_PROGRAM_BUILDER_H_