blob: 42375a92937e53d5a062e0f26a302303f037f100 [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"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000058#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000059#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000060#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000061#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000062#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000063#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000064#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000065#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000066#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000067#include "src/sem/f32_type.h"
68#include "src/sem/i32_type.h"
69#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000070#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000071#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/sampled_texture_type.h"
73#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000074#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000075#include "src/sem/u32_type.h"
76#include "src/sem/vector_type.h"
77#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000078#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000079
80namespace tint {
81
Ben Clayton401b96b2021-02-03 17:19:59 +000082// Forward declarations
83namespace ast {
84class VariableDeclStatement;
85} // namespace ast
86
Ben Claytona6b9a8e2021-01-26 16:57:10 +000087class CloneContext;
88
89/// ProgramBuilder is a mutable builder for a Program.
90/// To construct a Program, populate the builder and then `std::move` it to a
91/// Program.
92class ProgramBuilder {
93 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000094 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
95 using ASTNodeAllocator = BlockAllocator<ast::Node>;
96
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000097 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
98 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000099
100 /// `i32` is a type alias to `int`.
101 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
102 /// WGSL syntax.
103 /// Note: this is intentionally not aliased to uint32_t as we want integer
104 /// literals passed to the builder to match WGSL's integer literal types.
105 using i32 = decltype(1);
106 /// `u32` is a type alias to `unsigned int`.
107 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
108 /// WGSL syntax.
109 /// Note: this is intentionally not aliased to uint32_t as we want integer
110 /// literals passed to the builder to match WGSL's integer literal types.
111 using u32 = decltype(1u);
112 /// `f32` is a type alias to `float`
113 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
114 /// WGSL syntax.
115 using f32 = float;
116
117 /// Constructor
118 ProgramBuilder();
119
120 /// Move constructor
121 /// @param rhs the builder to move
122 ProgramBuilder(ProgramBuilder&& rhs);
123
124 /// Destructor
125 virtual ~ProgramBuilder();
126
127 /// Move assignment operator
128 /// @param rhs the builder to move
129 /// @return this builder
130 ProgramBuilder& operator=(ProgramBuilder&& rhs);
131
Ben Claytone43c8302021-01-29 11:59:32 +0000132 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
133 /// making a deep clone of the Program contents.
134 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
135 /// existing immutable program.
136 /// As the returned ProgramBuilder wraps `program`, `program` must not be
137 /// destructed or assigned while using the returned ProgramBuilder.
138 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
139 /// function. See crbug.com/tint/460.
140 /// @param program the immutable Program to wrap
141 /// @return the ProgramBuilder that wraps `program`
142 static ProgramBuilder Wrap(const Program* program);
143
Ben Claytone6995de2021-04-13 23:27:27 +0000144 /// @returns the unique identifier for this program
145 ProgramID ID() const { return id_; }
146
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000147 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000148 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000149 AssertNotMoved();
150 return types_;
151 }
152
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000153 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000154 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000155 AssertNotMoved();
156 return types_;
157 }
158
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000159 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000160 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000161 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000162 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000163 }
164
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000165 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000166 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000167 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000168 return ast_nodes_;
169 }
170
171 /// @returns a reference to the program's semantic nodes storage
172 SemNodeAllocator& SemNodes() {
173 AssertNotMoved();
174 return sem_nodes_;
175 }
176
177 /// @returns a reference to the program's semantic nodes storage
178 const SemNodeAllocator& SemNodes() const {
179 AssertNotMoved();
180 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000181 }
182
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000183 /// @returns a reference to the program's AST root Module
184 ast::Module& AST() {
185 AssertNotMoved();
186 return *ast_;
187 }
188
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000189 /// @returns a reference to the program's AST root Module
190 const ast::Module& AST() const {
191 AssertNotMoved();
192 return *ast_;
193 }
194
195 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000196 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000197 AssertNotMoved();
198 return sem_;
199 }
200
201 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000202 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000203 AssertNotMoved();
204 return sem_;
205 }
206
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000207 /// @returns a reference to the program's SymbolTable
208 SymbolTable& Symbols() {
209 AssertNotMoved();
210 return symbols_;
211 }
212
Ben Clayton708dc2d2021-01-29 11:22:40 +0000213 /// @returns a reference to the program's SymbolTable
214 const SymbolTable& Symbols() const {
215 AssertNotMoved();
216 return symbols_;
217 }
218
Ben Clayton844217f2021-01-27 18:49:05 +0000219 /// @returns a reference to the program's diagnostics
220 diag::List& Diagnostics() {
221 AssertNotMoved();
222 return diagnostics_;
223 }
224
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000225 /// @returns a reference to the program's diagnostics
226 const diag::List& Diagnostics() const {
227 AssertNotMoved();
228 return diagnostics_;
229 }
230
Ben Clayton5f0ea112021-03-09 10:54:37 +0000231 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000232 /// @param enable the new flag value (defaults to true)
233 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
234
Ben Clayton5f0ea112021-03-09 10:54:37 +0000235 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000236 /// built.
237 bool ResolveOnBuild() const { return resolve_on_build_; }
238
Ben Clayton844217f2021-01-27 18:49:05 +0000239 /// @returns true if the program has no error diagnostics and is not missing
240 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000241 bool IsValid() const;
242
Ben Clayton708dc2d2021-01-29 11:22:40 +0000243 /// Writes a representation of the node to the output stream
244 /// @note unlike str(), to_str() does not automatically demangle the string.
245 /// @param node the AST node
246 /// @param out the stream to write to
247 /// @param indent number of spaces to indent the node when writing
248 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
249 node->to_str(Sem(), out, indent);
250 }
251
252 /// Returns a demangled, string representation of `node`.
253 /// @param node the AST node
254 /// @returns a string representation of the node
255 std::string str(const ast::Node* node) const;
256
Ben Clayton7fdfff12021-01-29 15:17:30 +0000257 /// Creates a new ast::Node owned by the ProgramBuilder. When the
258 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000259 /// @param source the Source of the node
260 /// @param args the arguments to pass to the type constructor
261 /// @returns the node pointer
262 template <typename T, typename... ARGS>
263 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
264 ARGS&&... args) {
265 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000266 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000267 }
268
Ben Clayton7fdfff12021-01-29 15:17:30 +0000269 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
270 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000271 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000272 /// When the ProgramBuilder is destructed, the ast::Node will also be
273 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000274 /// @returns the node pointer
275 template <typename T>
276 traits::EnableIfIsType<T, ast::Node>* create() {
277 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000278 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000279 }
280
Ben Clayton7fdfff12021-01-29 15:17:30 +0000281 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
282 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000283 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000284 /// When the ProgramBuilder is destructed, the ast::Node will also be
285 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000286 /// @param arg0 the first arguments to pass to the type constructor
287 /// @param args the remaining arguments to pass to the type constructor
288 /// @returns the node pointer
289 template <typename T, typename ARG0, typename... ARGS>
290 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
291 traits::IsTypeOrDerived<T, ast::Node>::value &&
292 !traits::IsTypeOrDerived<ARG0, Source>::value,
293 T>*
294 create(ARG0&& arg0, ARGS&&... args) {
295 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000296 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000297 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000298 }
299
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000300 /// Creates a new sem::Node owned by the ProgramBuilder.
301 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000302 /// destructed.
303 /// @param args the arguments to pass to the type constructor
304 /// @returns the node pointer
305 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000306 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
307 !traits::IsTypeOrDerived<T, sem::Type>::value,
308 T>*
309 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000310 AssertNotMoved();
311 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
312 }
313
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000314 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000315 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
316 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000317 /// Types are unique (de-aliased), and so calling create() for the same `T`
318 /// and arguments will return the same pointer.
319 /// @warning Use this method to acquire a type only if all of its type
320 /// information is provided in the constructor arguments `args`.<br>
321 /// If the type requires additional configuration after construction that
322 /// affect its fundamental type, build the type with `std::make_unique`, make
323 /// any necessary alterations and then call unique_type() instead.
324 /// @param args the arguments to pass to the type constructor
325 /// @returns the de-aliased type pointer
326 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000327 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
328 static_assert(std::is_base_of<sem::Type, T>::value,
329 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000330 AssertNotMoved();
331 return types_.Get<T>(std::forward<ARGS>(args)...);
332 }
333
334 /// Marks this builder as moved, preventing any further use of the builder.
335 void MarkAsMoved();
336
337 //////////////////////////////////////////////////////////////////////////////
338 // TypesBuilder
339 //////////////////////////////////////////////////////////////////////////////
340
341 /// TypesBuilder holds basic `tint` types and methods for constructing
342 /// complex types.
343 class TypesBuilder {
344 public:
345 /// Constructor
346 /// @param builder the program builder
347 explicit TypesBuilder(ProgramBuilder* builder);
348
349 /// @return the tint AST type for the C type `T`.
350 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000351 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000352 return CToAST<T>::get(this);
353 }
354
355 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000356 typ::Bool bool_() const {
357 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
358 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000359
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000360 /// @param source the Source of the node
361 /// @returns a boolean type
362 typ::Bool bool_(const Source& source) const {
363 return {builder->create<ast::Bool>(source), builder->create<sem::Bool>()};
364 }
365
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000366 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000367 typ::F32 f32() const {
368 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
369 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000370
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000371 /// @param source the Source of the node
372 /// @returns a f32 type
373 typ::F32 f32(const Source& source) const {
374 return {builder->create<ast::F32>(source), builder->create<sem::F32>()};
375 }
376
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000377 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000378 typ::I32 i32() const {
379 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
380 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000381
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000382 /// @param source the Source of the node
383 /// @returns a i32 type
384 typ::I32 i32(const Source& source) const {
385 return {builder->create<ast::I32>(source), builder->create<sem::I32>()};
386 }
387
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000388 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000389 typ::U32 u32() const {
390 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
391 }
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 {
396 return {builder->create<ast::U32>(source), builder->create<sem::U32>()};
397 }
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
871 /// @param source the Source of the node
872 /// @returns the external texture
873 typ::ExternalTexture external_texture(const Source& source) const {
874 return {builder->create<ast::ExternalTexture>(source),
875 builder->create<sem::ExternalTexture>()};
876 }
877
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000878 /// If ty is a ast::Struct or ast::Alias, the returned type is an
879 /// ast::TypeName of the given type's name, otherwise type is returned.
880 /// @param type the type
881 /// @return either type or a pointer to a new ast::TypeName
882 typ::Type MaybeCreateTypename(typ::Type type) const;
883
Ben Claytonf5f311e2021-04-28 14:31:23 +0000884 /// The ProgramBuilder
885 ProgramBuilder* const builder;
886
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000887 private:
888 /// CToAST<T> is specialized for various `T` types and each specialization
889 /// contains a single static `get()` method for obtaining the corresponding
890 /// AST type for the C type `T`.
891 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000892 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000893 template <typename T>
894 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000895 };
896
897 //////////////////////////////////////////////////////////////////////////////
898 // AST helper methods
899 //////////////////////////////////////////////////////////////////////////////
900
James Price65ae64d2021-04-29 12:59:14 +0000901 /// @return a new unnamed symbol
902 Symbol Sym() { return Symbols().New(); }
903
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000904 /// @param name the symbol string
905 /// @return a Symbol with the given name
906 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
907
908 /// @param sym the symbol
909 /// @return `sym`
910 Symbol Sym(Symbol sym) { return sym; }
911
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000912 /// @param expr the expression
913 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000914 template <typename T>
915 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
916 return expr;
917 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000918
Ben Claytonb8ea5912021-04-19 16:52:42 +0000919 /// Passthrough for nullptr
920 /// @return nullptr
921 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
922
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000923 /// @param name the identifier name
924 /// @return an ast::IdentifierExpression with the given name
925 ast::IdentifierExpression* Expr(const std::string& name) {
926 return create<ast::IdentifierExpression>(Symbols().Register(name));
927 }
928
Ben Clayton46d78d72021-02-10 21:34:26 +0000929 /// @param symbol the identifier symbol
930 /// @return an ast::IdentifierExpression with the given symbol
931 ast::IdentifierExpression* Expr(Symbol symbol) {
932 return create<ast::IdentifierExpression>(symbol);
933 }
934
Ben Claytonb8ea5912021-04-19 16:52:42 +0000935 /// @param variable the AST variable
936 /// @return an ast::IdentifierExpression with the variable's symbol
937 ast::IdentifierExpression* Expr(ast::Variable* variable) {
938 return create<ast::IdentifierExpression>(variable->symbol());
939 }
940
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000941 /// @param source the source information
942 /// @param name the identifier name
943 /// @return an ast::IdentifierExpression with the given name
944 ast::IdentifierExpression* Expr(const Source& source,
945 const std::string& name) {
946 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
947 }
948
949 /// @param name the identifier name
950 /// @return an ast::IdentifierExpression with the given name
951 ast::IdentifierExpression* Expr(const char* name) {
952 return create<ast::IdentifierExpression>(Symbols().Register(name));
953 }
954
955 /// @param value the boolean value
956 /// @return a Scalar constructor for the given value
957 ast::ScalarConstructorExpression* Expr(bool value) {
958 return create<ast::ScalarConstructorExpression>(Literal(value));
959 }
960
961 /// @param value the float value
962 /// @return a Scalar constructor for the given value
963 ast::ScalarConstructorExpression* Expr(f32 value) {
964 return create<ast::ScalarConstructorExpression>(Literal(value));
965 }
966
967 /// @param value the integer value
968 /// @return a Scalar constructor for the given value
969 ast::ScalarConstructorExpression* Expr(i32 value) {
970 return create<ast::ScalarConstructorExpression>(Literal(value));
971 }
972
973 /// @param value the unsigned int value
974 /// @return a Scalar constructor for the given value
975 ast::ScalarConstructorExpression* Expr(u32 value) {
976 return create<ast::ScalarConstructorExpression>(Literal(value));
977 }
978
979 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
980 /// `list`.
981 /// @param list the list to append too
982 /// @param arg the arg to create
983 template <typename ARG>
984 void Append(ast::ExpressionList& list, ARG&& arg) {
985 list.emplace_back(Expr(std::forward<ARG>(arg)));
986 }
987
988 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
989 /// then appends them to `list`.
990 /// @param list the list to append too
991 /// @param arg0 the first argument
992 /// @param args the rest of the arguments
993 template <typename ARG0, typename... ARGS>
994 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
995 Append(list, std::forward<ARG0>(arg0));
996 Append(list, std::forward<ARGS>(args)...);
997 }
998
999 /// @return an empty list of expressions
1000 ast::ExpressionList ExprList() { return {}; }
1001
1002 /// @param args the list of expressions
1003 /// @return the list of expressions converted to `ast::Expression`s using
1004 /// `Expr()`,
1005 template <typename... ARGS>
1006 ast::ExpressionList ExprList(ARGS&&... args) {
1007 ast::ExpressionList list;
1008 list.reserve(sizeof...(args));
1009 Append(list, std::forward<ARGS>(args)...);
1010 return list;
1011 }
1012
1013 /// @param list the list of expressions
1014 /// @return `list`
1015 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1016
1017 /// @param val the boolan value
1018 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001019 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001020
1021 /// @param val the float value
1022 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001023 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001024
1025 /// @param val the unsigned int value
1026 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001027 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001028
1029 /// @param val the integer value
1030 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001031 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001032
1033 /// @param args the arguments for the type constructor
1034 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1035 /// of `args` converted to `ast::Expression`s using `Expr()`
1036 template <typename T, typename... ARGS>
1037 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001038 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001039 }
1040
1041 /// @param type the type to construct
1042 /// @param args the arguments for the constructor
1043 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1044 /// values `args`.
1045 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001046 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001047 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001048 return create<ast::TypeConstructorExpression>(
1049 type, ExprList(std::forward<ARGS>(args)...));
1050 }
1051
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001052 /// Creates a constructor expression that constructs an object of
1053 /// `type` filled with `elem_value`. For example,
1054 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1055 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1056 /// @param type the type to construct
1057 /// @param elem_value the initial or element value (for vec and mat) to
1058 /// construct with
1059 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001060 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001061 int elem_value = 0);
1062
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001063 /// @param args the arguments for the vector constructor
1064 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1065 /// `T`, constructed with the values `args`.
1066 template <typename T, typename... ARGS>
1067 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001068 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001069 }
1070
1071 /// @param args the arguments for the vector constructor
1072 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1073 /// `T`, constructed with the values `args`.
1074 template <typename T, typename... ARGS>
1075 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001076 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001077 }
1078
1079 /// @param args the arguments for the vector constructor
1080 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1081 /// `T`, constructed with the values `args`.
1082 template <typename T, typename... ARGS>
1083 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001084 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001085 }
1086
1087 /// @param args the arguments for the matrix constructor
1088 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1089 /// `T`, constructed with the values `args`.
1090 template <typename T, typename... ARGS>
1091 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001092 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001093 }
1094
1095 /// @param args the arguments for the matrix constructor
1096 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1097 /// `T`, constructed with the values `args`.
1098 template <typename T, typename... ARGS>
1099 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001100 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001101 }
1102
1103 /// @param args the arguments for the matrix constructor
1104 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1105 /// `T`, constructed with the values `args`.
1106 template <typename T, typename... ARGS>
1107 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001108 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001109 }
1110
1111 /// @param args the arguments for the matrix constructor
1112 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1113 /// `T`, constructed with the values `args`.
1114 template <typename T, typename... ARGS>
1115 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001116 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001117 }
1118
1119 /// @param args the arguments for the matrix constructor
1120 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1121 /// `T`, constructed with the values `args`.
1122 template <typename T, typename... ARGS>
1123 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001124 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001125 }
1126
1127 /// @param args the arguments for the matrix constructor
1128 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1129 /// `T`, constructed with the values `args`.
1130 template <typename T, typename... ARGS>
1131 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001132 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001133 }
1134
1135 /// @param args the arguments for the matrix constructor
1136 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1137 /// `T`, constructed with the values `args`.
1138 template <typename T, typename... ARGS>
1139 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001140 return Construct(ty.mat4x2<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 4x3 matrix of type
1145 /// `T`, constructed with the values `args`.
1146 template <typename T, typename... ARGS>
1147 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001148 return Construct(ty.mat4x3<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 4x4 matrix of type
1153 /// `T`, constructed with the values `args`.
1154 template <typename T, typename... ARGS>
1155 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001156 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001157 }
1158
1159 /// @param args the arguments for the array constructor
1160 /// @return an `ast::TypeConstructorExpression` of an array with element type
1161 /// `T`, constructed with the values `args`.
1162 template <typename T, int N = 0, typename... ARGS>
1163 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001164 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001165 }
1166
1167 /// @param subtype the array element type
1168 /// @param n the array size. 0 represents a runtime-array.
1169 /// @param args the arguments for the array constructor
1170 /// @return an `ast::TypeConstructorExpression` of an array with element type
1171 /// `subtype`, constructed with the values `args`.
1172 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001173 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001174 uint32_t n,
1175 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001176 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001177 }
1178
1179 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001180 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001181 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001182 /// @param constructor constructor expression
1183 /// @param decorations variable decorations
1184 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001185 template <typename NAME>
1186 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001187 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001188 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001189 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001190 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001191 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001192 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1193 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001194 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001195
1196 /// @param source the variable source
1197 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001198 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001199 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001200 /// @param constructor constructor expression
1201 /// @param decorations variable decorations
1202 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001203 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001204 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001205 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001206 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001207 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001208 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001209 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001210 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001211 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001212 type, false, constructor, decorations);
1213 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001214
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001217 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001219 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001220 template <typename NAME>
1221 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001222 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001223 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001224 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001225 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001226 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001227 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001228 constructor, decorations);
1229 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230
1231 /// @param source the variable source
1232 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001233 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001234 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001235 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001236 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001237 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001239 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001240 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001241 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001242 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001243 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001244 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001245 ast::StorageClass::kNone, type, true,
1246 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001247 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001248
James Pricedaf1f1c2021-04-08 22:15:48 +00001249 /// @param name the parameter name
1250 /// @param type the parameter type
1251 /// @param decorations optional parameter decorations
1252 /// @returns a constant `ast::Variable` with the given name and type
1253 template <typename NAME>
1254 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001255 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001256 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001257 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001258 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1259 ast::StorageClass::kNone, type, true, nullptr,
1260 decorations);
1261 }
1262
1263 /// @param source the parameter source
1264 /// @param name the parameter name
1265 /// @param type the parameter type
1266 /// @param decorations optional parameter decorations
1267 /// @returns a constant `ast::Variable` with the given name and type
1268 template <typename NAME>
1269 ast::Variable* Param(const Source& source,
1270 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001271 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001272 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001273 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001274 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1275 ast::StorageClass::kNone, type, true, nullptr,
1276 decorations);
1277 }
1278
Ben Clayton42708342021-04-21 17:55:12 +00001279 /// @param name the variable name
1280 /// @param type the variable type
1281 /// @param storage the variable storage class
1282 /// @param constructor constructor expression
1283 /// @param decorations variable decorations
1284 /// @returns a new `ast::Variable`, which is automatically registered as a
1285 /// global variable with the ast::Module.
1286 template <typename NAME>
1287 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001288 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001289 ast::StorageClass storage,
1290 ast::Expression* constructor = nullptr,
1291 ast::DecorationList decorations = {}) {
1292 auto* var =
1293 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1294 AST().AddGlobalVariable(var);
1295 return var;
1296 }
1297
1298 /// @param source the variable source
1299 /// @param name the variable name
1300 /// @param type the variable type
1301 /// @param storage the variable storage class
1302 /// @param constructor constructor expression
1303 /// @param decorations variable decorations
1304 /// @returns a new `ast::Variable`, which is automatically registered as a
1305 /// global variable with the ast::Module.
1306 template <typename NAME>
1307 ast::Variable* Global(const Source& source,
1308 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001309 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001310 ast::StorageClass storage,
1311 ast::Expression* constructor = nullptr,
1312 ast::DecorationList decorations = {}) {
1313 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1314 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001315 AST().AddGlobalVariable(var);
1316 return var;
1317 }
1318
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001319 /// @param name the variable name
1320 /// @param type the variable type
1321 /// @param constructor constructor expression
1322 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001323 /// @returns a const `ast::Variable` constructed by calling Var() with the
1324 /// arguments of `args`, which is automatically registered as a global
1325 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001326 template <typename NAME>
1327 ast::Variable* GlobalConst(NAME&& name,
1328 typ::Type type,
1329 ast::Expression* constructor,
1330 ast::DecorationList decorations = {}) {
1331 auto* var = Const(std::forward<NAME>(name), type, constructor,
1332 std::move(decorations));
1333 AST().AddGlobalVariable(var);
1334 return var;
1335 }
1336
1337 /// @param source the variable source
1338 /// @param name the variable name
1339 /// @param type the variable type
1340 /// @param constructor constructor expression
1341 /// @param decorations optional variable decorations
1342 /// @returns a const `ast::Variable` constructed by calling Var() with the
1343 /// arguments of `args`, which is automatically registered as a global
1344 /// variable with the ast::Module.
1345 template <typename NAME>
1346 ast::Variable* GlobalConst(const Source& source,
1347 NAME&& name,
1348 typ::Type type,
1349 ast::Expression* constructor,
1350 ast::DecorationList decorations = {}) {
1351 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1352 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001353 AST().AddGlobalVariable(var);
1354 return var;
1355 }
1356
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001357 /// @param func the function name
1358 /// @param args the function call arguments
1359 /// @returns a `ast::CallExpression` to the function `func`, with the
1360 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1361 template <typename NAME, typename... ARGS>
1362 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1363 return create<ast::CallExpression>(Expr(func),
1364 ExprList(std::forward<ARGS>(args)...));
1365 }
1366
1367 /// @param lhs the left hand argument to the addition operation
1368 /// @param rhs the right hand argument to the addition operation
1369 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1370 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001371 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001372 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1373 Expr(std::forward<LHS>(lhs)),
1374 Expr(std::forward<RHS>(rhs)));
1375 }
1376
1377 /// @param lhs the left hand argument to the subtraction operation
1378 /// @param rhs the right hand argument to the subtraction operation
1379 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1380 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001381 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001382 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1383 Expr(std::forward<LHS>(lhs)),
1384 Expr(std::forward<RHS>(rhs)));
1385 }
1386
1387 /// @param lhs the left hand argument to the multiplication operation
1388 /// @param rhs the right hand argument to the multiplication operation
1389 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1390 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001391 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001392 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1393 Expr(std::forward<LHS>(lhs)),
1394 Expr(std::forward<RHS>(rhs)));
1395 }
1396
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001397 /// @param source the source information
1398 /// @param lhs the left hand argument to the multiplication operation
1399 /// @param rhs the right hand argument to the multiplication operation
1400 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1401 template <typename LHS, typename RHS>
1402 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1403 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1404 Expr(std::forward<LHS>(lhs)),
1405 Expr(std::forward<RHS>(rhs)));
1406 }
1407
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001408 /// @param lhs the left hand argument to the division operation
1409 /// @param rhs the right hand argument to the division operation
1410 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1411 template <typename LHS, typename RHS>
1412 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1413 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1414 Expr(std::forward<LHS>(lhs)),
1415 Expr(std::forward<RHS>(rhs)));
1416 }
1417
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001418 /// @param arr the array argument for the array accessor expression
1419 /// @param idx the index argument for the array accessor expression
1420 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1421 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001422 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001423 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1424 Expr(std::forward<IDX>(idx)));
1425 }
1426
1427 /// @param obj the object for the member accessor expression
1428 /// @param idx the index argument for the array accessor expression
1429 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1430 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001431 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001432 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1433 Expr(std::forward<IDX>(idx)));
1434 }
1435
Ben Clayton42d1e092021-02-02 14:29:15 +00001436 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001437 /// @param val the offset value
1438 /// @returns the offset decoration pointer
1439 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1440 return create<ast::StructMemberOffsetDecoration>(source_, val);
1441 }
1442
Ben Claytond614dd52021-03-15 10:43:11 +00001443 /// Creates a ast::StructMemberSizeDecoration
1444 /// @param source the source information
1445 /// @param val the size value
1446 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001447 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1448 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001449 return create<ast::StructMemberSizeDecoration>(source, val);
1450 }
1451
1452 /// Creates a ast::StructMemberSizeDecoration
1453 /// @param val the size value
1454 /// @returns the size decoration pointer
1455 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1456 return create<ast::StructMemberSizeDecoration>(source_, val);
1457 }
1458
1459 /// Creates a ast::StructMemberAlignDecoration
1460 /// @param source the source information
1461 /// @param val the align value
1462 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001463 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1464 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001465 return create<ast::StructMemberAlignDecoration>(source, val);
1466 }
1467
1468 /// Creates a ast::StructMemberAlignDecoration
1469 /// @param val the align value
1470 /// @returns the align decoration pointer
1471 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1472 return create<ast::StructMemberAlignDecoration>(source_, val);
1473 }
1474
Ben Clayton42d1e092021-02-02 14:29:15 +00001475 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001476 /// @param source the source information
1477 /// @param name the function name
1478 /// @param params the function parameters
1479 /// @param type the function return type
1480 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001481 /// @param decorations the optional function decorations
1482 /// @param return_type_decorations the optional function return type
1483 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001484 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001485 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001486 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001487 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001488 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001489 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001490 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001491 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001492 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001493 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001494 auto* func =
1495 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1496 type, create<ast::BlockStatement>(body),
1497 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001498 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001499 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001500 }
1501
Ben Clayton42d1e092021-02-02 14:29:15 +00001502 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001503 /// @param name the function name
1504 /// @param params the function parameters
1505 /// @param type the function return type
1506 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001507 /// @param decorations the optional function decorations
1508 /// @param return_type_decorations the optional function return type
1509 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001510 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001511 template <typename NAME>
1512 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001513 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001514 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001515 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001516 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001517 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001518 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001519 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1520 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001521 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001522 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001523 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001524 }
1525
Ben Clayton49668bb2021-04-17 05:32:01 +00001526 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001527 /// @param source the source information
1528 /// @returns the return statement pointer
1529 ast::ReturnStatement* Return(const Source& source) {
1530 return create<ast::ReturnStatement>(source);
1531 }
1532
1533 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001534 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001535 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1536
1537 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001538 /// @param source the source information
1539 /// @param val the return value
1540 /// @returns the return statement pointer
1541 template <typename EXPR>
1542 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1543 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1544 }
1545
1546 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001547 /// @param val the return value
1548 /// @returns the return statement pointer
1549 template <typename EXPR>
1550 ast::ReturnStatement* Return(EXPR&& val) {
1551 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001552 }
1553
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001554 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001555 /// @param source the source information
1556 /// @param name the struct name
1557 /// @param members the struct members
1558 /// @param decorations the optional struct decorations
1559 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001560 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001561 ast::Struct* Structure(const Source& source,
1562 NAME&& name,
1563 ast::StructMemberList members,
1564 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001565 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001566 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001567 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001568 AST().AddConstructedType(type);
1569 return type;
1570 }
1571
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001572 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001573 /// @param name the struct name
1574 /// @param members the struct members
1575 /// @param decorations the optional struct decorations
1576 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001577 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001578 ast::Struct* Structure(NAME&& name,
1579 ast::StructMemberList members,
1580 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001581 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001582 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001583 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001584 AST().AddConstructedType(type);
1585 return type;
1586 }
1587
Ben Clayton42d1e092021-02-02 14:29:15 +00001588 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001589 /// @param source the source information
1590 /// @param name the struct member name
1591 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001592 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001593 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001594 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001595 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001596 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001597 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001598 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001599 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001600 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1601 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001602 }
1603
Ben Clayton42d1e092021-02-02 14:29:15 +00001604 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001605 /// @param name the struct member name
1606 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001607 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001608 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001609 template <typename NAME>
1610 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001611 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001612 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001613 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001614 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1615 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001616 }
1617
Ben Claytonbab31972021-02-08 21:16:21 +00001618 /// Creates a ast::StructMember with the given byte offset
1619 /// @param offset the offset to use in the StructMemberOffsetDecoration
1620 /// @param name the struct member name
1621 /// @param type the struct member type
1622 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001623 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001624 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001625 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001626 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001627 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001628 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001629 create<ast::StructMemberOffsetDecoration>(offset),
1630 });
1631 }
1632
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001633 /// Creates a ast::BlockStatement with input statements
1634 /// @param statements statements of block
1635 /// @returns the block statement pointer
1636 template <typename... Statements>
1637 ast::BlockStatement* Block(Statements&&... statements) {
1638 return create<ast::BlockStatement>(
1639 ast::StatementList{std::forward<Statements>(statements)...});
1640 }
1641
1642 /// Creates a ast::ElseStatement with input condition and body
1643 /// @param condition the else condition expression
1644 /// @param body the else body
1645 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001646 template <typename CONDITION>
1647 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1648 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1649 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001650 }
1651
1652 /// Creates a ast::IfStatement with input condition, body, and optional
1653 /// variadic else statements
1654 /// @param condition the if statement condition expression
1655 /// @param body the if statement body
1656 /// @param elseStatements optional variadic else statements
1657 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001658 template <typename CONDITION, typename... ELSE_STATEMENTS>
1659 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001660 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001661 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001662 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001663 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001664 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001665 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001666 }
1667
1668 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001669 /// @param source the source information
1670 /// @param lhs the left hand side expression initializer
1671 /// @param rhs the right hand side expression initializer
1672 /// @returns the assignment statement pointer
1673 template <typename LhsExpressionInit, typename RhsExpressionInit>
1674 ast::AssignmentStatement* Assign(const Source& source,
1675 LhsExpressionInit&& lhs,
1676 RhsExpressionInit&& rhs) {
1677 return create<ast::AssignmentStatement>(
1678 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1679 Expr(std::forward<RhsExpressionInit>(rhs)));
1680 }
1681
1682 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001683 /// @param lhs the left hand side expression initializer
1684 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001685 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001686 template <typename LhsExpressionInit, typename RhsExpressionInit>
1687 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1688 RhsExpressionInit&& rhs) {
1689 return create<ast::AssignmentStatement>(
1690 Expr(std::forward<LhsExpressionInit>(lhs)),
1691 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001692 }
1693
1694 /// Creates a ast::LoopStatement with input body and optional continuing
1695 /// @param body the loop body
1696 /// @param continuing the optional continuing block
1697 /// @returns the loop statement pointer
1698 ast::LoopStatement* Loop(ast::BlockStatement* body,
1699 ast::BlockStatement* continuing = nullptr) {
1700 return create<ast::LoopStatement>(body, continuing);
1701 }
1702
1703 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001704 /// @param source the source information
1705 /// @param var the variable to wrap in a decl statement
1706 /// @returns the variable decl statement pointer
1707 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1708 return create<ast::VariableDeclStatement>(source, var);
1709 }
1710
1711 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001712 /// @param var the variable to wrap in a decl statement
1713 /// @returns the variable decl statement pointer
1714 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1715 return create<ast::VariableDeclStatement>(var);
1716 }
1717
Antonio Maioranocea744d2021-03-25 12:55:27 +00001718 /// Creates a ast::SwitchStatement with input expression and cases
1719 /// @param condition the condition expression initializer
1720 /// @param cases case statements
1721 /// @returns the switch statement pointer
1722 template <typename ExpressionInit, typename... Cases>
1723 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1724 return create<ast::SwitchStatement>(
1725 Expr(std::forward<ExpressionInit>(condition)),
1726 ast::CaseStatementList{std::forward<Cases>(cases)...});
1727 }
1728
1729 /// Creates a ast::CaseStatement with input list of selectors, and body
1730 /// @param selectors list of selectors
1731 /// @param body the case body
1732 /// @returns the case statement pointer
1733 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1734 ast::BlockStatement* body = nullptr) {
1735 return create<ast::CaseStatement>(std::move(selectors),
1736 body ? body : Block());
1737 }
1738
1739 /// Convenient overload that takes a single selector
1740 /// @param selector a single case selector
1741 /// @param body the case body
1742 /// @returns the case statement pointer
1743 ast::CaseStatement* Case(ast::IntLiteral* selector,
1744 ast::BlockStatement* body = nullptr) {
1745 return Case(ast::CaseSelectorList{selector}, body);
1746 }
1747
1748 /// Convenience function that creates a 'default' ast::CaseStatement
1749 /// @param body the case body
1750 /// @returns the case statement pointer
1751 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1752 return Case(ast::CaseSelectorList{}, body);
1753 }
1754
James Price68f558f2021-04-06 15:51:47 +00001755 /// Creates an ast::BuiltinDecoration
1756 /// @param source the source information
1757 /// @param builtin the builtin value
1758 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001759 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001760 return create<ast::BuiltinDecoration>(source, builtin);
1761 }
1762
1763 /// Creates an ast::BuiltinDecoration
1764 /// @param builtin the builtin value
1765 /// @returns the builtin decoration pointer
1766 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1767 return create<ast::BuiltinDecoration>(source_, builtin);
1768 }
1769
1770 /// Creates an ast::LocationDecoration
1771 /// @param source the source information
1772 /// @param location the location value
1773 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001774 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001775 return create<ast::LocationDecoration>(source, location);
1776 }
1777
1778 /// Creates an ast::LocationDecoration
1779 /// @param location the location value
1780 /// @returns the location decoration pointer
1781 ast::LocationDecoration* Location(uint32_t location) {
1782 return create<ast::LocationDecoration>(source_, location);
1783 }
1784
James Pricef2f3bfc2021-05-13 20:32:32 +00001785 /// Creates an ast::OverrideDecoration with a specific constant ID
1786 /// @param source the source information
1787 /// @param id the id value
1788 /// @returns the override decoration pointer
1789 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1790 return create<ast::OverrideDecoration>(source, id);
1791 }
1792
1793 /// Creates an ast::OverrideDecoration with a specific constant ID
1794 /// @param id the optional id value
1795 /// @returns the override decoration pointer
1796 ast::OverrideDecoration* Override(uint32_t id) {
1797 return Override(source_, id);
1798 }
1799
1800 /// Creates an ast::OverrideDecoration without a constant ID
1801 /// @param source the source information
1802 /// @returns the override decoration pointer
1803 ast::OverrideDecoration* Override(const Source& source) {
1804 return create<ast::OverrideDecoration>(source);
1805 }
1806
1807 /// Creates an ast::OverrideDecoration without a constant ID
1808 /// @returns the override decoration pointer
1809 ast::OverrideDecoration* Override() { return Override(source_); }
1810
James Price68f558f2021-04-06 15:51:47 +00001811 /// Creates an ast::StageDecoration
1812 /// @param source the source information
1813 /// @param stage the pipeline stage
1814 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001815 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001816 return create<ast::StageDecoration>(source, stage);
1817 }
1818
1819 /// Creates an ast::StageDecoration
1820 /// @param stage the pipeline stage
1821 /// @returns the stage decoration pointer
1822 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1823 return create<ast::StageDecoration>(source_, stage);
1824 }
1825
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001826 /// Sets the current builder source to `src`
1827 /// @param src the Source used for future create() calls
1828 void SetSource(const Source& src) {
1829 AssertNotMoved();
1830 source_ = src;
1831 }
1832
1833 /// Sets the current builder source to `loc`
1834 /// @param loc the Source used for future create() calls
1835 void SetSource(const Source::Location& loc) {
1836 AssertNotMoved();
1837 source_ = Source(loc);
1838 }
1839
Ben Clayton33352542021-01-29 16:43:41 +00001840 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001841 /// @note As the Resolver is run when the Program is built, this will only be
1842 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001843 /// @param expr the AST expression
1844 /// @return the resolved semantic type for the expression, or nullptr if the
1845 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001846 sem::Type* TypeOf(const ast::Expression* expr) const;
1847
1848 /// Helper for returning the resolved semantic type of the AST type `type`.
1849 /// @note As the Resolver is run when the Program is built, this will only be
1850 /// useful for the Resolver itself and tests that use their own Resolver.
1851 /// @param expr the AST type
1852 /// @return the resolved semantic type for the type, or nullptr if the type
1853 /// has no resolved type.
1854 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001855
Ben Clayton169512e2021-04-17 05:52:11 +00001856 /// Wraps the ast::Literal in a statement. This is used by tests that
1857 /// construct a partial AST and require the Resolver to reach these
1858 /// nodes.
1859 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1860 /// @return the ast::Statement that wraps the ast::Statement
1861 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001862 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001863 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001864 /// nodes.
1865 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1866 /// @return the ast::Statement that wraps the ast::Expression
1867 ast::Statement* WrapInStatement(ast::Expression* expr);
1868 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001869 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001870 /// these nodes.
1871 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1872 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1873 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1874 /// Returns the statement argument. Used as a passthrough-overload by
1875 /// WrapInFunction().
1876 /// @param stmt the ast::Statement
1877 /// @return `stmt`
1878 ast::Statement* WrapInStatement(ast::Statement* stmt);
1879 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001880 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001881 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001882 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001883 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001884 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001885 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001886 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001887 }
1888 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001889 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001890 /// @returns the function
1891 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001892
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001893 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001894 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001895
1896 protected:
1897 /// Asserts that the builder has not been moved.
1898 void AssertNotMoved() const;
1899
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001900 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001901 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001902 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001903 ASTNodeAllocator ast_nodes_;
1904 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001905 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001906 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001907 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001908 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001909
1910 /// The source to use when creating AST nodes without providing a Source as
1911 /// the first argument.
1912 Source source_;
1913
Ben Clayton5f0ea112021-03-09 10:54:37 +00001914 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001915 /// program when built.
1916 bool resolve_on_build_ = true;
1917
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001918 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1919 bool moved_ = false;
1920};
1921
1922//! @cond Doxygen_Suppress
1923// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1924template <>
1925struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001926 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001927 return t->i32();
1928 }
1929};
1930template <>
1931struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001932 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001933 return t->u32();
1934 }
1935};
1936template <>
1937struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001938 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001939 return t->f32();
1940 }
1941};
1942template <>
1943struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001944 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001945 return t->bool_();
1946 }
1947};
1948template <>
1949struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001950 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001951 return t->void_();
1952 }
1953};
1954//! @endcond
1955
Ben Clayton917b14b2021-04-19 16:50:23 +00001956/// @param builder the ProgramBuilder
1957/// @returns the ProgramID of the ProgramBuilder
1958inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1959 return builder->ID();
1960}
1961
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001962} // namespace tint
1963
1964#endif // SRC_PROGRAM_BUILDER_H_