blob: dddf340f8996c47de90bad3733fa73746c9a85d5 [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 Clayton58dec172021-05-19 17:47:11 +0000391 typ::U32 u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000392
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000393 /// @param source the Source of the node
394 /// @returns a u32 type
395 typ::U32 u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000396 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000397 }
398
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000399 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000400 typ::Void void_() const {
401 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
402 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000403
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000404 /// @param source the Source of the node
405 /// @returns a void type
406 typ::Void void_(const Source& source) const {
407 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
408 }
409
Antonio Maiorano25436862021-04-13 13:32:33 +0000410 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000411 /// @param n vector width in elements
412 /// @return the tint AST type for a `n`-element vector of `type`.
413 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000414 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000415 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
416 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000417 }
418
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000419 /// @param source the Source of the node
420 /// @param type vector subtype
421 /// @param n vector width in elements
422 /// @return the tint AST type for a `n`-element vector of `type`.
423 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000424 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000425 return {
426 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
427 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000428 }
429
Antonio Maiorano25436862021-04-13 13:32:33 +0000430 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000431 /// @return the tint AST type for a 2-element vector of `type`.
432 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
433
434 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000435 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000436 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000437
438 /// @param type vector subtype
439 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000440 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
441
442 /// @param n vector width in elements
443 /// @return the tint AST type for a `n`-element vector of `type`.
444 template <typename T>
445 typ::Vector vec(uint32_t n) const {
446 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000447 }
448
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000449 /// @return the tint AST type for a 2-element vector of the C type `T`.
450 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000451 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000452 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000453 }
454
455 /// @return the tint AST type for a 3-element vector of the C type `T`.
456 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000457 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000458 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000459 }
460
461 /// @return the tint AST type for a 4-element vector of the C type `T`.
462 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000463 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000464 return vec4(Of<T>());
465 }
466
467 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000468 /// @param columns number of columns for the matrix
469 /// @param rows number of rows for the matrix
470 /// @return the tint AST type for a matrix of `type`
471 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000472 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000473 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
474 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000475 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000476 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000477 }
478
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000479 /// @param source the Source of the node
480 /// @param type matrix subtype
481 /// @param columns number of columns for the matrix
482 /// @param rows number of rows for the matrix
483 /// @return the tint AST type for a matrix of `type`
484 typ::Matrix mat(const Source& source,
485 typ::Type type,
486 uint32_t columns,
487 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000488 return {type.ast
489 ? builder->create<ast::Matrix>(source, type, rows, columns)
490 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000491 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000492 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000493 }
494
Ben Claytone204f272021-04-22 14:40:23 +0000495 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000496 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000497 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000498
499 /// @param type matrix subtype
500 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000501 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000502
503 /// @param type matrix subtype
504 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000505 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000506
507 /// @param type matrix subtype
508 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000509 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000510
511 /// @param type matrix subtype
512 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000513 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000514
515 /// @param type matrix subtype
516 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000517 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000518
519 /// @param type matrix subtype
520 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000521 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000522
523 /// @param type matrix subtype
524 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000525 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000526
527 /// @param type matrix subtype
528 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000529 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000530
Ben Claytone204f272021-04-22 14:40:23 +0000531 /// @param columns number of columns for the matrix
532 /// @param rows number of rows for the matrix
533 /// @return the tint AST type for a matrix of `type`
534 template <typename T>
535 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
536 return mat(Of<T>(), columns, rows);
537 }
538
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000539 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
540 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000541 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000542 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000543 }
544
545 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
546 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000547 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000548 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000549 }
550
551 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
552 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000553 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000554 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 }
556
557 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
558 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000559 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000560 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 }
562
563 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
564 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000565 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000566 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 }
568
569 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
570 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000571 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000572 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
575 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
576 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000577 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000578 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 }
580
581 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
582 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000583 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000584 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000585 }
586
587 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
588 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000589 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000590 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000591 }
592
593 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000594 /// @param n the array size. 0 represents a runtime-array
595 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000596 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000597 ast::Array* array(typ::Type subtype,
598 uint32_t n = 0,
599 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000600 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000601 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000602 }
603
604 /// @param source the Source of the node
605 /// @param subtype the array element type
606 /// @param n the array size. 0 represents a runtime-array
607 /// @param decos the optional decorations for the array
608 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000609 ast::Array* array(const Source& source,
610 typ::Type subtype,
611 uint32_t n = 0,
612 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000613 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000614 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000615 }
616
Ben Claytonbab31972021-02-08 21:16:21 +0000617 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000618 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000619 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000620 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000621 ast::Array* array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000622 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000623 ast::DecorationList decos;
624 if (stride) {
625 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
626 }
627 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000628 }
629
Ben Clayton0f88b312021-05-04 17:36:31 +0000630 /// @param source the Source of the node
631 /// @param subtype the array element type
632 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000633 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000634 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000635 ast::Array* array(const Source& source,
636 typ::Type subtype,
637 uint32_t n,
638 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000639 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000640 ast::DecorationList decos;
641 if (stride) {
642 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
643 }
644 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000645 }
646
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000647 /// @return the tint AST type for an array of size `N` of type `T`
648 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000649 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000650 return array(Of<T>(), N);
651 }
652
Ben Claytonbab31972021-02-08 21:16:21 +0000653 /// @param stride the array stride
654 /// @return the tint AST type for an array of size `N` of type `T`
655 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000656 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000657 return array(Of<T>(), N, stride);
658 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000659
Ben Clayton0f88b312021-05-04 17:36:31 +0000660 /// Creates a type name
661 /// @param name the name
662 /// @returns the type name
663 template <typename NAME>
664 ast::TypeName* type_name(NAME&& name) const {
665 return builder->create<ast::TypeName>(
666 builder->Sym(std::forward<NAME>(name)));
667 }
668
669 /// Creates a type name
670 /// @param source the Source of the node
671 /// @param name the name
672 /// @returns the type name
673 template <typename NAME>
674 ast::TypeName* type_name(const Source& source, NAME&& name) const {
675 return builder->create<ast::TypeName>(
676 source, builder->Sym(std::forward<NAME>(name)));
677 }
678
Ben Clayton42d1e092021-02-02 14:29:15 +0000679 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000680 /// @param name the alias name
681 /// @param type the alias type
682 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000683 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000684 ast::Alias* alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000685 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000686 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000687 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000688 }
689
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000690 /// Creates an alias type
691 /// @param source the Source of the node
692 /// @param name the alias name
693 /// @param type the alias type
694 /// @returns the alias pointer
695 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000696 ast::Alias* alias(const Source& source, NAME&& name, typ::Type type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000697 type = MaybeCreateTypename(type);
698 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000699 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000700 }
701
Ben Clayton4db10dd2021-04-16 08:47:14 +0000702 /// Creates an access control qualifier type
703 /// @param access the access control
704 /// @param type the inner type
705 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000706 ast::AccessControl* access(ast::AccessControl::Access access,
707 const ast::Type* type) const {
708 type = MaybeCreateTypename(type).ast;
709 return type ? builder->create<ast::AccessControl>(access, type) : nullptr;
Ben Clayton4db10dd2021-04-16 08:47:14 +0000710 }
711
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000712 /// Creates an access control qualifier type
713 /// @param source the Source of the node
714 /// @param access the access control
715 /// @param type the inner type
716 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000717 ast::AccessControl* access(const Source& source,
718 ast::AccessControl::Access access,
719 const ast::Type* type) const {
720 type = MaybeCreateTypename(type).ast;
721 return type ? builder->create<ast::AccessControl>(source, access, type)
722 : nullptr;
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000723 }
724
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000725 /// @param type the type of the pointer
726 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000727 /// @return the pointer to `type` with the given ast::StorageClass
728 typ::Pointer pointer(typ::Type type,
729 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000730 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000731 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
732 : nullptr,
733 type.sem ? builder->create<sem::Pointer>(type, storage_class)
734 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000735 }
736
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000737 /// @param source the Source of the node
738 /// @param type the type of the pointer
739 /// @param storage_class the storage class of the pointer
740 /// @return the pointer to `type` with the given ast::StorageClass
741 typ::Pointer pointer(const Source& source,
742 typ::Type type,
743 ast::StorageClass storage_class) const {
744 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000745 return {type.ast
746 ? builder->create<ast::Pointer>(source, type, storage_class)
747 : nullptr,
748 type.sem ? builder->create<sem::Pointer>(type, storage_class)
749 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000750 }
751
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000752 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000753 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000754 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000755 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000756 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000757 }
758
Ben Claytone204f272021-04-22 14:40:23 +0000759 /// @param kind the kind of sampler
760 /// @returns the sampler
761 typ::Sampler sampler(ast::SamplerKind kind) const {
762 return {builder->create<ast::Sampler>(kind),
763 builder->create<sem::Sampler>(kind)};
764 }
765
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000766 /// @param source the Source of the node
767 /// @param kind the kind of sampler
768 /// @returns the sampler
769 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
770 return {builder->create<ast::Sampler>(source, kind),
771 builder->create<sem::Sampler>(kind)};
772 }
773
Ben Claytone204f272021-04-22 14:40:23 +0000774 /// @param dims the dimensionality of the texture
775 /// @returns the depth texture
776 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
777 return {builder->create<ast::DepthTexture>(dims),
778 builder->create<sem::DepthTexture>(dims)};
779 }
780
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000781 /// @param source the Source of the node
782 /// @param dims the dimensionality of the texture
783 /// @returns the depth texture
784 typ::DepthTexture depth_texture(const Source& source,
785 ast::TextureDimension dims) const {
786 return {builder->create<ast::DepthTexture>(source, dims),
787 builder->create<sem::DepthTexture>(dims)};
788 }
789
Ben Claytone204f272021-04-22 14:40:23 +0000790 /// @param dims the dimensionality of the texture
791 /// @param subtype the texture subtype.
792 /// @returns the sampled texture
793 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
794 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000795 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
796 : nullptr,
797 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
798 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000799 }
800
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000801 /// @param source the Source of the node
802 /// @param dims the dimensionality of the texture
803 /// @param subtype the texture subtype.
804 /// @returns the sampled texture
805 typ::SampledTexture sampled_texture(const Source& source,
806 ast::TextureDimension dims,
807 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000808 return {subtype.ast
809 ? builder->create<ast::SampledTexture>(source, dims, subtype)
810 : nullptr,
811 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
812 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000813 }
814
Ben Claytone204f272021-04-22 14:40:23 +0000815 /// @param dims the dimensionality of the texture
816 /// @param subtype the texture subtype.
817 /// @returns the multisampled texture
818 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
819 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000820 return {
821 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
822 : nullptr,
823 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
824 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000825 }
826
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000827 /// @param source the Source of the node
828 /// @param dims the dimensionality of the texture
829 /// @param subtype the texture subtype.
830 /// @returns the multisampled texture
831 typ::MultisampledTexture multisampled_texture(const Source& source,
832 ast::TextureDimension dims,
833 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000834 return {
835 subtype.ast
836 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
837 : nullptr,
838 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
839 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000840 }
841
Ben Claytone204f272021-04-22 14:40:23 +0000842 /// @param dims the dimensionality of the texture
843 /// @param format the image format of the texture
844 /// @returns the storage texture
845 typ::StorageTexture storage_texture(ast::TextureDimension dims,
846 ast::ImageFormat format) const {
847 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
848 auto* sem_subtype =
849 sem::StorageTexture::SubtypeFor(format, builder->Types());
850 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000851 builder->create<sem::StorageTexture>(
852 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000853 }
854
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000855 /// @param source the Source of the node
856 /// @param dims the dimensionality of the texture
857 /// @param format the image format of the texture
858 /// @returns the storage texture
859 typ::StorageTexture storage_texture(const Source& source,
860 ast::TextureDimension dims,
861 ast::ImageFormat format) const {
862 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
863 auto* sem_subtype =
864 sem::StorageTexture::SubtypeFor(format, builder->Types());
865 return {builder->create<ast::StorageTexture>(source, dims, format,
866 ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000867 builder->create<sem::StorageTexture>(
868 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000869 }
870
Brandon Jones7b257692021-05-17 17:40:17 +0000871 /// @returns the external texture
872 typ::ExternalTexture external_texture() const {
873 return {builder->create<ast::ExternalTexture>(),
874 builder->create<sem::ExternalTexture>()};
875 }
876
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000877 /// @param source the Source of the node
878 /// @returns the external texture
879 typ::ExternalTexture external_texture(const Source& source) const {
880 return {builder->create<ast::ExternalTexture>(source),
881 builder->create<sem::ExternalTexture>()};
882 }
883
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000884 /// If ty is a ast::Struct or ast::Alias, the returned type is an
885 /// ast::TypeName of the given type's name, otherwise type is returned.
886 /// @param type the type
887 /// @return either type or a pointer to a new ast::TypeName
888 typ::Type MaybeCreateTypename(typ::Type type) const;
889
Ben Claytonf5f311e2021-04-28 14:31:23 +0000890 /// The ProgramBuilder
891 ProgramBuilder* const builder;
892
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000893 private:
894 /// CToAST<T> is specialized for various `T` types and each specialization
895 /// contains a single static `get()` method for obtaining the corresponding
896 /// AST type for the C type `T`.
897 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000898 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 template <typename T>
900 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000901 };
902
903 //////////////////////////////////////////////////////////////////////////////
904 // AST helper methods
905 //////////////////////////////////////////////////////////////////////////////
906
James Price65ae64d2021-04-29 12:59:14 +0000907 /// @return a new unnamed symbol
908 Symbol Sym() { return Symbols().New(); }
909
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000910 /// @param name the symbol string
911 /// @return a Symbol with the given name
912 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
913
914 /// @param sym the symbol
915 /// @return `sym`
916 Symbol Sym(Symbol sym) { return sym; }
917
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000918 /// @param expr the expression
919 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000920 template <typename T>
921 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
922 return expr;
923 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000924
Ben Claytonb8ea5912021-04-19 16:52:42 +0000925 /// Passthrough for nullptr
926 /// @return nullptr
927 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
928
Ben Claytonfe0910f2021-05-17 15:51:47 +0000929 /// @param source the source information
930 /// @param symbol the identifier symbol
931 /// @return an ast::IdentifierExpression with the given symbol
932 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
933 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000934 }
935
Ben Clayton46d78d72021-02-10 21:34:26 +0000936 /// @param symbol the identifier symbol
937 /// @return an ast::IdentifierExpression with the given symbol
938 ast::IdentifierExpression* Expr(Symbol symbol) {
939 return create<ast::IdentifierExpression>(symbol);
940 }
941
Ben Claytonfe0910f2021-05-17 15:51:47 +0000942 /// @param source the source information
943 /// @param variable the AST variable
944 /// @return an ast::IdentifierExpression with the variable's symbol
945 ast::IdentifierExpression* Expr(const Source& source,
946 ast::Variable* variable) {
947 return create<ast::IdentifierExpression>(source, variable->symbol());
948 }
949
Ben Claytonb8ea5912021-04-19 16:52:42 +0000950 /// @param variable the AST variable
951 /// @return an ast::IdentifierExpression with the variable's symbol
952 ast::IdentifierExpression* Expr(ast::Variable* variable) {
953 return create<ast::IdentifierExpression>(variable->symbol());
954 }
955
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000956 /// @param source the source information
957 /// @param name the identifier name
958 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000959 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
960 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
961 }
962
963 /// @param name the identifier name
964 /// @return an ast::IdentifierExpression with the given name
965 ast::IdentifierExpression* Expr(const char* name) {
966 return create<ast::IdentifierExpression>(Symbols().Register(name));
967 }
968
969 /// @param source the source information
970 /// @param name the identifier name
971 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000972 ast::IdentifierExpression* Expr(const Source& source,
973 const std::string& name) {
974 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
975 }
976
977 /// @param name the identifier name
978 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000979 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000980 return create<ast::IdentifierExpression>(Symbols().Register(name));
981 }
982
Ben Claytonfe0910f2021-05-17 15:51:47 +0000983 /// @param source the source information
984 /// @param value the boolean value
985 /// @return a Scalar constructor for the given value
986 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
987 return create<ast::ScalarConstructorExpression>(source, Literal(value));
988 }
989
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000990 /// @param value the boolean value
991 /// @return a Scalar constructor for the given value
992 ast::ScalarConstructorExpression* Expr(bool value) {
993 return create<ast::ScalarConstructorExpression>(Literal(value));
994 }
995
Ben Claytonfe0910f2021-05-17 15:51:47 +0000996 /// @param source the source information
997 /// @param value the float value
998 /// @return a Scalar constructor for the given value
999 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
1000 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1001 }
1002
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001003 /// @param value the float value
1004 /// @return a Scalar constructor for the given value
1005 ast::ScalarConstructorExpression* Expr(f32 value) {
1006 return create<ast::ScalarConstructorExpression>(Literal(value));
1007 }
1008
Ben Claytonfe0910f2021-05-17 15:51:47 +00001009 /// @param source the source information
1010 /// @param value the integer value
1011 /// @return a Scalar constructor for the given value
1012 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1013 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1014 }
1015
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001016 /// @param value the integer value
1017 /// @return a Scalar constructor for the given value
1018 ast::ScalarConstructorExpression* Expr(i32 value) {
1019 return create<ast::ScalarConstructorExpression>(Literal(value));
1020 }
1021
Ben Claytonfe0910f2021-05-17 15:51:47 +00001022 /// @param source the source information
1023 /// @param value the unsigned int value
1024 /// @return a Scalar constructor for the given value
1025 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1026 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1027 }
1028
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001029 /// @param value the unsigned int value
1030 /// @return a Scalar constructor for the given value
1031 ast::ScalarConstructorExpression* Expr(u32 value) {
1032 return create<ast::ScalarConstructorExpression>(Literal(value));
1033 }
1034
1035 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1036 /// `list`.
1037 /// @param list the list to append too
1038 /// @param arg the arg to create
1039 template <typename ARG>
1040 void Append(ast::ExpressionList& list, ARG&& arg) {
1041 list.emplace_back(Expr(std::forward<ARG>(arg)));
1042 }
1043
1044 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1045 /// then appends them to `list`.
1046 /// @param list the list to append too
1047 /// @param arg0 the first argument
1048 /// @param args the rest of the arguments
1049 template <typename ARG0, typename... ARGS>
1050 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1051 Append(list, std::forward<ARG0>(arg0));
1052 Append(list, std::forward<ARGS>(args)...);
1053 }
1054
1055 /// @return an empty list of expressions
1056 ast::ExpressionList ExprList() { return {}; }
1057
1058 /// @param args the list of expressions
1059 /// @return the list of expressions converted to `ast::Expression`s using
1060 /// `Expr()`,
1061 template <typename... ARGS>
1062 ast::ExpressionList ExprList(ARGS&&... args) {
1063 ast::ExpressionList list;
1064 list.reserve(sizeof...(args));
1065 Append(list, std::forward<ARGS>(args)...);
1066 return list;
1067 }
1068
1069 /// @param list the list of expressions
1070 /// @return `list`
1071 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1072
1073 /// @param val the boolan value
1074 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001075 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001076
1077 /// @param val the float value
1078 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001079 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001080
1081 /// @param val the unsigned int value
1082 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001083 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001084
1085 /// @param val the integer value
1086 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001087 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001088
1089 /// @param args the arguments for the type constructor
1090 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1091 /// of `args` converted to `ast::Expression`s using `Expr()`
1092 template <typename T, typename... ARGS>
1093 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001094 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001095 }
1096
1097 /// @param type the type to construct
1098 /// @param args the arguments for the constructor
1099 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1100 /// values `args`.
1101 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001102 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001103 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001104 return create<ast::TypeConstructorExpression>(
1105 type, ExprList(std::forward<ARGS>(args)...));
1106 }
1107
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001108 /// Creates a constructor expression that constructs an object of
1109 /// `type` filled with `elem_value`. For example,
1110 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1111 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1112 /// @param type the type to construct
1113 /// @param elem_value the initial or element value (for vec and mat) to
1114 /// construct with
1115 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001116 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001117 int elem_value = 0);
1118
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001119 /// @param args the arguments for the vector constructor
1120 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1121 /// `T`, constructed with the values `args`.
1122 template <typename T, typename... ARGS>
1123 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001124 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001125 }
1126
1127 /// @param args the arguments for the vector constructor
1128 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1129 /// `T`, constructed with the values `args`.
1130 template <typename T, typename... ARGS>
1131 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001132 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001133 }
1134
1135 /// @param args the arguments for the vector constructor
1136 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1137 /// `T`, constructed with the values `args`.
1138 template <typename T, typename... ARGS>
1139 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001140 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001141 }
1142
1143 /// @param args the arguments for the matrix constructor
1144 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1145 /// `T`, constructed with the values `args`.
1146 template <typename T, typename... ARGS>
1147 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001148 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001149 }
1150
1151 /// @param args the arguments for the matrix constructor
1152 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1153 /// `T`, constructed with the values `args`.
1154 template <typename T, typename... ARGS>
1155 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001156 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001157 }
1158
1159 /// @param args the arguments for the matrix constructor
1160 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1161 /// `T`, constructed with the values `args`.
1162 template <typename T, typename... ARGS>
1163 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001164 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001165 }
1166
1167 /// @param args the arguments for the matrix constructor
1168 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1169 /// `T`, constructed with the values `args`.
1170 template <typename T, typename... ARGS>
1171 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001172 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001173 }
1174
1175 /// @param args the arguments for the matrix constructor
1176 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1177 /// `T`, constructed with the values `args`.
1178 template <typename T, typename... ARGS>
1179 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001180 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181 }
1182
1183 /// @param args the arguments for the matrix constructor
1184 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1185 /// `T`, constructed with the values `args`.
1186 template <typename T, typename... ARGS>
1187 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001188 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001189 }
1190
1191 /// @param args the arguments for the matrix constructor
1192 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1193 /// `T`, constructed with the values `args`.
1194 template <typename T, typename... ARGS>
1195 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001196 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001197 }
1198
1199 /// @param args the arguments for the matrix constructor
1200 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1201 /// `T`, constructed with the values `args`.
1202 template <typename T, typename... ARGS>
1203 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001204 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001205 }
1206
1207 /// @param args the arguments for the matrix constructor
1208 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1209 /// `T`, constructed with the values `args`.
1210 template <typename T, typename... ARGS>
1211 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001212 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001213 }
1214
1215 /// @param args the arguments for the array constructor
1216 /// @return an `ast::TypeConstructorExpression` of an array with element type
1217 /// `T`, constructed with the values `args`.
1218 template <typename T, int N = 0, typename... ARGS>
1219 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001220 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001221 }
1222
1223 /// @param subtype the array element type
1224 /// @param n the array size. 0 represents a runtime-array.
1225 /// @param args the arguments for the array constructor
1226 /// @return an `ast::TypeConstructorExpression` of an array with element type
1227 /// `subtype`, constructed with the values `args`.
1228 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001229 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230 uint32_t n,
1231 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001232 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001233 }
1234
1235 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001236 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001237 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238 /// @param constructor constructor expression
1239 /// @param decorations variable decorations
1240 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001241 template <typename NAME>
1242 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001243 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001244 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001245 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001246 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001247 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001248 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1249 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001250 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001251
1252 /// @param source the variable source
1253 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001254 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001255 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001256 /// @param constructor constructor expression
1257 /// @param decorations variable decorations
1258 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001259 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001260 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001261 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001262 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001263 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001264 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001265 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001266 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001267 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001268 type, false, constructor, decorations);
1269 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001270
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001271 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001272 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001273 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001274 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001275 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001276 template <typename NAME>
1277 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001278 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001279 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001280 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001281 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001282 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001283 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001284 constructor, decorations);
1285 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001286
1287 /// @param source the variable source
1288 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001289 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001290 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001291 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001292 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001293 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001294 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001295 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001296 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001297 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001298 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001299 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001300 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001301 ast::StorageClass::kNone, type, true,
1302 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001303 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001304
James Pricedaf1f1c2021-04-08 22:15:48 +00001305 /// @param name the parameter name
1306 /// @param type the parameter type
1307 /// @param decorations optional parameter decorations
1308 /// @returns a constant `ast::Variable` with the given name and type
1309 template <typename NAME>
1310 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001311 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001312 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001313 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001314 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1315 ast::StorageClass::kNone, type, true, nullptr,
1316 decorations);
1317 }
1318
1319 /// @param source the parameter source
1320 /// @param name the parameter name
1321 /// @param type the parameter type
1322 /// @param decorations optional parameter decorations
1323 /// @returns a constant `ast::Variable` with the given name and type
1324 template <typename NAME>
1325 ast::Variable* Param(const Source& source,
1326 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001327 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001328 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001329 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001330 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1331 ast::StorageClass::kNone, type, true, nullptr,
1332 decorations);
1333 }
1334
Ben Clayton42708342021-04-21 17:55:12 +00001335 /// @param name the variable name
1336 /// @param type the variable type
1337 /// @param storage the variable storage class
1338 /// @param constructor constructor expression
1339 /// @param decorations variable decorations
1340 /// @returns a new `ast::Variable`, which is automatically registered as a
1341 /// global variable with the ast::Module.
1342 template <typename NAME>
1343 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001344 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001345 ast::StorageClass storage,
1346 ast::Expression* constructor = nullptr,
1347 ast::DecorationList decorations = {}) {
1348 auto* var =
1349 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1350 AST().AddGlobalVariable(var);
1351 return var;
1352 }
1353
1354 /// @param source the variable source
1355 /// @param name the variable name
1356 /// @param type the variable type
1357 /// @param storage the variable storage class
1358 /// @param constructor constructor expression
1359 /// @param decorations variable decorations
1360 /// @returns a new `ast::Variable`, which is automatically registered as a
1361 /// global variable with the ast::Module.
1362 template <typename NAME>
1363 ast::Variable* Global(const Source& source,
1364 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001365 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001366 ast::StorageClass storage,
1367 ast::Expression* constructor = nullptr,
1368 ast::DecorationList decorations = {}) {
1369 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1370 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001371 AST().AddGlobalVariable(var);
1372 return var;
1373 }
1374
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001375 /// @param name the variable name
1376 /// @param type the variable type
1377 /// @param constructor constructor expression
1378 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001379 /// @returns a const `ast::Variable` constructed by calling Var() with the
1380 /// arguments of `args`, which is automatically registered as a global
1381 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001382 template <typename NAME>
1383 ast::Variable* GlobalConst(NAME&& name,
1384 typ::Type type,
1385 ast::Expression* constructor,
1386 ast::DecorationList decorations = {}) {
1387 auto* var = Const(std::forward<NAME>(name), type, constructor,
1388 std::move(decorations));
1389 AST().AddGlobalVariable(var);
1390 return var;
1391 }
1392
1393 /// @param source the variable source
1394 /// @param name the variable name
1395 /// @param type the variable type
1396 /// @param constructor constructor expression
1397 /// @param decorations optional variable decorations
1398 /// @returns a const `ast::Variable` constructed by calling Var() with the
1399 /// arguments of `args`, which is automatically registered as a global
1400 /// variable with the ast::Module.
1401 template <typename NAME>
1402 ast::Variable* GlobalConst(const Source& source,
1403 NAME&& name,
1404 typ::Type type,
1405 ast::Expression* constructor,
1406 ast::DecorationList decorations = {}) {
1407 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1408 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001409 AST().AddGlobalVariable(var);
1410 return var;
1411 }
1412
Ben Claytonfe0910f2021-05-17 15:51:47 +00001413 /// @param source the source information
1414 /// @param expr the expression to take the address of
1415 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1416 template <typename EXPR>
1417 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1418 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1419 Expr(std::forward<EXPR>(expr)));
1420 }
1421
1422 /// @param expr the expression to take the address of
1423 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1424 template <typename EXPR>
1425 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1426 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1427 Expr(std::forward<EXPR>(expr)));
1428 }
1429
1430 /// @param source the source information
1431 /// @param expr the expression to perform an indirection on
1432 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1433 template <typename EXPR>
1434 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1435 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1436 Expr(std::forward<EXPR>(expr)));
1437 }
1438
1439 /// @param expr the expression to perform an indirection on
1440 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1441 template <typename EXPR>
1442 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1443 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1444 Expr(std::forward<EXPR>(expr)));
1445 }
1446
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001447 /// @param func the function name
1448 /// @param args the function call arguments
1449 /// @returns a `ast::CallExpression` to the function `func`, with the
1450 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1451 template <typename NAME, typename... ARGS>
1452 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1453 return create<ast::CallExpression>(Expr(func),
1454 ExprList(std::forward<ARGS>(args)...));
1455 }
1456
1457 /// @param lhs the left hand argument to the addition operation
1458 /// @param rhs the right hand argument to the addition operation
1459 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1460 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001461 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001462 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1463 Expr(std::forward<LHS>(lhs)),
1464 Expr(std::forward<RHS>(rhs)));
1465 }
1466
1467 /// @param lhs the left hand argument to the subtraction operation
1468 /// @param rhs the right hand argument to the subtraction operation
1469 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1470 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001471 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001472 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1473 Expr(std::forward<LHS>(lhs)),
1474 Expr(std::forward<RHS>(rhs)));
1475 }
1476
1477 /// @param lhs the left hand argument to the multiplication operation
1478 /// @param rhs the right hand argument to the multiplication operation
1479 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1480 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001481 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001482 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1483 Expr(std::forward<LHS>(lhs)),
1484 Expr(std::forward<RHS>(rhs)));
1485 }
1486
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001487 /// @param source the source information
1488 /// @param lhs the left hand argument to the multiplication operation
1489 /// @param rhs the right hand argument to the multiplication operation
1490 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1491 template <typename LHS, typename RHS>
1492 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1493 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1494 Expr(std::forward<LHS>(lhs)),
1495 Expr(std::forward<RHS>(rhs)));
1496 }
1497
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001498 /// @param lhs the left hand argument to the division operation
1499 /// @param rhs the right hand argument to the division operation
1500 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1501 template <typename LHS, typename RHS>
1502 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1503 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1504 Expr(std::forward<LHS>(lhs)),
1505 Expr(std::forward<RHS>(rhs)));
1506 }
1507
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001508 /// @param arr the array argument for the array accessor expression
1509 /// @param idx the index argument for the array accessor expression
1510 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1511 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001512 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001513 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1514 Expr(std::forward<IDX>(idx)));
1515 }
1516
1517 /// @param obj the object for the member accessor expression
1518 /// @param idx the index argument for the array accessor expression
1519 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1520 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001521 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001522 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1523 Expr(std::forward<IDX>(idx)));
1524 }
1525
Ben Clayton42d1e092021-02-02 14:29:15 +00001526 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001527 /// @param val the offset value
1528 /// @returns the offset decoration pointer
1529 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1530 return create<ast::StructMemberOffsetDecoration>(source_, val);
1531 }
1532
Ben Claytond614dd52021-03-15 10:43:11 +00001533 /// Creates a ast::StructMemberSizeDecoration
1534 /// @param source the source information
1535 /// @param val the size value
1536 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001537 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1538 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001539 return create<ast::StructMemberSizeDecoration>(source, val);
1540 }
1541
1542 /// Creates a ast::StructMemberSizeDecoration
1543 /// @param val the size value
1544 /// @returns the size decoration pointer
1545 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1546 return create<ast::StructMemberSizeDecoration>(source_, val);
1547 }
1548
1549 /// Creates a ast::StructMemberAlignDecoration
1550 /// @param source the source information
1551 /// @param val the align value
1552 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001553 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1554 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001555 return create<ast::StructMemberAlignDecoration>(source, val);
1556 }
1557
1558 /// Creates a ast::StructMemberAlignDecoration
1559 /// @param val the align value
1560 /// @returns the align decoration pointer
1561 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1562 return create<ast::StructMemberAlignDecoration>(source_, val);
1563 }
1564
Ben Clayton42d1e092021-02-02 14:29:15 +00001565 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001566 /// @param source the source information
1567 /// @param name the function name
1568 /// @param params the function parameters
1569 /// @param type the function return type
1570 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001571 /// @param decorations the optional function decorations
1572 /// @param return_type_decorations the optional function return type
1573 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001574 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001575 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001576 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001577 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001578 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001579 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001580 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001581 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001582 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001583 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001584 auto* func =
1585 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1586 type, create<ast::BlockStatement>(body),
1587 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001588 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001589 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001590 }
1591
Ben Clayton42d1e092021-02-02 14:29:15 +00001592 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001593 /// @param name the function name
1594 /// @param params the function parameters
1595 /// @param type the function return type
1596 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001597 /// @param decorations the optional function decorations
1598 /// @param return_type_decorations the optional function return type
1599 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001600 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001601 template <typename NAME>
1602 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001603 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001604 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001605 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001606 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001607 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001608 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001609 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1610 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001611 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001612 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001613 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001614 }
1615
Ben Clayton49668bb2021-04-17 05:32:01 +00001616 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001617 /// @param source the source information
1618 /// @returns the return statement pointer
1619 ast::ReturnStatement* Return(const Source& source) {
1620 return create<ast::ReturnStatement>(source);
1621 }
1622
1623 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001624 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001625 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1626
1627 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001628 /// @param source the source information
1629 /// @param val the return value
1630 /// @returns the return statement pointer
1631 template <typename EXPR>
1632 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1633 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1634 }
1635
1636 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001637 /// @param val the return value
1638 /// @returns the return statement pointer
1639 template <typename EXPR>
1640 ast::ReturnStatement* Return(EXPR&& val) {
1641 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001642 }
1643
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001644 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001645 /// @param source the source information
1646 /// @param name the struct name
1647 /// @param members the struct members
1648 /// @param decorations the optional struct decorations
1649 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001650 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001651 ast::Struct* Structure(const Source& source,
1652 NAME&& name,
1653 ast::StructMemberList members,
1654 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001655 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001656 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001657 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001658 AST().AddConstructedType(type);
1659 return type;
1660 }
1661
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001662 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001663 /// @param name the struct name
1664 /// @param members the struct members
1665 /// @param decorations the optional struct decorations
1666 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001667 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001668 ast::Struct* Structure(NAME&& name,
1669 ast::StructMemberList members,
1670 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001671 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001672 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001673 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001674 AST().AddConstructedType(type);
1675 return type;
1676 }
1677
Ben Clayton42d1e092021-02-02 14:29:15 +00001678 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001679 /// @param source the source information
1680 /// @param name the struct member name
1681 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001682 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001683 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001684 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001685 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001686 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001687 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001688 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001689 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001690 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1691 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001692 }
1693
Ben Clayton42d1e092021-02-02 14:29:15 +00001694 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001695 /// @param name the struct member name
1696 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001697 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001698 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001699 template <typename NAME>
1700 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001701 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001702 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001703 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001704 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1705 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001706 }
1707
Ben Claytonbab31972021-02-08 21:16:21 +00001708 /// Creates a ast::StructMember with the given byte offset
1709 /// @param offset the offset to use in the StructMemberOffsetDecoration
1710 /// @param name the struct member name
1711 /// @param type the struct member type
1712 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001713 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001714 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001715 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001716 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001717 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001718 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001719 create<ast::StructMemberOffsetDecoration>(offset),
1720 });
1721 }
1722
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001723 /// Creates a ast::BlockStatement with input statements
1724 /// @param statements statements of block
1725 /// @returns the block statement pointer
1726 template <typename... Statements>
1727 ast::BlockStatement* Block(Statements&&... statements) {
1728 return create<ast::BlockStatement>(
1729 ast::StatementList{std::forward<Statements>(statements)...});
1730 }
1731
1732 /// Creates a ast::ElseStatement with input condition and body
1733 /// @param condition the else condition expression
1734 /// @param body the else body
1735 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001736 template <typename CONDITION>
1737 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1738 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1739 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001740 }
1741
1742 /// Creates a ast::IfStatement with input condition, body, and optional
1743 /// variadic else statements
1744 /// @param condition the if statement condition expression
1745 /// @param body the if statement body
1746 /// @param elseStatements optional variadic else statements
1747 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001748 template <typename CONDITION, typename... ELSE_STATEMENTS>
1749 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001750 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001751 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001752 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001753 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001754 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001755 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001756 }
1757
1758 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001759 /// @param source the source information
1760 /// @param lhs the left hand side expression initializer
1761 /// @param rhs the right hand side expression initializer
1762 /// @returns the assignment statement pointer
1763 template <typename LhsExpressionInit, typename RhsExpressionInit>
1764 ast::AssignmentStatement* Assign(const Source& source,
1765 LhsExpressionInit&& lhs,
1766 RhsExpressionInit&& rhs) {
1767 return create<ast::AssignmentStatement>(
1768 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1769 Expr(std::forward<RhsExpressionInit>(rhs)));
1770 }
1771
1772 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001773 /// @param lhs the left hand side expression initializer
1774 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001775 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001776 template <typename LhsExpressionInit, typename RhsExpressionInit>
1777 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1778 RhsExpressionInit&& rhs) {
1779 return create<ast::AssignmentStatement>(
1780 Expr(std::forward<LhsExpressionInit>(lhs)),
1781 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001782 }
1783
1784 /// Creates a ast::LoopStatement with input body and optional continuing
1785 /// @param body the loop body
1786 /// @param continuing the optional continuing block
1787 /// @returns the loop statement pointer
1788 ast::LoopStatement* Loop(ast::BlockStatement* body,
1789 ast::BlockStatement* continuing = nullptr) {
1790 return create<ast::LoopStatement>(body, continuing);
1791 }
1792
1793 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001794 /// @param source the source information
1795 /// @param var the variable to wrap in a decl statement
1796 /// @returns the variable decl statement pointer
1797 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1798 return create<ast::VariableDeclStatement>(source, var);
1799 }
1800
1801 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001802 /// @param var the variable to wrap in a decl statement
1803 /// @returns the variable decl statement pointer
1804 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1805 return create<ast::VariableDeclStatement>(var);
1806 }
1807
Antonio Maioranocea744d2021-03-25 12:55:27 +00001808 /// Creates a ast::SwitchStatement with input expression and cases
1809 /// @param condition the condition expression initializer
1810 /// @param cases case statements
1811 /// @returns the switch statement pointer
1812 template <typename ExpressionInit, typename... Cases>
1813 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1814 return create<ast::SwitchStatement>(
1815 Expr(std::forward<ExpressionInit>(condition)),
1816 ast::CaseStatementList{std::forward<Cases>(cases)...});
1817 }
1818
1819 /// Creates a ast::CaseStatement with input list of selectors, and body
1820 /// @param selectors list of selectors
1821 /// @param body the case body
1822 /// @returns the case statement pointer
1823 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1824 ast::BlockStatement* body = nullptr) {
1825 return create<ast::CaseStatement>(std::move(selectors),
1826 body ? body : Block());
1827 }
1828
1829 /// Convenient overload that takes a single selector
1830 /// @param selector a single case selector
1831 /// @param body the case body
1832 /// @returns the case statement pointer
1833 ast::CaseStatement* Case(ast::IntLiteral* selector,
1834 ast::BlockStatement* body = nullptr) {
1835 return Case(ast::CaseSelectorList{selector}, body);
1836 }
1837
1838 /// Convenience function that creates a 'default' ast::CaseStatement
1839 /// @param body the case body
1840 /// @returns the case statement pointer
1841 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1842 return Case(ast::CaseSelectorList{}, body);
1843 }
1844
James Price68f558f2021-04-06 15:51:47 +00001845 /// Creates an ast::BuiltinDecoration
1846 /// @param source the source information
1847 /// @param builtin the builtin value
1848 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001849 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001850 return create<ast::BuiltinDecoration>(source, builtin);
1851 }
1852
1853 /// Creates an ast::BuiltinDecoration
1854 /// @param builtin the builtin value
1855 /// @returns the builtin decoration pointer
1856 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1857 return create<ast::BuiltinDecoration>(source_, builtin);
1858 }
1859
1860 /// Creates an ast::LocationDecoration
1861 /// @param source the source information
1862 /// @param location the location value
1863 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001864 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001865 return create<ast::LocationDecoration>(source, location);
1866 }
1867
1868 /// Creates an ast::LocationDecoration
1869 /// @param location the location value
1870 /// @returns the location decoration pointer
1871 ast::LocationDecoration* Location(uint32_t location) {
1872 return create<ast::LocationDecoration>(source_, location);
1873 }
1874
James Pricef2f3bfc2021-05-13 20:32:32 +00001875 /// Creates an ast::OverrideDecoration with a specific constant ID
1876 /// @param source the source information
1877 /// @param id the id value
1878 /// @returns the override decoration pointer
1879 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1880 return create<ast::OverrideDecoration>(source, id);
1881 }
1882
1883 /// Creates an ast::OverrideDecoration with a specific constant ID
1884 /// @param id the optional id value
1885 /// @returns the override decoration pointer
1886 ast::OverrideDecoration* Override(uint32_t id) {
1887 return Override(source_, id);
1888 }
1889
1890 /// Creates an ast::OverrideDecoration without a constant ID
1891 /// @param source the source information
1892 /// @returns the override decoration pointer
1893 ast::OverrideDecoration* Override(const Source& source) {
1894 return create<ast::OverrideDecoration>(source);
1895 }
1896
1897 /// Creates an ast::OverrideDecoration without a constant ID
1898 /// @returns the override decoration pointer
1899 ast::OverrideDecoration* Override() { return Override(source_); }
1900
James Price68f558f2021-04-06 15:51:47 +00001901 /// Creates an ast::StageDecoration
1902 /// @param source the source information
1903 /// @param stage the pipeline stage
1904 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001905 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001906 return create<ast::StageDecoration>(source, stage);
1907 }
1908
1909 /// Creates an ast::StageDecoration
1910 /// @param stage the pipeline stage
1911 /// @returns the stage decoration pointer
1912 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1913 return create<ast::StageDecoration>(source_, stage);
1914 }
1915
James Price70f80bb2021-05-19 13:40:08 +00001916 /// Creates an ast::WorkgroupDecoration
1917 /// @param x the x dimension expression
1918 /// @returns the workgroup decoration pointer
1919 template <typename EXPR_X>
1920 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1921 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1922 }
1923
1924 /// Creates an ast::WorkgroupDecoration
1925 /// @param x the x dimension expression
1926 /// @param y the y dimension expression
1927 /// @returns the workgroup decoration pointer
1928 template <typename EXPR_X, typename EXPR_Y>
1929 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1930 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1931 nullptr);
1932 }
1933
1934 /// Creates an ast::WorkgroupDecoration
1935 /// @param x the x dimension expression
1936 /// @param y the y dimension expression
1937 /// @param z the z dimension expression
1938 /// @returns the workgroup decoration pointer
1939 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1940 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1941 return create<ast::WorkgroupDecoration>(
1942 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1943 Expr(std::forward<EXPR_Z>(z)));
1944 }
1945
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001946 /// Sets the current builder source to `src`
1947 /// @param src the Source used for future create() calls
1948 void SetSource(const Source& src) {
1949 AssertNotMoved();
1950 source_ = src;
1951 }
1952
1953 /// Sets the current builder source to `loc`
1954 /// @param loc the Source used for future create() calls
1955 void SetSource(const Source::Location& loc) {
1956 AssertNotMoved();
1957 source_ = Source(loc);
1958 }
1959
Ben Clayton33352542021-01-29 16:43:41 +00001960 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001961 /// @note As the Resolver is run when the Program is built, this will only be
1962 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001963 /// @param expr the AST expression
1964 /// @return the resolved semantic type for the expression, or nullptr if the
1965 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001966 sem::Type* TypeOf(const ast::Expression* expr) const;
1967
Ben Claytonfe0910f2021-05-17 15:51:47 +00001968 /// Helper for returning the resolved semantic type of the variable `var`.
1969 /// @note As the Resolver is run when the Program is built, this will only be
1970 /// useful for the Resolver itself and tests that use their own Resolver.
1971 /// @param var the AST variable
1972 /// @return the resolved semantic type for the variable, or nullptr if the
1973 /// variable has no resolved type.
1974 sem::Type* TypeOf(const ast::Variable* var) const;
1975
Ben Claytonfbec46f2021-04-30 20:20:19 +00001976 /// Helper for returning the resolved semantic type of the AST type `type`.
1977 /// @note As the Resolver is run when the Program is built, this will only be
1978 /// useful for the Resolver itself and tests that use their own Resolver.
1979 /// @param expr the AST type
1980 /// @return the resolved semantic type for the type, or nullptr if the type
1981 /// has no resolved type.
1982 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001983
Ben Clayton169512e2021-04-17 05:52:11 +00001984 /// Wraps the ast::Literal in a statement. This is used by tests that
1985 /// construct a partial AST and require the Resolver to reach these
1986 /// nodes.
1987 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1988 /// @return the ast::Statement that wraps the ast::Statement
1989 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001990 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001991 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001992 /// nodes.
1993 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1994 /// @return the ast::Statement that wraps the ast::Expression
1995 ast::Statement* WrapInStatement(ast::Expression* expr);
1996 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001997 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001998 /// these nodes.
1999 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2000 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2001 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2002 /// Returns the statement argument. Used as a passthrough-overload by
2003 /// WrapInFunction().
2004 /// @param stmt the ast::Statement
2005 /// @return `stmt`
2006 ast::Statement* WrapInStatement(ast::Statement* stmt);
2007 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002008 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002009 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002010 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002011 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002012 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002013 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002014 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002015 }
2016 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002017 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002018 /// @returns the function
2019 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002020
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002021 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002022 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002023
2024 protected:
2025 /// Asserts that the builder has not been moved.
2026 void AssertNotMoved() const;
2027
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002028 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002029 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002030 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002031 ASTNodeAllocator ast_nodes_;
2032 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002033 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002034 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002035 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002036 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002037
2038 /// The source to use when creating AST nodes without providing a Source as
2039 /// the first argument.
2040 Source source_;
2041
Ben Clayton5f0ea112021-03-09 10:54:37 +00002042 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002043 /// program when built.
2044 bool resolve_on_build_ = true;
2045
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002046 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2047 bool moved_ = false;
2048};
2049
2050//! @cond Doxygen_Suppress
2051// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2052template <>
2053struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002054 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002055 return t->i32();
2056 }
2057};
2058template <>
2059struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002060 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002061 return t->u32();
2062 }
2063};
2064template <>
2065struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002066 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002067 return t->f32();
2068 }
2069};
2070template <>
2071struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002072 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002073 return t->bool_();
2074 }
2075};
2076template <>
2077struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002078 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002079 return t->void_();
2080 }
2081};
2082//! @endcond
2083
Ben Clayton917b14b2021-04-19 16:50:23 +00002084/// @param builder the ProgramBuilder
2085/// @returns the ProgramID of the ProgramBuilder
2086inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2087 return builder->ID();
2088}
2089
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002090} // namespace tint
2091
2092#endif // SRC_PROGRAM_BUILDER_H_