blob: 53ad9cb7969752adf4ea3cf53125e224741a13a0 [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"
41#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000042#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000043#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/ast/scalar_constructor_expression.h"
45#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000046#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000047#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000048#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000049#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000050#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000051#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000052#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000053#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000054#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000055#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000056#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000057#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000058#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000059#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000060#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000061#include "src/program_id.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000062#include "src/sem/access_control_type.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
Ben Claytone204f272021-04-22 14:40:23 +0000706 typ::AccessControl access(ast::AccessControl::Access access,
707 typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000708 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000709 return {type.ast ? builder->create<ast::AccessControl>(access, type)
710 : nullptr,
711 type.sem ? builder->create<sem::AccessControl>(access, type)
712 : nullptr};
Ben Clayton4db10dd2021-04-16 08:47:14 +0000713 }
714
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000715 /// Creates an access control qualifier type
716 /// @param source the Source of the node
717 /// @param access the access control
718 /// @param type the inner type
719 /// @returns the access control qualifier type
720 typ::AccessControl access(const Source& source,
721 ast::AccessControl::Access access,
722 typ::Type type) const {
723 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000724 return {type.ast
725 ? builder->create<ast::AccessControl>(source, access, type)
726 : nullptr,
727 type.sem ? builder->create<sem::AccessControl>(access, type)
728 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000729 }
730
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000731 /// @param type the type of the pointer
732 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000733 /// @return the pointer to `type` with the given ast::StorageClass
734 typ::Pointer pointer(typ::Type type,
735 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000736 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000737 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
738 : nullptr,
739 type.sem ? builder->create<sem::Pointer>(type, storage_class)
740 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000741 }
742
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000743 /// @param source the Source of the node
744 /// @param type the type of the pointer
745 /// @param storage_class the storage class of the pointer
746 /// @return the pointer to `type` with the given ast::StorageClass
747 typ::Pointer pointer(const Source& source,
748 typ::Type type,
749 ast::StorageClass storage_class) const {
750 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000751 return {type.ast
752 ? builder->create<ast::Pointer>(source, type, storage_class)
753 : nullptr,
754 type.sem ? builder->create<sem::Pointer>(type, storage_class)
755 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000756 }
757
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000758 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000759 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000760 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000761 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000762 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000763 }
764
Ben Claytone204f272021-04-22 14:40:23 +0000765 /// @param kind the kind of sampler
766 /// @returns the sampler
767 typ::Sampler sampler(ast::SamplerKind kind) const {
768 return {builder->create<ast::Sampler>(kind),
769 builder->create<sem::Sampler>(kind)};
770 }
771
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000772 /// @param source the Source of the node
773 /// @param kind the kind of sampler
774 /// @returns the sampler
775 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
776 return {builder->create<ast::Sampler>(source, kind),
777 builder->create<sem::Sampler>(kind)};
778 }
779
Ben Claytone204f272021-04-22 14:40:23 +0000780 /// @param dims the dimensionality of the texture
781 /// @returns the depth texture
782 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
783 return {builder->create<ast::DepthTexture>(dims),
784 builder->create<sem::DepthTexture>(dims)};
785 }
786
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000787 /// @param source the Source of the node
788 /// @param dims the dimensionality of the texture
789 /// @returns the depth texture
790 typ::DepthTexture depth_texture(const Source& source,
791 ast::TextureDimension dims) const {
792 return {builder->create<ast::DepthTexture>(source, dims),
793 builder->create<sem::DepthTexture>(dims)};
794 }
795
Ben Claytone204f272021-04-22 14:40:23 +0000796 /// @param dims the dimensionality of the texture
797 /// @param subtype the texture subtype.
798 /// @returns the sampled texture
799 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
800 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000801 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
802 : nullptr,
803 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
804 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000805 }
806
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000807 /// @param source the Source of the node
808 /// @param dims the dimensionality of the texture
809 /// @param subtype the texture subtype.
810 /// @returns the sampled texture
811 typ::SampledTexture sampled_texture(const Source& source,
812 ast::TextureDimension dims,
813 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000814 return {subtype.ast
815 ? builder->create<ast::SampledTexture>(source, dims, subtype)
816 : nullptr,
817 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
818 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000819 }
820
Ben Claytone204f272021-04-22 14:40:23 +0000821 /// @param dims the dimensionality of the texture
822 /// @param subtype the texture subtype.
823 /// @returns the multisampled texture
824 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
825 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000826 return {
827 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
828 : nullptr,
829 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
830 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000831 }
832
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000833 /// @param source the Source of the node
834 /// @param dims the dimensionality of the texture
835 /// @param subtype the texture subtype.
836 /// @returns the multisampled texture
837 typ::MultisampledTexture multisampled_texture(const Source& source,
838 ast::TextureDimension dims,
839 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000840 return {
841 subtype.ast
842 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
843 : nullptr,
844 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
845 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000846 }
847
Ben Claytone204f272021-04-22 14:40:23 +0000848 /// @param dims the dimensionality of the texture
849 /// @param format the image format of the texture
850 /// @returns the storage texture
851 typ::StorageTexture storage_texture(ast::TextureDimension dims,
852 ast::ImageFormat format) const {
853 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
854 auto* sem_subtype =
855 sem::StorageTexture::SubtypeFor(format, builder->Types());
856 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
857 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000858 }
859
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000860 /// @param source the Source of the node
861 /// @param dims the dimensionality of the texture
862 /// @param format the image format of the texture
863 /// @returns the storage texture
864 typ::StorageTexture storage_texture(const Source& source,
865 ast::TextureDimension dims,
866 ast::ImageFormat format) const {
867 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
868 auto* sem_subtype =
869 sem::StorageTexture::SubtypeFor(format, builder->Types());
870 return {builder->create<ast::StorageTexture>(source, dims, format,
871 ast_subtype),
872 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
873 }
874
875 /// @param source the Source of the node
876 /// @returns the external texture
877 typ::ExternalTexture external_texture(const Source& source) const {
878 return {builder->create<ast::ExternalTexture>(source),
879 builder->create<sem::ExternalTexture>()};
880 }
881
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000882 /// If ty is a ast::Struct or ast::Alias, the returned type is an
883 /// ast::TypeName of the given type's name, otherwise type is returned.
884 /// @param type the type
885 /// @return either type or a pointer to a new ast::TypeName
886 typ::Type MaybeCreateTypename(typ::Type type) const;
887
Ben Claytonf5f311e2021-04-28 14:31:23 +0000888 /// The ProgramBuilder
889 ProgramBuilder* const builder;
890
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000891 private:
892 /// CToAST<T> is specialized for various `T` types and each specialization
893 /// contains a single static `get()` method for obtaining the corresponding
894 /// AST type for the C type `T`.
895 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000896 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000897 template <typename T>
898 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 };
900
901 //////////////////////////////////////////////////////////////////////////////
902 // AST helper methods
903 //////////////////////////////////////////////////////////////////////////////
904
James Price65ae64d2021-04-29 12:59:14 +0000905 /// @return a new unnamed symbol
906 Symbol Sym() { return Symbols().New(); }
907
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000908 /// @param name the symbol string
909 /// @return a Symbol with the given name
910 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
911
912 /// @param sym the symbol
913 /// @return `sym`
914 Symbol Sym(Symbol sym) { return sym; }
915
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000916 /// @param expr the expression
917 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000918 template <typename T>
919 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
920 return expr;
921 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000922
Ben Claytonb8ea5912021-04-19 16:52:42 +0000923 /// Passthrough for nullptr
924 /// @return nullptr
925 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
926
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000927 /// @param name the identifier name
928 /// @return an ast::IdentifierExpression with the given name
929 ast::IdentifierExpression* Expr(const std::string& name) {
930 return create<ast::IdentifierExpression>(Symbols().Register(name));
931 }
932
Ben Clayton46d78d72021-02-10 21:34:26 +0000933 /// @param symbol the identifier symbol
934 /// @return an ast::IdentifierExpression with the given symbol
935 ast::IdentifierExpression* Expr(Symbol symbol) {
936 return create<ast::IdentifierExpression>(symbol);
937 }
938
Ben Claytonb8ea5912021-04-19 16:52:42 +0000939 /// @param variable the AST variable
940 /// @return an ast::IdentifierExpression with the variable's symbol
941 ast::IdentifierExpression* Expr(ast::Variable* variable) {
942 return create<ast::IdentifierExpression>(variable->symbol());
943 }
944
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000945 /// @param source the source information
946 /// @param name the identifier name
947 /// @return an ast::IdentifierExpression with the given name
948 ast::IdentifierExpression* Expr(const Source& source,
949 const std::string& name) {
950 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
951 }
952
953 /// @param name the identifier name
954 /// @return an ast::IdentifierExpression with the given name
955 ast::IdentifierExpression* Expr(const char* name) {
956 return create<ast::IdentifierExpression>(Symbols().Register(name));
957 }
958
959 /// @param value the boolean value
960 /// @return a Scalar constructor for the given value
961 ast::ScalarConstructorExpression* Expr(bool value) {
962 return create<ast::ScalarConstructorExpression>(Literal(value));
963 }
964
965 /// @param value the float value
966 /// @return a Scalar constructor for the given value
967 ast::ScalarConstructorExpression* Expr(f32 value) {
968 return create<ast::ScalarConstructorExpression>(Literal(value));
969 }
970
971 /// @param value the integer value
972 /// @return a Scalar constructor for the given value
973 ast::ScalarConstructorExpression* Expr(i32 value) {
974 return create<ast::ScalarConstructorExpression>(Literal(value));
975 }
976
977 /// @param value the unsigned int value
978 /// @return a Scalar constructor for the given value
979 ast::ScalarConstructorExpression* Expr(u32 value) {
980 return create<ast::ScalarConstructorExpression>(Literal(value));
981 }
982
983 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
984 /// `list`.
985 /// @param list the list to append too
986 /// @param arg the arg to create
987 template <typename ARG>
988 void Append(ast::ExpressionList& list, ARG&& arg) {
989 list.emplace_back(Expr(std::forward<ARG>(arg)));
990 }
991
992 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
993 /// then appends them to `list`.
994 /// @param list the list to append too
995 /// @param arg0 the first argument
996 /// @param args the rest of the arguments
997 template <typename ARG0, typename... ARGS>
998 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
999 Append(list, std::forward<ARG0>(arg0));
1000 Append(list, std::forward<ARGS>(args)...);
1001 }
1002
1003 /// @return an empty list of expressions
1004 ast::ExpressionList ExprList() { return {}; }
1005
1006 /// @param args the list of expressions
1007 /// @return the list of expressions converted to `ast::Expression`s using
1008 /// `Expr()`,
1009 template <typename... ARGS>
1010 ast::ExpressionList ExprList(ARGS&&... args) {
1011 ast::ExpressionList list;
1012 list.reserve(sizeof...(args));
1013 Append(list, std::forward<ARGS>(args)...);
1014 return list;
1015 }
1016
1017 /// @param list the list of expressions
1018 /// @return `list`
1019 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1020
1021 /// @param val the boolan value
1022 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001023 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001024
1025 /// @param val the float value
1026 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001027 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001028
1029 /// @param val the unsigned int value
1030 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001031 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001032
1033 /// @param val the integer value
1034 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001035 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001036
1037 /// @param args the arguments for the type constructor
1038 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1039 /// of `args` converted to `ast::Expression`s using `Expr()`
1040 template <typename T, typename... ARGS>
1041 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001042 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001043 }
1044
1045 /// @param type the type to construct
1046 /// @param args the arguments for the constructor
1047 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1048 /// values `args`.
1049 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001050 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001051 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001052 return create<ast::TypeConstructorExpression>(
1053 type, ExprList(std::forward<ARGS>(args)...));
1054 }
1055
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001056 /// Creates a constructor expression that constructs an object of
1057 /// `type` filled with `elem_value`. For example,
1058 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1059 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1060 /// @param type the type to construct
1061 /// @param elem_value the initial or element value (for vec and mat) to
1062 /// construct with
1063 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001064 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001065 int elem_value = 0);
1066
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001067 /// @param args the arguments for the vector constructor
1068 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1069 /// `T`, constructed with the values `args`.
1070 template <typename T, typename... ARGS>
1071 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001072 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001073 }
1074
1075 /// @param args the arguments for the vector constructor
1076 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1077 /// `T`, constructed with the values `args`.
1078 template <typename T, typename... ARGS>
1079 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001080 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001081 }
1082
1083 /// @param args the arguments for the vector constructor
1084 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1085 /// `T`, constructed with the values `args`.
1086 template <typename T, typename... ARGS>
1087 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001088 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 }
1090
1091 /// @param args the arguments for the matrix constructor
1092 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1093 /// `T`, constructed with the values `args`.
1094 template <typename T, typename... ARGS>
1095 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001096 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001097 }
1098
1099 /// @param args the arguments for the matrix constructor
1100 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1101 /// `T`, constructed with the values `args`.
1102 template <typename T, typename... ARGS>
1103 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001104 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001105 }
1106
1107 /// @param args the arguments for the matrix constructor
1108 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1109 /// `T`, constructed with the values `args`.
1110 template <typename T, typename... ARGS>
1111 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001112 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001113 }
1114
1115 /// @param args the arguments for the matrix constructor
1116 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1117 /// `T`, constructed with the values `args`.
1118 template <typename T, typename... ARGS>
1119 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001120 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 }
1122
1123 /// @param args the arguments for the matrix constructor
1124 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1125 /// `T`, constructed with the values `args`.
1126 template <typename T, typename... ARGS>
1127 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001128 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001129 }
1130
1131 /// @param args the arguments for the matrix constructor
1132 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1133 /// `T`, constructed with the values `args`.
1134 template <typename T, typename... ARGS>
1135 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001136 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 }
1138
1139 /// @param args the arguments for the matrix constructor
1140 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1141 /// `T`, constructed with the values `args`.
1142 template <typename T, typename... ARGS>
1143 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001144 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001145 }
1146
1147 /// @param args the arguments for the matrix constructor
1148 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1149 /// `T`, constructed with the values `args`.
1150 template <typename T, typename... ARGS>
1151 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001152 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001153 }
1154
1155 /// @param args the arguments for the matrix constructor
1156 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1157 /// `T`, constructed with the values `args`.
1158 template <typename T, typename... ARGS>
1159 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001160 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001161 }
1162
1163 /// @param args the arguments for the array constructor
1164 /// @return an `ast::TypeConstructorExpression` of an array with element type
1165 /// `T`, constructed with the values `args`.
1166 template <typename T, int N = 0, typename... ARGS>
1167 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001168 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001169 }
1170
1171 /// @param subtype the array element type
1172 /// @param n the array size. 0 represents a runtime-array.
1173 /// @param args the arguments for the array constructor
1174 /// @return an `ast::TypeConstructorExpression` of an array with element type
1175 /// `subtype`, constructed with the values `args`.
1176 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001177 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001178 uint32_t n,
1179 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001180 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001181 }
1182
1183 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001184 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001185 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001186 /// @param constructor constructor expression
1187 /// @param decorations variable decorations
1188 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001189 template <typename NAME>
1190 ast::Variable* Var(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001191 ast::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001192 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001193 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001194 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001195 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001196 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1197 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001198 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199
1200 /// @param source the variable source
1201 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001202 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001203 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001204 /// @param constructor constructor expression
1205 /// @param decorations variable decorations
1206 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001207 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001208 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001209 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001210 ast::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001211 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001212 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001213 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001214 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001215 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001216 type, false, constructor, decorations);
1217 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001219 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001220 /// @param type the variable type
1221 /// @param constructor optional constructor expression
1222 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001223 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001224 template <typename NAME>
1225 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001226 ast::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001227 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001228 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001229 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001230 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001231 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001232 constructor, decorations);
1233 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001234
1235 /// @param source the variable source
1236 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001237 /// @param type the variable type
1238 /// @param constructor optional constructor expression
1239 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001240 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001241 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001242 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001243 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001244 ast::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001245 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001246 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001247 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001248 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001249 ast::StorageClass::kNone, type, true,
1250 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001251 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001252
James Pricedaf1f1c2021-04-08 22:15:48 +00001253 /// @param name the parameter name
1254 /// @param type the parameter type
1255 /// @param decorations optional parameter decorations
1256 /// @returns a constant `ast::Variable` with the given name and type
1257 template <typename NAME>
1258 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001259 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001260 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001261 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001262 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1263 ast::StorageClass::kNone, type, true, nullptr,
1264 decorations);
1265 }
1266
1267 /// @param source the parameter source
1268 /// @param name the parameter name
1269 /// @param type the parameter type
1270 /// @param decorations optional parameter decorations
1271 /// @returns a constant `ast::Variable` with the given name and type
1272 template <typename NAME>
1273 ast::Variable* Param(const Source& source,
1274 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001275 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001276 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001277 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001278 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1279 ast::StorageClass::kNone, type, true, nullptr,
1280 decorations);
1281 }
1282
Ben Clayton42708342021-04-21 17:55:12 +00001283 /// @param name the variable name
1284 /// @param type the variable type
1285 /// @param storage the variable storage class
1286 /// @param constructor constructor expression
1287 /// @param decorations variable decorations
1288 /// @returns a new `ast::Variable`, which is automatically registered as a
1289 /// global variable with the ast::Module.
1290 template <typename NAME>
1291 ast::Variable* Global(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001292 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001293 ast::StorageClass storage,
1294 ast::Expression* constructor = nullptr,
1295 ast::DecorationList decorations = {}) {
1296 auto* var =
1297 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1298 AST().AddGlobalVariable(var);
1299 return var;
1300 }
1301
1302 /// @param source the variable source
1303 /// @param name the variable name
1304 /// @param type the variable type
1305 /// @param storage the variable storage class
1306 /// @param constructor constructor expression
1307 /// @param decorations variable decorations
1308 /// @returns a new `ast::Variable`, which is automatically registered as a
1309 /// global variable with the ast::Module.
1310 template <typename NAME>
1311 ast::Variable* Global(const Source& source,
1312 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001313 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001314 ast::StorageClass storage,
1315 ast::Expression* constructor = nullptr,
1316 ast::DecorationList decorations = {}) {
1317 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1318 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001319 AST().AddGlobalVariable(var);
1320 return var;
1321 }
1322
1323 /// @param args the arguments to pass to Const()
1324 /// @returns a const `ast::Variable` constructed by calling Var() with the
1325 /// arguments of `args`, which is automatically registered as a global
1326 /// variable with the ast::Module.
1327 template <typename... ARGS>
1328 ast::Variable* GlobalConst(ARGS&&... args) {
1329 auto* var = Const(std::forward<ARGS>(args)...);
1330 AST().AddGlobalVariable(var);
1331 return var;
1332 }
1333
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001334 /// @param func the function name
1335 /// @param args the function call arguments
1336 /// @returns a `ast::CallExpression` to the function `func`, with the
1337 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1338 template <typename NAME, typename... ARGS>
1339 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1340 return create<ast::CallExpression>(Expr(func),
1341 ExprList(std::forward<ARGS>(args)...));
1342 }
1343
1344 /// @param lhs the left hand argument to the addition operation
1345 /// @param rhs the right hand argument to the addition operation
1346 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1347 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001348 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001349 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1350 Expr(std::forward<LHS>(lhs)),
1351 Expr(std::forward<RHS>(rhs)));
1352 }
1353
1354 /// @param lhs the left hand argument to the subtraction operation
1355 /// @param rhs the right hand argument to the subtraction operation
1356 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1357 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001358 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001359 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1360 Expr(std::forward<LHS>(lhs)),
1361 Expr(std::forward<RHS>(rhs)));
1362 }
1363
1364 /// @param lhs the left hand argument to the multiplication operation
1365 /// @param rhs the right hand argument to the multiplication operation
1366 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1367 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001368 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001369 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1370 Expr(std::forward<LHS>(lhs)),
1371 Expr(std::forward<RHS>(rhs)));
1372 }
1373
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001374 /// @param source the source information
1375 /// @param lhs the left hand argument to the multiplication operation
1376 /// @param rhs the right hand argument to the multiplication operation
1377 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1378 template <typename LHS, typename RHS>
1379 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1380 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1381 Expr(std::forward<LHS>(lhs)),
1382 Expr(std::forward<RHS>(rhs)));
1383 }
1384
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001385 /// @param lhs the left hand argument to the division operation
1386 /// @param rhs the right hand argument to the division operation
1387 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1388 template <typename LHS, typename RHS>
1389 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1390 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1391 Expr(std::forward<LHS>(lhs)),
1392 Expr(std::forward<RHS>(rhs)));
1393 }
1394
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001395 /// @param arr the array argument for the array accessor expression
1396 /// @param idx the index argument for the array accessor expression
1397 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1398 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001399 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001400 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1401 Expr(std::forward<IDX>(idx)));
1402 }
1403
1404 /// @param obj the object for the member accessor expression
1405 /// @param idx the index argument for the array accessor expression
1406 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1407 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001408 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001409 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1410 Expr(std::forward<IDX>(idx)));
1411 }
1412
Ben Clayton42d1e092021-02-02 14:29:15 +00001413 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001414 /// @param val the offset value
1415 /// @returns the offset decoration pointer
1416 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1417 return create<ast::StructMemberOffsetDecoration>(source_, val);
1418 }
1419
Ben Claytond614dd52021-03-15 10:43:11 +00001420 /// Creates a ast::StructMemberSizeDecoration
1421 /// @param source the source information
1422 /// @param val the size value
1423 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001424 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1425 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001426 return create<ast::StructMemberSizeDecoration>(source, val);
1427 }
1428
1429 /// Creates a ast::StructMemberSizeDecoration
1430 /// @param val the size value
1431 /// @returns the size decoration pointer
1432 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1433 return create<ast::StructMemberSizeDecoration>(source_, val);
1434 }
1435
1436 /// Creates a ast::StructMemberAlignDecoration
1437 /// @param source the source information
1438 /// @param val the align value
1439 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001440 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1441 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001442 return create<ast::StructMemberAlignDecoration>(source, val);
1443 }
1444
1445 /// Creates a ast::StructMemberAlignDecoration
1446 /// @param val the align value
1447 /// @returns the align decoration pointer
1448 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1449 return create<ast::StructMemberAlignDecoration>(source_, val);
1450 }
1451
Ben Clayton42d1e092021-02-02 14:29:15 +00001452 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001453 /// @param source the source information
1454 /// @param name the function name
1455 /// @param params the function parameters
1456 /// @param type the function return type
1457 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001458 /// @param decorations the optional function decorations
1459 /// @param return_type_decorations the optional function return type
1460 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001461 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001462 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001463 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001464 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001465 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001466 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001467 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001468 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001469 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001470 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001471 auto* func =
1472 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1473 type, create<ast::BlockStatement>(body),
1474 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001475 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001476 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001477 }
1478
Ben Clayton42d1e092021-02-02 14:29:15 +00001479 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001480 /// @param name the function name
1481 /// @param params the function parameters
1482 /// @param type the function return type
1483 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001484 /// @param decorations the optional function decorations
1485 /// @param return_type_decorations the optional function return type
1486 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001487 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001488 template <typename NAME>
1489 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001490 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001491 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001492 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001493 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001494 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001495 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001496 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1497 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001498 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001499 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001500 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001501 }
1502
Ben Clayton49668bb2021-04-17 05:32:01 +00001503 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001504 /// @param source the source information
1505 /// @returns the return statement pointer
1506 ast::ReturnStatement* Return(const Source& source) {
1507 return create<ast::ReturnStatement>(source);
1508 }
1509
1510 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001511 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001512 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1513
1514 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001515 /// @param source the source information
1516 /// @param val the return value
1517 /// @returns the return statement pointer
1518 template <typename EXPR>
1519 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1520 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1521 }
1522
1523 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001524 /// @param val the return value
1525 /// @returns the return statement pointer
1526 template <typename EXPR>
1527 ast::ReturnStatement* Return(EXPR&& val) {
1528 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001529 }
1530
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001531 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001532 /// @param source the source information
1533 /// @param name the struct name
1534 /// @param members the struct members
1535 /// @param decorations the optional struct decorations
1536 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001537 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001538 ast::Struct* Structure(const Source& source,
1539 NAME&& name,
1540 ast::StructMemberList members,
1541 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001542 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001543 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001544 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001545 AST().AddConstructedType(type);
1546 return type;
1547 }
1548
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001549 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001550 /// @param name the struct name
1551 /// @param members the struct members
1552 /// @param decorations the optional struct decorations
1553 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001554 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001555 ast::Struct* Structure(NAME&& name,
1556 ast::StructMemberList members,
1557 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001558 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001559 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001560 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001561 AST().AddConstructedType(type);
1562 return type;
1563 }
1564
Ben Clayton42d1e092021-02-02 14:29:15 +00001565 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001566 /// @param source the source information
1567 /// @param name the struct member name
1568 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001569 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001570 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001571 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001572 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001573 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001574 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001575 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001576 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001577 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1578 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001579 }
1580
Ben Clayton42d1e092021-02-02 14:29:15 +00001581 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001582 /// @param name the struct member name
1583 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001584 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001585 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001586 template <typename NAME>
1587 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001588 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001589 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001590 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001591 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1592 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001593 }
1594
Ben Claytonbab31972021-02-08 21:16:21 +00001595 /// Creates a ast::StructMember with the given byte offset
1596 /// @param offset the offset to use in the StructMemberOffsetDecoration
1597 /// @param name the struct member name
1598 /// @param type the struct member type
1599 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001600 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001601 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001602 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001603 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001604 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001605 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001606 create<ast::StructMemberOffsetDecoration>(offset),
1607 });
1608 }
1609
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001610 /// Creates a ast::BlockStatement with input statements
1611 /// @param statements statements of block
1612 /// @returns the block statement pointer
1613 template <typename... Statements>
1614 ast::BlockStatement* Block(Statements&&... statements) {
1615 return create<ast::BlockStatement>(
1616 ast::StatementList{std::forward<Statements>(statements)...});
1617 }
1618
1619 /// Creates a ast::ElseStatement with input condition and body
1620 /// @param condition the else condition expression
1621 /// @param body the else body
1622 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001623 template <typename CONDITION>
1624 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1625 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1626 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001627 }
1628
1629 /// Creates a ast::IfStatement with input condition, body, and optional
1630 /// variadic else statements
1631 /// @param condition the if statement condition expression
1632 /// @param body the if statement body
1633 /// @param elseStatements optional variadic else statements
1634 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001635 template <typename CONDITION, typename... ELSE_STATEMENTS>
1636 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001637 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001638 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001639 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001640 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001641 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001642 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001643 }
1644
1645 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001646 /// @param source the source information
1647 /// @param lhs the left hand side expression initializer
1648 /// @param rhs the right hand side expression initializer
1649 /// @returns the assignment statement pointer
1650 template <typename LhsExpressionInit, typename RhsExpressionInit>
1651 ast::AssignmentStatement* Assign(const Source& source,
1652 LhsExpressionInit&& lhs,
1653 RhsExpressionInit&& rhs) {
1654 return create<ast::AssignmentStatement>(
1655 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1656 Expr(std::forward<RhsExpressionInit>(rhs)));
1657 }
1658
1659 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001660 /// @param lhs the left hand side expression initializer
1661 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001662 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001663 template <typename LhsExpressionInit, typename RhsExpressionInit>
1664 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1665 RhsExpressionInit&& rhs) {
1666 return create<ast::AssignmentStatement>(
1667 Expr(std::forward<LhsExpressionInit>(lhs)),
1668 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001669 }
1670
1671 /// Creates a ast::LoopStatement with input body and optional continuing
1672 /// @param body the loop body
1673 /// @param continuing the optional continuing block
1674 /// @returns the loop statement pointer
1675 ast::LoopStatement* Loop(ast::BlockStatement* body,
1676 ast::BlockStatement* continuing = nullptr) {
1677 return create<ast::LoopStatement>(body, continuing);
1678 }
1679
1680 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001681 /// @param source the source information
1682 /// @param var the variable to wrap in a decl statement
1683 /// @returns the variable decl statement pointer
1684 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1685 return create<ast::VariableDeclStatement>(source, var);
1686 }
1687
1688 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001689 /// @param var the variable to wrap in a decl statement
1690 /// @returns the variable decl statement pointer
1691 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1692 return create<ast::VariableDeclStatement>(var);
1693 }
1694
Antonio Maioranocea744d2021-03-25 12:55:27 +00001695 /// Creates a ast::SwitchStatement with input expression and cases
1696 /// @param condition the condition expression initializer
1697 /// @param cases case statements
1698 /// @returns the switch statement pointer
1699 template <typename ExpressionInit, typename... Cases>
1700 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1701 return create<ast::SwitchStatement>(
1702 Expr(std::forward<ExpressionInit>(condition)),
1703 ast::CaseStatementList{std::forward<Cases>(cases)...});
1704 }
1705
1706 /// Creates a ast::CaseStatement with input list of selectors, and body
1707 /// @param selectors list of selectors
1708 /// @param body the case body
1709 /// @returns the case statement pointer
1710 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1711 ast::BlockStatement* body = nullptr) {
1712 return create<ast::CaseStatement>(std::move(selectors),
1713 body ? body : Block());
1714 }
1715
1716 /// Convenient overload that takes a single selector
1717 /// @param selector a single case selector
1718 /// @param body the case body
1719 /// @returns the case statement pointer
1720 ast::CaseStatement* Case(ast::IntLiteral* selector,
1721 ast::BlockStatement* body = nullptr) {
1722 return Case(ast::CaseSelectorList{selector}, body);
1723 }
1724
1725 /// Convenience function that creates a 'default' ast::CaseStatement
1726 /// @param body the case body
1727 /// @returns the case statement pointer
1728 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1729 return Case(ast::CaseSelectorList{}, body);
1730 }
1731
James Price68f558f2021-04-06 15:51:47 +00001732 /// Creates an ast::BuiltinDecoration
1733 /// @param source the source information
1734 /// @param builtin the builtin value
1735 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001736 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001737 return create<ast::BuiltinDecoration>(source, builtin);
1738 }
1739
1740 /// Creates an ast::BuiltinDecoration
1741 /// @param builtin the builtin value
1742 /// @returns the builtin decoration pointer
1743 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1744 return create<ast::BuiltinDecoration>(source_, builtin);
1745 }
1746
1747 /// Creates an ast::LocationDecoration
1748 /// @param source the source information
1749 /// @param location the location value
1750 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001751 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001752 return create<ast::LocationDecoration>(source, location);
1753 }
1754
1755 /// Creates an ast::LocationDecoration
1756 /// @param location the location value
1757 /// @returns the location decoration pointer
1758 ast::LocationDecoration* Location(uint32_t location) {
1759 return create<ast::LocationDecoration>(source_, location);
1760 }
1761
1762 /// Creates an ast::StageDecoration
1763 /// @param source the source information
1764 /// @param stage the pipeline stage
1765 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001766 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001767 return create<ast::StageDecoration>(source, stage);
1768 }
1769
1770 /// Creates an ast::StageDecoration
1771 /// @param stage the pipeline stage
1772 /// @returns the stage decoration pointer
1773 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1774 return create<ast::StageDecoration>(source_, stage);
1775 }
1776
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001777 /// Sets the current builder source to `src`
1778 /// @param src the Source used for future create() calls
1779 void SetSource(const Source& src) {
1780 AssertNotMoved();
1781 source_ = src;
1782 }
1783
1784 /// Sets the current builder source to `loc`
1785 /// @param loc the Source used for future create() calls
1786 void SetSource(const Source::Location& loc) {
1787 AssertNotMoved();
1788 source_ = Source(loc);
1789 }
1790
Ben Clayton33352542021-01-29 16:43:41 +00001791 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001792 /// @note As the Resolver is run when the Program is built, this will only be
1793 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001794 /// @param expr the AST expression
1795 /// @return the resolved semantic type for the expression, or nullptr if the
1796 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001797 sem::Type* TypeOf(const ast::Expression* expr) const;
1798
1799 /// Helper for returning the resolved semantic type of the AST type `type`.
1800 /// @note As the Resolver is run when the Program is built, this will only be
1801 /// useful for the Resolver itself and tests that use their own Resolver.
1802 /// @param expr the AST type
1803 /// @return the resolved semantic type for the type, or nullptr if the type
1804 /// has no resolved type.
1805 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001806
Ben Clayton169512e2021-04-17 05:52:11 +00001807 /// Wraps the ast::Literal in a statement. This is used by tests that
1808 /// construct a partial AST and require the Resolver to reach these
1809 /// nodes.
1810 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1811 /// @return the ast::Statement that wraps the ast::Statement
1812 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001813 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001814 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001815 /// nodes.
1816 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1817 /// @return the ast::Statement that wraps the ast::Expression
1818 ast::Statement* WrapInStatement(ast::Expression* expr);
1819 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001820 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001821 /// these nodes.
1822 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1823 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1824 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1825 /// Returns the statement argument. Used as a passthrough-overload by
1826 /// WrapInFunction().
1827 /// @param stmt the ast::Statement
1828 /// @return `stmt`
1829 ast::Statement* WrapInStatement(ast::Statement* stmt);
1830 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001831 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001832 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001833 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001834 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001835 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001836 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001837 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001838 }
1839 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001840 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001841 /// @returns the function
1842 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001843
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001844 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001845 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001846
1847 protected:
1848 /// Asserts that the builder has not been moved.
1849 void AssertNotMoved() const;
1850
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001851 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001852 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001853 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001854 ASTNodeAllocator ast_nodes_;
1855 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001856 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001857 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001858 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001859 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001860
1861 /// The source to use when creating AST nodes without providing a Source as
1862 /// the first argument.
1863 Source source_;
1864
Ben Clayton5f0ea112021-03-09 10:54:37 +00001865 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001866 /// program when built.
1867 bool resolve_on_build_ = true;
1868
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001869 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1870 bool moved_ = false;
1871};
1872
1873//! @cond Doxygen_Suppress
1874// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1875template <>
1876struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001877 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001878 return t->i32();
1879 }
1880};
1881template <>
1882struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001883 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001884 return t->u32();
1885 }
1886};
1887template <>
1888struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001889 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001890 return t->f32();
1891 }
1892};
1893template <>
1894struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001895 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001896 return t->bool_();
1897 }
1898};
1899template <>
1900struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001901 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001902 return t->void_();
1903 }
1904};
1905//! @endcond
1906
Ben Clayton917b14b2021-04-19 16:50:23 +00001907/// @param builder the ProgramBuilder
1908/// @returns the ProgramID of the ProgramBuilder
1909inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1910 return builder->ID();
1911}
1912
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001913} // namespace tint
1914
1915#endif // SRC_PROGRAM_BUILDER_H_