blob: da8aa40505a85dc061e7fe0b1182ed013097c1c6 [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000026#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/bool_literal.h"
28#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000029#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000030#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000031#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000032#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000035#include "src/ast/if_statement.h"
36#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000037#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/member_accessor_expression.h"
39#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000040#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000041#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000043#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/scalar_constructor_expression.h"
46#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000047#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000049#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000050#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000053#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000055#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000056#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000058#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000059#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000060#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000061#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000062#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000064#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000065#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000066#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000067#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000068#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000069#include "src/sem/f32_type.h"
70#include "src/sem/i32_type.h"
71#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000073#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/sampled_texture_type.h"
75#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000076#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000077#include "src/sem/u32_type.h"
78#include "src/sem/vector_type.h"
79#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000080#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000081
82namespace tint {
83
Ben Clayton401b96b2021-02-03 17:19:59 +000084// Forward declarations
85namespace ast {
86class VariableDeclStatement;
87} // namespace ast
88
Ben Claytona6b9a8e2021-01-26 16:57:10 +000089class CloneContext;
90
91/// ProgramBuilder is a mutable builder for a Program.
92/// To construct a Program, populate the builder and then `std::move` it to a
93/// Program.
94class ProgramBuilder {
95 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000096 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
97 using ASTNodeAllocator = BlockAllocator<ast::Node>;
98
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000099 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
100 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000101
102 /// `i32` is a type alias to `int`.
103 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
104 /// WGSL syntax.
105 /// Note: this is intentionally not aliased to uint32_t as we want integer
106 /// literals passed to the builder to match WGSL's integer literal types.
107 using i32 = decltype(1);
108 /// `u32` is a type alias to `unsigned int`.
109 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
110 /// WGSL syntax.
111 /// Note: this is intentionally not aliased to uint32_t as we want integer
112 /// literals passed to the builder to match WGSL's integer literal types.
113 using u32 = decltype(1u);
114 /// `f32` is a type alias to `float`
115 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
116 /// WGSL syntax.
117 using f32 = float;
118
119 /// Constructor
120 ProgramBuilder();
121
122 /// Move constructor
123 /// @param rhs the builder to move
124 ProgramBuilder(ProgramBuilder&& rhs);
125
126 /// Destructor
127 virtual ~ProgramBuilder();
128
129 /// Move assignment operator
130 /// @param rhs the builder to move
131 /// @return this builder
132 ProgramBuilder& operator=(ProgramBuilder&& rhs);
133
Ben Claytone43c8302021-01-29 11:59:32 +0000134 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
135 /// making a deep clone of the Program contents.
136 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
137 /// existing immutable program.
138 /// As the returned ProgramBuilder wraps `program`, `program` must not be
139 /// destructed or assigned while using the returned ProgramBuilder.
140 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
141 /// function. See crbug.com/tint/460.
142 /// @param program the immutable Program to wrap
143 /// @return the ProgramBuilder that wraps `program`
144 static ProgramBuilder Wrap(const Program* program);
145
Ben Claytone6995de2021-04-13 23:27:27 +0000146 /// @returns the unique identifier for this program
147 ProgramID ID() const { return id_; }
148
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000149 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000150 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000151 AssertNotMoved();
152 return types_;
153 }
154
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000156 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000157 AssertNotMoved();
158 return types_;
159 }
160
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000162 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000163 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000164 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000165 }
166
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000168 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000169 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000170 return ast_nodes_;
171 }
172
173 /// @returns a reference to the program's semantic nodes storage
174 SemNodeAllocator& SemNodes() {
175 AssertNotMoved();
176 return sem_nodes_;
177 }
178
179 /// @returns a reference to the program's semantic nodes storage
180 const SemNodeAllocator& SemNodes() const {
181 AssertNotMoved();
182 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000183 }
184
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000185 /// @returns a reference to the program's AST root Module
186 ast::Module& AST() {
187 AssertNotMoved();
188 return *ast_;
189 }
190
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000191 /// @returns a reference to the program's AST root Module
192 const ast::Module& AST() const {
193 AssertNotMoved();
194 return *ast_;
195 }
196
197 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000198 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000199 AssertNotMoved();
200 return sem_;
201 }
202
203 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000204 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000205 AssertNotMoved();
206 return sem_;
207 }
208
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000209 /// @returns a reference to the program's SymbolTable
210 SymbolTable& Symbols() {
211 AssertNotMoved();
212 return symbols_;
213 }
214
Ben Clayton708dc2d2021-01-29 11:22:40 +0000215 /// @returns a reference to the program's SymbolTable
216 const SymbolTable& Symbols() const {
217 AssertNotMoved();
218 return symbols_;
219 }
220
Ben Clayton844217f2021-01-27 18:49:05 +0000221 /// @returns a reference to the program's diagnostics
222 diag::List& Diagnostics() {
223 AssertNotMoved();
224 return diagnostics_;
225 }
226
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000227 /// @returns a reference to the program's diagnostics
228 const diag::List& Diagnostics() const {
229 AssertNotMoved();
230 return diagnostics_;
231 }
232
Ben Clayton5f0ea112021-03-09 10:54:37 +0000233 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000234 /// @param enable the new flag value (defaults to true)
235 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
236
Ben Clayton5f0ea112021-03-09 10:54:37 +0000237 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000238 /// built.
239 bool ResolveOnBuild() const { return resolve_on_build_; }
240
Ben Clayton844217f2021-01-27 18:49:05 +0000241 /// @returns true if the program has no error diagnostics and is not missing
242 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000243 bool IsValid() const;
244
Ben Clayton708dc2d2021-01-29 11:22:40 +0000245 /// Writes a representation of the node to the output stream
246 /// @note unlike str(), to_str() does not automatically demangle the string.
247 /// @param node the AST node
248 /// @param out the stream to write to
249 /// @param indent number of spaces to indent the node when writing
250 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
251 node->to_str(Sem(), out, indent);
252 }
253
254 /// Returns a demangled, string representation of `node`.
255 /// @param node the AST node
256 /// @returns a string representation of the node
257 std::string str(const ast::Node* node) const;
258
Ben Clayton7fdfff12021-01-29 15:17:30 +0000259 /// Creates a new ast::Node owned by the ProgramBuilder. When the
260 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000261 /// @param source the Source of the node
262 /// @param args the arguments to pass to the type constructor
263 /// @returns the node pointer
264 template <typename T, typename... ARGS>
265 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
266 ARGS&&... args) {
267 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000268 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000269 }
270
Ben Clayton7fdfff12021-01-29 15:17:30 +0000271 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
272 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000273 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000274 /// When the ProgramBuilder is destructed, the ast::Node will also be
275 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000276 /// @returns the node pointer
277 template <typename T>
278 traits::EnableIfIsType<T, ast::Node>* create() {
279 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000280 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000281 }
282
Ben Clayton7fdfff12021-01-29 15:17:30 +0000283 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
284 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000285 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000286 /// When the ProgramBuilder is destructed, the ast::Node will also be
287 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000288 /// @param arg0 the first arguments to pass to the type constructor
289 /// @param args the remaining arguments to pass to the type constructor
290 /// @returns the node pointer
291 template <typename T, typename ARG0, typename... ARGS>
292 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
293 traits::IsTypeOrDerived<T, ast::Node>::value &&
294 !traits::IsTypeOrDerived<ARG0, Source>::value,
295 T>*
296 create(ARG0&& arg0, ARGS&&... args) {
297 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000298 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000299 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000300 }
301
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000302 /// Creates a new sem::Node owned by the ProgramBuilder.
303 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000304 /// destructed.
305 /// @param args the arguments to pass to the type constructor
306 /// @returns the node pointer
307 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000308 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
309 !traits::IsTypeOrDerived<T, sem::Type>::value,
310 T>*
311 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000312 AssertNotMoved();
313 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
314 }
315
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000316 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000317 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
318 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000319 /// Types are unique (de-aliased), and so calling create() for the same `T`
320 /// and arguments will return the same pointer.
321 /// @warning Use this method to acquire a type only if all of its type
322 /// information is provided in the constructor arguments `args`.<br>
323 /// If the type requires additional configuration after construction that
324 /// affect its fundamental type, build the type with `std::make_unique`, make
325 /// any necessary alterations and then call unique_type() instead.
326 /// @param args the arguments to pass to the type constructor
327 /// @returns the de-aliased type pointer
328 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000329 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
330 static_assert(std::is_base_of<sem::Type, T>::value,
331 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000332 AssertNotMoved();
333 return types_.Get<T>(std::forward<ARGS>(args)...);
334 }
335
336 /// Marks this builder as moved, preventing any further use of the builder.
337 void MarkAsMoved();
338
339 //////////////////////////////////////////////////////////////////////////////
340 // TypesBuilder
341 //////////////////////////////////////////////////////////////////////////////
342
343 /// TypesBuilder holds basic `tint` types and methods for constructing
344 /// complex types.
345 class TypesBuilder {
346 public:
347 /// Constructor
348 /// @param builder the program builder
349 explicit TypesBuilder(ProgramBuilder* builder);
350
351 /// @return the tint AST type for the C type `T`.
352 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000353 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000354 return CToAST<T>::get(this);
355 }
356
357 /// @returns a boolean type
Ben Claytonc03d3bd2021-05-20 14:22:28 +0000358 typ::Bool bool_() const { return {builder->create<ast::Bool>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000359
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000360 /// @param source the Source of the node
361 /// @returns a boolean type
362 typ::Bool bool_(const Source& source) const {
Ben Claytonc03d3bd2021-05-20 14:22:28 +0000363 return {builder->create<ast::Bool>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000364 }
365
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000366 /// @returns a f32 type
Ben Clayton512d60c2021-05-19 19:51:22 +0000367 typ::F32 f32() const { return {builder->create<ast::F32>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000369 /// @param source the Source of the node
370 /// @returns a f32 type
371 typ::F32 f32(const Source& source) const {
Ben Clayton512d60c2021-05-19 19:51:22 +0000372 return {builder->create<ast::F32>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000373 }
374
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000375 /// @returns a i32 type
Ben Claytoncf421bb2021-05-19 17:47:11 +0000376 typ::I32 i32() const { return {builder->create<ast::I32>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000377
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000378 /// @param source the Source of the node
379 /// @returns a i32 type
380 typ::I32 i32(const Source& source) const {
Ben Claytoncf421bb2021-05-19 17:47:11 +0000381 return {builder->create<ast::I32>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000382 }
383
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000384 /// @returns a u32 type
Ben Clayton58dec172021-05-19 17:47:11 +0000385 typ::U32 u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000386
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000387 /// @param source the Source of the node
388 /// @returns a u32 type
389 typ::U32 u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000390 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000391 }
392
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000393 /// @returns a void type
Ben Claytoncc98b4f2021-05-20 14:31:48 +0000394 typ::Void void_() const { return {builder->create<ast::Void>()}; }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000395
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000396 /// @param source the Source of the node
397 /// @returns a void type
398 typ::Void void_(const Source& source) const {
Ben Claytoncc98b4f2021-05-20 14:31:48 +0000399 return {builder->create<ast::Void>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000400 }
401
Antonio Maiorano25436862021-04-13 13:32:33 +0000402 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000403 /// @param n vector width in elements
404 /// @return the tint AST type for a `n`-element vector of `type`.
405 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000406 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000407 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
408 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000409 }
410
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000411 /// @param source the Source of the node
412 /// @param type vector subtype
413 /// @param n vector width in elements
414 /// @return the tint AST type for a `n`-element vector of `type`.
415 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000416 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000417 return {
418 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
419 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 }
421
Antonio Maiorano25436862021-04-13 13:32:33 +0000422 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000423 /// @return the tint AST type for a 2-element vector of `type`.
424 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
425
426 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000427 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000428 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000429
430 /// @param type vector subtype
431 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000432 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
433
434 /// @param n vector width in elements
435 /// @return the tint AST type for a `n`-element vector of `type`.
436 template <typename T>
437 typ::Vector vec(uint32_t n) const {
438 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000439 }
440
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000441 /// @return the tint AST type for a 2-element vector of the C type `T`.
442 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000443 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000444 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000445 }
446
447 /// @return the tint AST type for a 3-element vector of the C type `T`.
448 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000449 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000450 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000451 }
452
453 /// @return the tint AST type for a 4-element vector of the C type `T`.
454 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000455 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000456 return vec4(Of<T>());
457 }
458
459 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000460 /// @param columns number of columns for the matrix
461 /// @param rows number of rows for the matrix
462 /// @return the tint AST type for a matrix of `type`
463 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000464 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000465 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
466 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000467 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000468 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000469 }
470
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000471 /// @param source the Source of the node
472 /// @param type matrix subtype
473 /// @param columns number of columns for the matrix
474 /// @param rows number of rows for the matrix
475 /// @return the tint AST type for a matrix of `type`
476 typ::Matrix mat(const Source& source,
477 typ::Type type,
478 uint32_t columns,
479 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000480 return {type.ast
481 ? builder->create<ast::Matrix>(source, type, rows, columns)
482 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000483 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000484 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000485 }
486
Ben Claytone204f272021-04-22 14:40:23 +0000487 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000488 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000489 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000490
491 /// @param type matrix subtype
492 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000493 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000494
495 /// @param type matrix subtype
496 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000497 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000498
499 /// @param type matrix subtype
500 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000501 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000502
503 /// @param type matrix subtype
504 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000505 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000506
507 /// @param type matrix subtype
508 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000509 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000510
511 /// @param type matrix subtype
512 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000513 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000514
515 /// @param type matrix subtype
516 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000517 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000518
519 /// @param type matrix subtype
520 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000521 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000522
Ben Claytone204f272021-04-22 14:40:23 +0000523 /// @param columns number of columns for the matrix
524 /// @param rows number of rows for the matrix
525 /// @return the tint AST type for a matrix of `type`
526 template <typename T>
527 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
528 return mat(Of<T>(), columns, rows);
529 }
530
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000531 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
532 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000533 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000534 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000535 }
536
537 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
538 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000539 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000540 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000541 }
542
543 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
544 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000545 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000546 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000547 }
548
549 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
550 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000551 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000552 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000553 }
554
555 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
556 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000557 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000558 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559 }
560
561 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
562 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000563 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000564 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000565 }
566
567 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
568 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000569 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000570 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 }
572
573 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
574 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000575 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000576 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 }
578
579 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
580 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000581 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000582 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000583 }
584
585 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000586 /// @param n the array size. 0 represents a runtime-array
587 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000588 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000589 ast::Array* array(typ::Type subtype,
590 uint32_t n = 0,
591 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000592 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000593 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000594 }
595
596 /// @param source the Source of the node
597 /// @param subtype the array element type
598 /// @param n the array size. 0 represents a runtime-array
599 /// @param decos the optional decorations for the array
600 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000601 ast::Array* array(const Source& source,
602 typ::Type subtype,
603 uint32_t n = 0,
604 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000605 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000606 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000607 }
608
Ben Claytonbab31972021-02-08 21:16:21 +0000609 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000610 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000611 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000612 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000613 ast::Array* array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000614 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000615 ast::DecorationList decos;
616 if (stride) {
617 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
618 }
619 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000620 }
621
Ben Clayton0f88b312021-05-04 17:36:31 +0000622 /// @param source the Source of the node
623 /// @param subtype the array element type
624 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000625 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000626 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000627 ast::Array* array(const Source& source,
628 typ::Type subtype,
629 uint32_t n,
630 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000631 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000632 ast::DecorationList decos;
633 if (stride) {
634 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
635 }
636 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000637 }
638
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000639 /// @return the tint AST type for an array of size `N` of type `T`
640 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000641 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000642 return array(Of<T>(), N);
643 }
644
Ben Claytonbab31972021-02-08 21:16:21 +0000645 /// @param stride the array stride
646 /// @return the tint AST type for an array of size `N` of type `T`
647 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000648 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000649 return array(Of<T>(), N, stride);
650 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000651
Ben Clayton0f88b312021-05-04 17:36:31 +0000652 /// Creates a type name
653 /// @param name the name
654 /// @returns the type name
655 template <typename NAME>
656 ast::TypeName* type_name(NAME&& name) const {
657 return builder->create<ast::TypeName>(
658 builder->Sym(std::forward<NAME>(name)));
659 }
660
661 /// Creates a type name
662 /// @param source the Source of the node
663 /// @param name the name
664 /// @returns the type name
665 template <typename NAME>
666 ast::TypeName* type_name(const Source& source, NAME&& name) const {
667 return builder->create<ast::TypeName>(
668 source, builder->Sym(std::forward<NAME>(name)));
669 }
670
Ben Clayton42d1e092021-02-02 14:29:15 +0000671 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000672 /// @param name the alias name
673 /// @param type the alias type
674 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000675 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000676 ast::Alias* alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000677 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000678 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000679 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000680 }
681
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000682 /// Creates an alias type
683 /// @param source the Source of the node
684 /// @param name the alias name
685 /// @param type the alias type
686 /// @returns the alias pointer
687 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000688 ast::Alias* alias(const Source& source, NAME&& name, typ::Type type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000689 type = MaybeCreateTypename(type);
690 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000691 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000692 }
693
Ben Clayton4db10dd2021-04-16 08:47:14 +0000694 /// Creates an access control qualifier type
695 /// @param access the access control
696 /// @param type the inner type
697 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000698 ast::AccessControl* access(ast::AccessControl::Access access,
699 const ast::Type* type) const {
700 type = MaybeCreateTypename(type).ast;
701 return type ? builder->create<ast::AccessControl>(access, type) : nullptr;
Ben Clayton4db10dd2021-04-16 08:47:14 +0000702 }
703
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000704 /// Creates an access control qualifier type
705 /// @param source the Source of the node
706 /// @param access the access control
707 /// @param type the inner type
708 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000709 ast::AccessControl* access(const Source& source,
710 ast::AccessControl::Access access,
711 const ast::Type* type) const {
712 type = MaybeCreateTypename(type).ast;
713 return type ? builder->create<ast::AccessControl>(source, access, type)
714 : nullptr;
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000715 }
716
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000717 /// @param type the type of the pointer
718 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000719 /// @return the pointer to `type` with the given ast::StorageClass
720 typ::Pointer pointer(typ::Type type,
721 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000722 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000723 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
724 : nullptr,
725 type.sem ? builder->create<sem::Pointer>(type, storage_class)
726 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000727 }
728
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000729 /// @param source the Source of the node
730 /// @param type the type of the pointer
731 /// @param storage_class the storage class of the pointer
732 /// @return the pointer to `type` with the given ast::StorageClass
733 typ::Pointer pointer(const Source& source,
734 typ::Type type,
735 ast::StorageClass storage_class) const {
736 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000737 return {type.ast
738 ? builder->create<ast::Pointer>(source, type, storage_class)
739 : nullptr,
740 type.sem ? builder->create<sem::Pointer>(type, storage_class)
741 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000742 }
743
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000744 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000745 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000746 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000747 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000748 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000749 }
750
Ben Claytone204f272021-04-22 14:40:23 +0000751 /// @param kind the kind of sampler
752 /// @returns the sampler
753 typ::Sampler sampler(ast::SamplerKind kind) const {
754 return {builder->create<ast::Sampler>(kind),
755 builder->create<sem::Sampler>(kind)};
756 }
757
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000758 /// @param source the Source of the node
759 /// @param kind the kind of sampler
760 /// @returns the sampler
761 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
762 return {builder->create<ast::Sampler>(source, kind),
763 builder->create<sem::Sampler>(kind)};
764 }
765
Ben Claytone204f272021-04-22 14:40:23 +0000766 /// @param dims the dimensionality of the texture
767 /// @returns the depth texture
768 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000769 return {builder->create<ast::DepthTexture>(dims)};
Ben Claytone204f272021-04-22 14:40:23 +0000770 }
771
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000772 /// @param source the Source of the node
773 /// @param dims the dimensionality of the texture
774 /// @returns the depth texture
775 typ::DepthTexture depth_texture(const Source& source,
776 ast::TextureDimension dims) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000777 return {builder->create<ast::DepthTexture>(source, dims)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000778 }
779
Ben Claytone204f272021-04-22 14:40:23 +0000780 /// @param dims the dimensionality of the texture
781 /// @param subtype the texture subtype.
782 /// @returns the sampled texture
783 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
784 typ::Type subtype) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000785 return {builder->create<ast::SampledTexture>(dims, subtype)};
Ben Claytone204f272021-04-22 14:40:23 +0000786 }
787
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000788 /// @param source the Source of the node
789 /// @param dims the dimensionality of the texture
790 /// @param subtype the texture subtype.
791 /// @returns the sampled texture
792 typ::SampledTexture sampled_texture(const Source& source,
793 ast::TextureDimension dims,
794 typ::Type subtype) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000795 return {builder->create<ast::SampledTexture>(source, dims, subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000796 }
797
Ben Claytone204f272021-04-22 14:40:23 +0000798 /// @param dims the dimensionality of the texture
799 /// @param subtype the texture subtype.
800 /// @returns the multisampled texture
801 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
802 typ::Type subtype) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000803 return {builder->create<ast::MultisampledTexture>(dims, subtype)};
Ben Claytone204f272021-04-22 14:40:23 +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 subtype the texture subtype.
809 /// @returns the multisampled texture
810 typ::MultisampledTexture multisampled_texture(const Source& source,
811 ast::TextureDimension dims,
812 typ::Type subtype) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000813 return {builder->create<ast::MultisampledTexture>(source, dims, subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000814 }
815
Ben Claytone204f272021-04-22 14:40:23 +0000816 /// @param dims the dimensionality of the texture
817 /// @param format the image format of the texture
818 /// @returns the storage texture
819 typ::StorageTexture storage_texture(ast::TextureDimension dims,
820 ast::ImageFormat format) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000821 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
822 return {builder->create<ast::StorageTexture>(dims, format, subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000823 }
824
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000825 /// @param source the Source of the node
826 /// @param dims the dimensionality of the texture
827 /// @param format the image format of the texture
828 /// @returns the storage texture
829 typ::StorageTexture storage_texture(const Source& source,
830 ast::TextureDimension dims,
831 ast::ImageFormat format) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000832 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
833 return {
834 builder->create<ast::StorageTexture>(source, dims, format, subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000835 }
836
Brandon Jones7b257692021-05-17 17:40:17 +0000837 /// @returns the external texture
838 typ::ExternalTexture external_texture() const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000839 return {builder->create<ast::ExternalTexture>()};
Brandon Jones7b257692021-05-17 17:40:17 +0000840 }
841
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000842 /// @param source the Source of the node
843 /// @returns the external texture
844 typ::ExternalTexture external_texture(const Source& source) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000845 return {builder->create<ast::ExternalTexture>(source)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000846 }
847
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000848 /// If ty is a ast::Struct or ast::Alias, the returned type is an
849 /// ast::TypeName of the given type's name, otherwise type is returned.
850 /// @param type the type
851 /// @return either type or a pointer to a new ast::TypeName
852 typ::Type MaybeCreateTypename(typ::Type type) const;
853
Ben Claytonf5f311e2021-04-28 14:31:23 +0000854 /// The ProgramBuilder
855 ProgramBuilder* const builder;
856
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000857 private:
858 /// CToAST<T> is specialized for various `T` types and each specialization
859 /// contains a single static `get()` method for obtaining the corresponding
860 /// AST type for the C type `T`.
861 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000862 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000863 template <typename T>
864 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000865 };
866
867 //////////////////////////////////////////////////////////////////////////////
868 // AST helper methods
869 //////////////////////////////////////////////////////////////////////////////
870
James Price65ae64d2021-04-29 12:59:14 +0000871 /// @return a new unnamed symbol
872 Symbol Sym() { return Symbols().New(); }
873
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000874 /// @param name the symbol string
875 /// @return a Symbol with the given name
876 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
877
878 /// @param sym the symbol
879 /// @return `sym`
880 Symbol Sym(Symbol sym) { return sym; }
881
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000882 /// @param expr the expression
883 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000884 template <typename T>
885 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
886 return expr;
887 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000888
Ben Claytonb8ea5912021-04-19 16:52:42 +0000889 /// Passthrough for nullptr
890 /// @return nullptr
891 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
892
Ben Claytonfe0910f2021-05-17 15:51:47 +0000893 /// @param source the source information
894 /// @param symbol the identifier symbol
895 /// @return an ast::IdentifierExpression with the given symbol
896 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
897 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000898 }
899
Ben Clayton46d78d72021-02-10 21:34:26 +0000900 /// @param symbol the identifier symbol
901 /// @return an ast::IdentifierExpression with the given symbol
902 ast::IdentifierExpression* Expr(Symbol symbol) {
903 return create<ast::IdentifierExpression>(symbol);
904 }
905
Ben Claytonfe0910f2021-05-17 15:51:47 +0000906 /// @param source the source information
907 /// @param variable the AST variable
908 /// @return an ast::IdentifierExpression with the variable's symbol
909 ast::IdentifierExpression* Expr(const Source& source,
910 ast::Variable* variable) {
911 return create<ast::IdentifierExpression>(source, variable->symbol());
912 }
913
Ben Claytonb8ea5912021-04-19 16:52:42 +0000914 /// @param variable the AST variable
915 /// @return an ast::IdentifierExpression with the variable's symbol
916 ast::IdentifierExpression* Expr(ast::Variable* variable) {
917 return create<ast::IdentifierExpression>(variable->symbol());
918 }
919
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000920 /// @param source the source information
921 /// @param name the identifier name
922 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000923 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
924 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
925 }
926
927 /// @param name the identifier name
928 /// @return an ast::IdentifierExpression with the given name
929 ast::IdentifierExpression* Expr(const char* name) {
930 return create<ast::IdentifierExpression>(Symbols().Register(name));
931 }
932
933 /// @param source the source information
934 /// @param name the identifier name
935 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000936 ast::IdentifierExpression* Expr(const Source& source,
937 const std::string& name) {
938 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
939 }
940
941 /// @param name the identifier name
942 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000943 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000944 return create<ast::IdentifierExpression>(Symbols().Register(name));
945 }
946
Ben Claytonfe0910f2021-05-17 15:51:47 +0000947 /// @param source the source information
948 /// @param value the boolean value
949 /// @return a Scalar constructor for the given value
950 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
951 return create<ast::ScalarConstructorExpression>(source, Literal(value));
952 }
953
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000954 /// @param value the boolean value
955 /// @return a Scalar constructor for the given value
956 ast::ScalarConstructorExpression* Expr(bool value) {
957 return create<ast::ScalarConstructorExpression>(Literal(value));
958 }
959
Ben Claytonfe0910f2021-05-17 15:51:47 +0000960 /// @param source the source information
961 /// @param value the float value
962 /// @return a Scalar constructor for the given value
963 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
964 return create<ast::ScalarConstructorExpression>(source, Literal(value));
965 }
966
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000967 /// @param value the float value
968 /// @return a Scalar constructor for the given value
969 ast::ScalarConstructorExpression* Expr(f32 value) {
970 return create<ast::ScalarConstructorExpression>(Literal(value));
971 }
972
Ben Claytonfe0910f2021-05-17 15:51:47 +0000973 /// @param source the source information
974 /// @param value the integer value
975 /// @return a Scalar constructor for the given value
976 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
977 return create<ast::ScalarConstructorExpression>(source, Literal(value));
978 }
979
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000980 /// @param value the integer value
981 /// @return a Scalar constructor for the given value
982 ast::ScalarConstructorExpression* Expr(i32 value) {
983 return create<ast::ScalarConstructorExpression>(Literal(value));
984 }
985
Ben Claytonfe0910f2021-05-17 15:51:47 +0000986 /// @param source the source information
987 /// @param value the unsigned int value
988 /// @return a Scalar constructor for the given value
989 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
990 return create<ast::ScalarConstructorExpression>(source, Literal(value));
991 }
992
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000993 /// @param value the unsigned int value
994 /// @return a Scalar constructor for the given value
995 ast::ScalarConstructorExpression* Expr(u32 value) {
996 return create<ast::ScalarConstructorExpression>(Literal(value));
997 }
998
999 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1000 /// `list`.
1001 /// @param list the list to append too
1002 /// @param arg the arg to create
1003 template <typename ARG>
1004 void Append(ast::ExpressionList& list, ARG&& arg) {
1005 list.emplace_back(Expr(std::forward<ARG>(arg)));
1006 }
1007
1008 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1009 /// then appends them to `list`.
1010 /// @param list the list to append too
1011 /// @param arg0 the first argument
1012 /// @param args the rest of the arguments
1013 template <typename ARG0, typename... ARGS>
1014 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1015 Append(list, std::forward<ARG0>(arg0));
1016 Append(list, std::forward<ARGS>(args)...);
1017 }
1018
1019 /// @return an empty list of expressions
1020 ast::ExpressionList ExprList() { return {}; }
1021
1022 /// @param args the list of expressions
1023 /// @return the list of expressions converted to `ast::Expression`s using
1024 /// `Expr()`,
1025 template <typename... ARGS>
1026 ast::ExpressionList ExprList(ARGS&&... args) {
1027 ast::ExpressionList list;
1028 list.reserve(sizeof...(args));
1029 Append(list, std::forward<ARGS>(args)...);
1030 return list;
1031 }
1032
1033 /// @param list the list of expressions
1034 /// @return `list`
1035 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1036
1037 /// @param val the boolan value
1038 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001039 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001040
1041 /// @param val the float value
1042 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001043 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001044
1045 /// @param val the unsigned int value
1046 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001047 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001048
1049 /// @param val the integer value
1050 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001051 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001052
1053 /// @param args the arguments for the type constructor
1054 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1055 /// of `args` converted to `ast::Expression`s using `Expr()`
1056 template <typename T, typename... ARGS>
1057 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001058 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001059 }
1060
1061 /// @param type the type to construct
1062 /// @param args the arguments for the constructor
1063 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1064 /// values `args`.
1065 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001066 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001067 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001068 return create<ast::TypeConstructorExpression>(
1069 type, ExprList(std::forward<ARGS>(args)...));
1070 }
1071
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001072 /// Creates a constructor expression that constructs an object of
1073 /// `type` filled with `elem_value`. For example,
1074 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1075 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1076 /// @param type the type to construct
1077 /// @param elem_value the initial or element value (for vec and mat) to
1078 /// construct with
1079 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001080 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001081 int elem_value = 0);
1082
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001083 /// @param args the arguments for the vector constructor
1084 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1085 /// `T`, constructed with the values `args`.
1086 template <typename T, typename... ARGS>
1087 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001088 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 }
1090
1091 /// @param args the arguments for the vector constructor
1092 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1093 /// `T`, constructed with the values `args`.
1094 template <typename T, typename... ARGS>
1095 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001096 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001097 }
1098
1099 /// @param args the arguments for the vector constructor
1100 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1101 /// `T`, constructed with the values `args`.
1102 template <typename T, typename... ARGS>
1103 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001104 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001105 }
1106
1107 /// @param args the arguments for the matrix constructor
1108 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1109 /// `T`, constructed with the values `args`.
1110 template <typename T, typename... ARGS>
1111 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001112 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001113 }
1114
1115 /// @param args the arguments for the matrix constructor
1116 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1117 /// `T`, constructed with the values `args`.
1118 template <typename T, typename... ARGS>
1119 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001120 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 }
1122
1123 /// @param args the arguments for the matrix constructor
1124 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1125 /// `T`, constructed with the values `args`.
1126 template <typename T, typename... ARGS>
1127 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001128 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001129 }
1130
1131 /// @param args the arguments for the matrix constructor
1132 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1133 /// `T`, constructed with the values `args`.
1134 template <typename T, typename... ARGS>
1135 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001136 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 }
1138
1139 /// @param args the arguments for the matrix constructor
1140 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1141 /// `T`, constructed with the values `args`.
1142 template <typename T, typename... ARGS>
1143 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001144 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001145 }
1146
1147 /// @param args the arguments for the matrix constructor
1148 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1149 /// `T`, constructed with the values `args`.
1150 template <typename T, typename... ARGS>
1151 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001152 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001153 }
1154
1155 /// @param args the arguments for the matrix constructor
1156 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1157 /// `T`, constructed with the values `args`.
1158 template <typename T, typename... ARGS>
1159 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001160 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001161 }
1162
1163 /// @param args the arguments for the matrix constructor
1164 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1165 /// `T`, constructed with the values `args`.
1166 template <typename T, typename... ARGS>
1167 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001168 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001169 }
1170
1171 /// @param args the arguments for the matrix constructor
1172 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1173 /// `T`, constructed with the values `args`.
1174 template <typename T, typename... ARGS>
1175 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001176 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001177 }
1178
1179 /// @param args the arguments for the array constructor
1180 /// @return an `ast::TypeConstructorExpression` of an array with element type
1181 /// `T`, constructed with the values `args`.
1182 template <typename T, int N = 0, typename... ARGS>
1183 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001184 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001185 }
1186
1187 /// @param subtype the array element type
1188 /// @param n the array size. 0 represents a runtime-array.
1189 /// @param args the arguments for the array constructor
1190 /// @return an `ast::TypeConstructorExpression` of an array with element type
1191 /// `subtype`, constructed with the values `args`.
1192 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001193 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001194 uint32_t n,
1195 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001196 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001197 }
1198
1199 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001200 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001201 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001202 /// @param constructor constructor expression
1203 /// @param decorations variable decorations
1204 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001205 template <typename NAME>
1206 ast::Variable* Var(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>(Sym(std::forward<NAME>(name)), storage, type,
1213 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001214 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215
1216 /// @param source the variable source
1217 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001219 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001220 /// @param constructor constructor expression
1221 /// @param decorations variable decorations
1222 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001223 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001224 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001225 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001226 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001227 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001228 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001229 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001230 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001231 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001232 type, false, constructor, decorations);
1233 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001234
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001235 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001236 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001237 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001239 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001240 template <typename NAME>
1241 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001242 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001243 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001244 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001245 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001246 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001247 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001248 constructor, decorations);
1249 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250
1251 /// @param source the variable source
1252 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001254 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001255 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001256 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001257 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001258 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001259 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001260 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001261 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001262 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001263 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001264 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001265 ast::StorageClass::kNone, type, true,
1266 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001267 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001268
James Pricedaf1f1c2021-04-08 22:15:48 +00001269 /// @param name the parameter name
1270 /// @param type the parameter type
1271 /// @param decorations optional parameter decorations
1272 /// @returns a constant `ast::Variable` with the given name and type
1273 template <typename NAME>
1274 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001275 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001276 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001277 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001278 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1279 ast::StorageClass::kNone, type, true, nullptr,
1280 decorations);
1281 }
1282
1283 /// @param source the parameter source
1284 /// @param name the parameter name
1285 /// @param type the parameter type
1286 /// @param decorations optional parameter decorations
1287 /// @returns a constant `ast::Variable` with the given name and type
1288 template <typename NAME>
1289 ast::Variable* Param(const Source& source,
1290 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001291 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001292 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001293 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001294 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1295 ast::StorageClass::kNone, type, true, nullptr,
1296 decorations);
1297 }
1298
Ben Clayton42708342021-04-21 17:55:12 +00001299 /// @param name the variable name
1300 /// @param type the variable type
1301 /// @param storage the variable storage class
1302 /// @param constructor constructor expression
1303 /// @param decorations variable decorations
1304 /// @returns a new `ast::Variable`, which is automatically registered as a
1305 /// global variable with the ast::Module.
1306 template <typename NAME>
1307 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001308 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001309 ast::StorageClass storage,
1310 ast::Expression* constructor = nullptr,
1311 ast::DecorationList decorations = {}) {
1312 auto* var =
1313 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1314 AST().AddGlobalVariable(var);
1315 return var;
1316 }
1317
1318 /// @param source the variable source
1319 /// @param name the variable name
1320 /// @param type the variable type
1321 /// @param storage the variable storage class
1322 /// @param constructor constructor expression
1323 /// @param decorations variable decorations
1324 /// @returns a new `ast::Variable`, which is automatically registered as a
1325 /// global variable with the ast::Module.
1326 template <typename NAME>
1327 ast::Variable* Global(const Source& source,
1328 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001329 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001330 ast::StorageClass storage,
1331 ast::Expression* constructor = nullptr,
1332 ast::DecorationList decorations = {}) {
1333 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1334 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001335 AST().AddGlobalVariable(var);
1336 return var;
1337 }
1338
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001339 /// @param name the variable name
1340 /// @param type the variable type
1341 /// @param constructor constructor expression
1342 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001343 /// @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.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001346 template <typename NAME>
1347 ast::Variable* GlobalConst(NAME&& name,
1348 typ::Type type,
1349 ast::Expression* constructor,
1350 ast::DecorationList decorations = {}) {
1351 auto* var = Const(std::forward<NAME>(name), type, constructor,
1352 std::move(decorations));
1353 AST().AddGlobalVariable(var);
1354 return var;
1355 }
1356
1357 /// @param source the variable source
1358 /// @param name the variable name
1359 /// @param type the variable type
1360 /// @param constructor constructor expression
1361 /// @param decorations optional variable decorations
1362 /// @returns a const `ast::Variable` constructed by calling Var() with the
1363 /// arguments of `args`, which is automatically registered as a global
1364 /// variable with the ast::Module.
1365 template <typename NAME>
1366 ast::Variable* GlobalConst(const Source& source,
1367 NAME&& name,
1368 typ::Type type,
1369 ast::Expression* constructor,
1370 ast::DecorationList decorations = {}) {
1371 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1372 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001373 AST().AddGlobalVariable(var);
1374 return var;
1375 }
1376
Ben Claytonfe0910f2021-05-17 15:51:47 +00001377 /// @param source the source information
1378 /// @param expr the expression to take the address of
1379 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1380 template <typename EXPR>
1381 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1382 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1383 Expr(std::forward<EXPR>(expr)));
1384 }
1385
1386 /// @param expr the expression to take the address of
1387 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1388 template <typename EXPR>
1389 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1390 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1391 Expr(std::forward<EXPR>(expr)));
1392 }
1393
1394 /// @param source the source information
1395 /// @param expr the expression to perform an indirection on
1396 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1397 template <typename EXPR>
1398 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1399 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1400 Expr(std::forward<EXPR>(expr)));
1401 }
1402
1403 /// @param expr the expression to perform an indirection on
1404 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1405 template <typename EXPR>
1406 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1407 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1408 Expr(std::forward<EXPR>(expr)));
1409 }
1410
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001411 /// @param func the function name
1412 /// @param args the function call arguments
1413 /// @returns a `ast::CallExpression` to the function `func`, with the
1414 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1415 template <typename NAME, typename... ARGS>
1416 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1417 return create<ast::CallExpression>(Expr(func),
1418 ExprList(std::forward<ARGS>(args)...));
1419 }
1420
1421 /// @param lhs the left hand argument to the addition operation
1422 /// @param rhs the right hand argument to the addition operation
1423 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1424 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001425 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001426 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1427 Expr(std::forward<LHS>(lhs)),
1428 Expr(std::forward<RHS>(rhs)));
1429 }
1430
1431 /// @param lhs the left hand argument to the subtraction operation
1432 /// @param rhs the right hand argument to the subtraction operation
1433 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1434 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001435 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001436 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1437 Expr(std::forward<LHS>(lhs)),
1438 Expr(std::forward<RHS>(rhs)));
1439 }
1440
1441 /// @param lhs the left hand argument to the multiplication operation
1442 /// @param rhs the right hand argument to the multiplication operation
1443 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1444 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001445 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001446 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1447 Expr(std::forward<LHS>(lhs)),
1448 Expr(std::forward<RHS>(rhs)));
1449 }
1450
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001451 /// @param source the source information
1452 /// @param lhs the left hand argument to the multiplication operation
1453 /// @param rhs the right hand argument to the multiplication operation
1454 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1455 template <typename LHS, typename RHS>
1456 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1457 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1458 Expr(std::forward<LHS>(lhs)),
1459 Expr(std::forward<RHS>(rhs)));
1460 }
1461
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001462 /// @param lhs the left hand argument to the division operation
1463 /// @param rhs the right hand argument to the division operation
1464 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1465 template <typename LHS, typename RHS>
1466 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1467 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1468 Expr(std::forward<LHS>(lhs)),
1469 Expr(std::forward<RHS>(rhs)));
1470 }
1471
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001472 /// @param arr the array argument for the array accessor expression
1473 /// @param idx the index argument for the array accessor expression
1474 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1475 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001476 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001477 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1478 Expr(std::forward<IDX>(idx)));
1479 }
1480
1481 /// @param obj the object for the member accessor expression
1482 /// @param idx the index argument for the array accessor expression
1483 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1484 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001485 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001486 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1487 Expr(std::forward<IDX>(idx)));
1488 }
1489
Ben Clayton42d1e092021-02-02 14:29:15 +00001490 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001491 /// @param val the offset value
1492 /// @returns the offset decoration pointer
1493 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1494 return create<ast::StructMemberOffsetDecoration>(source_, val);
1495 }
1496
Ben Claytond614dd52021-03-15 10:43:11 +00001497 /// Creates a ast::StructMemberSizeDecoration
1498 /// @param source the source information
1499 /// @param val the size value
1500 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001501 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1502 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001503 return create<ast::StructMemberSizeDecoration>(source, val);
1504 }
1505
1506 /// Creates a ast::StructMemberSizeDecoration
1507 /// @param val the size value
1508 /// @returns the size decoration pointer
1509 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1510 return create<ast::StructMemberSizeDecoration>(source_, val);
1511 }
1512
1513 /// Creates a ast::StructMemberAlignDecoration
1514 /// @param source the source information
1515 /// @param val the align value
1516 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001517 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1518 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001519 return create<ast::StructMemberAlignDecoration>(source, val);
1520 }
1521
1522 /// Creates a ast::StructMemberAlignDecoration
1523 /// @param val the align value
1524 /// @returns the align decoration pointer
1525 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1526 return create<ast::StructMemberAlignDecoration>(source_, val);
1527 }
1528
Ben Clayton42d1e092021-02-02 14:29:15 +00001529 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001530 /// @param source the source information
1531 /// @param name the function name
1532 /// @param params the function parameters
1533 /// @param type the function return type
1534 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001535 /// @param decorations the optional function decorations
1536 /// @param return_type_decorations the optional function return type
1537 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001538 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001539 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001540 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001541 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001542 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001543 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001544 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001545 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001546 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001547 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001548 auto* func =
1549 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1550 type, create<ast::BlockStatement>(body),
1551 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001552 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001553 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001554 }
1555
Ben Clayton42d1e092021-02-02 14:29:15 +00001556 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001557 /// @param name the function name
1558 /// @param params the function parameters
1559 /// @param type the function return type
1560 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001561 /// @param decorations the optional function decorations
1562 /// @param return_type_decorations the optional function return type
1563 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001564 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001565 template <typename NAME>
1566 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001567 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001568 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001569 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001570 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001571 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001572 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001573 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1574 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001575 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001576 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001577 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001578 }
1579
Ben Clayton49668bb2021-04-17 05:32:01 +00001580 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001581 /// @param source the source information
1582 /// @returns the return statement pointer
1583 ast::ReturnStatement* Return(const Source& source) {
1584 return create<ast::ReturnStatement>(source);
1585 }
1586
1587 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001588 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001589 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1590
1591 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001592 /// @param source the source information
1593 /// @param val the return value
1594 /// @returns the return statement pointer
1595 template <typename EXPR>
1596 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1597 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1598 }
1599
1600 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001601 /// @param val the return value
1602 /// @returns the return statement pointer
1603 template <typename EXPR>
1604 ast::ReturnStatement* Return(EXPR&& val) {
1605 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001606 }
1607
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001608 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001609 /// @param source the source information
1610 /// @param name the struct name
1611 /// @param members the struct members
1612 /// @param decorations the optional struct decorations
1613 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001614 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001615 ast::Struct* Structure(const Source& source,
1616 NAME&& name,
1617 ast::StructMemberList members,
1618 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001619 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001620 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001621 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001622 AST().AddConstructedType(type);
1623 return type;
1624 }
1625
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001626 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001627 /// @param name the struct name
1628 /// @param members the struct members
1629 /// @param decorations the optional struct decorations
1630 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001631 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001632 ast::Struct* Structure(NAME&& name,
1633 ast::StructMemberList members,
1634 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001635 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001636 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001637 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001638 AST().AddConstructedType(type);
1639 return type;
1640 }
1641
Ben Clayton42d1e092021-02-02 14:29:15 +00001642 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001643 /// @param source the source information
1644 /// @param name the struct member name
1645 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001646 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001647 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001648 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001649 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001650 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001651 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001652 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001653 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001654 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1655 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001656 }
1657
Ben Clayton42d1e092021-02-02 14:29:15 +00001658 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001659 /// @param name the struct member name
1660 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001661 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001662 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001663 template <typename NAME>
1664 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001665 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001666 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001667 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001668 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1669 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001670 }
1671
Ben Claytonbab31972021-02-08 21:16:21 +00001672 /// Creates a ast::StructMember with the given byte offset
1673 /// @param offset the offset to use in the StructMemberOffsetDecoration
1674 /// @param name the struct member name
1675 /// @param type the struct member type
1676 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001677 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001678 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001679 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001680 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001681 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001682 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001683 create<ast::StructMemberOffsetDecoration>(offset),
1684 });
1685 }
1686
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001687 /// Creates a ast::BlockStatement with input statements
1688 /// @param statements statements of block
1689 /// @returns the block statement pointer
1690 template <typename... Statements>
1691 ast::BlockStatement* Block(Statements&&... statements) {
1692 return create<ast::BlockStatement>(
1693 ast::StatementList{std::forward<Statements>(statements)...});
1694 }
1695
1696 /// Creates a ast::ElseStatement with input condition and body
1697 /// @param condition the else condition expression
1698 /// @param body the else body
1699 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001700 template <typename CONDITION>
1701 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1702 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1703 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001704 }
1705
1706 /// Creates a ast::IfStatement with input condition, body, and optional
1707 /// variadic else statements
1708 /// @param condition the if statement condition expression
1709 /// @param body the if statement body
1710 /// @param elseStatements optional variadic else statements
1711 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001712 template <typename CONDITION, typename... ELSE_STATEMENTS>
1713 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001714 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001715 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001716 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001717 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001718 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001719 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001720 }
1721
1722 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001723 /// @param source the source information
1724 /// @param lhs the left hand side expression initializer
1725 /// @param rhs the right hand side expression initializer
1726 /// @returns the assignment statement pointer
1727 template <typename LhsExpressionInit, typename RhsExpressionInit>
1728 ast::AssignmentStatement* Assign(const Source& source,
1729 LhsExpressionInit&& lhs,
1730 RhsExpressionInit&& rhs) {
1731 return create<ast::AssignmentStatement>(
1732 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1733 Expr(std::forward<RhsExpressionInit>(rhs)));
1734 }
1735
1736 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001737 /// @param lhs the left hand side expression initializer
1738 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001739 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001740 template <typename LhsExpressionInit, typename RhsExpressionInit>
1741 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1742 RhsExpressionInit&& rhs) {
1743 return create<ast::AssignmentStatement>(
1744 Expr(std::forward<LhsExpressionInit>(lhs)),
1745 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001746 }
1747
1748 /// Creates a ast::LoopStatement with input body and optional continuing
1749 /// @param body the loop body
1750 /// @param continuing the optional continuing block
1751 /// @returns the loop statement pointer
1752 ast::LoopStatement* Loop(ast::BlockStatement* body,
1753 ast::BlockStatement* continuing = nullptr) {
1754 return create<ast::LoopStatement>(body, continuing);
1755 }
1756
1757 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001758 /// @param source the source information
1759 /// @param var the variable to wrap in a decl statement
1760 /// @returns the variable decl statement pointer
1761 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1762 return create<ast::VariableDeclStatement>(source, var);
1763 }
1764
1765 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001766 /// @param var the variable to wrap in a decl statement
1767 /// @returns the variable decl statement pointer
1768 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1769 return create<ast::VariableDeclStatement>(var);
1770 }
1771
Antonio Maioranocea744d2021-03-25 12:55:27 +00001772 /// Creates a ast::SwitchStatement with input expression and cases
1773 /// @param condition the condition expression initializer
1774 /// @param cases case statements
1775 /// @returns the switch statement pointer
1776 template <typename ExpressionInit, typename... Cases>
1777 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1778 return create<ast::SwitchStatement>(
1779 Expr(std::forward<ExpressionInit>(condition)),
1780 ast::CaseStatementList{std::forward<Cases>(cases)...});
1781 }
1782
1783 /// Creates a ast::CaseStatement with input list of selectors, and body
1784 /// @param selectors list of selectors
1785 /// @param body the case body
1786 /// @returns the case statement pointer
1787 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1788 ast::BlockStatement* body = nullptr) {
1789 return create<ast::CaseStatement>(std::move(selectors),
1790 body ? body : Block());
1791 }
1792
1793 /// Convenient overload that takes a single selector
1794 /// @param selector a single case selector
1795 /// @param body the case body
1796 /// @returns the case statement pointer
1797 ast::CaseStatement* Case(ast::IntLiteral* selector,
1798 ast::BlockStatement* body = nullptr) {
1799 return Case(ast::CaseSelectorList{selector}, body);
1800 }
1801
1802 /// Convenience function that creates a 'default' ast::CaseStatement
1803 /// @param body the case body
1804 /// @returns the case statement pointer
1805 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1806 return Case(ast::CaseSelectorList{}, body);
1807 }
1808
James Price68f558f2021-04-06 15:51:47 +00001809 /// Creates an ast::BuiltinDecoration
1810 /// @param source the source information
1811 /// @param builtin the builtin value
1812 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001813 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001814 return create<ast::BuiltinDecoration>(source, builtin);
1815 }
1816
1817 /// Creates an ast::BuiltinDecoration
1818 /// @param builtin the builtin value
1819 /// @returns the builtin decoration pointer
1820 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1821 return create<ast::BuiltinDecoration>(source_, builtin);
1822 }
1823
1824 /// Creates an ast::LocationDecoration
1825 /// @param source the source information
1826 /// @param location the location value
1827 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001828 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001829 return create<ast::LocationDecoration>(source, location);
1830 }
1831
1832 /// Creates an ast::LocationDecoration
1833 /// @param location the location value
1834 /// @returns the location decoration pointer
1835 ast::LocationDecoration* Location(uint32_t location) {
1836 return create<ast::LocationDecoration>(source_, location);
1837 }
1838
James Pricef2f3bfc2021-05-13 20:32:32 +00001839 /// Creates an ast::OverrideDecoration with a specific constant ID
1840 /// @param source the source information
1841 /// @param id the id value
1842 /// @returns the override decoration pointer
1843 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1844 return create<ast::OverrideDecoration>(source, id);
1845 }
1846
1847 /// Creates an ast::OverrideDecoration with a specific constant ID
1848 /// @param id the optional id value
1849 /// @returns the override decoration pointer
1850 ast::OverrideDecoration* Override(uint32_t id) {
1851 return Override(source_, id);
1852 }
1853
1854 /// Creates an ast::OverrideDecoration without a constant ID
1855 /// @param source the source information
1856 /// @returns the override decoration pointer
1857 ast::OverrideDecoration* Override(const Source& source) {
1858 return create<ast::OverrideDecoration>(source);
1859 }
1860
1861 /// Creates an ast::OverrideDecoration without a constant ID
1862 /// @returns the override decoration pointer
1863 ast::OverrideDecoration* Override() { return Override(source_); }
1864
James Price68f558f2021-04-06 15:51:47 +00001865 /// Creates an ast::StageDecoration
1866 /// @param source the source information
1867 /// @param stage the pipeline stage
1868 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001869 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001870 return create<ast::StageDecoration>(source, stage);
1871 }
1872
1873 /// Creates an ast::StageDecoration
1874 /// @param stage the pipeline stage
1875 /// @returns the stage decoration pointer
1876 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1877 return create<ast::StageDecoration>(source_, stage);
1878 }
1879
James Price70f80bb2021-05-19 13:40:08 +00001880 /// Creates an ast::WorkgroupDecoration
1881 /// @param x the x dimension expression
1882 /// @returns the workgroup decoration pointer
1883 template <typename EXPR_X>
1884 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1885 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1886 }
1887
1888 /// Creates an ast::WorkgroupDecoration
1889 /// @param x the x dimension expression
1890 /// @param y the y dimension expression
1891 /// @returns the workgroup decoration pointer
1892 template <typename EXPR_X, typename EXPR_Y>
1893 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1894 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1895 nullptr);
1896 }
1897
1898 /// Creates an ast::WorkgroupDecoration
1899 /// @param x the x dimension expression
1900 /// @param y the y dimension expression
1901 /// @param z the z dimension expression
1902 /// @returns the workgroup decoration pointer
1903 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1904 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1905 return create<ast::WorkgroupDecoration>(
1906 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1907 Expr(std::forward<EXPR_Z>(z)));
1908 }
1909
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001910 /// Sets the current builder source to `src`
1911 /// @param src the Source used for future create() calls
1912 void SetSource(const Source& src) {
1913 AssertNotMoved();
1914 source_ = src;
1915 }
1916
1917 /// Sets the current builder source to `loc`
1918 /// @param loc the Source used for future create() calls
1919 void SetSource(const Source::Location& loc) {
1920 AssertNotMoved();
1921 source_ = Source(loc);
1922 }
1923
Ben Clayton33352542021-01-29 16:43:41 +00001924 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001925 /// @note As the Resolver is run when the Program is built, this will only be
1926 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001927 /// @param expr the AST expression
1928 /// @return the resolved semantic type for the expression, or nullptr if the
1929 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001930 sem::Type* TypeOf(const ast::Expression* expr) const;
1931
Ben Claytonfe0910f2021-05-17 15:51:47 +00001932 /// Helper for returning the resolved semantic type of the variable `var`.
1933 /// @note As the Resolver is run when the Program is built, this will only be
1934 /// useful for the Resolver itself and tests that use their own Resolver.
1935 /// @param var the AST variable
1936 /// @return the resolved semantic type for the variable, or nullptr if the
1937 /// variable has no resolved type.
1938 sem::Type* TypeOf(const ast::Variable* var) const;
1939
Ben Claytonfbec46f2021-04-30 20:20:19 +00001940 /// Helper for returning the resolved semantic type of the AST type `type`.
1941 /// @note As the Resolver is run when the Program is built, this will only be
1942 /// useful for the Resolver itself and tests that use their own Resolver.
1943 /// @param expr the AST type
1944 /// @return the resolved semantic type for the type, or nullptr if the type
1945 /// has no resolved type.
1946 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001947
Ben Clayton169512e2021-04-17 05:52:11 +00001948 /// Wraps the ast::Literal in a statement. This is used by tests that
1949 /// construct a partial AST and require the Resolver to reach these
1950 /// nodes.
1951 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1952 /// @return the ast::Statement that wraps the ast::Statement
1953 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001954 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001955 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001956 /// nodes.
1957 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1958 /// @return the ast::Statement that wraps the ast::Expression
1959 ast::Statement* WrapInStatement(ast::Expression* expr);
1960 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001961 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001962 /// these nodes.
1963 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1964 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1965 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1966 /// Returns the statement argument. Used as a passthrough-overload by
1967 /// WrapInFunction().
1968 /// @param stmt the ast::Statement
1969 /// @return `stmt`
1970 ast::Statement* WrapInStatement(ast::Statement* stmt);
1971 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001972 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001973 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001974 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001975 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001976 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001977 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001978 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001979 }
1980 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001981 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001982 /// @returns the function
1983 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001984
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001985 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001986 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001987
1988 protected:
1989 /// Asserts that the builder has not been moved.
1990 void AssertNotMoved() const;
1991
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001992 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001993 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001994 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001995 ASTNodeAllocator ast_nodes_;
1996 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001997 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001998 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001999 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002000 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002001
2002 /// The source to use when creating AST nodes without providing a Source as
2003 /// the first argument.
2004 Source source_;
2005
Ben Clayton5f0ea112021-03-09 10:54:37 +00002006 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002007 /// program when built.
2008 bool resolve_on_build_ = true;
2009
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002010 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2011 bool moved_ = false;
2012};
2013
2014//! @cond Doxygen_Suppress
2015// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2016template <>
2017struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002018 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002019 return t->i32();
2020 }
2021};
2022template <>
2023struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002024 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002025 return t->u32();
2026 }
2027};
2028template <>
2029struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002030 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002031 return t->f32();
2032 }
2033};
2034template <>
2035struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002036 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002037 return t->bool_();
2038 }
2039};
2040template <>
2041struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002042 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002043 return t->void_();
2044 }
2045};
2046//! @endcond
2047
Ben Clayton917b14b2021-04-19 16:50:23 +00002048/// @param builder the ProgramBuilder
2049/// @returns the ProgramID of the ProgramBuilder
2050inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2051 return builder->ID();
2052}
2053
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002054} // namespace tint
2055
2056#endif // SRC_PROGRAM_BUILDER_H_