blob: 9a13b7e1cc247400d86fe33dd4b5fd43435b439d [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"
64#include "src/sem/array_type.h"
65#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"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000075#include "src/sem/struct_type.h"
76#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>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000307 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000308 AssertNotMoved();
309 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
310 }
311
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000312 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000313 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
314 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000315 /// Types are unique (de-aliased), and so calling create() for the same `T`
316 /// and arguments will return the same pointer.
317 /// @warning Use this method to acquire a type only if all of its type
318 /// information is provided in the constructor arguments `args`.<br>
319 /// If the type requires additional configuration after construction that
320 /// affect its fundamental type, build the type with `std::make_unique`, make
321 /// any necessary alterations and then call unique_type() instead.
322 /// @param args the arguments to pass to the type constructor
323 /// @returns the de-aliased type pointer
324 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000325 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
326 static_assert(std::is_base_of<sem::Type, T>::value,
327 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000328 AssertNotMoved();
329 return types_.Get<T>(std::forward<ARGS>(args)...);
330 }
331
332 /// Marks this builder as moved, preventing any further use of the builder.
333 void MarkAsMoved();
334
335 //////////////////////////////////////////////////////////////////////////////
336 // TypesBuilder
337 //////////////////////////////////////////////////////////////////////////////
338
339 /// TypesBuilder holds basic `tint` types and methods for constructing
340 /// complex types.
341 class TypesBuilder {
342 public:
343 /// Constructor
344 /// @param builder the program builder
345 explicit TypesBuilder(ProgramBuilder* builder);
346
347 /// @return the tint AST type for the C type `T`.
348 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000349 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000350 return CToAST<T>::get(this);
351 }
352
353 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000354 typ::Bool bool_() const {
355 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
356 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000357
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000358 /// @param source the Source of the node
359 /// @returns a boolean type
360 typ::Bool bool_(const Source& source) const {
361 return {builder->create<ast::Bool>(source), builder->create<sem::Bool>()};
362 }
363
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000364 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000365 typ::F32 f32() const {
366 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
367 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000369 /// @param source the Source of the node
370 /// @returns a f32 type
371 typ::F32 f32(const Source& source) const {
372 return {builder->create<ast::F32>(source), builder->create<sem::F32>()};
373 }
374
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000375 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000376 typ::I32 i32() const {
377 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
378 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000379
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000380 /// @param source the Source of the node
381 /// @returns a i32 type
382 typ::I32 i32(const Source& source) const {
383 return {builder->create<ast::I32>(source), builder->create<sem::I32>()};
384 }
385
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000386 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000387 typ::U32 u32() const {
388 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
389 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000390
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000391 /// @param source the Source of the node
392 /// @returns a u32 type
393 typ::U32 u32(const Source& source) const {
394 return {builder->create<ast::U32>(source), builder->create<sem::U32>()};
395 }
396
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000397 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000398 typ::Void void_() const {
399 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
400 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000401
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000402 /// @param source the Source of the node
403 /// @returns a void type
404 typ::Void void_(const Source& source) const {
405 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
406 }
407
Antonio Maiorano25436862021-04-13 13:32:33 +0000408 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000409 /// @param n vector width in elements
410 /// @return the tint AST type for a `n`-element vector of `type`.
411 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000412 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000413 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
414 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000415 }
416
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000417 /// @param source the Source of the node
418 /// @param type vector subtype
419 /// @param n vector width in elements
420 /// @return the tint AST type for a `n`-element vector of `type`.
421 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000422 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000423 return {
424 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
425 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000426 }
427
Antonio Maiorano25436862021-04-13 13:32:33 +0000428 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000429 /// @return the tint AST type for a 2-element vector of `type`.
430 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
431
432 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000433 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000434 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000435
436 /// @param type vector subtype
437 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000438 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
439
440 /// @param n vector width in elements
441 /// @return the tint AST type for a `n`-element vector of `type`.
442 template <typename T>
443 typ::Vector vec(uint32_t n) const {
444 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000445 }
446
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000447 /// @return the tint AST type for a 2-element vector of the C type `T`.
448 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000449 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000450 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000451 }
452
453 /// @return the tint AST type for a 3-element vector of the C type `T`.
454 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000455 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000456 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000457 }
458
459 /// @return the tint AST type for a 4-element vector of the C type `T`.
460 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000461 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000462 return vec4(Of<T>());
463 }
464
465 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000466 /// @param columns number of columns for the matrix
467 /// @param rows number of rows for the matrix
468 /// @return the tint AST type for a matrix of `type`
469 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000470 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000471 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
472 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000473 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000474 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000475 }
476
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000477 /// @param source the Source of the node
478 /// @param type matrix subtype
479 /// @param columns number of columns for the matrix
480 /// @param rows number of rows for the matrix
481 /// @return the tint AST type for a matrix of `type`
482 typ::Matrix mat(const Source& source,
483 typ::Type type,
484 uint32_t columns,
485 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000486 return {type.ast
487 ? builder->create<ast::Matrix>(source, type, rows, columns)
488 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000489 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000490 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000491 }
492
Ben Claytone204f272021-04-22 14:40:23 +0000493 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000494 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000495 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000496
497 /// @param type matrix subtype
498 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000499 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000500
501 /// @param type matrix subtype
502 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000503 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000504
505 /// @param type matrix subtype
506 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000507 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000508
509 /// @param type matrix subtype
510 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000511 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000512
513 /// @param type matrix subtype
514 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000515 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000516
517 /// @param type matrix subtype
518 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000519 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000520
521 /// @param type matrix subtype
522 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000523 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000524
525 /// @param type matrix subtype
526 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000527 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000528
Ben Claytone204f272021-04-22 14:40:23 +0000529 /// @param columns number of columns for the matrix
530 /// @param rows number of rows for the matrix
531 /// @return the tint AST type for a matrix of `type`
532 template <typename T>
533 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
534 return mat(Of<T>(), columns, rows);
535 }
536
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000537 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
538 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000539 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000540 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000541 }
542
543 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
544 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000545 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000546 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000547 }
548
549 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
550 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000551 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000552 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000553 }
554
555 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
556 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000557 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000558 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559 }
560
561 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
562 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000563 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000564 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000565 }
566
567 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
568 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000569 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000570 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 }
572
573 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
574 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000575 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000576 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 }
578
579 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
580 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000581 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000582 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000583 }
584
585 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
586 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000587 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000588 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000589 }
590
591 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000592 /// @param n the array size. 0 represents a runtime-array
593 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000594 /// @return the tint AST type for a array of size `n` of type `T`
Ben Claytone204f272021-04-22 14:40:23 +0000595 typ::Array array(typ::Type subtype,
596 uint32_t n = 0,
597 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000598 subtype = MaybeCreateTypename(subtype);
Ben Clayton95c2e952021-04-28 12:58:13 +0000599 return {subtype.ast ? builder->create<ast::Array>(subtype, n, decos)
600 : nullptr,
601 subtype.sem ? builder->create<sem::ArrayType>(subtype, n,
602 std::move(decos))
603 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000604 }
605
606 /// @param source the Source of the node
607 /// @param subtype the array element type
608 /// @param n the array size. 0 represents a runtime-array
609 /// @param decos the optional decorations for the array
610 /// @return the tint AST type for a array of size `n` of type `T`
611 typ::Array array(const Source& source,
612 typ::Type subtype,
613 uint32_t n = 0,
614 ast::DecorationList decos = {}) const {
615 subtype = MaybeCreateTypename(subtype);
Ben Clayton95c2e952021-04-28 12:58:13 +0000616 return {
617 subtype.ast ? builder->create<ast::Array>(source, subtype, n, decos)
618 : nullptr,
619 subtype.sem
620 ? builder->create<sem::ArrayType>(subtype, n, std::move(decos))
621 : nullptr};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000622 }
623
Ben Claytonbab31972021-02-08 21:16:21 +0000624 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000625 /// @param n the array size. 0 represents a runtime-array
626 /// @param stride the array stride
Ben Claytonbab31972021-02-08 21:16:21 +0000627 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton7241a502021-04-22 14:32:53 +0000628 typ::Array array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000629 subtype = MaybeCreateTypename(subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000630 return array(subtype, n,
631 {builder->create<ast::StrideDecoration>(stride)});
Ben Claytonbab31972021-02-08 21:16:21 +0000632 }
633
Ben Clayton0f88b312021-05-04 17:36:31 +0000634 /// @param source the Source of the node
635 /// @param subtype the array element type
636 /// @param n the array size. 0 represents a runtime-array
637 /// @param stride the array stride
638 /// @return the tint AST type for a array of size `n` of type `T`
639 typ::Array array(const Source& source,
640 typ::Type subtype,
641 uint32_t n,
642 uint32_t stride) const {
643 subtype = MaybeCreateTypename(subtype);
644 return array(source, subtype, n,
645 {builder->create<ast::StrideDecoration>(stride)});
646 }
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 Clayton7241a502021-04-22 14:32:53 +0000650 typ::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 Clayton7241a502021-04-22 14:32:53 +0000657 typ::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 Claytona6b9a8e2021-01-26 16:57:10 +0000772 /// @param impl the struct implementation
773 /// @returns a struct pointer
Ben Claytone204f272021-04-22 14:40:23 +0000774 typ::Struct struct_(ast::Struct* impl) const {
775 return {impl, builder->create<sem::StructType>(impl)};
776 }
777
778 /// @param kind the kind of sampler
779 /// @returns the sampler
780 typ::Sampler sampler(ast::SamplerKind kind) const {
781 return {builder->create<ast::Sampler>(kind),
782 builder->create<sem::Sampler>(kind)};
783 }
784
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000785 /// @param source the Source of the node
786 /// @param kind the kind of sampler
787 /// @returns the sampler
788 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
789 return {builder->create<ast::Sampler>(source, kind),
790 builder->create<sem::Sampler>(kind)};
791 }
792
Ben Claytone204f272021-04-22 14:40:23 +0000793 /// @param dims the dimensionality of the texture
794 /// @returns the depth texture
795 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
796 return {builder->create<ast::DepthTexture>(dims),
797 builder->create<sem::DepthTexture>(dims)};
798 }
799
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000800 /// @param source the Source of the node
801 /// @param dims the dimensionality of the texture
802 /// @returns the depth texture
803 typ::DepthTexture depth_texture(const Source& source,
804 ast::TextureDimension dims) const {
805 return {builder->create<ast::DepthTexture>(source, dims),
806 builder->create<sem::DepthTexture>(dims)};
807 }
808
Ben Claytone204f272021-04-22 14:40:23 +0000809 /// @param dims the dimensionality of the texture
810 /// @param subtype the texture subtype.
811 /// @returns the sampled texture
812 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
813 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000814 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
815 : nullptr,
816 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
817 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000818 }
819
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000820 /// @param source the Source of the node
821 /// @param dims the dimensionality of the texture
822 /// @param subtype the texture subtype.
823 /// @returns the sampled texture
824 typ::SampledTexture sampled_texture(const Source& source,
825 ast::TextureDimension dims,
826 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000827 return {subtype.ast
828 ? builder->create<ast::SampledTexture>(source, dims, subtype)
829 : nullptr,
830 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
831 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000832 }
833
Ben Claytone204f272021-04-22 14:40:23 +0000834 /// @param dims the dimensionality of the texture
835 /// @param subtype the texture subtype.
836 /// @returns the multisampled texture
837 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
838 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000839 return {
840 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
841 : nullptr,
842 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
843 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000844 }
845
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000846 /// @param source the Source of the node
847 /// @param dims the dimensionality of the texture
848 /// @param subtype the texture subtype.
849 /// @returns the multisampled texture
850 typ::MultisampledTexture multisampled_texture(const Source& source,
851 ast::TextureDimension dims,
852 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000853 return {
854 subtype.ast
855 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
856 : nullptr,
857 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
858 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000859 }
860
Ben Claytone204f272021-04-22 14:40:23 +0000861 /// @param dims the dimensionality of the texture
862 /// @param format the image format of the texture
863 /// @returns the storage texture
864 typ::StorageTexture storage_texture(ast::TextureDimension dims,
865 ast::ImageFormat format) const {
866 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
867 auto* sem_subtype =
868 sem::StorageTexture::SubtypeFor(format, builder->Types());
869 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
870 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000871 }
872
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000873 /// @param source the Source of the node
874 /// @param dims the dimensionality of the texture
875 /// @param format the image format of the texture
876 /// @returns the storage texture
877 typ::StorageTexture storage_texture(const Source& source,
878 ast::TextureDimension dims,
879 ast::ImageFormat format) const {
880 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
881 auto* sem_subtype =
882 sem::StorageTexture::SubtypeFor(format, builder->Types());
883 return {builder->create<ast::StorageTexture>(source, dims, format,
884 ast_subtype),
885 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
886 }
887
888 /// @param source the Source of the node
889 /// @returns the external texture
890 typ::ExternalTexture external_texture(const Source& source) const {
891 return {builder->create<ast::ExternalTexture>(source),
892 builder->create<sem::ExternalTexture>()};
893 }
894
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000895 /// If ty is a ast::Struct or ast::Alias, the returned type is an
896 /// ast::TypeName of the given type's name, otherwise type is returned.
897 /// @param type the type
898 /// @return either type or a pointer to a new ast::TypeName
899 typ::Type MaybeCreateTypename(typ::Type type) const;
900
Ben Claytonf5f311e2021-04-28 14:31:23 +0000901 /// The ProgramBuilder
902 ProgramBuilder* const builder;
903
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000904 private:
905 /// CToAST<T> is specialized for various `T` types and each specialization
906 /// contains a single static `get()` method for obtaining the corresponding
907 /// AST type for the C type `T`.
908 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000909 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000910 template <typename T>
911 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000912 };
913
914 //////////////////////////////////////////////////////////////////////////////
915 // AST helper methods
916 //////////////////////////////////////////////////////////////////////////////
917
James Price65ae64d2021-04-29 12:59:14 +0000918 /// @return a new unnamed symbol
919 Symbol Sym() { return Symbols().New(); }
920
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000921 /// @param name the symbol string
922 /// @return a Symbol with the given name
923 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
924
925 /// @param sym the symbol
926 /// @return `sym`
927 Symbol Sym(Symbol sym) { return sym; }
928
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000929 /// @param expr the expression
930 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000931 template <typename T>
932 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
933 return expr;
934 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000935
Ben Claytonb8ea5912021-04-19 16:52:42 +0000936 /// Passthrough for nullptr
937 /// @return nullptr
938 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
939
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000940 /// @param name the identifier name
941 /// @return an ast::IdentifierExpression with the given name
942 ast::IdentifierExpression* Expr(const std::string& name) {
943 return create<ast::IdentifierExpression>(Symbols().Register(name));
944 }
945
Ben Clayton46d78d72021-02-10 21:34:26 +0000946 /// @param symbol the identifier symbol
947 /// @return an ast::IdentifierExpression with the given symbol
948 ast::IdentifierExpression* Expr(Symbol symbol) {
949 return create<ast::IdentifierExpression>(symbol);
950 }
951
Ben Claytonb8ea5912021-04-19 16:52:42 +0000952 /// @param variable the AST variable
953 /// @return an ast::IdentifierExpression with the variable's symbol
954 ast::IdentifierExpression* Expr(ast::Variable* variable) {
955 return create<ast::IdentifierExpression>(variable->symbol());
956 }
957
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000958 /// @param source the source information
959 /// @param name the identifier name
960 /// @return an ast::IdentifierExpression with the given name
961 ast::IdentifierExpression* Expr(const Source& source,
962 const std::string& name) {
963 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
964 }
965
966 /// @param name the identifier name
967 /// @return an ast::IdentifierExpression with the given name
968 ast::IdentifierExpression* Expr(const char* name) {
969 return create<ast::IdentifierExpression>(Symbols().Register(name));
970 }
971
972 /// @param value the boolean value
973 /// @return a Scalar constructor for the given value
974 ast::ScalarConstructorExpression* Expr(bool value) {
975 return create<ast::ScalarConstructorExpression>(Literal(value));
976 }
977
978 /// @param value the float value
979 /// @return a Scalar constructor for the given value
980 ast::ScalarConstructorExpression* Expr(f32 value) {
981 return create<ast::ScalarConstructorExpression>(Literal(value));
982 }
983
984 /// @param value the integer value
985 /// @return a Scalar constructor for the given value
986 ast::ScalarConstructorExpression* Expr(i32 value) {
987 return create<ast::ScalarConstructorExpression>(Literal(value));
988 }
989
990 /// @param value the unsigned int value
991 /// @return a Scalar constructor for the given value
992 ast::ScalarConstructorExpression* Expr(u32 value) {
993 return create<ast::ScalarConstructorExpression>(Literal(value));
994 }
995
996 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
997 /// `list`.
998 /// @param list the list to append too
999 /// @param arg the arg to create
1000 template <typename ARG>
1001 void Append(ast::ExpressionList& list, ARG&& arg) {
1002 list.emplace_back(Expr(std::forward<ARG>(arg)));
1003 }
1004
1005 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1006 /// then appends them to `list`.
1007 /// @param list the list to append too
1008 /// @param arg0 the first argument
1009 /// @param args the rest of the arguments
1010 template <typename ARG0, typename... ARGS>
1011 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1012 Append(list, std::forward<ARG0>(arg0));
1013 Append(list, std::forward<ARGS>(args)...);
1014 }
1015
1016 /// @return an empty list of expressions
1017 ast::ExpressionList ExprList() { return {}; }
1018
1019 /// @param args the list of expressions
1020 /// @return the list of expressions converted to `ast::Expression`s using
1021 /// `Expr()`,
1022 template <typename... ARGS>
1023 ast::ExpressionList ExprList(ARGS&&... args) {
1024 ast::ExpressionList list;
1025 list.reserve(sizeof...(args));
1026 Append(list, std::forward<ARGS>(args)...);
1027 return list;
1028 }
1029
1030 /// @param list the list of expressions
1031 /// @return `list`
1032 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1033
1034 /// @param val the boolan value
1035 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001036 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001037
1038 /// @param val the float value
1039 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001040 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001041
1042 /// @param val the unsigned int value
1043 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001044 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001045
1046 /// @param val the integer value
1047 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001048 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001049
1050 /// @param args the arguments for the type constructor
1051 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1052 /// of `args` converted to `ast::Expression`s using `Expr()`
1053 template <typename T, typename... ARGS>
1054 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001055 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001056 }
1057
1058 /// @param type the type to construct
1059 /// @param args the arguments for the constructor
1060 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1061 /// values `args`.
1062 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001063 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001064 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001065 return create<ast::TypeConstructorExpression>(
1066 type, ExprList(std::forward<ARGS>(args)...));
1067 }
1068
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001069 /// Creates a constructor expression that constructs an object of
1070 /// `type` filled with `elem_value`. For example,
1071 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1072 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1073 /// @param type the type to construct
1074 /// @param elem_value the initial or element value (for vec and mat) to
1075 /// construct with
1076 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001077 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001078 int elem_value = 0);
1079
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001080 /// @param args the arguments for the vector constructor
1081 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1082 /// `T`, constructed with the values `args`.
1083 template <typename T, typename... ARGS>
1084 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001085 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001086 }
1087
1088 /// @param args the arguments for the vector constructor
1089 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1090 /// `T`, constructed with the values `args`.
1091 template <typename T, typename... ARGS>
1092 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001093 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001094 }
1095
1096 /// @param args the arguments for the vector constructor
1097 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1098 /// `T`, constructed with the values `args`.
1099 template <typename T, typename... ARGS>
1100 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001101 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001102 }
1103
1104 /// @param args the arguments for the matrix constructor
1105 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1106 /// `T`, constructed with the values `args`.
1107 template <typename T, typename... ARGS>
1108 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001109 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001110 }
1111
1112 /// @param args the arguments for the matrix constructor
1113 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1114 /// `T`, constructed with the values `args`.
1115 template <typename T, typename... ARGS>
1116 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001117 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001118 }
1119
1120 /// @param args the arguments for the matrix constructor
1121 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1122 /// `T`, constructed with the values `args`.
1123 template <typename T, typename... ARGS>
1124 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001125 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001126 }
1127
1128 /// @param args the arguments for the matrix constructor
1129 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1130 /// `T`, constructed with the values `args`.
1131 template <typename T, typename... ARGS>
1132 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001133 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001134 }
1135
1136 /// @param args the arguments for the matrix constructor
1137 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1138 /// `T`, constructed with the values `args`.
1139 template <typename T, typename... ARGS>
1140 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001141 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001142 }
1143
1144 /// @param args the arguments for the matrix constructor
1145 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1146 /// `T`, constructed with the values `args`.
1147 template <typename T, typename... ARGS>
1148 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001149 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001150 }
1151
1152 /// @param args the arguments for the matrix constructor
1153 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1154 /// `T`, constructed with the values `args`.
1155 template <typename T, typename... ARGS>
1156 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001157 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001158 }
1159
1160 /// @param args the arguments for the matrix constructor
1161 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1162 /// `T`, constructed with the values `args`.
1163 template <typename T, typename... ARGS>
1164 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001165 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001166 }
1167
1168 /// @param args the arguments for the matrix constructor
1169 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1170 /// `T`, constructed with the values `args`.
1171 template <typename T, typename... ARGS>
1172 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001173 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001174 }
1175
1176 /// @param args the arguments for the array constructor
1177 /// @return an `ast::TypeConstructorExpression` of an array with element type
1178 /// `T`, constructed with the values `args`.
1179 template <typename T, int N = 0, typename... ARGS>
1180 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001181 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001182 }
1183
1184 /// @param subtype the array element type
1185 /// @param n the array size. 0 represents a runtime-array.
1186 /// @param args the arguments for the array constructor
1187 /// @return an `ast::TypeConstructorExpression` of an array with element type
1188 /// `subtype`, constructed with the values `args`.
1189 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001190 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001191 uint32_t n,
1192 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001193 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001194 }
1195
1196 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001197 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001198 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001199 /// @param constructor constructor expression
1200 /// @param decorations variable decorations
1201 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001202 template <typename NAME>
1203 ast::Variable* Var(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001204 ast::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001205 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001206 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001207 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001208 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001209 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1210 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001211 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001212
1213 /// @param source the variable source
1214 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001216 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001217 /// @param constructor constructor expression
1218 /// @param decorations variable decorations
1219 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001220 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001221 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001222 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001223 ast::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001224 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001225 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001226 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001227 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001228 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001229 type, false, constructor, decorations);
1230 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001232 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001233 /// @param type the variable type
1234 /// @param constructor optional constructor expression
1235 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001236 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001237 template <typename NAME>
1238 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001239 ast::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001240 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001241 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001242 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001243 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001244 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001245 constructor, decorations);
1246 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001247
1248 /// @param source the variable source
1249 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250 /// @param type the variable type
1251 /// @param constructor optional constructor expression
1252 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001253 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001254 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001255 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001256 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001257 ast::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001258 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001259 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001260 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001261 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001262 ast::StorageClass::kNone, type, true,
1263 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001264 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265
James Pricedaf1f1c2021-04-08 22:15:48 +00001266 /// @param name the parameter name
1267 /// @param type the parameter type
1268 /// @param decorations optional parameter decorations
1269 /// @returns a constant `ast::Variable` with the given name and type
1270 template <typename NAME>
1271 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001272 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001273 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001274 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001275 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1276 ast::StorageClass::kNone, type, true, nullptr,
1277 decorations);
1278 }
1279
1280 /// @param source the parameter source
1281 /// @param name the parameter name
1282 /// @param type the parameter type
1283 /// @param decorations optional parameter decorations
1284 /// @returns a constant `ast::Variable` with the given name and type
1285 template <typename NAME>
1286 ast::Variable* Param(const Source& source,
1287 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001288 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001289 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001290 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001291 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1292 ast::StorageClass::kNone, type, true, nullptr,
1293 decorations);
1294 }
1295
Ben Clayton42708342021-04-21 17:55:12 +00001296 /// @param name the variable name
1297 /// @param type the variable type
1298 /// @param storage the variable storage class
1299 /// @param constructor constructor expression
1300 /// @param decorations variable decorations
1301 /// @returns a new `ast::Variable`, which is automatically registered as a
1302 /// global variable with the ast::Module.
1303 template <typename NAME>
1304 ast::Variable* Global(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001305 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001306 ast::StorageClass storage,
1307 ast::Expression* constructor = nullptr,
1308 ast::DecorationList decorations = {}) {
1309 auto* var =
1310 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1311 AST().AddGlobalVariable(var);
1312 return var;
1313 }
1314
1315 /// @param source the variable source
1316 /// @param name the variable name
1317 /// @param type the variable type
1318 /// @param storage the variable storage class
1319 /// @param constructor constructor expression
1320 /// @param decorations variable decorations
1321 /// @returns a new `ast::Variable`, which is automatically registered as a
1322 /// global variable with the ast::Module.
1323 template <typename NAME>
1324 ast::Variable* Global(const Source& source,
1325 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001326 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001327 ast::StorageClass storage,
1328 ast::Expression* constructor = nullptr,
1329 ast::DecorationList decorations = {}) {
1330 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1331 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001332 AST().AddGlobalVariable(var);
1333 return var;
1334 }
1335
1336 /// @param args the arguments to pass to Const()
1337 /// @returns a const `ast::Variable` constructed by calling Var() with the
1338 /// arguments of `args`, which is automatically registered as a global
1339 /// variable with the ast::Module.
1340 template <typename... ARGS>
1341 ast::Variable* GlobalConst(ARGS&&... args) {
1342 auto* var = Const(std::forward<ARGS>(args)...);
1343 AST().AddGlobalVariable(var);
1344 return var;
1345 }
1346
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001347 /// @param func the function name
1348 /// @param args the function call arguments
1349 /// @returns a `ast::CallExpression` to the function `func`, with the
1350 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1351 template <typename NAME, typename... ARGS>
1352 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1353 return create<ast::CallExpression>(Expr(func),
1354 ExprList(std::forward<ARGS>(args)...));
1355 }
1356
1357 /// @param lhs the left hand argument to the addition operation
1358 /// @param rhs the right hand argument to the addition operation
1359 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1360 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001361 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001362 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1363 Expr(std::forward<LHS>(lhs)),
1364 Expr(std::forward<RHS>(rhs)));
1365 }
1366
1367 /// @param lhs the left hand argument to the subtraction operation
1368 /// @param rhs the right hand argument to the subtraction operation
1369 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1370 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001371 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001372 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1373 Expr(std::forward<LHS>(lhs)),
1374 Expr(std::forward<RHS>(rhs)));
1375 }
1376
1377 /// @param lhs the left hand argument to the multiplication operation
1378 /// @param rhs the right hand argument to the multiplication operation
1379 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1380 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001381 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001382 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1383 Expr(std::forward<LHS>(lhs)),
1384 Expr(std::forward<RHS>(rhs)));
1385 }
1386
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001387 /// @param source the source information
1388 /// @param lhs the left hand argument to the multiplication operation
1389 /// @param rhs the right hand argument to the multiplication operation
1390 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1391 template <typename LHS, typename RHS>
1392 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1393 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1394 Expr(std::forward<LHS>(lhs)),
1395 Expr(std::forward<RHS>(rhs)));
1396 }
1397
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001398 /// @param lhs the left hand argument to the division operation
1399 /// @param rhs the right hand argument to the division operation
1400 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1401 template <typename LHS, typename RHS>
1402 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1403 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1404 Expr(std::forward<LHS>(lhs)),
1405 Expr(std::forward<RHS>(rhs)));
1406 }
1407
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001408 /// @param arr the array argument for the array accessor expression
1409 /// @param idx the index argument for the array accessor expression
1410 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1411 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001412 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001413 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1414 Expr(std::forward<IDX>(idx)));
1415 }
1416
1417 /// @param obj the object for the member accessor expression
1418 /// @param idx the index argument for the array accessor expression
1419 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1420 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001421 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001422 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1423 Expr(std::forward<IDX>(idx)));
1424 }
1425
Ben Clayton42d1e092021-02-02 14:29:15 +00001426 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001427 /// @param val the offset value
1428 /// @returns the offset decoration pointer
1429 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1430 return create<ast::StructMemberOffsetDecoration>(source_, val);
1431 }
1432
Ben Claytond614dd52021-03-15 10:43:11 +00001433 /// Creates a ast::StructMemberSizeDecoration
1434 /// @param source the source information
1435 /// @param val the size value
1436 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001437 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1438 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001439 return create<ast::StructMemberSizeDecoration>(source, val);
1440 }
1441
1442 /// Creates a ast::StructMemberSizeDecoration
1443 /// @param val the size value
1444 /// @returns the size decoration pointer
1445 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1446 return create<ast::StructMemberSizeDecoration>(source_, val);
1447 }
1448
1449 /// Creates a ast::StructMemberAlignDecoration
1450 /// @param source the source information
1451 /// @param val the align value
1452 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001453 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1454 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001455 return create<ast::StructMemberAlignDecoration>(source, val);
1456 }
1457
1458 /// Creates a ast::StructMemberAlignDecoration
1459 /// @param val the align value
1460 /// @returns the align decoration pointer
1461 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1462 return create<ast::StructMemberAlignDecoration>(source_, val);
1463 }
1464
Ben Clayton42d1e092021-02-02 14:29:15 +00001465 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001466 /// @param source the source information
1467 /// @param name the function name
1468 /// @param params the function parameters
1469 /// @param type the function return type
1470 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001471 /// @param decorations the optional function decorations
1472 /// @param return_type_decorations the optional function return type
1473 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001474 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001475 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001476 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001477 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001478 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001479 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001480 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001481 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001482 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001483 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001484 auto* func =
1485 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1486 type, create<ast::BlockStatement>(body),
1487 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001488 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001489 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001490 }
1491
Ben Clayton42d1e092021-02-02 14:29:15 +00001492 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001493 /// @param name the function name
1494 /// @param params the function parameters
1495 /// @param type the function return type
1496 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001497 /// @param decorations the optional function decorations
1498 /// @param return_type_decorations the optional function return type
1499 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001500 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001501 template <typename NAME>
1502 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001503 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001504 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001505 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001506 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001507 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001508 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001509 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1510 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001511 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001512 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001513 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001514 }
1515
Ben Clayton49668bb2021-04-17 05:32:01 +00001516 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001517 /// @param source the source information
1518 /// @returns the return statement pointer
1519 ast::ReturnStatement* Return(const Source& source) {
1520 return create<ast::ReturnStatement>(source);
1521 }
1522
1523 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001524 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001525 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1526
1527 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001528 /// @param source the source information
1529 /// @param val the return value
1530 /// @returns the return statement pointer
1531 template <typename EXPR>
1532 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1533 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1534 }
1535
1536 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001537 /// @param val the return value
1538 /// @returns the return statement pointer
1539 template <typename EXPR>
1540 ast::ReturnStatement* Return(EXPR&& val) {
1541 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001542 }
1543
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001544 /// Creates a ast::Struct and sem::StructType, registering the
1545 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001546 /// @param source the source information
1547 /// @param name the struct name
1548 /// @param members the struct members
1549 /// @param decorations the optional struct decorations
1550 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001551 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001552 typ::Struct Structure(const Source& source,
1553 NAME&& name,
1554 ast::StructMemberList members,
1555 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001556 auto sym = Sym(std::forward<NAME>(name));
1557 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1558 std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001559 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001560 AST().AddConstructedType(type);
1561 return type;
1562 }
1563
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001564 /// Creates a ast::Struct and sem::StructType, registering the
1565 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001566 /// @param name the struct name
1567 /// @param members the struct members
1568 /// @param decorations the optional struct decorations
1569 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001570 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001571 typ::Struct Structure(NAME&& name,
1572 ast::StructMemberList members,
1573 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001574 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001575 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001576 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001577 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001578 AST().AddConstructedType(type);
1579 return type;
1580 }
1581
Ben Clayton42d1e092021-02-02 14:29:15 +00001582 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001583 /// @param source the source information
1584 /// @param name the struct member name
1585 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001586 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001587 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001588 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001589 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001590 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001591 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001592 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001593 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001594 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1595 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001596 }
1597
Ben Clayton42d1e092021-02-02 14:29:15 +00001598 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001599 /// @param name the struct member name
1600 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001601 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001602 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001603 template <typename NAME>
1604 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001605 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001606 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001607 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001608 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1609 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001610 }
1611
Ben Claytonbab31972021-02-08 21:16:21 +00001612 /// Creates a ast::StructMember with the given byte offset
1613 /// @param offset the offset to use in the StructMemberOffsetDecoration
1614 /// @param name the struct member name
1615 /// @param type the struct member type
1616 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001617 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001618 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001619 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001620 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001621 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001622 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001623 create<ast::StructMemberOffsetDecoration>(offset),
1624 });
1625 }
1626
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001627 /// Creates a ast::BlockStatement with input statements
1628 /// @param statements statements of block
1629 /// @returns the block statement pointer
1630 template <typename... Statements>
1631 ast::BlockStatement* Block(Statements&&... statements) {
1632 return create<ast::BlockStatement>(
1633 ast::StatementList{std::forward<Statements>(statements)...});
1634 }
1635
1636 /// Creates a ast::ElseStatement with input condition and body
1637 /// @param condition the else condition expression
1638 /// @param body the else body
1639 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001640 template <typename CONDITION>
1641 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1642 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1643 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001644 }
1645
1646 /// Creates a ast::IfStatement with input condition, body, and optional
1647 /// variadic else statements
1648 /// @param condition the if statement condition expression
1649 /// @param body the if statement body
1650 /// @param elseStatements optional variadic else statements
1651 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001652 template <typename CONDITION, typename... ELSE_STATEMENTS>
1653 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001654 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001655 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001656 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001657 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001658 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001659 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001660 }
1661
1662 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001663 /// @param source the source information
1664 /// @param lhs the left hand side expression initializer
1665 /// @param rhs the right hand side expression initializer
1666 /// @returns the assignment statement pointer
1667 template <typename LhsExpressionInit, typename RhsExpressionInit>
1668 ast::AssignmentStatement* Assign(const Source& source,
1669 LhsExpressionInit&& lhs,
1670 RhsExpressionInit&& rhs) {
1671 return create<ast::AssignmentStatement>(
1672 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1673 Expr(std::forward<RhsExpressionInit>(rhs)));
1674 }
1675
1676 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001677 /// @param lhs the left hand side expression initializer
1678 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001679 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001680 template <typename LhsExpressionInit, typename RhsExpressionInit>
1681 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1682 RhsExpressionInit&& rhs) {
1683 return create<ast::AssignmentStatement>(
1684 Expr(std::forward<LhsExpressionInit>(lhs)),
1685 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001686 }
1687
1688 /// Creates a ast::LoopStatement with input body and optional continuing
1689 /// @param body the loop body
1690 /// @param continuing the optional continuing block
1691 /// @returns the loop statement pointer
1692 ast::LoopStatement* Loop(ast::BlockStatement* body,
1693 ast::BlockStatement* continuing = nullptr) {
1694 return create<ast::LoopStatement>(body, continuing);
1695 }
1696
1697 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001698 /// @param source the source information
1699 /// @param var the variable to wrap in a decl statement
1700 /// @returns the variable decl statement pointer
1701 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1702 return create<ast::VariableDeclStatement>(source, var);
1703 }
1704
1705 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001706 /// @param var the variable to wrap in a decl statement
1707 /// @returns the variable decl statement pointer
1708 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1709 return create<ast::VariableDeclStatement>(var);
1710 }
1711
Antonio Maioranocea744d2021-03-25 12:55:27 +00001712 /// Creates a ast::SwitchStatement with input expression and cases
1713 /// @param condition the condition expression initializer
1714 /// @param cases case statements
1715 /// @returns the switch statement pointer
1716 template <typename ExpressionInit, typename... Cases>
1717 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1718 return create<ast::SwitchStatement>(
1719 Expr(std::forward<ExpressionInit>(condition)),
1720 ast::CaseStatementList{std::forward<Cases>(cases)...});
1721 }
1722
1723 /// Creates a ast::CaseStatement with input list of selectors, and body
1724 /// @param selectors list of selectors
1725 /// @param body the case body
1726 /// @returns the case statement pointer
1727 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1728 ast::BlockStatement* body = nullptr) {
1729 return create<ast::CaseStatement>(std::move(selectors),
1730 body ? body : Block());
1731 }
1732
1733 /// Convenient overload that takes a single selector
1734 /// @param selector a single case selector
1735 /// @param body the case body
1736 /// @returns the case statement pointer
1737 ast::CaseStatement* Case(ast::IntLiteral* selector,
1738 ast::BlockStatement* body = nullptr) {
1739 return Case(ast::CaseSelectorList{selector}, body);
1740 }
1741
1742 /// Convenience function that creates a 'default' ast::CaseStatement
1743 /// @param body the case body
1744 /// @returns the case statement pointer
1745 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1746 return Case(ast::CaseSelectorList{}, body);
1747 }
1748
James Price68f558f2021-04-06 15:51:47 +00001749 /// Creates an ast::BuiltinDecoration
1750 /// @param source the source information
1751 /// @param builtin the builtin value
1752 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001753 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001754 return create<ast::BuiltinDecoration>(source, builtin);
1755 }
1756
1757 /// Creates an ast::BuiltinDecoration
1758 /// @param builtin the builtin value
1759 /// @returns the builtin decoration pointer
1760 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1761 return create<ast::BuiltinDecoration>(source_, builtin);
1762 }
1763
1764 /// Creates an ast::LocationDecoration
1765 /// @param source the source information
1766 /// @param location the location value
1767 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001768 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001769 return create<ast::LocationDecoration>(source, location);
1770 }
1771
1772 /// Creates an ast::LocationDecoration
1773 /// @param location the location value
1774 /// @returns the location decoration pointer
1775 ast::LocationDecoration* Location(uint32_t location) {
1776 return create<ast::LocationDecoration>(source_, location);
1777 }
1778
1779 /// Creates an ast::StageDecoration
1780 /// @param source the source information
1781 /// @param stage the pipeline stage
1782 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001783 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001784 return create<ast::StageDecoration>(source, stage);
1785 }
1786
1787 /// Creates an ast::StageDecoration
1788 /// @param stage the pipeline stage
1789 /// @returns the stage decoration pointer
1790 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1791 return create<ast::StageDecoration>(source_, stage);
1792 }
1793
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001794 /// Sets the current builder source to `src`
1795 /// @param src the Source used for future create() calls
1796 void SetSource(const Source& src) {
1797 AssertNotMoved();
1798 source_ = src;
1799 }
1800
1801 /// Sets the current builder source to `loc`
1802 /// @param loc the Source used for future create() calls
1803 void SetSource(const Source::Location& loc) {
1804 AssertNotMoved();
1805 source_ = Source(loc);
1806 }
1807
Ben Clayton33352542021-01-29 16:43:41 +00001808 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001809 /// @note As the Resolver is run when the Program is built, this will only be
1810 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001811 /// @param expr the AST expression
1812 /// @return the resolved semantic type for the expression, or nullptr if the
1813 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001814 sem::Type* TypeOf(const ast::Expression* expr) const;
1815
1816 /// Helper for returning the resolved semantic type of the AST type `type`.
1817 /// @note As the Resolver is run when the Program is built, this will only be
1818 /// useful for the Resolver itself and tests that use their own Resolver.
1819 /// @param expr the AST type
1820 /// @return the resolved semantic type for the type, or nullptr if the type
1821 /// has no resolved type.
1822 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001823
Ben Clayton169512e2021-04-17 05:52:11 +00001824 /// Wraps the ast::Literal in a statement. This is used by tests that
1825 /// construct a partial AST and require the Resolver to reach these
1826 /// nodes.
1827 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1828 /// @return the ast::Statement that wraps the ast::Statement
1829 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001830 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001831 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001832 /// nodes.
1833 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1834 /// @return the ast::Statement that wraps the ast::Expression
1835 ast::Statement* WrapInStatement(ast::Expression* expr);
1836 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001837 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001838 /// these nodes.
1839 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1840 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1841 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1842 /// Returns the statement argument. Used as a passthrough-overload by
1843 /// WrapInFunction().
1844 /// @param stmt the ast::Statement
1845 /// @return `stmt`
1846 ast::Statement* WrapInStatement(ast::Statement* stmt);
1847 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001848 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001849 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001850 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001851 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001852 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001853 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001854 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001855 }
1856 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001857 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001858 /// @returns the function
1859 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001860
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001861 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001862 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001863
1864 protected:
1865 /// Asserts that the builder has not been moved.
1866 void AssertNotMoved() const;
1867
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001868 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001869 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001870 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001871 ASTNodeAllocator ast_nodes_;
1872 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001873 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001874 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001875 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001876 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001877
1878 /// The source to use when creating AST nodes without providing a Source as
1879 /// the first argument.
1880 Source source_;
1881
Ben Clayton5f0ea112021-03-09 10:54:37 +00001882 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001883 /// program when built.
1884 bool resolve_on_build_ = true;
1885
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001886 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1887 bool moved_ = false;
1888};
1889
1890//! @cond Doxygen_Suppress
1891// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1892template <>
1893struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001894 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001895 return t->i32();
1896 }
1897};
1898template <>
1899struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001900 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001901 return t->u32();
1902 }
1903};
1904template <>
1905struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001906 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001907 return t->f32();
1908 }
1909};
1910template <>
1911struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001912 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001913 return t->bool_();
1914 }
1915};
1916template <>
1917struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001918 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001919 return t->void_();
1920 }
1921};
1922//! @endcond
1923
Ben Clayton917b14b2021-04-19 16:50:23 +00001924/// @param builder the ProgramBuilder
1925/// @returns the ProgramID of the ProgramBuilder
1926inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1927 return builder->ID();
1928}
1929
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001930} // namespace tint
1931
1932#endif // SRC_PROGRAM_BUILDER_H_