blob: 2ab6624465584c1925058e71d1ef81ad9a7f106b [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>
Ben Clayton58750ea2021-05-06 15:52:33 +0000307 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
308 !traits::IsTypeOrDerived<T, sem::Type>::value,
309 T>*
310 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000311 AssertNotMoved();
312 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
313 }
314
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000315 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000316 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
317 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000318 /// Types are unique (de-aliased), and so calling create() for the same `T`
319 /// and arguments will return the same pointer.
320 /// @warning Use this method to acquire a type only if all of its type
321 /// information is provided in the constructor arguments `args`.<br>
322 /// If the type requires additional configuration after construction that
323 /// affect its fundamental type, build the type with `std::make_unique`, make
324 /// any necessary alterations and then call unique_type() instead.
325 /// @param args the arguments to pass to the type constructor
326 /// @returns the de-aliased type pointer
327 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000328 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
329 static_assert(std::is_base_of<sem::Type, T>::value,
330 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000331 AssertNotMoved();
332 return types_.Get<T>(std::forward<ARGS>(args)...);
333 }
334
335 /// Marks this builder as moved, preventing any further use of the builder.
336 void MarkAsMoved();
337
338 //////////////////////////////////////////////////////////////////////////////
339 // TypesBuilder
340 //////////////////////////////////////////////////////////////////////////////
341
342 /// TypesBuilder holds basic `tint` types and methods for constructing
343 /// complex types.
344 class TypesBuilder {
345 public:
346 /// Constructor
347 /// @param builder the program builder
348 explicit TypesBuilder(ProgramBuilder* builder);
349
350 /// @return the tint AST type for the C type `T`.
351 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000352 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000353 return CToAST<T>::get(this);
354 }
355
356 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000357 typ::Bool bool_() const {
358 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
359 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000360
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000361 /// @param source the Source of the node
362 /// @returns a boolean type
363 typ::Bool bool_(const Source& source) const {
364 return {builder->create<ast::Bool>(source), builder->create<sem::Bool>()};
365 }
366
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000367 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000368 typ::F32 f32() const {
369 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
370 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000371
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000372 /// @param source the Source of the node
373 /// @returns a f32 type
374 typ::F32 f32(const Source& source) const {
375 return {builder->create<ast::F32>(source), builder->create<sem::F32>()};
376 }
377
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000378 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000379 typ::I32 i32() const {
380 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
381 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000382
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000383 /// @param source the Source of the node
384 /// @returns a i32 type
385 typ::I32 i32(const Source& source) const {
386 return {builder->create<ast::I32>(source), builder->create<sem::I32>()};
387 }
388
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000389 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000390 typ::U32 u32() const {
391 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
392 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000393
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000394 /// @param source the Source of the node
395 /// @returns a u32 type
396 typ::U32 u32(const Source& source) const {
397 return {builder->create<ast::U32>(source), builder->create<sem::U32>()};
398 }
399
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000400 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000401 typ::Void void_() const {
402 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
403 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000404
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000405 /// @param source the Source of the node
406 /// @returns a void type
407 typ::Void void_(const Source& source) const {
408 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
409 }
410
Antonio Maiorano25436862021-04-13 13:32:33 +0000411 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000412 /// @param n vector width in elements
413 /// @return the tint AST type for a `n`-element vector of `type`.
414 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000415 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000416 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
417 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000418 }
419
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 /// @param source the Source of the node
421 /// @param type vector subtype
422 /// @param n vector width in elements
423 /// @return the tint AST type for a `n`-element vector of `type`.
424 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000425 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000426 return {
427 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
428 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000429 }
430
Antonio Maiorano25436862021-04-13 13:32:33 +0000431 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000432 /// @return the tint AST type for a 2-element vector of `type`.
433 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
434
435 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000436 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000437 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000438
439 /// @param type vector subtype
440 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000441 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
442
443 /// @param n vector width in elements
444 /// @return the tint AST type for a `n`-element vector of `type`.
445 template <typename T>
446 typ::Vector vec(uint32_t n) const {
447 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000448 }
449
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000450 /// @return the tint AST type for a 2-element vector of the C type `T`.
451 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000452 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000453 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000454 }
455
456 /// @return the tint AST type for a 3-element vector of the C type `T`.
457 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000458 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000459 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000460 }
461
462 /// @return the tint AST type for a 4-element vector of the C type `T`.
463 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000464 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000465 return vec4(Of<T>());
466 }
467
468 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000469 /// @param columns number of columns for the matrix
470 /// @param rows number of rows for the matrix
471 /// @return the tint AST type for a matrix of `type`
472 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000473 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000474 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
475 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000476 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000477 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000478 }
479
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000480 /// @param source the Source of the node
481 /// @param type matrix subtype
482 /// @param columns number of columns for the matrix
483 /// @param rows number of rows for the matrix
484 /// @return the tint AST type for a matrix of `type`
485 typ::Matrix mat(const Source& source,
486 typ::Type type,
487 uint32_t columns,
488 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000489 return {type.ast
490 ? builder->create<ast::Matrix>(source, type, rows, columns)
491 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000492 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000493 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000494 }
495
Ben Claytone204f272021-04-22 14:40:23 +0000496 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000497 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000498 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000499
500 /// @param type matrix subtype
501 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000502 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000503
504 /// @param type matrix subtype
505 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000506 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000507
508 /// @param type matrix subtype
509 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000510 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000511
512 /// @param type matrix subtype
513 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000514 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000515
516 /// @param type matrix subtype
517 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000518 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000519
520 /// @param type matrix subtype
521 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000522 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000523
524 /// @param type matrix subtype
525 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000526 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000527
528 /// @param type matrix subtype
529 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000530 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000531
Ben Claytone204f272021-04-22 14:40:23 +0000532 /// @param columns number of columns for the matrix
533 /// @param rows number of rows for the matrix
534 /// @return the tint AST type for a matrix of `type`
535 template <typename T>
536 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
537 return mat(Of<T>(), columns, rows);
538 }
539
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000540 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
541 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000542 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000543 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000544 }
545
546 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
547 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000548 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000549 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000550 }
551
552 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
553 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000554 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000555 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000556 }
557
558 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
559 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000560 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000561 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000562 }
563
564 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
565 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000566 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000567 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000568 }
569
570 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
571 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000572 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000573 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000574 }
575
576 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
577 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000578 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000579 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000580 }
581
582 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
583 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000584 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000585 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000586 }
587
588 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
589 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000590 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000591 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000592 }
593
594 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000595 /// @param n the array size. 0 represents a runtime-array
596 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000597 /// @return the tint AST type for a array of size `n` of type `T`
Ben Claytone204f272021-04-22 14:40:23 +0000598 typ::Array array(typ::Type subtype,
599 uint32_t n = 0,
600 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000601 subtype = MaybeCreateTypename(subtype);
Ben Clayton95c2e952021-04-28 12:58:13 +0000602 return {subtype.ast ? builder->create<ast::Array>(subtype, n, decos)
603 : nullptr,
604 subtype.sem ? builder->create<sem::ArrayType>(subtype, n,
605 std::move(decos))
606 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000607 }
608
609 /// @param source the Source of the node
610 /// @param subtype the array element type
611 /// @param n the array size. 0 represents a runtime-array
612 /// @param decos the optional decorations for the array
613 /// @return the tint AST type for a array of size `n` of type `T`
614 typ::Array array(const Source& source,
615 typ::Type subtype,
616 uint32_t n = 0,
617 ast::DecorationList decos = {}) const {
618 subtype = MaybeCreateTypename(subtype);
Ben Clayton95c2e952021-04-28 12:58:13 +0000619 return {
620 subtype.ast ? builder->create<ast::Array>(source, subtype, n, decos)
621 : nullptr,
622 subtype.sem
623 ? builder->create<sem::ArrayType>(subtype, n, std::move(decos))
624 : nullptr};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000625 }
626
Ben Claytonbab31972021-02-08 21:16:21 +0000627 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000628 /// @param n the array size. 0 represents a runtime-array
629 /// @param stride the array stride
Ben Claytonbab31972021-02-08 21:16:21 +0000630 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton7241a502021-04-22 14:32:53 +0000631 typ::Array array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000632 subtype = MaybeCreateTypename(subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000633 return array(subtype, n,
634 {builder->create<ast::StrideDecoration>(stride)});
Ben Claytonbab31972021-02-08 21:16:21 +0000635 }
636
Ben Clayton0f88b312021-05-04 17:36:31 +0000637 /// @param source the Source of the node
638 /// @param subtype the array element type
639 /// @param n the array size. 0 represents a runtime-array
640 /// @param stride the array stride
641 /// @return the tint AST type for a array of size `n` of type `T`
642 typ::Array array(const Source& source,
643 typ::Type subtype,
644 uint32_t n,
645 uint32_t stride) const {
646 subtype = MaybeCreateTypename(subtype);
647 return array(source, subtype, n,
648 {builder->create<ast::StrideDecoration>(stride)});
649 }
650
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000651 /// @return the tint AST type for an array of size `N` of type `T`
652 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000653 typ::Array array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000654 return array(Of<T>(), N);
655 }
656
Ben Claytonbab31972021-02-08 21:16:21 +0000657 /// @param stride the array stride
658 /// @return the tint AST type for an array of size `N` of type `T`
659 template <typename T, int N = 0>
Ben Clayton7241a502021-04-22 14:32:53 +0000660 typ::Array array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000661 return array(Of<T>(), N, stride);
662 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000663
Ben Clayton0f88b312021-05-04 17:36:31 +0000664 /// Creates a type name
665 /// @param name the name
666 /// @returns the type name
667 template <typename NAME>
668 ast::TypeName* type_name(NAME&& name) const {
669 return builder->create<ast::TypeName>(
670 builder->Sym(std::forward<NAME>(name)));
671 }
672
673 /// Creates a type name
674 /// @param source the Source of the node
675 /// @param name the name
676 /// @returns the type name
677 template <typename NAME>
678 ast::TypeName* type_name(const Source& source, NAME&& name) const {
679 return builder->create<ast::TypeName>(
680 source, builder->Sym(std::forward<NAME>(name)));
681 }
682
Ben Clayton42d1e092021-02-02 14:29:15 +0000683 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000684 /// @param name the alias name
685 /// @param type the alias type
686 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000687 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +0000688 typ::Alias alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000689 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000690 auto sym = builder->Sym(std::forward<NAME>(name));
691 return {
Ben Clayton0f88b312021-05-04 17:36:31 +0000692 type.ast ? builder->create<ast::Alias>(sym, type) : nullptr,
693 type.sem ? builder->create<sem::Alias>(sym, type) : nullptr,
Ben Claytone204f272021-04-22 14:40:23 +0000694 };
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000695 }
696
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000697 /// Creates an alias type
698 /// @param source the Source of the node
699 /// @param name the alias name
700 /// @param type the alias type
701 /// @returns the alias pointer
702 template <typename NAME>
703 typ::Alias alias(const Source& source, NAME&& name, typ::Type type) const {
704 type = MaybeCreateTypename(type);
705 auto sym = builder->Sym(std::forward<NAME>(name));
706 return {
Ben Clayton95c2e952021-04-28 12:58:13 +0000707 type.ast ? builder->create<ast::Alias>(source, sym, type) : nullptr,
708 type.sem ? builder->create<sem::Alias>(sym, type) : nullptr,
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000709 };
710 }
711
Ben Clayton4db10dd2021-04-16 08:47:14 +0000712 /// Creates an access control qualifier type
713 /// @param access the access control
714 /// @param type the inner type
715 /// @returns the access control qualifier type
Ben Claytone204f272021-04-22 14:40:23 +0000716 typ::AccessControl access(ast::AccessControl::Access access,
717 typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000718 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000719 return {type.ast ? builder->create<ast::AccessControl>(access, type)
720 : nullptr,
721 type.sem ? builder->create<sem::AccessControl>(access, type)
722 : nullptr};
Ben Clayton4db10dd2021-04-16 08:47:14 +0000723 }
724
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000725 /// Creates an access control qualifier type
726 /// @param source the Source of the node
727 /// @param access the access control
728 /// @param type the inner type
729 /// @returns the access control qualifier type
730 typ::AccessControl access(const Source& source,
731 ast::AccessControl::Access access,
732 typ::Type type) const {
733 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000734 return {type.ast
735 ? builder->create<ast::AccessControl>(source, access, type)
736 : nullptr,
737 type.sem ? builder->create<sem::AccessControl>(access, type)
738 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000739 }
740
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000741 /// @param type the type of the pointer
742 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000743 /// @return the pointer to `type` with the given ast::StorageClass
744 typ::Pointer pointer(typ::Type type,
745 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000746 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000747 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
748 : nullptr,
749 type.sem ? builder->create<sem::Pointer>(type, storage_class)
750 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000751 }
752
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000753 /// @param source the Source of the node
754 /// @param type the type of the pointer
755 /// @param storage_class the storage class of the pointer
756 /// @return the pointer to `type` with the given ast::StorageClass
757 typ::Pointer pointer(const Source& source,
758 typ::Type type,
759 ast::StorageClass storage_class) const {
760 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000761 return {type.ast
762 ? builder->create<ast::Pointer>(source, type, storage_class)
763 : nullptr,
764 type.sem ? builder->create<sem::Pointer>(type, storage_class)
765 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000766 }
767
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000768 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000769 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000770 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000771 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000772 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000773 }
774
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000775 /// @param impl the struct implementation
776 /// @returns a struct pointer
Ben Claytone204f272021-04-22 14:40:23 +0000777 typ::Struct struct_(ast::Struct* impl) const {
778 return {impl, builder->create<sem::StructType>(impl)};
779 }
780
781 /// @param kind the kind of sampler
782 /// @returns the sampler
783 typ::Sampler sampler(ast::SamplerKind kind) const {
784 return {builder->create<ast::Sampler>(kind),
785 builder->create<sem::Sampler>(kind)};
786 }
787
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000788 /// @param source the Source of the node
789 /// @param kind the kind of sampler
790 /// @returns the sampler
791 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
792 return {builder->create<ast::Sampler>(source, kind),
793 builder->create<sem::Sampler>(kind)};
794 }
795
Ben Claytone204f272021-04-22 14:40:23 +0000796 /// @param dims the dimensionality of the texture
797 /// @returns the depth texture
798 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
799 return {builder->create<ast::DepthTexture>(dims),
800 builder->create<sem::DepthTexture>(dims)};
801 }
802
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000803 /// @param source the Source of the node
804 /// @param dims the dimensionality of the texture
805 /// @returns the depth texture
806 typ::DepthTexture depth_texture(const Source& source,
807 ast::TextureDimension dims) const {
808 return {builder->create<ast::DepthTexture>(source, dims),
809 builder->create<sem::DepthTexture>(dims)};
810 }
811
Ben Claytone204f272021-04-22 14:40:23 +0000812 /// @param dims the dimensionality of the texture
813 /// @param subtype the texture subtype.
814 /// @returns the sampled texture
815 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
816 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000817 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
818 : nullptr,
819 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
820 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000821 }
822
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000823 /// @param source the Source of the node
824 /// @param dims the dimensionality of the texture
825 /// @param subtype the texture subtype.
826 /// @returns the sampled texture
827 typ::SampledTexture sampled_texture(const Source& source,
828 ast::TextureDimension dims,
829 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000830 return {subtype.ast
831 ? builder->create<ast::SampledTexture>(source, dims, subtype)
832 : nullptr,
833 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
834 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000835 }
836
Ben Claytone204f272021-04-22 14:40:23 +0000837 /// @param dims the dimensionality of the texture
838 /// @param subtype the texture subtype.
839 /// @returns the multisampled texture
840 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
841 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000842 return {
843 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
844 : nullptr,
845 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
846 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000847 }
848
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000849 /// @param source the Source of the node
850 /// @param dims the dimensionality of the texture
851 /// @param subtype the texture subtype.
852 /// @returns the multisampled texture
853 typ::MultisampledTexture multisampled_texture(const Source& source,
854 ast::TextureDimension dims,
855 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000856 return {
857 subtype.ast
858 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
859 : nullptr,
860 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
861 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000862 }
863
Ben Claytone204f272021-04-22 14:40:23 +0000864 /// @param dims the dimensionality of the texture
865 /// @param format the image format of the texture
866 /// @returns the storage texture
867 typ::StorageTexture storage_texture(ast::TextureDimension dims,
868 ast::ImageFormat format) const {
869 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
870 auto* sem_subtype =
871 sem::StorageTexture::SubtypeFor(format, builder->Types());
872 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
873 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000874 }
875
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000876 /// @param source the Source of the node
877 /// @param dims the dimensionality of the texture
878 /// @param format the image format of the texture
879 /// @returns the storage texture
880 typ::StorageTexture storage_texture(const Source& source,
881 ast::TextureDimension dims,
882 ast::ImageFormat format) const {
883 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
884 auto* sem_subtype =
885 sem::StorageTexture::SubtypeFor(format, builder->Types());
886 return {builder->create<ast::StorageTexture>(source, dims, format,
887 ast_subtype),
888 builder->create<sem::StorageTexture>(dims, format, sem_subtype)};
889 }
890
891 /// @param source the Source of the node
892 /// @returns the external texture
893 typ::ExternalTexture external_texture(const Source& source) const {
894 return {builder->create<ast::ExternalTexture>(source),
895 builder->create<sem::ExternalTexture>()};
896 }
897
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000898 /// If ty is a ast::Struct or ast::Alias, the returned type is an
899 /// ast::TypeName of the given type's name, otherwise type is returned.
900 /// @param type the type
901 /// @return either type or a pointer to a new ast::TypeName
902 typ::Type MaybeCreateTypename(typ::Type type) const;
903
Ben Claytonf5f311e2021-04-28 14:31:23 +0000904 /// The ProgramBuilder
905 ProgramBuilder* const builder;
906
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000907 private:
908 /// CToAST<T> is specialized for various `T` types and each specialization
909 /// contains a single static `get()` method for obtaining the corresponding
910 /// AST type for the C type `T`.
911 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000912 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000913 template <typename T>
914 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000915 };
916
917 //////////////////////////////////////////////////////////////////////////////
918 // AST helper methods
919 //////////////////////////////////////////////////////////////////////////////
920
James Price65ae64d2021-04-29 12:59:14 +0000921 /// @return a new unnamed symbol
922 Symbol Sym() { return Symbols().New(); }
923
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000924 /// @param name the symbol string
925 /// @return a Symbol with the given name
926 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
927
928 /// @param sym the symbol
929 /// @return `sym`
930 Symbol Sym(Symbol sym) { return sym; }
931
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 /// @param expr the expression
933 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000934 template <typename T>
935 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
936 return expr;
937 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000938
Ben Claytonb8ea5912021-04-19 16:52:42 +0000939 /// Passthrough for nullptr
940 /// @return nullptr
941 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
942
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000943 /// @param name the identifier name
944 /// @return an ast::IdentifierExpression with the given name
945 ast::IdentifierExpression* Expr(const std::string& name) {
946 return create<ast::IdentifierExpression>(Symbols().Register(name));
947 }
948
Ben Clayton46d78d72021-02-10 21:34:26 +0000949 /// @param symbol the identifier symbol
950 /// @return an ast::IdentifierExpression with the given symbol
951 ast::IdentifierExpression* Expr(Symbol symbol) {
952 return create<ast::IdentifierExpression>(symbol);
953 }
954
Ben Claytonb8ea5912021-04-19 16:52:42 +0000955 /// @param variable the AST variable
956 /// @return an ast::IdentifierExpression with the variable's symbol
957 ast::IdentifierExpression* Expr(ast::Variable* variable) {
958 return create<ast::IdentifierExpression>(variable->symbol());
959 }
960
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000961 /// @param source the source information
962 /// @param name the identifier name
963 /// @return an ast::IdentifierExpression with the given name
964 ast::IdentifierExpression* Expr(const Source& source,
965 const std::string& name) {
966 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
967 }
968
969 /// @param name the identifier name
970 /// @return an ast::IdentifierExpression with the given name
971 ast::IdentifierExpression* Expr(const char* name) {
972 return create<ast::IdentifierExpression>(Symbols().Register(name));
973 }
974
975 /// @param value the boolean value
976 /// @return a Scalar constructor for the given value
977 ast::ScalarConstructorExpression* Expr(bool value) {
978 return create<ast::ScalarConstructorExpression>(Literal(value));
979 }
980
981 /// @param value the float value
982 /// @return a Scalar constructor for the given value
983 ast::ScalarConstructorExpression* Expr(f32 value) {
984 return create<ast::ScalarConstructorExpression>(Literal(value));
985 }
986
987 /// @param value the integer value
988 /// @return a Scalar constructor for the given value
989 ast::ScalarConstructorExpression* Expr(i32 value) {
990 return create<ast::ScalarConstructorExpression>(Literal(value));
991 }
992
993 /// @param value the unsigned int value
994 /// @return a Scalar constructor for the given value
995 ast::ScalarConstructorExpression* Expr(u32 value) {
996 return create<ast::ScalarConstructorExpression>(Literal(value));
997 }
998
999 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1000 /// `list`.
1001 /// @param list the list to append too
1002 /// @param arg the arg to create
1003 template <typename ARG>
1004 void Append(ast::ExpressionList& list, ARG&& arg) {
1005 list.emplace_back(Expr(std::forward<ARG>(arg)));
1006 }
1007
1008 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1009 /// then appends them to `list`.
1010 /// @param list the list to append too
1011 /// @param arg0 the first argument
1012 /// @param args the rest of the arguments
1013 template <typename ARG0, typename... ARGS>
1014 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1015 Append(list, std::forward<ARG0>(arg0));
1016 Append(list, std::forward<ARGS>(args)...);
1017 }
1018
1019 /// @return an empty list of expressions
1020 ast::ExpressionList ExprList() { return {}; }
1021
1022 /// @param args the list of expressions
1023 /// @return the list of expressions converted to `ast::Expression`s using
1024 /// `Expr()`,
1025 template <typename... ARGS>
1026 ast::ExpressionList ExprList(ARGS&&... args) {
1027 ast::ExpressionList list;
1028 list.reserve(sizeof...(args));
1029 Append(list, std::forward<ARGS>(args)...);
1030 return list;
1031 }
1032
1033 /// @param list the list of expressions
1034 /// @return `list`
1035 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1036
1037 /// @param val the boolan value
1038 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001039 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001040
1041 /// @param val the float value
1042 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001043 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001044
1045 /// @param val the unsigned int value
1046 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001047 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001048
1049 /// @param val the integer value
1050 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001051 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001052
1053 /// @param args the arguments for the type constructor
1054 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1055 /// of `args` converted to `ast::Expression`s using `Expr()`
1056 template <typename T, typename... ARGS>
1057 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001058 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001059 }
1060
1061 /// @param type the type to construct
1062 /// @param args the arguments for the constructor
1063 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1064 /// values `args`.
1065 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001066 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001067 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001068 return create<ast::TypeConstructorExpression>(
1069 type, ExprList(std::forward<ARGS>(args)...));
1070 }
1071
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001072 /// Creates a constructor expression that constructs an object of
1073 /// `type` filled with `elem_value`. For example,
1074 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1075 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1076 /// @param type the type to construct
1077 /// @param elem_value the initial or element value (for vec and mat) to
1078 /// construct with
1079 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001080 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001081 int elem_value = 0);
1082
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001083 /// @param args the arguments for the vector constructor
1084 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1085 /// `T`, constructed with the values `args`.
1086 template <typename T, typename... ARGS>
1087 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001088 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001089 }
1090
1091 /// @param args the arguments for the vector constructor
1092 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1093 /// `T`, constructed with the values `args`.
1094 template <typename T, typename... ARGS>
1095 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001096 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001097 }
1098
1099 /// @param args the arguments for the vector constructor
1100 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1101 /// `T`, constructed with the values `args`.
1102 template <typename T, typename... ARGS>
1103 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001104 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001105 }
1106
1107 /// @param args the arguments for the matrix constructor
1108 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1109 /// `T`, constructed with the values `args`.
1110 template <typename T, typename... ARGS>
1111 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001112 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001113 }
1114
1115 /// @param args the arguments for the matrix constructor
1116 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1117 /// `T`, constructed with the values `args`.
1118 template <typename T, typename... ARGS>
1119 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001120 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 }
1122
1123 /// @param args the arguments for the matrix constructor
1124 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1125 /// `T`, constructed with the values `args`.
1126 template <typename T, typename... ARGS>
1127 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001128 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001129 }
1130
1131 /// @param args the arguments for the matrix constructor
1132 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1133 /// `T`, constructed with the values `args`.
1134 template <typename T, typename... ARGS>
1135 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001136 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 }
1138
1139 /// @param args the arguments for the matrix constructor
1140 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1141 /// `T`, constructed with the values `args`.
1142 template <typename T, typename... ARGS>
1143 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001144 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001145 }
1146
1147 /// @param args the arguments for the matrix constructor
1148 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1149 /// `T`, constructed with the values `args`.
1150 template <typename T, typename... ARGS>
1151 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001152 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001153 }
1154
1155 /// @param args the arguments for the matrix constructor
1156 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1157 /// `T`, constructed with the values `args`.
1158 template <typename T, typename... ARGS>
1159 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001160 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001161 }
1162
1163 /// @param args the arguments for the matrix constructor
1164 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1165 /// `T`, constructed with the values `args`.
1166 template <typename T, typename... ARGS>
1167 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001168 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001169 }
1170
1171 /// @param args the arguments for the matrix constructor
1172 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1173 /// `T`, constructed with the values `args`.
1174 template <typename T, typename... ARGS>
1175 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001176 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001177 }
1178
1179 /// @param args the arguments for the array constructor
1180 /// @return an `ast::TypeConstructorExpression` of an array with element type
1181 /// `T`, constructed with the values `args`.
1182 template <typename T, int N = 0, typename... ARGS>
1183 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001184 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001185 }
1186
1187 /// @param subtype the array element type
1188 /// @param n the array size. 0 represents a runtime-array.
1189 /// @param args the arguments for the array constructor
1190 /// @return an `ast::TypeConstructorExpression` of an array with element type
1191 /// `subtype`, constructed with the values `args`.
1192 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001193 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001194 uint32_t n,
1195 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001196 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001197 }
1198
1199 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001200 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001201 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001202 /// @param constructor constructor expression
1203 /// @param decorations variable decorations
1204 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001205 template <typename NAME>
1206 ast::Variable* Var(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001207 ast::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001208 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001209 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001210 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001211 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001212 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1213 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001214 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001215
1216 /// @param source the variable source
1217 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001219 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001220 /// @param constructor constructor expression
1221 /// @param decorations variable decorations
1222 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001223 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001224 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001225 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001226 ast::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +00001227 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001228 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001229 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001230 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001231 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001232 type, false, constructor, decorations);
1233 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001234
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001235 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001236 /// @param type the variable type
1237 /// @param constructor optional constructor expression
1238 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001239 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001240 template <typename NAME>
1241 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001242 ast::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001243 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001244 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001245 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001246 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001247 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001248 constructor, decorations);
1249 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001250
1251 /// @param source the variable source
1252 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001253 /// @param type the variable type
1254 /// @param constructor optional constructor expression
1255 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001256 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001257 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001258 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001259 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001260 ast::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +00001261 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001262 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001263 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001264 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001265 ast::StorageClass::kNone, type, true,
1266 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001267 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001268
James Pricedaf1f1c2021-04-08 22:15:48 +00001269 /// @param name the parameter name
1270 /// @param type the parameter type
1271 /// @param decorations optional parameter decorations
1272 /// @returns a constant `ast::Variable` with the given name and type
1273 template <typename NAME>
1274 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001275 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001276 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001277 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001278 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1279 ast::StorageClass::kNone, type, true, nullptr,
1280 decorations);
1281 }
1282
1283 /// @param source the parameter source
1284 /// @param name the parameter name
1285 /// @param type the parameter type
1286 /// @param decorations optional parameter decorations
1287 /// @returns a constant `ast::Variable` with the given name and type
1288 template <typename NAME>
1289 ast::Variable* Param(const Source& source,
1290 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001291 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001292 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001293 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001294 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1295 ast::StorageClass::kNone, type, true, nullptr,
1296 decorations);
1297 }
1298
Ben Clayton42708342021-04-21 17:55:12 +00001299 /// @param name the variable name
1300 /// @param type the variable type
1301 /// @param storage the variable storage class
1302 /// @param constructor constructor expression
1303 /// @param decorations variable decorations
1304 /// @returns a new `ast::Variable`, which is automatically registered as a
1305 /// global variable with the ast::Module.
1306 template <typename NAME>
1307 ast::Variable* Global(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001308 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001309 ast::StorageClass storage,
1310 ast::Expression* constructor = nullptr,
1311 ast::DecorationList decorations = {}) {
1312 auto* var =
1313 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1314 AST().AddGlobalVariable(var);
1315 return var;
1316 }
1317
1318 /// @param source the variable source
1319 /// @param name the variable name
1320 /// @param type the variable type
1321 /// @param storage the variable storage class
1322 /// @param constructor constructor expression
1323 /// @param decorations variable decorations
1324 /// @returns a new `ast::Variable`, which is automatically registered as a
1325 /// global variable with the ast::Module.
1326 template <typename NAME>
1327 ast::Variable* Global(const Source& source,
1328 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001329 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001330 ast::StorageClass storage,
1331 ast::Expression* constructor = nullptr,
1332 ast::DecorationList decorations = {}) {
1333 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1334 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001335 AST().AddGlobalVariable(var);
1336 return var;
1337 }
1338
1339 /// @param args the arguments to pass to Const()
1340 /// @returns a const `ast::Variable` constructed by calling Var() with the
1341 /// arguments of `args`, which is automatically registered as a global
1342 /// variable with the ast::Module.
1343 template <typename... ARGS>
1344 ast::Variable* GlobalConst(ARGS&&... args) {
1345 auto* var = Const(std::forward<ARGS>(args)...);
1346 AST().AddGlobalVariable(var);
1347 return var;
1348 }
1349
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001350 /// @param func the function name
1351 /// @param args the function call arguments
1352 /// @returns a `ast::CallExpression` to the function `func`, with the
1353 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1354 template <typename NAME, typename... ARGS>
1355 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1356 return create<ast::CallExpression>(Expr(func),
1357 ExprList(std::forward<ARGS>(args)...));
1358 }
1359
1360 /// @param lhs the left hand argument to the addition operation
1361 /// @param rhs the right hand argument to the addition operation
1362 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1363 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001364 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001365 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1366 Expr(std::forward<LHS>(lhs)),
1367 Expr(std::forward<RHS>(rhs)));
1368 }
1369
1370 /// @param lhs the left hand argument to the subtraction operation
1371 /// @param rhs the right hand argument to the subtraction operation
1372 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1373 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001374 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001375 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1376 Expr(std::forward<LHS>(lhs)),
1377 Expr(std::forward<RHS>(rhs)));
1378 }
1379
1380 /// @param lhs the left hand argument to the multiplication operation
1381 /// @param rhs the right hand argument to the multiplication operation
1382 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1383 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001384 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001385 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1386 Expr(std::forward<LHS>(lhs)),
1387 Expr(std::forward<RHS>(rhs)));
1388 }
1389
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001390 /// @param source the source information
1391 /// @param lhs the left hand argument to the multiplication operation
1392 /// @param rhs the right hand argument to the multiplication operation
1393 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1394 template <typename LHS, typename RHS>
1395 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1396 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1397 Expr(std::forward<LHS>(lhs)),
1398 Expr(std::forward<RHS>(rhs)));
1399 }
1400
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001401 /// @param lhs the left hand argument to the division operation
1402 /// @param rhs the right hand argument to the division operation
1403 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1404 template <typename LHS, typename RHS>
1405 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1406 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1407 Expr(std::forward<LHS>(lhs)),
1408 Expr(std::forward<RHS>(rhs)));
1409 }
1410
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001411 /// @param arr the array argument for the array accessor expression
1412 /// @param idx the index argument for the array accessor expression
1413 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1414 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001415 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001416 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1417 Expr(std::forward<IDX>(idx)));
1418 }
1419
1420 /// @param obj the object for the member accessor expression
1421 /// @param idx the index argument for the array accessor expression
1422 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1423 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001424 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001425 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1426 Expr(std::forward<IDX>(idx)));
1427 }
1428
Ben Clayton42d1e092021-02-02 14:29:15 +00001429 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001430 /// @param val the offset value
1431 /// @returns the offset decoration pointer
1432 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1433 return create<ast::StructMemberOffsetDecoration>(source_, val);
1434 }
1435
Ben Claytond614dd52021-03-15 10:43:11 +00001436 /// Creates a ast::StructMemberSizeDecoration
1437 /// @param source the source information
1438 /// @param val the size value
1439 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001440 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1441 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001442 return create<ast::StructMemberSizeDecoration>(source, val);
1443 }
1444
1445 /// Creates a ast::StructMemberSizeDecoration
1446 /// @param val the size value
1447 /// @returns the size decoration pointer
1448 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1449 return create<ast::StructMemberSizeDecoration>(source_, val);
1450 }
1451
1452 /// Creates a ast::StructMemberAlignDecoration
1453 /// @param source the source information
1454 /// @param val the align value
1455 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001456 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1457 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001458 return create<ast::StructMemberAlignDecoration>(source, val);
1459 }
1460
1461 /// Creates a ast::StructMemberAlignDecoration
1462 /// @param val the align value
1463 /// @returns the align decoration pointer
1464 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1465 return create<ast::StructMemberAlignDecoration>(source_, val);
1466 }
1467
Ben Clayton42d1e092021-02-02 14:29:15 +00001468 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001469 /// @param source the source information
1470 /// @param name the function name
1471 /// @param params the function parameters
1472 /// @param type the function return type
1473 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001474 /// @param decorations the optional function decorations
1475 /// @param return_type_decorations the optional function return type
1476 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001477 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001478 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001479 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001480 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001481 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001482 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001483 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001484 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001485 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001486 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001487 auto* func =
1488 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1489 type, create<ast::BlockStatement>(body),
1490 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001491 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001492 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001493 }
1494
Ben Clayton42d1e092021-02-02 14:29:15 +00001495 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001496 /// @param name the function name
1497 /// @param params the function parameters
1498 /// @param type the function return type
1499 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001500 /// @param decorations the optional function decorations
1501 /// @param return_type_decorations the optional function return type
1502 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001503 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001504 template <typename NAME>
1505 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001506 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001507 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001508 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001509 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001510 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001511 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001512 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1513 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001514 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001515 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001516 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001517 }
1518
Ben Clayton49668bb2021-04-17 05:32:01 +00001519 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001520 /// @param source the source information
1521 /// @returns the return statement pointer
1522 ast::ReturnStatement* Return(const Source& source) {
1523 return create<ast::ReturnStatement>(source);
1524 }
1525
1526 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001527 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001528 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1529
1530 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001531 /// @param source the source information
1532 /// @param val the return value
1533 /// @returns the return statement pointer
1534 template <typename EXPR>
1535 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1536 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1537 }
1538
1539 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001540 /// @param val the return value
1541 /// @returns the return statement pointer
1542 template <typename EXPR>
1543 ast::ReturnStatement* Return(EXPR&& val) {
1544 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001545 }
1546
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001547 /// Creates a ast::Struct and sem::StructType, registering the
1548 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001549 /// @param source the source information
1550 /// @param name the struct name
1551 /// @param members the struct members
1552 /// @param decorations the optional struct decorations
1553 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001554 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001555 typ::Struct Structure(const Source& source,
1556 NAME&& name,
1557 ast::StructMemberList members,
1558 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001559 auto sym = Sym(std::forward<NAME>(name));
1560 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1561 std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001562 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001563 AST().AddConstructedType(type);
1564 return type;
1565 }
1566
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001567 /// Creates a ast::Struct and sem::StructType, registering the
1568 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001569 /// @param name the struct name
1570 /// @param members the struct members
1571 /// @param decorations the optional struct decorations
1572 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001573 template <typename NAME>
Ben Claytone204f272021-04-22 14:40:23 +00001574 typ::Struct Structure(NAME&& name,
1575 ast::StructMemberList members,
1576 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001577 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001578 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001579 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytone204f272021-04-22 14:40:23 +00001580 auto type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001581 AST().AddConstructedType(type);
1582 return type;
1583 }
1584
Ben Clayton42d1e092021-02-02 14:29:15 +00001585 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001586 /// @param source the source information
1587 /// @param name the struct member name
1588 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001589 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001590 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001591 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001592 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001593 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001594 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001595 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001596 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001597 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1598 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001599 }
1600
Ben Clayton42d1e092021-02-02 14:29:15 +00001601 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001602 /// @param name the struct member name
1603 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001604 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001605 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001606 template <typename NAME>
1607 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001608 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001609 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001610 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001611 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1612 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001613 }
1614
Ben Claytonbab31972021-02-08 21:16:21 +00001615 /// Creates a ast::StructMember with the given byte offset
1616 /// @param offset the offset to use in the StructMemberOffsetDecoration
1617 /// @param name the struct member name
1618 /// @param type the struct member type
1619 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001620 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001621 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001622 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001623 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001624 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001625 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001626 create<ast::StructMemberOffsetDecoration>(offset),
1627 });
1628 }
1629
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001630 /// Creates a ast::BlockStatement with input statements
1631 /// @param statements statements of block
1632 /// @returns the block statement pointer
1633 template <typename... Statements>
1634 ast::BlockStatement* Block(Statements&&... statements) {
1635 return create<ast::BlockStatement>(
1636 ast::StatementList{std::forward<Statements>(statements)...});
1637 }
1638
1639 /// Creates a ast::ElseStatement with input condition and body
1640 /// @param condition the else condition expression
1641 /// @param body the else body
1642 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001643 template <typename CONDITION>
1644 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1645 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1646 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001647 }
1648
1649 /// Creates a ast::IfStatement with input condition, body, and optional
1650 /// variadic else statements
1651 /// @param condition the if statement condition expression
1652 /// @param body the if statement body
1653 /// @param elseStatements optional variadic else statements
1654 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001655 template <typename CONDITION, typename... ELSE_STATEMENTS>
1656 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001657 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001658 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001659 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001660 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001661 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001662 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001663 }
1664
1665 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001666 /// @param source the source information
1667 /// @param lhs the left hand side expression initializer
1668 /// @param rhs the right hand side expression initializer
1669 /// @returns the assignment statement pointer
1670 template <typename LhsExpressionInit, typename RhsExpressionInit>
1671 ast::AssignmentStatement* Assign(const Source& source,
1672 LhsExpressionInit&& lhs,
1673 RhsExpressionInit&& rhs) {
1674 return create<ast::AssignmentStatement>(
1675 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1676 Expr(std::forward<RhsExpressionInit>(rhs)));
1677 }
1678
1679 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001680 /// @param lhs the left hand side expression initializer
1681 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001682 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001683 template <typename LhsExpressionInit, typename RhsExpressionInit>
1684 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1685 RhsExpressionInit&& rhs) {
1686 return create<ast::AssignmentStatement>(
1687 Expr(std::forward<LhsExpressionInit>(lhs)),
1688 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001689 }
1690
1691 /// Creates a ast::LoopStatement with input body and optional continuing
1692 /// @param body the loop body
1693 /// @param continuing the optional continuing block
1694 /// @returns the loop statement pointer
1695 ast::LoopStatement* Loop(ast::BlockStatement* body,
1696 ast::BlockStatement* continuing = nullptr) {
1697 return create<ast::LoopStatement>(body, continuing);
1698 }
1699
1700 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001701 /// @param source the source information
1702 /// @param var the variable to wrap in a decl statement
1703 /// @returns the variable decl statement pointer
1704 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1705 return create<ast::VariableDeclStatement>(source, var);
1706 }
1707
1708 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001709 /// @param var the variable to wrap in a decl statement
1710 /// @returns the variable decl statement pointer
1711 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1712 return create<ast::VariableDeclStatement>(var);
1713 }
1714
Antonio Maioranocea744d2021-03-25 12:55:27 +00001715 /// Creates a ast::SwitchStatement with input expression and cases
1716 /// @param condition the condition expression initializer
1717 /// @param cases case statements
1718 /// @returns the switch statement pointer
1719 template <typename ExpressionInit, typename... Cases>
1720 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1721 return create<ast::SwitchStatement>(
1722 Expr(std::forward<ExpressionInit>(condition)),
1723 ast::CaseStatementList{std::forward<Cases>(cases)...});
1724 }
1725
1726 /// Creates a ast::CaseStatement with input list of selectors, and body
1727 /// @param selectors list of selectors
1728 /// @param body the case body
1729 /// @returns the case statement pointer
1730 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1731 ast::BlockStatement* body = nullptr) {
1732 return create<ast::CaseStatement>(std::move(selectors),
1733 body ? body : Block());
1734 }
1735
1736 /// Convenient overload that takes a single selector
1737 /// @param selector a single case selector
1738 /// @param body the case body
1739 /// @returns the case statement pointer
1740 ast::CaseStatement* Case(ast::IntLiteral* selector,
1741 ast::BlockStatement* body = nullptr) {
1742 return Case(ast::CaseSelectorList{selector}, body);
1743 }
1744
1745 /// Convenience function that creates a 'default' ast::CaseStatement
1746 /// @param body the case body
1747 /// @returns the case statement pointer
1748 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1749 return Case(ast::CaseSelectorList{}, body);
1750 }
1751
James Price68f558f2021-04-06 15:51:47 +00001752 /// Creates an ast::BuiltinDecoration
1753 /// @param source the source information
1754 /// @param builtin the builtin value
1755 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001756 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001757 return create<ast::BuiltinDecoration>(source, builtin);
1758 }
1759
1760 /// Creates an ast::BuiltinDecoration
1761 /// @param builtin the builtin value
1762 /// @returns the builtin decoration pointer
1763 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1764 return create<ast::BuiltinDecoration>(source_, builtin);
1765 }
1766
1767 /// Creates an ast::LocationDecoration
1768 /// @param source the source information
1769 /// @param location the location value
1770 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001771 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001772 return create<ast::LocationDecoration>(source, location);
1773 }
1774
1775 /// Creates an ast::LocationDecoration
1776 /// @param location the location value
1777 /// @returns the location decoration pointer
1778 ast::LocationDecoration* Location(uint32_t location) {
1779 return create<ast::LocationDecoration>(source_, location);
1780 }
1781
1782 /// Creates an ast::StageDecoration
1783 /// @param source the source information
1784 /// @param stage the pipeline stage
1785 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001786 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001787 return create<ast::StageDecoration>(source, stage);
1788 }
1789
1790 /// Creates an ast::StageDecoration
1791 /// @param stage the pipeline stage
1792 /// @returns the stage decoration pointer
1793 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1794 return create<ast::StageDecoration>(source_, stage);
1795 }
1796
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001797 /// Sets the current builder source to `src`
1798 /// @param src the Source used for future create() calls
1799 void SetSource(const Source& src) {
1800 AssertNotMoved();
1801 source_ = src;
1802 }
1803
1804 /// Sets the current builder source to `loc`
1805 /// @param loc the Source used for future create() calls
1806 void SetSource(const Source::Location& loc) {
1807 AssertNotMoved();
1808 source_ = Source(loc);
1809 }
1810
Ben Clayton33352542021-01-29 16:43:41 +00001811 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001812 /// @note As the Resolver is run when the Program is built, this will only be
1813 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001814 /// @param expr the AST expression
1815 /// @return the resolved semantic type for the expression, or nullptr if the
1816 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001817 sem::Type* TypeOf(const ast::Expression* expr) const;
1818
1819 /// Helper for returning the resolved semantic type of the AST type `type`.
1820 /// @note As the Resolver is run when the Program is built, this will only be
1821 /// useful for the Resolver itself and tests that use their own Resolver.
1822 /// @param expr the AST type
1823 /// @return the resolved semantic type for the type, or nullptr if the type
1824 /// has no resolved type.
1825 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001826
Ben Clayton169512e2021-04-17 05:52:11 +00001827 /// Wraps the ast::Literal in a statement. This is used by tests that
1828 /// construct a partial AST and require the Resolver to reach these
1829 /// nodes.
1830 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1831 /// @return the ast::Statement that wraps the ast::Statement
1832 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001833 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001834 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001835 /// nodes.
1836 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1837 /// @return the ast::Statement that wraps the ast::Expression
1838 ast::Statement* WrapInStatement(ast::Expression* expr);
1839 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001840 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001841 /// these nodes.
1842 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1843 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1844 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1845 /// Returns the statement argument. Used as a passthrough-overload by
1846 /// WrapInFunction().
1847 /// @param stmt the ast::Statement
1848 /// @return `stmt`
1849 ast::Statement* WrapInStatement(ast::Statement* stmt);
1850 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001851 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001852 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001853 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001854 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001855 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001856 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001857 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001858 }
1859 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001860 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001861 /// @returns the function
1862 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001863
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001864 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001865 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001866
1867 protected:
1868 /// Asserts that the builder has not been moved.
1869 void AssertNotMoved() const;
1870
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001871 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001872 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001873 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001874 ASTNodeAllocator ast_nodes_;
1875 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001876 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001877 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001878 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001879 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001880
1881 /// The source to use when creating AST nodes without providing a Source as
1882 /// the first argument.
1883 Source source_;
1884
Ben Clayton5f0ea112021-03-09 10:54:37 +00001885 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001886 /// program when built.
1887 bool resolve_on_build_ = true;
1888
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001889 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1890 bool moved_ = false;
1891};
1892
1893//! @cond Doxygen_Suppress
1894// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1895template <>
1896struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001897 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001898 return t->i32();
1899 }
1900};
1901template <>
1902struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001903 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001904 return t->u32();
1905 }
1906};
1907template <>
1908struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001909 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001910 return t->f32();
1911 }
1912};
1913template <>
1914struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001915 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001916 return t->bool_();
1917 }
1918};
1919template <>
1920struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001921 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001922 return t->void_();
1923 }
1924};
1925//! @endcond
1926
Ben Clayton917b14b2021-04-19 16:50:23 +00001927/// @param builder the ProgramBuilder
1928/// @returns the ProgramID of the ProgramBuilder
1929inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1930 return builder->ID();
1931}
1932
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001933} // namespace tint
1934
1935#endif // SRC_PROGRAM_BUILDER_H_