blob: 05acee43594b4453ae2edd4d8d3862ab771575f3 [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000026#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/bool_literal.h"
28#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000029#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000030#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000031#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000032#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000035#include "src/ast/if_statement.h"
36#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000037#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/member_accessor_expression.h"
39#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000040#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000041#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000043#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/scalar_constructor_expression.h"
46#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000047#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000049#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000050#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000053#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000055#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000056#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000058#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000059#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000060#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000061#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000062#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000064#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000065#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000066#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000067#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000068#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000069#include "src/sem/f32_type.h"
70#include "src/sem/i32_type.h"
71#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000073#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/sampled_texture_type.h"
75#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000076#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000077#include "src/sem/u32_type.h"
78#include "src/sem/vector_type.h"
79#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000080#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000081
82namespace tint {
83
Ben Clayton401b96b2021-02-03 17:19:59 +000084// Forward declarations
85namespace ast {
86class VariableDeclStatement;
87} // namespace ast
88
Ben Claytona6b9a8e2021-01-26 16:57:10 +000089class CloneContext;
90
91/// ProgramBuilder is a mutable builder for a Program.
92/// To construct a Program, populate the builder and then `std::move` it to a
93/// Program.
94class ProgramBuilder {
95 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000096 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
97 using ASTNodeAllocator = BlockAllocator<ast::Node>;
98
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000099 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
100 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000101
102 /// `i32` is a type alias to `int`.
103 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
104 /// WGSL syntax.
105 /// Note: this is intentionally not aliased to uint32_t as we want integer
106 /// literals passed to the builder to match WGSL's integer literal types.
107 using i32 = decltype(1);
108 /// `u32` is a type alias to `unsigned int`.
109 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
110 /// WGSL syntax.
111 /// Note: this is intentionally not aliased to uint32_t as we want integer
112 /// literals passed to the builder to match WGSL's integer literal types.
113 using u32 = decltype(1u);
114 /// `f32` is a type alias to `float`
115 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
116 /// WGSL syntax.
117 using f32 = float;
118
119 /// Constructor
120 ProgramBuilder();
121
122 /// Move constructor
123 /// @param rhs the builder to move
124 ProgramBuilder(ProgramBuilder&& rhs);
125
126 /// Destructor
127 virtual ~ProgramBuilder();
128
129 /// Move assignment operator
130 /// @param rhs the builder to move
131 /// @return this builder
132 ProgramBuilder& operator=(ProgramBuilder&& rhs);
133
Ben Claytone43c8302021-01-29 11:59:32 +0000134 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
135 /// making a deep clone of the Program contents.
136 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
137 /// existing immutable program.
138 /// As the returned ProgramBuilder wraps `program`, `program` must not be
139 /// destructed or assigned while using the returned ProgramBuilder.
140 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
141 /// function. See crbug.com/tint/460.
142 /// @param program the immutable Program to wrap
143 /// @return the ProgramBuilder that wraps `program`
144 static ProgramBuilder Wrap(const Program* program);
145
Ben Claytone6995de2021-04-13 23:27:27 +0000146 /// @returns the unique identifier for this program
147 ProgramID ID() const { return id_; }
148
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000149 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000150 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000151 AssertNotMoved();
152 return types_;
153 }
154
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000156 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000157 AssertNotMoved();
158 return types_;
159 }
160
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000162 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000163 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000164 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000165 }
166
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000168 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000169 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000170 return ast_nodes_;
171 }
172
173 /// @returns a reference to the program's semantic nodes storage
174 SemNodeAllocator& SemNodes() {
175 AssertNotMoved();
176 return sem_nodes_;
177 }
178
179 /// @returns a reference to the program's semantic nodes storage
180 const SemNodeAllocator& SemNodes() const {
181 AssertNotMoved();
182 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000183 }
184
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000185 /// @returns a reference to the program's AST root Module
186 ast::Module& AST() {
187 AssertNotMoved();
188 return *ast_;
189 }
190
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000191 /// @returns a reference to the program's AST root Module
192 const ast::Module& AST() const {
193 AssertNotMoved();
194 return *ast_;
195 }
196
197 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000198 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000199 AssertNotMoved();
200 return sem_;
201 }
202
203 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000204 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000205 AssertNotMoved();
206 return sem_;
207 }
208
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000209 /// @returns a reference to the program's SymbolTable
210 SymbolTable& Symbols() {
211 AssertNotMoved();
212 return symbols_;
213 }
214
Ben Clayton708dc2d2021-01-29 11:22:40 +0000215 /// @returns a reference to the program's SymbolTable
216 const SymbolTable& Symbols() const {
217 AssertNotMoved();
218 return symbols_;
219 }
220
Ben Clayton844217f2021-01-27 18:49:05 +0000221 /// @returns a reference to the program's diagnostics
222 diag::List& Diagnostics() {
223 AssertNotMoved();
224 return diagnostics_;
225 }
226
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000227 /// @returns a reference to the program's diagnostics
228 const diag::List& Diagnostics() const {
229 AssertNotMoved();
230 return diagnostics_;
231 }
232
Ben Clayton5f0ea112021-03-09 10:54:37 +0000233 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000234 /// @param enable the new flag value (defaults to true)
235 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
236
Ben Clayton5f0ea112021-03-09 10:54:37 +0000237 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000238 /// built.
239 bool ResolveOnBuild() const { return resolve_on_build_; }
240
Ben Clayton844217f2021-01-27 18:49:05 +0000241 /// @returns true if the program has no error diagnostics and is not missing
242 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000243 bool IsValid() const;
244
Ben Clayton708dc2d2021-01-29 11:22:40 +0000245 /// Writes a representation of the node to the output stream
246 /// @note unlike str(), to_str() does not automatically demangle the string.
247 /// @param node the AST node
248 /// @param out the stream to write to
249 /// @param indent number of spaces to indent the node when writing
250 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
251 node->to_str(Sem(), out, indent);
252 }
253
254 /// Returns a demangled, string representation of `node`.
255 /// @param node the AST node
256 /// @returns a string representation of the node
257 std::string str(const ast::Node* node) const;
258
Ben Clayton7fdfff12021-01-29 15:17:30 +0000259 /// Creates a new ast::Node owned by the ProgramBuilder. When the
260 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000261 /// @param source the Source of the node
262 /// @param args the arguments to pass to the type constructor
263 /// @returns the node pointer
264 template <typename T, typename... ARGS>
265 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
266 ARGS&&... args) {
267 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000268 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000269 }
270
Ben Clayton7fdfff12021-01-29 15:17:30 +0000271 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
272 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000273 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000274 /// When the ProgramBuilder is destructed, the ast::Node will also be
275 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000276 /// @returns the node pointer
277 template <typename T>
278 traits::EnableIfIsType<T, ast::Node>* create() {
279 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000280 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000281 }
282
Ben Clayton7fdfff12021-01-29 15:17:30 +0000283 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
284 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000285 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000286 /// When the ProgramBuilder is destructed, the ast::Node will also be
287 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000288 /// @param arg0 the first arguments to pass to the type constructor
289 /// @param args the remaining arguments to pass to the type constructor
290 /// @returns the node pointer
291 template <typename T, typename ARG0, typename... ARGS>
292 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
293 traits::IsTypeOrDerived<T, ast::Node>::value &&
294 !traits::IsTypeOrDerived<ARG0, Source>::value,
295 T>*
296 create(ARG0&& arg0, ARGS&&... args) {
297 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000298 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000299 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000300 }
301
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000302 /// Creates a new sem::Node owned by the ProgramBuilder.
303 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000304 /// destructed.
305 /// @param args the arguments to pass to the type constructor
306 /// @returns the node pointer
307 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000308 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
309 !traits::IsTypeOrDerived<T, sem::Type>::value,
310 T>*
311 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000312 AssertNotMoved();
313 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
314 }
315
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000316 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000317 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
318 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000319 /// Types are unique (de-aliased), and so calling create() for the same `T`
320 /// and arguments will return the same pointer.
321 /// @warning Use this method to acquire a type only if all of its type
322 /// information is provided in the constructor arguments `args`.<br>
323 /// If the type requires additional configuration after construction that
324 /// affect its fundamental type, build the type with `std::make_unique`, make
325 /// any necessary alterations and then call unique_type() instead.
326 /// @param args the arguments to pass to the type constructor
327 /// @returns the de-aliased type pointer
328 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000329 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
330 static_assert(std::is_base_of<sem::Type, T>::value,
331 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000332 AssertNotMoved();
333 return types_.Get<T>(std::forward<ARGS>(args)...);
334 }
335
336 /// Marks this builder as moved, preventing any further use of the builder.
337 void MarkAsMoved();
338
339 //////////////////////////////////////////////////////////////////////////////
340 // TypesBuilder
341 //////////////////////////////////////////////////////////////////////////////
342
343 /// TypesBuilder holds basic `tint` types and methods for constructing
344 /// complex types.
345 class TypesBuilder {
346 public:
347 /// Constructor
348 /// @param builder the program builder
349 explicit TypesBuilder(ProgramBuilder* builder);
350
351 /// @return the tint AST type for the C type `T`.
352 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000353 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000354 return CToAST<T>::get(this);
355 }
356
357 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000358 typ::Bool bool_() const {
359 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
360 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000361
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000362 /// @param source the Source of the node
363 /// @returns a boolean type
364 typ::Bool bool_(const Source& source) const {
365 return {builder->create<ast::Bool>(source), builder->create<sem::Bool>()};
366 }
367
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000369 typ::F32 f32() const {
370 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
371 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000372
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000373 /// @param source the Source of the node
374 /// @returns a f32 type
375 typ::F32 f32(const Source& source) const {
376 return {builder->create<ast::F32>(source), builder->create<sem::F32>()};
377 }
378
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000379 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000380 typ::I32 i32() const {
381 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
382 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000383
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000384 /// @param source the Source of the node
385 /// @returns a i32 type
386 typ::I32 i32(const Source& source) const {
387 return {builder->create<ast::I32>(source), builder->create<sem::I32>()};
388 }
389
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000390 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000391 typ::U32 u32() const {
392 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
393 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000394
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000395 /// @param source the Source of the node
396 /// @returns a u32 type
397 typ::U32 u32(const Source& source) const {
398 return {builder->create<ast::U32>(source), builder->create<sem::U32>()};
399 }
400
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000401 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000402 typ::Void void_() const {
403 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
404 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000405
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000406 /// @param source the Source of the node
407 /// @returns a void type
408 typ::Void void_(const Source& source) const {
409 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
410 }
411
Antonio Maiorano25436862021-04-13 13:32:33 +0000412 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000413 /// @param n vector width in elements
414 /// @return the tint AST type for a `n`-element vector of `type`.
415 typ::Vector vec(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 {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
418 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000419 }
420
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000421 /// @param source the Source of the node
422 /// @param type vector subtype
423 /// @param n vector width in elements
424 /// @return the tint AST type for a `n`-element vector of `type`.
425 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000426 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000427 return {
428 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
429 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000430 }
431
Antonio Maiorano25436862021-04-13 13:32:33 +0000432 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000433 /// @return the tint AST type for a 2-element vector of `type`.
434 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
435
436 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000437 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000438 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000439
440 /// @param type vector subtype
441 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000442 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
443
444 /// @param n vector width in elements
445 /// @return the tint AST type for a `n`-element vector of `type`.
446 template <typename T>
447 typ::Vector vec(uint32_t n) const {
448 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000449 }
450
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000451 /// @return the tint AST type for a 2-element vector of the C type `T`.
452 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000453 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000454 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000455 }
456
457 /// @return the tint AST type for a 3-element vector of the C type `T`.
458 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000459 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000460 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000461 }
462
463 /// @return the tint AST type for a 4-element vector of the C type `T`.
464 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000465 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000466 return vec4(Of<T>());
467 }
468
469 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000470 /// @param columns number of columns for the matrix
471 /// @param rows number of rows for the matrix
472 /// @return the tint AST type for a matrix of `type`
473 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000474 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000475 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
476 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000477 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000478 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000479 }
480
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000481 /// @param source the Source of the node
482 /// @param type matrix subtype
483 /// @param columns number of columns for the matrix
484 /// @param rows number of rows for the matrix
485 /// @return the tint AST type for a matrix of `type`
486 typ::Matrix mat(const Source& source,
487 typ::Type type,
488 uint32_t columns,
489 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000490 return {type.ast
491 ? builder->create<ast::Matrix>(source, type, rows, columns)
492 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000493 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000494 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000495 }
496
Ben Claytone204f272021-04-22 14:40:23 +0000497 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000498 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000499 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000500
501 /// @param type matrix subtype
502 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000503 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000504
505 /// @param type matrix subtype
506 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000507 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000508
509 /// @param type matrix subtype
510 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000511 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000512
513 /// @param type matrix subtype
514 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000515 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000516
517 /// @param type matrix subtype
518 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000519 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000520
521 /// @param type matrix subtype
522 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000523 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000524
525 /// @param type matrix subtype
526 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000527 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000528
529 /// @param type matrix subtype
530 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000531 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000532
Ben Claytone204f272021-04-22 14:40:23 +0000533 /// @param columns number of columns for the matrix
534 /// @param rows number of rows for the matrix
535 /// @return the tint AST type for a matrix of `type`
536 template <typename T>
537 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
538 return mat(Of<T>(), columns, rows);
539 }
540
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000541 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
542 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000543 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000544 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000545 }
546
547 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
548 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000549 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000550 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000551 }
552
553 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
554 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000555 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000556 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000557 }
558
559 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
560 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000561 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000562 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000563 }
564
565 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
566 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000567 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000568 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000569 }
570
571 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
572 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000573 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000574 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000575 }
576
577 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
578 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000579 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000580 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000581 }
582
583 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
584 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000585 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000586 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000587 }
588
589 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
590 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000591 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000592 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000593 }
594
595 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000596 /// @param n the array size. 0 represents a runtime-array
597 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000598 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000599 ast::Array* array(typ::Type subtype,
600 uint32_t n = 0,
601 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000602 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000603 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000604 }
605
606 /// @param source the Source of the node
607 /// @param subtype the array element type
608 /// @param n the array size. 0 represents a runtime-array
609 /// @param decos the optional decorations for the array
610 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000611 ast::Array* array(const Source& source,
612 typ::Type subtype,
613 uint32_t n = 0,
614 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000615 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000616 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000617 }
618
Ben Claytonbab31972021-02-08 21:16:21 +0000619 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000620 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000621 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000622 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000623 ast::Array* array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000624 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000625 ast::DecorationList decos;
626 if (stride) {
627 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
628 }
629 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000630 }
631
Ben Clayton0f88b312021-05-04 17:36:31 +0000632 /// @param source the Source of the node
633 /// @param subtype the array element type
634 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000635 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000636 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000637 ast::Array* array(const Source& source,
638 typ::Type subtype,
639 uint32_t n,
640 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000641 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000642 ast::DecorationList decos;
643 if (stride) {
644 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
645 }
646 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000647 }
648
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000649 /// @return the tint AST type for an array of size `N` of type `T`
650 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000651 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000652 return array(Of<T>(), N);
653 }
654
Ben Claytonbab31972021-02-08 21:16:21 +0000655 /// @param stride the array stride
656 /// @return the tint AST type for an array of size `N` of type `T`
657 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000658 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000659 return array(Of<T>(), N, stride);
660 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000661
Ben Clayton0f88b312021-05-04 17:36:31 +0000662 /// Creates a type name
663 /// @param name the name
664 /// @returns the type name
665 template <typename NAME>
666 ast::TypeName* type_name(NAME&& name) const {
667 return builder->create<ast::TypeName>(
668 builder->Sym(std::forward<NAME>(name)));
669 }
670
671 /// Creates a type name
672 /// @param source the Source of the node
673 /// @param name the name
674 /// @returns the type name
675 template <typename NAME>
676 ast::TypeName* type_name(const Source& source, NAME&& name) const {
677 return builder->create<ast::TypeName>(
678 source, builder->Sym(std::forward<NAME>(name)));
679 }
680
Ben Clayton42d1e092021-02-02 14:29:15 +0000681 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000682 /// @param name the alias name
683 /// @param type the alias type
684 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000685 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000686 ast::Alias* alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000687 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000688 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000689 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000690 }
691
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000692 /// Creates an alias type
693 /// @param source the Source of the node
694 /// @param name the alias name
695 /// @param type the alias type
696 /// @returns the alias pointer
697 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000698 ast::Alias* alias(const Source& source, NAME&& name, typ::Type type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000699 type = MaybeCreateTypename(type);
700 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000701 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000702 }
703
Ben Clayton4db10dd2021-04-16 08:47:14 +0000704 /// Creates an access control qualifier type
705 /// @param access the access control
706 /// @param type the inner type
707 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000708 ast::AccessControl* access(ast::AccessControl::Access access,
709 const ast::Type* type) const {
710 type = MaybeCreateTypename(type).ast;
711 return type ? builder->create<ast::AccessControl>(access, type) : nullptr;
Ben Clayton4db10dd2021-04-16 08:47:14 +0000712 }
713
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000714 /// Creates an access control qualifier type
715 /// @param source the Source of the node
716 /// @param access the access control
717 /// @param type the inner type
718 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000719 ast::AccessControl* access(const Source& source,
720 ast::AccessControl::Access access,
721 const ast::Type* type) const {
722 type = MaybeCreateTypename(type).ast;
723 return type ? builder->create<ast::AccessControl>(source, access, type)
724 : nullptr;
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000725 }
726
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000727 /// @param type the type of the pointer
728 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000729 /// @return the pointer to `type` with the given ast::StorageClass
730 typ::Pointer pointer(typ::Type type,
731 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000732 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000733 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
734 : nullptr,
735 type.sem ? builder->create<sem::Pointer>(type, storage_class)
736 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000737 }
738
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000739 /// @param source the Source of the node
740 /// @param type the type of the pointer
741 /// @param storage_class the storage class of the pointer
742 /// @return the pointer to `type` with the given ast::StorageClass
743 typ::Pointer pointer(const Source& source,
744 typ::Type type,
745 ast::StorageClass storage_class) const {
746 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000747 return {type.ast
748 ? builder->create<ast::Pointer>(source, type, storage_class)
749 : nullptr,
750 type.sem ? builder->create<sem::Pointer>(type, storage_class)
751 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000752 }
753
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000754 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000755 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000756 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000757 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000758 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000759 }
760
Ben Claytone204f272021-04-22 14:40:23 +0000761 /// @param kind the kind of sampler
762 /// @returns the sampler
763 typ::Sampler sampler(ast::SamplerKind kind) const {
764 return {builder->create<ast::Sampler>(kind),
765 builder->create<sem::Sampler>(kind)};
766 }
767
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000768 /// @param source the Source of the node
769 /// @param kind the kind of sampler
770 /// @returns the sampler
771 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
772 return {builder->create<ast::Sampler>(source, kind),
773 builder->create<sem::Sampler>(kind)};
774 }
775
Ben Claytone204f272021-04-22 14:40:23 +0000776 /// @param dims the dimensionality of the texture
777 /// @returns the depth texture
778 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
779 return {builder->create<ast::DepthTexture>(dims),
780 builder->create<sem::DepthTexture>(dims)};
781 }
782
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000783 /// @param source the Source of the node
784 /// @param dims the dimensionality of the texture
785 /// @returns the depth texture
786 typ::DepthTexture depth_texture(const Source& source,
787 ast::TextureDimension dims) const {
788 return {builder->create<ast::DepthTexture>(source, dims),
789 builder->create<sem::DepthTexture>(dims)};
790 }
791
Ben Claytone204f272021-04-22 14:40:23 +0000792 /// @param dims the dimensionality of the texture
793 /// @param subtype the texture subtype.
794 /// @returns the sampled texture
795 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
796 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000797 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
798 : nullptr,
799 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
800 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000801 }
802
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000803 /// @param source the Source of the node
804 /// @param dims the dimensionality of the texture
805 /// @param subtype the texture subtype.
806 /// @returns the sampled texture
807 typ::SampledTexture sampled_texture(const Source& source,
808 ast::TextureDimension dims,
809 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000810 return {subtype.ast
811 ? builder->create<ast::SampledTexture>(source, dims, subtype)
812 : nullptr,
813 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
814 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000815 }
816
Ben Claytone204f272021-04-22 14:40:23 +0000817 /// @param dims the dimensionality of the texture
818 /// @param subtype the texture subtype.
819 /// @returns the multisampled texture
820 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
821 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000822 return {
823 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
824 : nullptr,
825 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
826 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000827 }
828
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000829 /// @param source the Source of the node
830 /// @param dims the dimensionality of the texture
831 /// @param subtype the texture subtype.
832 /// @returns the multisampled texture
833 typ::MultisampledTexture multisampled_texture(const Source& source,
834 ast::TextureDimension dims,
835 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000836 return {
837 subtype.ast
838 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
839 : nullptr,
840 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
841 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000842 }
843
Ben Claytone204f272021-04-22 14:40:23 +0000844 /// @param dims the dimensionality of the texture
845 /// @param format the image format of the texture
846 /// @returns the storage texture
847 typ::StorageTexture storage_texture(ast::TextureDimension dims,
848 ast::ImageFormat format) const {
849 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
850 auto* sem_subtype =
851 sem::StorageTexture::SubtypeFor(format, builder->Types());
852 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000853 builder->create<sem::StorageTexture>(
854 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000855 }
856
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000857 /// @param source the Source of the node
858 /// @param dims the dimensionality of the texture
859 /// @param format the image format of the texture
860 /// @returns the storage texture
861 typ::StorageTexture storage_texture(const Source& source,
862 ast::TextureDimension dims,
863 ast::ImageFormat format) const {
864 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
865 auto* sem_subtype =
866 sem::StorageTexture::SubtypeFor(format, builder->Types());
867 return {builder->create<ast::StorageTexture>(source, dims, format,
868 ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000869 builder->create<sem::StorageTexture>(
870 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000871 }
872
Brandon Jones7b257692021-05-17 17:40:17 +0000873 /// @returns the external texture
874 typ::ExternalTexture external_texture() const {
875 return {builder->create<ast::ExternalTexture>(),
876 builder->create<sem::ExternalTexture>()};
877 }
878
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000879 /// @param source the Source of the node
880 /// @returns the external texture
881 typ::ExternalTexture external_texture(const Source& source) const {
882 return {builder->create<ast::ExternalTexture>(source),
883 builder->create<sem::ExternalTexture>()};
884 }
885
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000886 /// If ty is a ast::Struct or ast::Alias, the returned type is an
887 /// ast::TypeName of the given type's name, otherwise type is returned.
888 /// @param type the type
889 /// @return either type or a pointer to a new ast::TypeName
890 typ::Type MaybeCreateTypename(typ::Type type) const;
891
Ben Claytonf5f311e2021-04-28 14:31:23 +0000892 /// The ProgramBuilder
893 ProgramBuilder* const builder;
894
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000895 private:
896 /// CToAST<T> is specialized for various `T` types and each specialization
897 /// contains a single static `get()` method for obtaining the corresponding
898 /// AST type for the C type `T`.
899 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000900 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000901 template <typename T>
902 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000903 };
904
905 //////////////////////////////////////////////////////////////////////////////
906 // AST helper methods
907 //////////////////////////////////////////////////////////////////////////////
908
James Price65ae64d2021-04-29 12:59:14 +0000909 /// @return a new unnamed symbol
910 Symbol Sym() { return Symbols().New(); }
911
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000912 /// @param name the symbol string
913 /// @return a Symbol with the given name
914 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
915
916 /// @param sym the symbol
917 /// @return `sym`
918 Symbol Sym(Symbol sym) { return sym; }
919
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000920 /// @param expr the expression
921 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000922 template <typename T>
923 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
924 return expr;
925 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000926
Ben Claytonb8ea5912021-04-19 16:52:42 +0000927 /// Passthrough for nullptr
928 /// @return nullptr
929 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
930
Ben Claytonfe0910f2021-05-17 15:51:47 +0000931 /// @param source the source information
932 /// @param symbol the identifier symbol
933 /// @return an ast::IdentifierExpression with the given symbol
934 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
935 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000936 }
937
Ben Clayton46d78d72021-02-10 21:34:26 +0000938 /// @param symbol the identifier symbol
939 /// @return an ast::IdentifierExpression with the given symbol
940 ast::IdentifierExpression* Expr(Symbol symbol) {
941 return create<ast::IdentifierExpression>(symbol);
942 }
943
Ben Claytonfe0910f2021-05-17 15:51:47 +0000944 /// @param source the source information
945 /// @param variable the AST variable
946 /// @return an ast::IdentifierExpression with the variable's symbol
947 ast::IdentifierExpression* Expr(const Source& source,
948 ast::Variable* variable) {
949 return create<ast::IdentifierExpression>(source, variable->symbol());
950 }
951
Ben Claytonb8ea5912021-04-19 16:52:42 +0000952 /// @param variable the AST variable
953 /// @return an ast::IdentifierExpression with the variable's symbol
954 ast::IdentifierExpression* Expr(ast::Variable* variable) {
955 return create<ast::IdentifierExpression>(variable->symbol());
956 }
957
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000958 /// @param source the source information
959 /// @param name the identifier name
960 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000961 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
962 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
963 }
964
965 /// @param name the identifier name
966 /// @return an ast::IdentifierExpression with the given name
967 ast::IdentifierExpression* Expr(const char* name) {
968 return create<ast::IdentifierExpression>(Symbols().Register(name));
969 }
970
971 /// @param source the source information
972 /// @param name the identifier name
973 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000974 ast::IdentifierExpression* Expr(const Source& source,
975 const std::string& name) {
976 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
977 }
978
979 /// @param name the identifier name
980 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000981 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000982 return create<ast::IdentifierExpression>(Symbols().Register(name));
983 }
984
Ben Claytonfe0910f2021-05-17 15:51:47 +0000985 /// @param source the source information
986 /// @param value the boolean value
987 /// @return a Scalar constructor for the given value
988 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
989 return create<ast::ScalarConstructorExpression>(source, Literal(value));
990 }
991
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000992 /// @param value the boolean value
993 /// @return a Scalar constructor for the given value
994 ast::ScalarConstructorExpression* Expr(bool value) {
995 return create<ast::ScalarConstructorExpression>(Literal(value));
996 }
997
Ben Claytonfe0910f2021-05-17 15:51:47 +0000998 /// @param source the source information
999 /// @param value the float value
1000 /// @return a Scalar constructor for the given value
1001 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
1002 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1003 }
1004
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001005 /// @param value the float value
1006 /// @return a Scalar constructor for the given value
1007 ast::ScalarConstructorExpression* Expr(f32 value) {
1008 return create<ast::ScalarConstructorExpression>(Literal(value));
1009 }
1010
Ben Claytonfe0910f2021-05-17 15:51:47 +00001011 /// @param source the source information
1012 /// @param value the integer value
1013 /// @return a Scalar constructor for the given value
1014 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1015 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1016 }
1017
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001018 /// @param value the integer value
1019 /// @return a Scalar constructor for the given value
1020 ast::ScalarConstructorExpression* Expr(i32 value) {
1021 return create<ast::ScalarConstructorExpression>(Literal(value));
1022 }
1023
Ben Claytonfe0910f2021-05-17 15:51:47 +00001024 /// @param source the source information
1025 /// @param value the unsigned int value
1026 /// @return a Scalar constructor for the given value
1027 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1028 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1029 }
1030
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001031 /// @param value the unsigned int value
1032 /// @return a Scalar constructor for the given value
1033 ast::ScalarConstructorExpression* Expr(u32 value) {
1034 return create<ast::ScalarConstructorExpression>(Literal(value));
1035 }
1036
1037 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1038 /// `list`.
1039 /// @param list the list to append too
1040 /// @param arg the arg to create
1041 template <typename ARG>
1042 void Append(ast::ExpressionList& list, ARG&& arg) {
1043 list.emplace_back(Expr(std::forward<ARG>(arg)));
1044 }
1045
1046 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1047 /// then appends them to `list`.
1048 /// @param list the list to append too
1049 /// @param arg0 the first argument
1050 /// @param args the rest of the arguments
1051 template <typename ARG0, typename... ARGS>
1052 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1053 Append(list, std::forward<ARG0>(arg0));
1054 Append(list, std::forward<ARGS>(args)...);
1055 }
1056
1057 /// @return an empty list of expressions
1058 ast::ExpressionList ExprList() { return {}; }
1059
1060 /// @param args the list of expressions
1061 /// @return the list of expressions converted to `ast::Expression`s using
1062 /// `Expr()`,
1063 template <typename... ARGS>
1064 ast::ExpressionList ExprList(ARGS&&... args) {
1065 ast::ExpressionList list;
1066 list.reserve(sizeof...(args));
1067 Append(list, std::forward<ARGS>(args)...);
1068 return list;
1069 }
1070
1071 /// @param list the list of expressions
1072 /// @return `list`
1073 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1074
1075 /// @param val the boolan value
1076 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001077 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078
1079 /// @param val the float value
1080 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001081 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001082
1083 /// @param val the unsigned int value
1084 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001085 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001086
1087 /// @param val the integer value
1088 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001089 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001090
1091 /// @param args the arguments for the type constructor
1092 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1093 /// of `args` converted to `ast::Expression`s using `Expr()`
1094 template <typename T, typename... ARGS>
1095 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001096 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001097 }
1098
1099 /// @param type the type to construct
1100 /// @param args the arguments for the constructor
1101 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1102 /// values `args`.
1103 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001104 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001105 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001106 return create<ast::TypeConstructorExpression>(
1107 type, ExprList(std::forward<ARGS>(args)...));
1108 }
1109
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001110 /// Creates a constructor expression that constructs an object of
1111 /// `type` filled with `elem_value`. For example,
1112 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1113 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1114 /// @param type the type to construct
1115 /// @param elem_value the initial or element value (for vec and mat) to
1116 /// construct with
1117 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001118 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001119 int elem_value = 0);
1120
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 /// @param args the arguments for the vector constructor
1122 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1123 /// `T`, constructed with the values `args`.
1124 template <typename T, typename... ARGS>
1125 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001126 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 }
1128
1129 /// @param args the arguments for the vector constructor
1130 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1131 /// `T`, constructed with the values `args`.
1132 template <typename T, typename... ARGS>
1133 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001134 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001135 }
1136
1137 /// @param args the arguments for the vector constructor
1138 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1139 /// `T`, constructed with the values `args`.
1140 template <typename T, typename... ARGS>
1141 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001142 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001143 }
1144
1145 /// @param args the arguments for the matrix constructor
1146 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1147 /// `T`, constructed with the values `args`.
1148 template <typename T, typename... ARGS>
1149 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001150 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001151 }
1152
1153 /// @param args the arguments for the matrix constructor
1154 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1155 /// `T`, constructed with the values `args`.
1156 template <typename T, typename... ARGS>
1157 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001158 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001159 }
1160
1161 /// @param args the arguments for the matrix constructor
1162 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1163 /// `T`, constructed with the values `args`.
1164 template <typename T, typename... ARGS>
1165 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001166 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001167 }
1168
1169 /// @param args the arguments for the matrix constructor
1170 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1171 /// `T`, constructed with the values `args`.
1172 template <typename T, typename... ARGS>
1173 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001174 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001175 }
1176
1177 /// @param args the arguments for the matrix constructor
1178 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1179 /// `T`, constructed with the values `args`.
1180 template <typename T, typename... ARGS>
1181 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001182 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001183 }
1184
1185 /// @param args the arguments for the matrix constructor
1186 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1187 /// `T`, constructed with the values `args`.
1188 template <typename T, typename... ARGS>
1189 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001190 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001191 }
1192
1193 /// @param args the arguments for the matrix constructor
1194 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1195 /// `T`, constructed with the values `args`.
1196 template <typename T, typename... ARGS>
1197 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001198 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 }
1200
1201 /// @param args the arguments for the matrix constructor
1202 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1203 /// `T`, constructed with the values `args`.
1204 template <typename T, typename... ARGS>
1205 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001206 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 }
1208
1209 /// @param args the arguments for the matrix constructor
1210 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1211 /// `T`, constructed with the values `args`.
1212 template <typename T, typename... ARGS>
1213 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001214 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 }
1216
1217 /// @param args the arguments for the array constructor
1218 /// @return an `ast::TypeConstructorExpression` of an array with element type
1219 /// `T`, constructed with the values `args`.
1220 template <typename T, int N = 0, typename... ARGS>
1221 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001222 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001223 }
1224
1225 /// @param subtype the array element type
1226 /// @param n the array size. 0 represents a runtime-array.
1227 /// @param args the arguments for the array constructor
1228 /// @return an `ast::TypeConstructorExpression` of an array with element type
1229 /// `subtype`, constructed with the values `args`.
1230 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001231 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001232 uint32_t n,
1233 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001234 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001235 }
1236
1237 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001239 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001240 /// @param constructor constructor expression
1241 /// @param decorations variable decorations
1242 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001243 template <typename NAME>
1244 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001245 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001246 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001247 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001248 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001249 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001250 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1251 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001252 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253
1254 /// @param source the variable source
1255 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001256 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001257 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001258 /// @param constructor constructor expression
1259 /// @param decorations variable decorations
1260 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001261 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001262 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001263 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001264 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001265 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001266 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001267 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001268 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001269 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001270 type, false, constructor, decorations);
1271 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001272
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001273 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001274 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001275 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001276 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001277 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001278 template <typename NAME>
1279 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001280 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001281 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001282 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001283 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001284 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001285 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001286 constructor, decorations);
1287 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001288
1289 /// @param source the variable source
1290 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001292 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001293 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001294 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001295 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001296 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001297 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001298 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001299 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001300 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001301 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001302 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001303 ast::StorageClass::kNone, type, true,
1304 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001305 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001306
James Pricedaf1f1c2021-04-08 22:15:48 +00001307 /// @param name the parameter name
1308 /// @param type the parameter type
1309 /// @param decorations optional parameter decorations
1310 /// @returns a constant `ast::Variable` with the given name and type
1311 template <typename NAME>
1312 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001313 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001314 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001315 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001316 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1317 ast::StorageClass::kNone, type, true, nullptr,
1318 decorations);
1319 }
1320
1321 /// @param source the parameter source
1322 /// @param name the parameter name
1323 /// @param type the parameter type
1324 /// @param decorations optional parameter decorations
1325 /// @returns a constant `ast::Variable` with the given name and type
1326 template <typename NAME>
1327 ast::Variable* Param(const Source& source,
1328 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001329 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001330 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001331 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001332 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1333 ast::StorageClass::kNone, type, true, nullptr,
1334 decorations);
1335 }
1336
Ben Clayton42708342021-04-21 17:55:12 +00001337 /// @param name the variable name
1338 /// @param type the variable type
1339 /// @param storage the variable storage class
1340 /// @param constructor constructor expression
1341 /// @param decorations variable decorations
1342 /// @returns a new `ast::Variable`, which is automatically registered as a
1343 /// global variable with the ast::Module.
1344 template <typename NAME>
1345 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001346 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001347 ast::StorageClass storage,
1348 ast::Expression* constructor = nullptr,
1349 ast::DecorationList decorations = {}) {
1350 auto* var =
1351 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1352 AST().AddGlobalVariable(var);
1353 return var;
1354 }
1355
1356 /// @param source the variable source
1357 /// @param name the variable name
1358 /// @param type the variable type
1359 /// @param storage the variable storage class
1360 /// @param constructor constructor expression
1361 /// @param decorations variable decorations
1362 /// @returns a new `ast::Variable`, which is automatically registered as a
1363 /// global variable with the ast::Module.
1364 template <typename NAME>
1365 ast::Variable* Global(const Source& source,
1366 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001367 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001368 ast::StorageClass storage,
1369 ast::Expression* constructor = nullptr,
1370 ast::DecorationList decorations = {}) {
1371 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1372 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001373 AST().AddGlobalVariable(var);
1374 return var;
1375 }
1376
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001377 /// @param name the variable name
1378 /// @param type the variable type
1379 /// @param constructor constructor expression
1380 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001381 /// @returns a const `ast::Variable` constructed by calling Var() with the
1382 /// arguments of `args`, which is automatically registered as a global
1383 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001384 template <typename NAME>
1385 ast::Variable* GlobalConst(NAME&& name,
1386 typ::Type type,
1387 ast::Expression* constructor,
1388 ast::DecorationList decorations = {}) {
1389 auto* var = Const(std::forward<NAME>(name), type, constructor,
1390 std::move(decorations));
1391 AST().AddGlobalVariable(var);
1392 return var;
1393 }
1394
1395 /// @param source the variable source
1396 /// @param name the variable name
1397 /// @param type the variable type
1398 /// @param constructor constructor expression
1399 /// @param decorations optional variable decorations
1400 /// @returns a const `ast::Variable` constructed by calling Var() with the
1401 /// arguments of `args`, which is automatically registered as a global
1402 /// variable with the ast::Module.
1403 template <typename NAME>
1404 ast::Variable* GlobalConst(const Source& source,
1405 NAME&& name,
1406 typ::Type type,
1407 ast::Expression* constructor,
1408 ast::DecorationList decorations = {}) {
1409 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1410 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001411 AST().AddGlobalVariable(var);
1412 return var;
1413 }
1414
Ben Claytonfe0910f2021-05-17 15:51:47 +00001415 /// @param source the source information
1416 /// @param expr the expression to take the address of
1417 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1418 template <typename EXPR>
1419 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1420 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1421 Expr(std::forward<EXPR>(expr)));
1422 }
1423
1424 /// @param expr the expression to take the address of
1425 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1426 template <typename EXPR>
1427 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1428 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1429 Expr(std::forward<EXPR>(expr)));
1430 }
1431
1432 /// @param source the source information
1433 /// @param expr the expression to perform an indirection on
1434 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1435 template <typename EXPR>
1436 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1437 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1438 Expr(std::forward<EXPR>(expr)));
1439 }
1440
1441 /// @param expr the expression to perform an indirection on
1442 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1443 template <typename EXPR>
1444 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1445 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1446 Expr(std::forward<EXPR>(expr)));
1447 }
1448
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001449 /// @param func the function name
1450 /// @param args the function call arguments
1451 /// @returns a `ast::CallExpression` to the function `func`, with the
1452 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1453 template <typename NAME, typename... ARGS>
1454 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1455 return create<ast::CallExpression>(Expr(func),
1456 ExprList(std::forward<ARGS>(args)...));
1457 }
1458
1459 /// @param lhs the left hand argument to the addition operation
1460 /// @param rhs the right hand argument to the addition operation
1461 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1462 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001463 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001464 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1465 Expr(std::forward<LHS>(lhs)),
1466 Expr(std::forward<RHS>(rhs)));
1467 }
1468
1469 /// @param lhs the left hand argument to the subtraction operation
1470 /// @param rhs the right hand argument to the subtraction operation
1471 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1472 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001473 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001474 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1475 Expr(std::forward<LHS>(lhs)),
1476 Expr(std::forward<RHS>(rhs)));
1477 }
1478
1479 /// @param lhs the left hand argument to the multiplication operation
1480 /// @param rhs the right hand argument to the multiplication operation
1481 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1482 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001483 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001484 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1485 Expr(std::forward<LHS>(lhs)),
1486 Expr(std::forward<RHS>(rhs)));
1487 }
1488
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001489 /// @param source the source information
1490 /// @param lhs the left hand argument to the multiplication operation
1491 /// @param rhs the right hand argument to the multiplication operation
1492 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1493 template <typename LHS, typename RHS>
1494 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1495 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1496 Expr(std::forward<LHS>(lhs)),
1497 Expr(std::forward<RHS>(rhs)));
1498 }
1499
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001500 /// @param lhs the left hand argument to the division operation
1501 /// @param rhs the right hand argument to the division operation
1502 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1503 template <typename LHS, typename RHS>
1504 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1505 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1506 Expr(std::forward<LHS>(lhs)),
1507 Expr(std::forward<RHS>(rhs)));
1508 }
1509
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001510 /// @param arr the array argument for the array accessor expression
1511 /// @param idx the index argument for the array accessor expression
1512 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1513 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001514 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001515 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1516 Expr(std::forward<IDX>(idx)));
1517 }
1518
1519 /// @param obj the object for the member accessor expression
1520 /// @param idx the index argument for the array accessor expression
1521 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1522 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001523 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001524 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1525 Expr(std::forward<IDX>(idx)));
1526 }
1527
Ben Clayton42d1e092021-02-02 14:29:15 +00001528 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001529 /// @param val the offset value
1530 /// @returns the offset decoration pointer
1531 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1532 return create<ast::StructMemberOffsetDecoration>(source_, val);
1533 }
1534
Ben Claytond614dd52021-03-15 10:43:11 +00001535 /// Creates a ast::StructMemberSizeDecoration
1536 /// @param source the source information
1537 /// @param val the size value
1538 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001539 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1540 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001541 return create<ast::StructMemberSizeDecoration>(source, val);
1542 }
1543
1544 /// Creates a ast::StructMemberSizeDecoration
1545 /// @param val the size value
1546 /// @returns the size decoration pointer
1547 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1548 return create<ast::StructMemberSizeDecoration>(source_, val);
1549 }
1550
1551 /// Creates a ast::StructMemberAlignDecoration
1552 /// @param source the source information
1553 /// @param val the align value
1554 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001555 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1556 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001557 return create<ast::StructMemberAlignDecoration>(source, val);
1558 }
1559
1560 /// Creates a ast::StructMemberAlignDecoration
1561 /// @param val the align value
1562 /// @returns the align decoration pointer
1563 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1564 return create<ast::StructMemberAlignDecoration>(source_, val);
1565 }
1566
Ben Clayton42d1e092021-02-02 14:29:15 +00001567 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001568 /// @param source the source information
1569 /// @param name the function name
1570 /// @param params the function parameters
1571 /// @param type the function return type
1572 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001573 /// @param decorations the optional function decorations
1574 /// @param return_type_decorations the optional function return type
1575 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001576 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001577 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001578 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001579 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001580 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001581 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001582 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001583 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001584 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001585 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001586 auto* func =
1587 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1588 type, create<ast::BlockStatement>(body),
1589 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001590 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001591 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001592 }
1593
Ben Clayton42d1e092021-02-02 14:29:15 +00001594 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001595 /// @param name the function name
1596 /// @param params the function parameters
1597 /// @param type the function return type
1598 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001599 /// @param decorations the optional function decorations
1600 /// @param return_type_decorations the optional function return type
1601 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001602 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001603 template <typename NAME>
1604 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001605 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001606 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001607 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001608 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001609 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001610 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001611 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1612 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001613 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001614 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001615 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001616 }
1617
Ben Clayton49668bb2021-04-17 05:32:01 +00001618 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001619 /// @param source the source information
1620 /// @returns the return statement pointer
1621 ast::ReturnStatement* Return(const Source& source) {
1622 return create<ast::ReturnStatement>(source);
1623 }
1624
1625 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001626 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001627 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1628
1629 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001630 /// @param source the source information
1631 /// @param val the return value
1632 /// @returns the return statement pointer
1633 template <typename EXPR>
1634 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1635 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1636 }
1637
1638 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001639 /// @param val the return value
1640 /// @returns the return statement pointer
1641 template <typename EXPR>
1642 ast::ReturnStatement* Return(EXPR&& val) {
1643 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001644 }
1645
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001646 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001647 /// @param source the source information
1648 /// @param name the struct name
1649 /// @param members the struct members
1650 /// @param decorations the optional struct decorations
1651 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001652 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001653 ast::Struct* Structure(const Source& source,
1654 NAME&& name,
1655 ast::StructMemberList members,
1656 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001657 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001658 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001659 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001660 AST().AddConstructedType(type);
1661 return type;
1662 }
1663
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001664 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001665 /// @param name the struct name
1666 /// @param members the struct members
1667 /// @param decorations the optional struct decorations
1668 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001669 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001670 ast::Struct* Structure(NAME&& name,
1671 ast::StructMemberList members,
1672 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001673 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001674 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001675 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001676 AST().AddConstructedType(type);
1677 return type;
1678 }
1679
Ben Clayton42d1e092021-02-02 14:29:15 +00001680 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001681 /// @param source the source information
1682 /// @param name the struct member name
1683 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001684 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001685 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001686 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001687 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001688 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001689 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001690 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001691 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001692 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1693 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001694 }
1695
Ben Clayton42d1e092021-02-02 14:29:15 +00001696 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001697 /// @param name the struct member name
1698 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001699 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001700 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001701 template <typename NAME>
1702 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001703 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001704 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001705 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001706 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1707 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001708 }
1709
Ben Claytonbab31972021-02-08 21:16:21 +00001710 /// Creates a ast::StructMember with the given byte offset
1711 /// @param offset the offset to use in the StructMemberOffsetDecoration
1712 /// @param name the struct member name
1713 /// @param type the struct member type
1714 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001715 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001716 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001717 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001718 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001719 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001720 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001721 create<ast::StructMemberOffsetDecoration>(offset),
1722 });
1723 }
1724
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001725 /// Creates a ast::BlockStatement with input statements
1726 /// @param statements statements of block
1727 /// @returns the block statement pointer
1728 template <typename... Statements>
1729 ast::BlockStatement* Block(Statements&&... statements) {
1730 return create<ast::BlockStatement>(
1731 ast::StatementList{std::forward<Statements>(statements)...});
1732 }
1733
1734 /// Creates a ast::ElseStatement with input condition and body
1735 /// @param condition the else condition expression
1736 /// @param body the else body
1737 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001738 template <typename CONDITION>
1739 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1740 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1741 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001742 }
1743
1744 /// Creates a ast::IfStatement with input condition, body, and optional
1745 /// variadic else statements
1746 /// @param condition the if statement condition expression
1747 /// @param body the if statement body
1748 /// @param elseStatements optional variadic else statements
1749 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001750 template <typename CONDITION, typename... ELSE_STATEMENTS>
1751 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001752 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001753 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001754 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001755 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001756 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001757 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001758 }
1759
1760 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001761 /// @param source the source information
1762 /// @param lhs the left hand side expression initializer
1763 /// @param rhs the right hand side expression initializer
1764 /// @returns the assignment statement pointer
1765 template <typename LhsExpressionInit, typename RhsExpressionInit>
1766 ast::AssignmentStatement* Assign(const Source& source,
1767 LhsExpressionInit&& lhs,
1768 RhsExpressionInit&& rhs) {
1769 return create<ast::AssignmentStatement>(
1770 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1771 Expr(std::forward<RhsExpressionInit>(rhs)));
1772 }
1773
1774 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001775 /// @param lhs the left hand side expression initializer
1776 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001777 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001778 template <typename LhsExpressionInit, typename RhsExpressionInit>
1779 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1780 RhsExpressionInit&& rhs) {
1781 return create<ast::AssignmentStatement>(
1782 Expr(std::forward<LhsExpressionInit>(lhs)),
1783 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001784 }
1785
1786 /// Creates a ast::LoopStatement with input body and optional continuing
1787 /// @param body the loop body
1788 /// @param continuing the optional continuing block
1789 /// @returns the loop statement pointer
1790 ast::LoopStatement* Loop(ast::BlockStatement* body,
1791 ast::BlockStatement* continuing = nullptr) {
1792 return create<ast::LoopStatement>(body, continuing);
1793 }
1794
1795 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001796 /// @param source the source information
1797 /// @param var the variable to wrap in a decl statement
1798 /// @returns the variable decl statement pointer
1799 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1800 return create<ast::VariableDeclStatement>(source, var);
1801 }
1802
1803 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001804 /// @param var the variable to wrap in a decl statement
1805 /// @returns the variable decl statement pointer
1806 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1807 return create<ast::VariableDeclStatement>(var);
1808 }
1809
Antonio Maioranocea744d2021-03-25 12:55:27 +00001810 /// Creates a ast::SwitchStatement with input expression and cases
1811 /// @param condition the condition expression initializer
1812 /// @param cases case statements
1813 /// @returns the switch statement pointer
1814 template <typename ExpressionInit, typename... Cases>
1815 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1816 return create<ast::SwitchStatement>(
1817 Expr(std::forward<ExpressionInit>(condition)),
1818 ast::CaseStatementList{std::forward<Cases>(cases)...});
1819 }
1820
1821 /// Creates a ast::CaseStatement with input list of selectors, and body
1822 /// @param selectors list of selectors
1823 /// @param body the case body
1824 /// @returns the case statement pointer
1825 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1826 ast::BlockStatement* body = nullptr) {
1827 return create<ast::CaseStatement>(std::move(selectors),
1828 body ? body : Block());
1829 }
1830
1831 /// Convenient overload that takes a single selector
1832 /// @param selector a single case selector
1833 /// @param body the case body
1834 /// @returns the case statement pointer
1835 ast::CaseStatement* Case(ast::IntLiteral* selector,
1836 ast::BlockStatement* body = nullptr) {
1837 return Case(ast::CaseSelectorList{selector}, body);
1838 }
1839
1840 /// Convenience function that creates a 'default' ast::CaseStatement
1841 /// @param body the case body
1842 /// @returns the case statement pointer
1843 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1844 return Case(ast::CaseSelectorList{}, body);
1845 }
1846
James Price68f558f2021-04-06 15:51:47 +00001847 /// Creates an ast::BuiltinDecoration
1848 /// @param source the source information
1849 /// @param builtin the builtin value
1850 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001851 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001852 return create<ast::BuiltinDecoration>(source, builtin);
1853 }
1854
1855 /// Creates an ast::BuiltinDecoration
1856 /// @param builtin the builtin value
1857 /// @returns the builtin decoration pointer
1858 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1859 return create<ast::BuiltinDecoration>(source_, builtin);
1860 }
1861
1862 /// Creates an ast::LocationDecoration
1863 /// @param source the source information
1864 /// @param location the location value
1865 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001866 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001867 return create<ast::LocationDecoration>(source, location);
1868 }
1869
1870 /// Creates an ast::LocationDecoration
1871 /// @param location the location value
1872 /// @returns the location decoration pointer
1873 ast::LocationDecoration* Location(uint32_t location) {
1874 return create<ast::LocationDecoration>(source_, location);
1875 }
1876
James Pricef2f3bfc2021-05-13 20:32:32 +00001877 /// Creates an ast::OverrideDecoration with a specific constant ID
1878 /// @param source the source information
1879 /// @param id the id value
1880 /// @returns the override decoration pointer
1881 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1882 return create<ast::OverrideDecoration>(source, id);
1883 }
1884
1885 /// Creates an ast::OverrideDecoration with a specific constant ID
1886 /// @param id the optional id value
1887 /// @returns the override decoration pointer
1888 ast::OverrideDecoration* Override(uint32_t id) {
1889 return Override(source_, id);
1890 }
1891
1892 /// Creates an ast::OverrideDecoration without a constant ID
1893 /// @param source the source information
1894 /// @returns the override decoration pointer
1895 ast::OverrideDecoration* Override(const Source& source) {
1896 return create<ast::OverrideDecoration>(source);
1897 }
1898
1899 /// Creates an ast::OverrideDecoration without a constant ID
1900 /// @returns the override decoration pointer
1901 ast::OverrideDecoration* Override() { return Override(source_); }
1902
James Price68f558f2021-04-06 15:51:47 +00001903 /// Creates an ast::StageDecoration
1904 /// @param source the source information
1905 /// @param stage the pipeline stage
1906 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001907 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001908 return create<ast::StageDecoration>(source, stage);
1909 }
1910
1911 /// Creates an ast::StageDecoration
1912 /// @param stage the pipeline stage
1913 /// @returns the stage decoration pointer
1914 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1915 return create<ast::StageDecoration>(source_, stage);
1916 }
1917
James Price70f80bb2021-05-19 13:40:08 +00001918 /// Creates an ast::WorkgroupDecoration
1919 /// @param x the x dimension expression
1920 /// @returns the workgroup decoration pointer
1921 template <typename EXPR_X>
1922 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1923 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1924 }
1925
1926 /// Creates an ast::WorkgroupDecoration
1927 /// @param x the x dimension expression
1928 /// @param y the y dimension expression
1929 /// @returns the workgroup decoration pointer
1930 template <typename EXPR_X, typename EXPR_Y>
1931 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1932 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1933 nullptr);
1934 }
1935
1936 /// Creates an ast::WorkgroupDecoration
1937 /// @param x the x dimension expression
1938 /// @param y the y dimension expression
1939 /// @param z the z dimension expression
1940 /// @returns the workgroup decoration pointer
1941 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1942 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1943 return create<ast::WorkgroupDecoration>(
1944 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1945 Expr(std::forward<EXPR_Z>(z)));
1946 }
1947
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001948 /// Sets the current builder source to `src`
1949 /// @param src the Source used for future create() calls
1950 void SetSource(const Source& src) {
1951 AssertNotMoved();
1952 source_ = src;
1953 }
1954
1955 /// Sets the current builder source to `loc`
1956 /// @param loc the Source used for future create() calls
1957 void SetSource(const Source::Location& loc) {
1958 AssertNotMoved();
1959 source_ = Source(loc);
1960 }
1961
Ben Clayton33352542021-01-29 16:43:41 +00001962 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001963 /// @note As the Resolver is run when the Program is built, this will only be
1964 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001965 /// @param expr the AST expression
1966 /// @return the resolved semantic type for the expression, or nullptr if the
1967 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001968 sem::Type* TypeOf(const ast::Expression* expr) const;
1969
Ben Claytonfe0910f2021-05-17 15:51:47 +00001970 /// Helper for returning the resolved semantic type of the variable `var`.
1971 /// @note As the Resolver is run when the Program is built, this will only be
1972 /// useful for the Resolver itself and tests that use their own Resolver.
1973 /// @param var the AST variable
1974 /// @return the resolved semantic type for the variable, or nullptr if the
1975 /// variable has no resolved type.
1976 sem::Type* TypeOf(const ast::Variable* var) const;
1977
Ben Claytonfbec46f2021-04-30 20:20:19 +00001978 /// Helper for returning the resolved semantic type of the AST type `type`.
1979 /// @note As the Resolver is run when the Program is built, this will only be
1980 /// useful for the Resolver itself and tests that use their own Resolver.
1981 /// @param expr the AST type
1982 /// @return the resolved semantic type for the type, or nullptr if the type
1983 /// has no resolved type.
1984 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001985
Ben Clayton169512e2021-04-17 05:52:11 +00001986 /// Wraps the ast::Literal in a statement. This is used by tests that
1987 /// construct a partial AST and require the Resolver to reach these
1988 /// nodes.
1989 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1990 /// @return the ast::Statement that wraps the ast::Statement
1991 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001992 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001993 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001994 /// nodes.
1995 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1996 /// @return the ast::Statement that wraps the ast::Expression
1997 ast::Statement* WrapInStatement(ast::Expression* expr);
1998 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001999 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002000 /// these nodes.
2001 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2002 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2003 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2004 /// Returns the statement argument. Used as a passthrough-overload by
2005 /// WrapInFunction().
2006 /// @param stmt the ast::Statement
2007 /// @return `stmt`
2008 ast::Statement* WrapInStatement(ast::Statement* stmt);
2009 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002010 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002011 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002012 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002013 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002014 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002015 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002016 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002017 }
2018 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002019 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002020 /// @returns the function
2021 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002022
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002023 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002024 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002025
2026 protected:
2027 /// Asserts that the builder has not been moved.
2028 void AssertNotMoved() const;
2029
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002030 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002031 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002032 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002033 ASTNodeAllocator ast_nodes_;
2034 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002035 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002036 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002037 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002038 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002039
2040 /// The source to use when creating AST nodes without providing a Source as
2041 /// the first argument.
2042 Source source_;
2043
Ben Clayton5f0ea112021-03-09 10:54:37 +00002044 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002045 /// program when built.
2046 bool resolve_on_build_ = true;
2047
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002048 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2049 bool moved_ = false;
2050};
2051
2052//! @cond Doxygen_Suppress
2053// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2054template <>
2055struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002056 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002057 return t->i32();
2058 }
2059};
2060template <>
2061struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002062 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002063 return t->u32();
2064 }
2065};
2066template <>
2067struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002068 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002069 return t->f32();
2070 }
2071};
2072template <>
2073struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002074 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002075 return t->bool_();
2076 }
2077};
2078template <>
2079struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002080 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002081 return t->void_();
2082 }
2083};
2084//! @endcond
2085
Ben Clayton917b14b2021-04-19 16:50:23 +00002086/// @param builder the ProgramBuilder
2087/// @returns the ProgramID of the ProgramBuilder
2088inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2089 return builder->ID();
2090}
2091
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002092} // namespace tint
2093
2094#endif // SRC_PROGRAM_BUILDER_H_