blob: 41d3842837f35ed019e451ffa7c20d80c9fbe7ff [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"
63#include "src/sem/alias_type.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000064#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000065#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000066#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000067#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000068#include "src/sem/f32_type.h"
69#include "src/sem/i32_type.h"
70#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000071#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000072#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000073#include "src/sem/sampled_texture_type.h"
74#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000075#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000076#include "src/sem/u32_type.h"
77#include "src/sem/vector_type.h"
78#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000079#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000080
81namespace tint {
82
Ben Clayton401b96b2021-02-03 17:19:59 +000083// Forward declarations
84namespace ast {
85class VariableDeclStatement;
86} // namespace ast
87
Ben Claytona6b9a8e2021-01-26 16:57:10 +000088class CloneContext;
89
90/// ProgramBuilder is a mutable builder for a Program.
91/// To construct a Program, populate the builder and then `std::move` it to a
92/// Program.
93class ProgramBuilder {
94 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000095 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
96 using ASTNodeAllocator = BlockAllocator<ast::Node>;
97
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000098 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
99 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000100
101 /// `i32` is a type alias to `int`.
102 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
103 /// WGSL syntax.
104 /// Note: this is intentionally not aliased to uint32_t as we want integer
105 /// literals passed to the builder to match WGSL's integer literal types.
106 using i32 = decltype(1);
107 /// `u32` is a type alias to `unsigned int`.
108 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
109 /// WGSL syntax.
110 /// Note: this is intentionally not aliased to uint32_t as we want integer
111 /// literals passed to the builder to match WGSL's integer literal types.
112 using u32 = decltype(1u);
113 /// `f32` is a type alias to `float`
114 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
115 /// WGSL syntax.
116 using f32 = float;
117
118 /// Constructor
119 ProgramBuilder();
120
121 /// Move constructor
122 /// @param rhs the builder to move
123 ProgramBuilder(ProgramBuilder&& rhs);
124
125 /// Destructor
126 virtual ~ProgramBuilder();
127
128 /// Move assignment operator
129 /// @param rhs the builder to move
130 /// @return this builder
131 ProgramBuilder& operator=(ProgramBuilder&& rhs);
132
Ben Claytone43c8302021-01-29 11:59:32 +0000133 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
134 /// making a deep clone of the Program contents.
135 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
136 /// existing immutable program.
137 /// As the returned ProgramBuilder wraps `program`, `program` must not be
138 /// destructed or assigned while using the returned ProgramBuilder.
139 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
140 /// function. See crbug.com/tint/460.
141 /// @param program the immutable Program to wrap
142 /// @return the ProgramBuilder that wraps `program`
143 static ProgramBuilder Wrap(const Program* program);
144
Ben Claytone6995de2021-04-13 23:27:27 +0000145 /// @returns the unique identifier for this program
146 ProgramID ID() const { return id_; }
147
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000148 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000149 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000150 AssertNotMoved();
151 return types_;
152 }
153
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000154 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000155 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000156 AssertNotMoved();
157 return types_;
158 }
159
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000160 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000161 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000162 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000163 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000164 }
165
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000166 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000167 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000168 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000169 return ast_nodes_;
170 }
171
172 /// @returns a reference to the program's semantic nodes storage
173 SemNodeAllocator& SemNodes() {
174 AssertNotMoved();
175 return sem_nodes_;
176 }
177
178 /// @returns a reference to the program's semantic nodes storage
179 const SemNodeAllocator& SemNodes() const {
180 AssertNotMoved();
181 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000182 }
183
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000184 /// @returns a reference to the program's AST root Module
185 ast::Module& AST() {
186 AssertNotMoved();
187 return *ast_;
188 }
189
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000190 /// @returns a reference to the program's AST root Module
191 const ast::Module& AST() const {
192 AssertNotMoved();
193 return *ast_;
194 }
195
196 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000197 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000198 AssertNotMoved();
199 return sem_;
200 }
201
202 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000203 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000204 AssertNotMoved();
205 return sem_;
206 }
207
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000208 /// @returns a reference to the program's SymbolTable
209 SymbolTable& Symbols() {
210 AssertNotMoved();
211 return symbols_;
212 }
213
Ben Clayton708dc2d2021-01-29 11:22:40 +0000214 /// @returns a reference to the program's SymbolTable
215 const SymbolTable& Symbols() const {
216 AssertNotMoved();
217 return symbols_;
218 }
219
Ben Clayton844217f2021-01-27 18:49:05 +0000220 /// @returns a reference to the program's diagnostics
221 diag::List& Diagnostics() {
222 AssertNotMoved();
223 return diagnostics_;
224 }
225
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000226 /// @returns a reference to the program's diagnostics
227 const diag::List& Diagnostics() const {
228 AssertNotMoved();
229 return diagnostics_;
230 }
231
Ben Clayton5f0ea112021-03-09 10:54:37 +0000232 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000233 /// @param enable the new flag value (defaults to true)
234 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
235
Ben Clayton5f0ea112021-03-09 10:54:37 +0000236 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000237 /// built.
238 bool ResolveOnBuild() const { return resolve_on_build_; }
239
Ben Clayton844217f2021-01-27 18:49:05 +0000240 /// @returns true if the program has no error diagnostics and is not missing
241 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000242 bool IsValid() const;
243
Ben Clayton708dc2d2021-01-29 11:22:40 +0000244 /// Writes a representation of the node to the output stream
245 /// @note unlike str(), to_str() does not automatically demangle the string.
246 /// @param node the AST node
247 /// @param out the stream to write to
248 /// @param indent number of spaces to indent the node when writing
249 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
250 node->to_str(Sem(), out, indent);
251 }
252
253 /// Returns a demangled, string representation of `node`.
254 /// @param node the AST node
255 /// @returns a string representation of the node
256 std::string str(const ast::Node* node) const;
257
Ben Clayton7fdfff12021-01-29 15:17:30 +0000258 /// Creates a new ast::Node owned by the ProgramBuilder. When the
259 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000260 /// @param source the Source of the node
261 /// @param args the arguments to pass to the type constructor
262 /// @returns the node pointer
263 template <typename T, typename... ARGS>
264 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
265 ARGS&&... args) {
266 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000267 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000268 }
269
Ben Clayton7fdfff12021-01-29 15:17:30 +0000270 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
271 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000272 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000273 /// When the ProgramBuilder is destructed, the ast::Node will also be
274 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000275 /// @returns the node pointer
276 template <typename T>
277 traits::EnableIfIsType<T, ast::Node>* create() {
278 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000279 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000280 }
281
Ben Clayton7fdfff12021-01-29 15:17:30 +0000282 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
283 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000284 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000285 /// When the ProgramBuilder is destructed, the ast::Node will also be
286 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000287 /// @param arg0 the first arguments to pass to the type constructor
288 /// @param args the remaining arguments to pass to the type constructor
289 /// @returns the node pointer
290 template <typename T, typename ARG0, typename... ARGS>
291 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
292 traits::IsTypeOrDerived<T, ast::Node>::value &&
293 !traits::IsTypeOrDerived<ARG0, Source>::value,
294 T>*
295 create(ARG0&& arg0, ARGS&&... args) {
296 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000297 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000298 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000299 }
300
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000301 /// Creates a new sem::Node owned by the ProgramBuilder.
302 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000303 /// destructed.
304 /// @param args the arguments to pass to the type constructor
305 /// @returns the node pointer
306 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000307 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
308 !traits::IsTypeOrDerived<T, sem::Type>::value,
309 T>*
310 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000311 AssertNotMoved();
312 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
313 }
314
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000315 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000316 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
317 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000318 /// Types are unique (de-aliased), and so calling create() for the same `T`
319 /// and arguments will return the same pointer.
320 /// @warning Use this method to acquire a type only if all of its type
321 /// information is provided in the constructor arguments `args`.<br>
322 /// If the type requires additional configuration after construction that
323 /// affect its fundamental type, build the type with `std::make_unique`, make
324 /// any necessary alterations and then call unique_type() instead.
325 /// @param args the arguments to pass to the type constructor
326 /// @returns the de-aliased type pointer
327 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000328 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
329 static_assert(std::is_base_of<sem::Type, T>::value,
330 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000331 AssertNotMoved();
332 return types_.Get<T>(std::forward<ARGS>(args)...);
333 }
334
335 /// Marks this builder as moved, preventing any further use of the builder.
336 void MarkAsMoved();
337
338 //////////////////////////////////////////////////////////////////////////////
339 // TypesBuilder
340 //////////////////////////////////////////////////////////////////////////////
341
342 /// TypesBuilder holds basic `tint` types and methods for constructing
343 /// complex types.
344 class TypesBuilder {
345 public:
346 /// Constructor
347 /// @param builder the program builder
348 explicit TypesBuilder(ProgramBuilder* builder);
349
350 /// @return the tint AST type for the C type `T`.
351 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000352 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000353 return CToAST<T>::get(this);
354 }
355
356 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000357 typ::Bool bool_() const {
358 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
359 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000360
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000361 /// @param source the Source of the node
362 /// @returns a boolean type
363 typ::Bool bool_(const Source& source) const {
364 return {builder->create<ast::Bool>(source), builder->create<sem::Bool>()};
365 }
366
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000367 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000368 typ::F32 f32() const {
369 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
370 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000371
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000372 /// @param source the Source of the node
373 /// @returns a f32 type
374 typ::F32 f32(const Source& source) const {
375 return {builder->create<ast::F32>(source), builder->create<sem::F32>()};
376 }
377
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000378 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000379 typ::I32 i32() const {
380 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
381 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000382
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000383 /// @param source the Source of the node
384 /// @returns a i32 type
385 typ::I32 i32(const Source& source) const {
386 return {builder->create<ast::I32>(source), builder->create<sem::I32>()};
387 }
388
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000389 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000390 typ::U32 u32() const {
391 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
392 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000393
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000394 /// @param source the Source of the node
395 /// @returns a u32 type
396 typ::U32 u32(const Source& source) const {
397 return {builder->create<ast::U32>(source), builder->create<sem::U32>()};
398 }
399
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000400 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000401 typ::Void void_() const {
402 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
403 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000404
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000405 /// @param source the Source of the node
406 /// @returns a void type
407 typ::Void void_(const Source& source) const {
408 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
409 }
410
Antonio Maiorano25436862021-04-13 13:32:33 +0000411 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000412 /// @param n vector width in elements
413 /// @return the tint AST type for a `n`-element vector of `type`.
414 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000415 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000416 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
417 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000418 }
419
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 /// @param source the Source of the node
421 /// @param type vector subtype
422 /// @param n vector width in elements
423 /// @return the tint AST type for a `n`-element vector of `type`.
424 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000425 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000426 return {
427 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
428 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000429 }
430
Antonio Maiorano25436862021-04-13 13:32:33 +0000431 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000432 /// @return the tint AST type for a 2-element vector of `type`.
433 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
434
435 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000436 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000437 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000438
439 /// @param type vector subtype
440 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000441 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
442
443 /// @param n vector width in elements
444 /// @return the tint AST type for a `n`-element vector of `type`.
445 template <typename T>
446 typ::Vector vec(uint32_t n) const {
447 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000448 }
449
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000450 /// @return the tint AST type for a 2-element vector of the C type `T`.
451 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000452 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000453 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000454 }
455
456 /// @return the tint AST type for a 3-element vector of the C type `T`.
457 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000458 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000459 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000460 }
461
462 /// @return the tint AST type for a 4-element vector of the C type `T`.
463 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000464 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000465 return vec4(Of<T>());
466 }
467
468 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000469 /// @param columns number of columns for the matrix
470 /// @param rows number of rows for the matrix
471 /// @return the tint AST type for a matrix of `type`
472 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000473 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000474 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
475 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000476 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000477 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000478 }
479
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000480 /// @param source the Source of the node
481 /// @param type matrix subtype
482 /// @param columns number of columns for the matrix
483 /// @param rows number of rows for the matrix
484 /// @return the tint AST type for a matrix of `type`
485 typ::Matrix mat(const Source& source,
486 typ::Type type,
487 uint32_t columns,
488 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000489 return {type.ast
490 ? builder->create<ast::Matrix>(source, type, rows, columns)
491 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000492 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000493 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000494 }
495
Ben Claytone204f272021-04-22 14:40:23 +0000496 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000497 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000498 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000499
500 /// @param type matrix subtype
501 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000502 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000503
504 /// @param type matrix subtype
505 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000506 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000507
508 /// @param type matrix subtype
509 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000510 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000511
512 /// @param type matrix subtype
513 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000514 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000515
516 /// @param type matrix subtype
517 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000518 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000519
520 /// @param type matrix subtype
521 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000522 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000523
524 /// @param type matrix subtype
525 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000526 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000527
528 /// @param type matrix subtype
529 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000530 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000531
Ben Claytone204f272021-04-22 14:40:23 +0000532 /// @param columns number of columns for the matrix
533 /// @param rows number of rows for the matrix
534 /// @return the tint AST type for a matrix of `type`
535 template <typename T>
536 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
537 return mat(Of<T>(), columns, rows);
538 }
539
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000540 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
541 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000542 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000543 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000544 }
545
546 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
547 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000548 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000549 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000550 }
551
552 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
553 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000554 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000555 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000556 }
557
558 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
559 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000560 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000561 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000562 }
563
564 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
565 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000566 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000567 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000568 }
569
570 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
571 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000572 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000573 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000574 }
575
576 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
577 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000578 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000579 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000580 }
581
582 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
583 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000584 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000585 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000586 }
587
588 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
589 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000590 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000591 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000592 }
593
594 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000595 /// @param n the array size. 0 represents a runtime-array
596 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000597 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000598 ast::Array* array(typ::Type subtype,
599 uint32_t n = 0,
600 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000601 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000602 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000603 }
604
605 /// @param source the Source of the node
606 /// @param subtype the array element type
607 /// @param n the array size. 0 represents a runtime-array
608 /// @param decos the optional decorations for the array
609 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000610 ast::Array* array(const Source& source,
611 typ::Type subtype,
612 uint32_t n = 0,
613 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000614 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000615 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000616 }
617
Ben Claytonbab31972021-02-08 21:16:21 +0000618 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000619 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000620 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000621 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000622 ast::Array* array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000623 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000624 ast::DecorationList decos;
625 if (stride) {
626 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
627 }
628 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000629 }
630
Ben Clayton0f88b312021-05-04 17:36:31 +0000631 /// @param source the Source of the node
632 /// @param subtype the array element type
633 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000634 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000635 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000636 ast::Array* array(const Source& source,
637 typ::Type subtype,
638 uint32_t n,
639 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000640 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000641 ast::DecorationList decos;
642 if (stride) {
643 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
644 }
645 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000646 }
647
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000648 /// @return the tint AST type for an array of size `N` of type `T`
649 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000650 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000651 return array(Of<T>(), N);
652 }
653
Ben Claytonbab31972021-02-08 21:16:21 +0000654 /// @param stride the array stride
655 /// @return the tint AST type for an array of size `N` of type `T`
656 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000657 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000658 return array(Of<T>(), N, stride);
659 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000660
Ben Clayton0f88b312021-05-04 17:36:31 +0000661 /// Creates a type name
662 /// @param name the name
663 /// @returns the type name
664 template <typename NAME>
665 ast::TypeName* type_name(NAME&& name) const {
666 return builder->create<ast::TypeName>(
667 builder->Sym(std::forward<NAME>(name)));
668 }
669
670 /// Creates a type name
671 /// @param source the Source of the node
672 /// @param name the name
673 /// @returns the type name
674 template <typename NAME>
675 ast::TypeName* type_name(const Source& source, NAME&& name) const {
676 return builder->create<ast::TypeName>(
677 source, builder->Sym(std::forward<NAME>(name)));
678 }
679
Ben Clayton42d1e092021-02-02 14:29:15 +0000680 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000681 /// @param name the alias name
682 /// @param type the alias type
683 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000684 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +0000685 typ::Alias alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000686 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000687 auto sym = builder->Sym(std::forward<NAME>(name));
688 return {
Ben Clayton0f88b312021-05-04 17:36:31 +0000689 type.ast ? builder->create<ast::Alias>(sym, type) : nullptr,
690 type.sem ? builder->create<sem::Alias>(sym, type) : nullptr,
Ben Claytone204f272021-04-22 14:40:23 +0000691 };
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000692 }
693
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000694 /// Creates an alias type
695 /// @param source the Source of the node
696 /// @param name the alias name
697 /// @param type the alias type
698 /// @returns the alias pointer
699 template <typename NAME>
700 typ::Alias alias(const Source& source, NAME&& name, typ::Type type) const {
701 type = MaybeCreateTypename(type);
702 auto sym = builder->Sym(std::forward<NAME>(name));
703 return {
Ben Clayton95c2e952021-04-28 12:58:13 +0000704 type.ast ? builder->create<ast::Alias>(source, sym, type) : nullptr,
705 type.sem ? builder->create<sem::Alias>(sym, type) : nullptr,
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000706 };
707 }
708
Ben Clayton4db10dd2021-04-16 08:47:14 +0000709 /// Creates an access control qualifier type
710 /// @param access the access control
711 /// @param type the inner type
712 /// @returns the access control qualifier type
Ben Claytone204f272021-04-22 14:40:23 +0000713 typ::AccessControl access(ast::AccessControl::Access access,
714 typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000715 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000716 return {type.ast ? builder->create<ast::AccessControl>(access, type)
717 : nullptr,
718 type.sem ? builder->create<sem::AccessControl>(access, type)
719 : nullptr};
Ben Clayton4db10dd2021-04-16 08:47:14 +0000720 }
721
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000722 /// Creates an access control qualifier type
723 /// @param source the Source of the node
724 /// @param access the access control
725 /// @param type the inner type
726 /// @returns the access control qualifier type
727 typ::AccessControl access(const Source& source,
728 ast::AccessControl::Access access,
729 typ::Type type) const {
730 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000731 return {type.ast
732 ? builder->create<ast::AccessControl>(source, access, type)
733 : nullptr,
734 type.sem ? builder->create<sem::AccessControl>(access, type)
735 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000736 }
737
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000738 /// @param type the type of the pointer
739 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000740 /// @return the pointer to `type` with the given ast::StorageClass
741 typ::Pointer pointer(typ::Type type,
742 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000743 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000744 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
745 : nullptr,
746 type.sem ? builder->create<sem::Pointer>(type, storage_class)
747 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000748 }
749
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000750 /// @param source the Source of the node
751 /// @param type the type of the pointer
752 /// @param storage_class the storage class of the pointer
753 /// @return the pointer to `type` with the given ast::StorageClass
754 typ::Pointer pointer(const Source& source,
755 typ::Type type,
756 ast::StorageClass storage_class) const {
757 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000758 return {type.ast
759 ? builder->create<ast::Pointer>(source, type, storage_class)
760 : nullptr,
761 type.sem ? builder->create<sem::Pointer>(type, storage_class)
762 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000763 }
764
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000765 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000766 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000767 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000768 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000769 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000770 }
771
Ben Claytone204f272021-04-22 14:40:23 +0000772 /// @param kind the kind of sampler
773 /// @returns the sampler
774 typ::Sampler sampler(ast::SamplerKind kind) const {
775 return {builder->create<ast::Sampler>(kind),
776 builder->create<sem::Sampler>(kind)};
777 }
778
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000779 /// @param source the Source of the node
780 /// @param kind the kind of sampler
781 /// @returns the sampler
782 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
783 return {builder->create<ast::Sampler>(source, kind),
784 builder->create<sem::Sampler>(kind)};
785 }
786
Ben Claytone204f272021-04-22 14:40:23 +0000787 /// @param dims the dimensionality of the texture
788 /// @returns the depth texture
789 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
790 return {builder->create<ast::DepthTexture>(dims),
791 builder->create<sem::DepthTexture>(dims)};
792 }
793
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000794 /// @param source the Source of the node
795 /// @param dims the dimensionality of the texture
796 /// @returns the depth texture
797 typ::DepthTexture depth_texture(const Source& source,
798 ast::TextureDimension dims) const {
799 return {builder->create<ast::DepthTexture>(source, dims),
800 builder->create<sem::DepthTexture>(dims)};
801 }
802
Ben Claytone204f272021-04-22 14:40:23 +0000803 /// @param dims the dimensionality of the texture
804 /// @param subtype the texture subtype.
805 /// @returns the sampled texture
806 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
807 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000808 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
809 : nullptr,
810 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
811 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000812 }
813
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000814 /// @param source the Source of the node
815 /// @param dims the dimensionality of the texture
816 /// @param subtype the texture subtype.
817 /// @returns the sampled texture
818 typ::SampledTexture sampled_texture(const Source& source,
819 ast::TextureDimension dims,
820 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000821 return {subtype.ast
822 ? builder->create<ast::SampledTexture>(source, dims, subtype)
823 : nullptr,
824 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
825 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000826 }
827
Ben Claytone204f272021-04-22 14:40:23 +0000828 /// @param dims the dimensionality of the texture
829 /// @param subtype the texture subtype.
830 /// @returns the multisampled texture
831 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
832 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000833 return {
834 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
835 : nullptr,
836 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
837 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000838 }
839
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000840 /// @param source the Source of the node
841 /// @param dims the dimensionality of the texture
842 /// @param subtype the texture subtype.
843 /// @returns the multisampled texture
844 typ::MultisampledTexture multisampled_texture(const Source& source,
845 ast::TextureDimension dims,
846 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000847 return {
848 subtype.ast
849 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
850 : nullptr,
851 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
852 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000853 }
854
Ben Claytone204f272021-04-22 14:40:23 +0000855 /// @param dims the dimensionality of the texture
856 /// @param format the image format of the texture
857 /// @returns the storage texture
858 typ::StorageTexture storage_texture(ast::TextureDimension dims,
859 ast::ImageFormat format) const {
860 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
861 auto* sem_subtype =
862 sem::StorageTexture::SubtypeFor(format, builder->Types());
863 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
864 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000865 }
866
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000867 /// @param source the Source of the node
868 /// @param dims the dimensionality of the texture
869 /// @param format the image format of the texture
870 /// @returns the storage texture
871 typ::StorageTexture storage_texture(const Source& source,
872 ast::TextureDimension dims,
873 ast::ImageFormat format) const {
874 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
875 auto* sem_subtype =
876 sem::StorageTexture::SubtypeFor(format, builder->Types());
877 return {builder->create<ast::StorageTexture>(source, dims, format,
878 ast_subtype),
879 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
880 }
881
882 /// @param source the Source of the node
883 /// @returns the external texture
884 typ::ExternalTexture external_texture(const Source& source) const {
885 return {builder->create<ast::ExternalTexture>(source),
886 builder->create<sem::ExternalTexture>()};
887 }
888
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000889 /// If ty is a ast::Struct or ast::Alias, the returned type is an
890 /// ast::TypeName of the given type's name, otherwise type is returned.
891 /// @param type the type
892 /// @return either type or a pointer to a new ast::TypeName
893 typ::Type MaybeCreateTypename(typ::Type type) const;
894
Ben Claytonf5f311e2021-04-28 14:31:23 +0000895 /// The ProgramBuilder
896 ProgramBuilder* const builder;
897
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000898 private:
899 /// CToAST<T> is specialized for various `T` types and each specialization
900 /// contains a single static `get()` method for obtaining the corresponding
901 /// AST type for the C type `T`.
902 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000903 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000904 template <typename T>
905 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000906 };
907
908 //////////////////////////////////////////////////////////////////////////////
909 // AST helper methods
910 //////////////////////////////////////////////////////////////////////////////
911
James Price65ae64d2021-04-29 12:59:14 +0000912 /// @return a new unnamed symbol
913 Symbol Sym() { return Symbols().New(); }
914
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000915 /// @param name the symbol string
916 /// @return a Symbol with the given name
917 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
918
919 /// @param sym the symbol
920 /// @return `sym`
921 Symbol Sym(Symbol sym) { return sym; }
922
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000923 /// @param expr the expression
924 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000925 template <typename T>
926 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
927 return expr;
928 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000929
Ben Claytonb8ea5912021-04-19 16:52:42 +0000930 /// Passthrough for nullptr
931 /// @return nullptr
932 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
933
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000934 /// @param name the identifier name
935 /// @return an ast::IdentifierExpression with the given name
936 ast::IdentifierExpression* Expr(const std::string& name) {
937 return create<ast::IdentifierExpression>(Symbols().Register(name));
938 }
939
Ben Clayton46d78d72021-02-10 21:34:26 +0000940 /// @param symbol the identifier symbol
941 /// @return an ast::IdentifierExpression with the given symbol
942 ast::IdentifierExpression* Expr(Symbol symbol) {
943 return create<ast::IdentifierExpression>(symbol);
944 }
945
Ben Claytonb8ea5912021-04-19 16:52:42 +0000946 /// @param variable the AST variable
947 /// @return an ast::IdentifierExpression with the variable's symbol
948 ast::IdentifierExpression* Expr(ast::Variable* variable) {
949 return create<ast::IdentifierExpression>(variable->symbol());
950 }
951
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000952 /// @param source the source information
953 /// @param name the identifier name
954 /// @return an ast::IdentifierExpression with the given name
955 ast::IdentifierExpression* Expr(const Source& source,
956 const std::string& name) {
957 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
958 }
959
960 /// @param name the identifier name
961 /// @return an ast::IdentifierExpression with the given name
962 ast::IdentifierExpression* Expr(const char* name) {
963 return create<ast::IdentifierExpression>(Symbols().Register(name));
964 }
965
966 /// @param value the boolean value
967 /// @return a Scalar constructor for the given value
968 ast::ScalarConstructorExpression* Expr(bool value) {
969 return create<ast::ScalarConstructorExpression>(Literal(value));
970 }
971
972 /// @param value the float value
973 /// @return a Scalar constructor for the given value
974 ast::ScalarConstructorExpression* Expr(f32 value) {
975 return create<ast::ScalarConstructorExpression>(Literal(value));
976 }
977
978 /// @param value the integer value
979 /// @return a Scalar constructor for the given value
980 ast::ScalarConstructorExpression* Expr(i32 value) {
981 return create<ast::ScalarConstructorExpression>(Literal(value));
982 }
983
984 /// @param value the unsigned int value
985 /// @return a Scalar constructor for the given value
986 ast::ScalarConstructorExpression* Expr(u32 value) {
987 return create<ast::ScalarConstructorExpression>(Literal(value));
988 }
989
990 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
991 /// `list`.
992 /// @param list the list to append too
993 /// @param arg the arg to create
994 template <typename ARG>
995 void Append(ast::ExpressionList& list, ARG&& arg) {
996 list.emplace_back(Expr(std::forward<ARG>(arg)));
997 }
998
999 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1000 /// then appends them to `list`.
1001 /// @param list the list to append too
1002 /// @param arg0 the first argument
1003 /// @param args the rest of the arguments
1004 template <typename ARG0, typename... ARGS>
1005 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1006 Append(list, std::forward<ARG0>(arg0));
1007 Append(list, std::forward<ARGS>(args)...);
1008 }
1009
1010 /// @return an empty list of expressions
1011 ast::ExpressionList ExprList() { return {}; }
1012
1013 /// @param args the list of expressions
1014 /// @return the list of expressions converted to `ast::Expression`s using
1015 /// `Expr()`,
1016 template <typename... ARGS>
1017 ast::ExpressionList ExprList(ARGS&&... args) {
1018 ast::ExpressionList list;
1019 list.reserve(sizeof...(args));
1020 Append(list, std::forward<ARGS>(args)...);
1021 return list;
1022 }
1023
1024 /// @param list the list of expressions
1025 /// @return `list`
1026 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1027
1028 /// @param val the boolan value
1029 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001030 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001031
1032 /// @param val the float value
1033 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001034 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001035
1036 /// @param val the unsigned int value
1037 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001038 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001039
1040 /// @param val the integer value
1041 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001042 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001043
1044 /// @param args the arguments for the type constructor
1045 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1046 /// of `args` converted to `ast::Expression`s using `Expr()`
1047 template <typename T, typename... ARGS>
1048 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001049 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001050 }
1051
1052 /// @param type the type to construct
1053 /// @param args the arguments for the constructor
1054 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1055 /// values `args`.
1056 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001057 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001058 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001059 return create<ast::TypeConstructorExpression>(
1060 type, ExprList(std::forward<ARGS>(args)...));
1061 }
1062
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001063 /// Creates a constructor expression that constructs an object of
1064 /// `type` filled with `elem_value`. For example,
1065 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1066 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1067 /// @param type the type to construct
1068 /// @param elem_value the initial or element value (for vec and mat) to
1069 /// construct with
1070 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001071 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001072 int elem_value = 0);
1073
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074 /// @param args the arguments for the vector constructor
1075 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1076 /// `T`, constructed with the values `args`.
1077 template <typename T, typename... ARGS>
1078 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001079 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001080 }
1081
1082 /// @param args the arguments for the vector constructor
1083 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1084 /// `T`, constructed with the values `args`.
1085 template <typename T, typename... ARGS>
1086 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001087 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001088 }
1089
1090 /// @param args the arguments for the vector constructor
1091 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1092 /// `T`, constructed with the values `args`.
1093 template <typename T, typename... ARGS>
1094 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001095 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001096 }
1097
1098 /// @param args the arguments for the matrix constructor
1099 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1100 /// `T`, constructed with the values `args`.
1101 template <typename T, typename... ARGS>
1102 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001103 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001104 }
1105
1106 /// @param args the arguments for the matrix constructor
1107 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1108 /// `T`, constructed with the values `args`.
1109 template <typename T, typename... ARGS>
1110 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001111 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001112 }
1113
1114 /// @param args the arguments for the matrix constructor
1115 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1116 /// `T`, constructed with the values `args`.
1117 template <typename T, typename... ARGS>
1118 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001119 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001120 }
1121
1122 /// @param args the arguments for the matrix constructor
1123 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1124 /// `T`, constructed with the values `args`.
1125 template <typename T, typename... ARGS>
1126 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001127 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001128 }
1129
1130 /// @param args the arguments for the matrix constructor
1131 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1132 /// `T`, constructed with the values `args`.
1133 template <typename T, typename... ARGS>
1134 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001135 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001136 }
1137
1138 /// @param args the arguments for the matrix constructor
1139 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1140 /// `T`, constructed with the values `args`.
1141 template <typename T, typename... ARGS>
1142 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001143 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001144 }
1145
1146 /// @param args the arguments for the matrix constructor
1147 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1148 /// `T`, constructed with the values `args`.
1149 template <typename T, typename... ARGS>
1150 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001151 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001152 }
1153
1154 /// @param args the arguments for the matrix constructor
1155 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1156 /// `T`, constructed with the values `args`.
1157 template <typename T, typename... ARGS>
1158 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001159 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001160 }
1161
1162 /// @param args the arguments for the matrix constructor
1163 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1164 /// `T`, constructed with the values `args`.
1165 template <typename T, typename... ARGS>
1166 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001167 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001168 }
1169
1170 /// @param args the arguments for the array constructor
1171 /// @return an `ast::TypeConstructorExpression` of an array with element type
1172 /// `T`, constructed with the values `args`.
1173 template <typename T, int N = 0, typename... ARGS>
1174 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001175 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001176 }
1177
1178 /// @param subtype the array element type
1179 /// @param n the array size. 0 represents a runtime-array.
1180 /// @param args the arguments for the array constructor
1181 /// @return an `ast::TypeConstructorExpression` of an array with element type
1182 /// `subtype`, constructed with the values `args`.
1183 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001184 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001185 uint32_t n,
1186 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001187 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001188 }
1189
1190 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001191 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001192 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001193 /// @param constructor constructor expression
1194 /// @param decorations variable decorations
1195 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001196 template <typename NAME>
1197 ast::Variable* Var(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001198 ast::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001199 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001200 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001201 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001202 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001203 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1204 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001205 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001206
1207 /// @param source the variable source
1208 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001209 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001210 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001211 /// @param constructor constructor expression
1212 /// @param decorations variable decorations
1213 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001214 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001216 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001217 ast::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001218 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001219 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001220 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001221 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001222 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001223 type, false, constructor, decorations);
1224 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001225
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001226 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227 /// @param type the variable type
1228 /// @param constructor optional constructor expression
1229 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001230 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001231 template <typename NAME>
1232 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001233 ast::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001234 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001235 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001236 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001237 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001238 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001239 constructor, decorations);
1240 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001241
1242 /// @param source the variable source
1243 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001244 /// @param type the variable type
1245 /// @param constructor optional constructor expression
1246 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001247 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001248 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001249 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001250 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001251 ast::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001252 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001253 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001254 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001255 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001256 ast::StorageClass::kNone, type, true,
1257 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001258 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001259
James Pricedaf1f1c2021-04-08 22:15:48 +00001260 /// @param name the parameter name
1261 /// @param type the parameter type
1262 /// @param decorations optional parameter decorations
1263 /// @returns a constant `ast::Variable` with the given name and type
1264 template <typename NAME>
1265 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001266 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001267 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001268 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001269 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1270 ast::StorageClass::kNone, type, true, nullptr,
1271 decorations);
1272 }
1273
1274 /// @param source the parameter source
1275 /// @param name the parameter name
1276 /// @param type the parameter type
1277 /// @param decorations optional parameter decorations
1278 /// @returns a constant `ast::Variable` with the given name and type
1279 template <typename NAME>
1280 ast::Variable* Param(const Source& source,
1281 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001282 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001283 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001284 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001285 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1286 ast::StorageClass::kNone, type, true, nullptr,
1287 decorations);
1288 }
1289
Ben Clayton42708342021-04-21 17:55:12 +00001290 /// @param name the variable name
1291 /// @param type the variable type
1292 /// @param storage the variable storage class
1293 /// @param constructor constructor expression
1294 /// @param decorations variable decorations
1295 /// @returns a new `ast::Variable`, which is automatically registered as a
1296 /// global variable with the ast::Module.
1297 template <typename NAME>
1298 ast::Variable* Global(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001299 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001300 ast::StorageClass storage,
1301 ast::Expression* constructor = nullptr,
1302 ast::DecorationList decorations = {}) {
1303 auto* var =
1304 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1305 AST().AddGlobalVariable(var);
1306 return var;
1307 }
1308
1309 /// @param source the variable source
1310 /// @param name the variable name
1311 /// @param type the variable type
1312 /// @param storage the variable storage class
1313 /// @param constructor constructor expression
1314 /// @param decorations variable decorations
1315 /// @returns a new `ast::Variable`, which is automatically registered as a
1316 /// global variable with the ast::Module.
1317 template <typename NAME>
1318 ast::Variable* Global(const Source& source,
1319 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001320 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001321 ast::StorageClass storage,
1322 ast::Expression* constructor = nullptr,
1323 ast::DecorationList decorations = {}) {
1324 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1325 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001326 AST().AddGlobalVariable(var);
1327 return var;
1328 }
1329
1330 /// @param args the arguments to pass to Const()
1331 /// @returns a const `ast::Variable` constructed by calling Var() with the
1332 /// arguments of `args`, which is automatically registered as a global
1333 /// variable with the ast::Module.
1334 template <typename... ARGS>
1335 ast::Variable* GlobalConst(ARGS&&... args) {
1336 auto* var = Const(std::forward<ARGS>(args)...);
1337 AST().AddGlobalVariable(var);
1338 return var;
1339 }
1340
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001341 /// @param func the function name
1342 /// @param args the function call arguments
1343 /// @returns a `ast::CallExpression` to the function `func`, with the
1344 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1345 template <typename NAME, typename... ARGS>
1346 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1347 return create<ast::CallExpression>(Expr(func),
1348 ExprList(std::forward<ARGS>(args)...));
1349 }
1350
1351 /// @param lhs the left hand argument to the addition operation
1352 /// @param rhs the right hand argument to the addition operation
1353 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1354 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001355 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001356 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1357 Expr(std::forward<LHS>(lhs)),
1358 Expr(std::forward<RHS>(rhs)));
1359 }
1360
1361 /// @param lhs the left hand argument to the subtraction operation
1362 /// @param rhs the right hand argument to the subtraction operation
1363 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1364 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001365 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001366 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1367 Expr(std::forward<LHS>(lhs)),
1368 Expr(std::forward<RHS>(rhs)));
1369 }
1370
1371 /// @param lhs the left hand argument to the multiplication operation
1372 /// @param rhs the right hand argument to the multiplication operation
1373 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1374 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001375 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001376 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1377 Expr(std::forward<LHS>(lhs)),
1378 Expr(std::forward<RHS>(rhs)));
1379 }
1380
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001381 /// @param source the source information
1382 /// @param lhs the left hand argument to the multiplication operation
1383 /// @param rhs the right hand argument to the multiplication operation
1384 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1385 template <typename LHS, typename RHS>
1386 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1387 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1388 Expr(std::forward<LHS>(lhs)),
1389 Expr(std::forward<RHS>(rhs)));
1390 }
1391
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001392 /// @param lhs the left hand argument to the division operation
1393 /// @param rhs the right hand argument to the division operation
1394 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1395 template <typename LHS, typename RHS>
1396 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1397 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1398 Expr(std::forward<LHS>(lhs)),
1399 Expr(std::forward<RHS>(rhs)));
1400 }
1401
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001402 /// @param arr the array argument for the array accessor expression
1403 /// @param idx the index argument for the array accessor expression
1404 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1405 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001406 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001407 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1408 Expr(std::forward<IDX>(idx)));
1409 }
1410
1411 /// @param obj the object for the member accessor expression
1412 /// @param idx the index argument for the array accessor expression
1413 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1414 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001415 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001416 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1417 Expr(std::forward<IDX>(idx)));
1418 }
1419
Ben Clayton42d1e092021-02-02 14:29:15 +00001420 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001421 /// @param val the offset value
1422 /// @returns the offset decoration pointer
1423 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1424 return create<ast::StructMemberOffsetDecoration>(source_, val);
1425 }
1426
Ben Claytond614dd52021-03-15 10:43:11 +00001427 /// Creates a ast::StructMemberSizeDecoration
1428 /// @param source the source information
1429 /// @param val the size value
1430 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001431 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1432 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001433 return create<ast::StructMemberSizeDecoration>(source, val);
1434 }
1435
1436 /// Creates a ast::StructMemberSizeDecoration
1437 /// @param val the size value
1438 /// @returns the size decoration pointer
1439 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1440 return create<ast::StructMemberSizeDecoration>(source_, val);
1441 }
1442
1443 /// Creates a ast::StructMemberAlignDecoration
1444 /// @param source the source information
1445 /// @param val the align value
1446 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001447 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1448 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001449 return create<ast::StructMemberAlignDecoration>(source, val);
1450 }
1451
1452 /// Creates a ast::StructMemberAlignDecoration
1453 /// @param val the align value
1454 /// @returns the align decoration pointer
1455 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1456 return create<ast::StructMemberAlignDecoration>(source_, val);
1457 }
1458
Ben Clayton42d1e092021-02-02 14:29:15 +00001459 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001460 /// @param source the source information
1461 /// @param name the function name
1462 /// @param params the function parameters
1463 /// @param type the function return type
1464 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001465 /// @param decorations the optional function decorations
1466 /// @param return_type_decorations the optional function return type
1467 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001468 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001469 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001470 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001471 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001472 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001473 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001474 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001475 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001476 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001477 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001478 auto* func =
1479 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1480 type, create<ast::BlockStatement>(body),
1481 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001482 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001483 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001484 }
1485
Ben Clayton42d1e092021-02-02 14:29:15 +00001486 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001487 /// @param name the function name
1488 /// @param params the function parameters
1489 /// @param type the function return type
1490 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001491 /// @param decorations the optional function decorations
1492 /// @param return_type_decorations the optional function return type
1493 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001494 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001495 template <typename NAME>
1496 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001497 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001498 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001499 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001500 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001501 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001502 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001503 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1504 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001505 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001506 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001507 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001508 }
1509
Ben Clayton49668bb2021-04-17 05:32:01 +00001510 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001511 /// @param source the source information
1512 /// @returns the return statement pointer
1513 ast::ReturnStatement* Return(const Source& source) {
1514 return create<ast::ReturnStatement>(source);
1515 }
1516
1517 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001518 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001519 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1520
1521 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001522 /// @param source the source information
1523 /// @param val the return value
1524 /// @returns the return statement pointer
1525 template <typename EXPR>
1526 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1527 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1528 }
1529
1530 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001531 /// @param val the return value
1532 /// @returns the return statement pointer
1533 template <typename EXPR>
1534 ast::ReturnStatement* Return(EXPR&& val) {
1535 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001536 }
1537
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001538 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001539 /// @param source the source information
1540 /// @param name the struct name
1541 /// @param members the struct members
1542 /// @param decorations the optional struct decorations
1543 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001544 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001545 ast::Struct* Structure(const Source& source,
1546 NAME&& name,
1547 ast::StructMemberList members,
1548 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001549 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001550 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001551 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001552 AST().AddConstructedType(type);
1553 return type;
1554 }
1555
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001556 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001557 /// @param name the struct name
1558 /// @param members the struct members
1559 /// @param decorations the optional struct decorations
1560 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001561 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001562 ast::Struct* Structure(NAME&& name,
1563 ast::StructMemberList members,
1564 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001565 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001566 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001567 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001568 AST().AddConstructedType(type);
1569 return type;
1570 }
1571
Ben Clayton42d1e092021-02-02 14:29:15 +00001572 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001573 /// @param source the source information
1574 /// @param name the struct member name
1575 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001576 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001577 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001578 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001579 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001580 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001581 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001582 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001583 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001584 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1585 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001586 }
1587
Ben Clayton42d1e092021-02-02 14:29:15 +00001588 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001589 /// @param name the struct member name
1590 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001591 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001592 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001593 template <typename NAME>
1594 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001595 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001596 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001597 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001598 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1599 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001600 }
1601
Ben Claytonbab31972021-02-08 21:16:21 +00001602 /// Creates a ast::StructMember with the given byte offset
1603 /// @param offset the offset to use in the StructMemberOffsetDecoration
1604 /// @param name the struct member name
1605 /// @param type the struct member type
1606 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001607 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001608 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001609 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001610 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001611 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001612 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001613 create<ast::StructMemberOffsetDecoration>(offset),
1614 });
1615 }
1616
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001617 /// Creates a ast::BlockStatement with input statements
1618 /// @param statements statements of block
1619 /// @returns the block statement pointer
1620 template <typename... Statements>
1621 ast::BlockStatement* Block(Statements&&... statements) {
1622 return create<ast::BlockStatement>(
1623 ast::StatementList{std::forward<Statements>(statements)...});
1624 }
1625
1626 /// Creates a ast::ElseStatement with input condition and body
1627 /// @param condition the else condition expression
1628 /// @param body the else body
1629 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001630 template <typename CONDITION>
1631 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1632 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1633 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001634 }
1635
1636 /// Creates a ast::IfStatement with input condition, body, and optional
1637 /// variadic else statements
1638 /// @param condition the if statement condition expression
1639 /// @param body the if statement body
1640 /// @param elseStatements optional variadic else statements
1641 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001642 template <typename CONDITION, typename... ELSE_STATEMENTS>
1643 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001644 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001645 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001646 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001647 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001648 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001649 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001650 }
1651
1652 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001653 /// @param source the source information
1654 /// @param lhs the left hand side expression initializer
1655 /// @param rhs the right hand side expression initializer
1656 /// @returns the assignment statement pointer
1657 template <typename LhsExpressionInit, typename RhsExpressionInit>
1658 ast::AssignmentStatement* Assign(const Source& source,
1659 LhsExpressionInit&& lhs,
1660 RhsExpressionInit&& rhs) {
1661 return create<ast::AssignmentStatement>(
1662 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1663 Expr(std::forward<RhsExpressionInit>(rhs)));
1664 }
1665
1666 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001667 /// @param lhs the left hand side expression initializer
1668 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001669 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001670 template <typename LhsExpressionInit, typename RhsExpressionInit>
1671 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1672 RhsExpressionInit&& rhs) {
1673 return create<ast::AssignmentStatement>(
1674 Expr(std::forward<LhsExpressionInit>(lhs)),
1675 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001676 }
1677
1678 /// Creates a ast::LoopStatement with input body and optional continuing
1679 /// @param body the loop body
1680 /// @param continuing the optional continuing block
1681 /// @returns the loop statement pointer
1682 ast::LoopStatement* Loop(ast::BlockStatement* body,
1683 ast::BlockStatement* continuing = nullptr) {
1684 return create<ast::LoopStatement>(body, continuing);
1685 }
1686
1687 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001688 /// @param source the source information
1689 /// @param var the variable to wrap in a decl statement
1690 /// @returns the variable decl statement pointer
1691 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1692 return create<ast::VariableDeclStatement>(source, var);
1693 }
1694
1695 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001696 /// @param var the variable to wrap in a decl statement
1697 /// @returns the variable decl statement pointer
1698 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1699 return create<ast::VariableDeclStatement>(var);
1700 }
1701
Antonio Maioranocea744d2021-03-25 12:55:27 +00001702 /// Creates a ast::SwitchStatement with input expression and cases
1703 /// @param condition the condition expression initializer
1704 /// @param cases case statements
1705 /// @returns the switch statement pointer
1706 template <typename ExpressionInit, typename... Cases>
1707 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1708 return create<ast::SwitchStatement>(
1709 Expr(std::forward<ExpressionInit>(condition)),
1710 ast::CaseStatementList{std::forward<Cases>(cases)...});
1711 }
1712
1713 /// Creates a ast::CaseStatement with input list of selectors, and body
1714 /// @param selectors list of selectors
1715 /// @param body the case body
1716 /// @returns the case statement pointer
1717 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1718 ast::BlockStatement* body = nullptr) {
1719 return create<ast::CaseStatement>(std::move(selectors),
1720 body ? body : Block());
1721 }
1722
1723 /// Convenient overload that takes a single selector
1724 /// @param selector a single case selector
1725 /// @param body the case body
1726 /// @returns the case statement pointer
1727 ast::CaseStatement* Case(ast::IntLiteral* selector,
1728 ast::BlockStatement* body = nullptr) {
1729 return Case(ast::CaseSelectorList{selector}, body);
1730 }
1731
1732 /// Convenience function that creates a 'default' ast::CaseStatement
1733 /// @param body the case body
1734 /// @returns the case statement pointer
1735 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1736 return Case(ast::CaseSelectorList{}, body);
1737 }
1738
James Price68f558f2021-04-06 15:51:47 +00001739 /// Creates an ast::BuiltinDecoration
1740 /// @param source the source information
1741 /// @param builtin the builtin value
1742 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001743 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001744 return create<ast::BuiltinDecoration>(source, builtin);
1745 }
1746
1747 /// Creates an ast::BuiltinDecoration
1748 /// @param builtin the builtin value
1749 /// @returns the builtin decoration pointer
1750 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1751 return create<ast::BuiltinDecoration>(source_, builtin);
1752 }
1753
1754 /// Creates an ast::LocationDecoration
1755 /// @param source the source information
1756 /// @param location the location value
1757 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001758 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001759 return create<ast::LocationDecoration>(source, location);
1760 }
1761
1762 /// Creates an ast::LocationDecoration
1763 /// @param location the location value
1764 /// @returns the location decoration pointer
1765 ast::LocationDecoration* Location(uint32_t location) {
1766 return create<ast::LocationDecoration>(source_, location);
1767 }
1768
1769 /// Creates an ast::StageDecoration
1770 /// @param source the source information
1771 /// @param stage the pipeline stage
1772 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001773 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001774 return create<ast::StageDecoration>(source, stage);
1775 }
1776
1777 /// Creates an ast::StageDecoration
1778 /// @param stage the pipeline stage
1779 /// @returns the stage decoration pointer
1780 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1781 return create<ast::StageDecoration>(source_, stage);
1782 }
1783
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001784 /// Sets the current builder source to `src`
1785 /// @param src the Source used for future create() calls
1786 void SetSource(const Source& src) {
1787 AssertNotMoved();
1788 source_ = src;
1789 }
1790
1791 /// Sets the current builder source to `loc`
1792 /// @param loc the Source used for future create() calls
1793 void SetSource(const Source::Location& loc) {
1794 AssertNotMoved();
1795 source_ = Source(loc);
1796 }
1797
Ben Clayton33352542021-01-29 16:43:41 +00001798 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001799 /// @note As the Resolver is run when the Program is built, this will only be
1800 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001801 /// @param expr the AST expression
1802 /// @return the resolved semantic type for the expression, or nullptr if the
1803 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001804 sem::Type* TypeOf(const ast::Expression* expr) const;
1805
1806 /// Helper for returning the resolved semantic type of the AST type `type`.
1807 /// @note As the Resolver is run when the Program is built, this will only be
1808 /// useful for the Resolver itself and tests that use their own Resolver.
1809 /// @param expr the AST type
1810 /// @return the resolved semantic type for the type, or nullptr if the type
1811 /// has no resolved type.
1812 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001813
Ben Clayton169512e2021-04-17 05:52:11 +00001814 /// Wraps the ast::Literal in a statement. This is used by tests that
1815 /// construct a partial AST and require the Resolver to reach these
1816 /// nodes.
1817 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1818 /// @return the ast::Statement that wraps the ast::Statement
1819 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001820 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001821 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001822 /// nodes.
1823 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1824 /// @return the ast::Statement that wraps the ast::Expression
1825 ast::Statement* WrapInStatement(ast::Expression* expr);
1826 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001827 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001828 /// these nodes.
1829 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1830 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1831 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1832 /// Returns the statement argument. Used as a passthrough-overload by
1833 /// WrapInFunction().
1834 /// @param stmt the ast::Statement
1835 /// @return `stmt`
1836 ast::Statement* WrapInStatement(ast::Statement* stmt);
1837 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001838 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001839 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001840 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001841 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001842 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001843 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001844 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001845 }
1846 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001847 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001848 /// @returns the function
1849 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001850
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001851 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001852 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001853
1854 protected:
1855 /// Asserts that the builder has not been moved.
1856 void AssertNotMoved() const;
1857
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001858 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001859 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001860 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001861 ASTNodeAllocator ast_nodes_;
1862 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001863 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001864 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001865 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001866 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001867
1868 /// The source to use when creating AST nodes without providing a Source as
1869 /// the first argument.
1870 Source source_;
1871
Ben Clayton5f0ea112021-03-09 10:54:37 +00001872 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001873 /// program when built.
1874 bool resolve_on_build_ = true;
1875
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001876 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1877 bool moved_ = false;
1878};
1879
1880//! @cond Doxygen_Suppress
1881// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1882template <>
1883struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001884 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001885 return t->i32();
1886 }
1887};
1888template <>
1889struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001890 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001891 return t->u32();
1892 }
1893};
1894template <>
1895struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001896 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001897 return t->f32();
1898 }
1899};
1900template <>
1901struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001902 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001903 return t->bool_();
1904 }
1905};
1906template <>
1907struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001908 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001909 return t->void_();
1910 }
1911};
1912//! @endcond
1913
Ben Clayton917b14b2021-04-19 16:50:23 +00001914/// @param builder the ProgramBuilder
1915/// @returns the ProgramID of the ProgramBuilder
1916inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1917 return builder->ID();
1918}
1919
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001920} // namespace tint
1921
1922#endif // SRC_PROGRAM_BUILDER_H_