blob: 3a102ff09c7e89dfdfd12d5d33ff37085dd6e676 [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000026#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/bool_literal.h"
28#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000029#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000030#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000031#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000032#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000035#include "src/ast/if_statement.h"
36#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000037#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/member_accessor_expression.h"
39#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000040#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000041#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000043#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/scalar_constructor_expression.h"
46#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000047#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000049#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000050#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000053#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000055#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000056#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000058#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000059#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000060#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000061#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000062#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000063#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000064#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000065#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000066#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000067#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000068#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000069#include "src/sem/f32_type.h"
70#include "src/sem/i32_type.h"
71#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000073#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/sampled_texture_type.h"
75#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000076#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000077#include "src/sem/u32_type.h"
78#include "src/sem/vector_type.h"
79#include "src/sem/void_type.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 Clayton19d32052021-05-20 15:10:48 +0000352 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000353 return CToAST<T>::get(this);
354 }
355
356 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000357 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000358
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000359 /// @param source the Source of the node
360 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000361 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000362 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000363 }
364
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000365 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000366 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000367
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000368 /// @param source the Source of the node
369 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000370 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000371 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000372 }
373
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000374 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000375 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000376
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000377 /// @param source the Source of the node
378 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000379 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000380 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000381 }
382
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000383 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000384 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000385
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000386 /// @param source the Source of the node
387 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000388 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000389 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000390 }
391
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000392 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000393 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000394
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000395 /// @param source the Source of the node
396 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000397 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000398 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000399 }
400
Antonio Maiorano25436862021-04-13 13:32:33 +0000401 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000402 /// @param n vector width in elements
403 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000404 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000405 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000406 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000407 }
408
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000409 /// @param source the Source of the node
410 /// @param type vector subtype
411 /// @param n vector width in elements
412 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000413 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000414 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000415 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000416 }
417
Antonio Maiorano25436862021-04-13 13:32:33 +0000418 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000419 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000420 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000421
422 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000423 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000424 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000425
426 /// @param type vector subtype
427 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000428 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000429
430 /// @param n vector width in elements
431 /// @return the tint AST type for a `n`-element vector of `type`.
432 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000433 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000434 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000435 }
436
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000437 /// @return the tint AST type for a 2-element vector of the C type `T`.
438 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000439 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000440 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000441 }
442
443 /// @return the tint AST type for a 3-element vector of the C type `T`.
444 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000445 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000446 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000447 }
448
449 /// @return the tint AST type for a 4-element vector of the C type `T`.
450 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000451 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000452 return vec4(Of<T>());
453 }
454
455 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000456 /// @param columns number of columns for the matrix
457 /// @param rows number of rows for the matrix
458 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000459 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000460 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000461 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000462 }
463
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000464 /// @param source the Source of the node
465 /// @param type matrix subtype
466 /// @param columns number of columns for the matrix
467 /// @param rows number of rows for the matrix
468 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000469 ast::Matrix* mat(const Source& source,
470 ast::Type* type,
471 uint32_t columns,
472 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000473 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000474 }
475
Ben Claytone204f272021-04-22 14:40:23 +0000476 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000477 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000478 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000479
480 /// @param type matrix subtype
481 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000482 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000483
484 /// @param type matrix subtype
485 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000486 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000487
488 /// @param type matrix subtype
489 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000490 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000491
492 /// @param type matrix subtype
493 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000494 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000495
496 /// @param type matrix subtype
497 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000498 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000499
500 /// @param type matrix subtype
501 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000502 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000503
504 /// @param type matrix subtype
505 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000506 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000507
508 /// @param type matrix subtype
509 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000510 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000511
Ben Claytone204f272021-04-22 14:40:23 +0000512 /// @param columns number of columns for the matrix
513 /// @param rows number of rows for the matrix
514 /// @return the tint AST type for a matrix of `type`
515 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000516 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000517 return mat(Of<T>(), columns, rows);
518 }
519
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000520 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
521 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000522 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000523 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000524 }
525
526 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
527 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000528 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000529 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000530 }
531
532 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
533 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000534 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000535 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000536 }
537
538 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
539 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000540 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000541 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000542 }
543
544 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
545 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000546 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000547 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000548 }
549
550 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
551 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000552 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000553 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000554 }
555
556 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
557 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000558 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000559 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000560 }
561
562 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
563 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000564 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000565 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000566 }
567
568 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
569 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000570 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000571 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000572 }
573
574 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000575 /// @param n the array size. 0 represents a runtime-array
576 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000578 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000579 uint32_t n = 0,
580 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000581 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000582 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000583 }
584
585 /// @param source the Source of the node
586 /// @param subtype the array element type
587 /// @param n the array size. 0 represents a runtime-array
588 /// @param decos the optional decorations for the array
589 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000590 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000591 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000592 uint32_t n = 0,
593 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000594 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000595 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000596 }
597
Ben Claytonbab31972021-02-08 21:16:21 +0000598 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000599 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000600 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000601 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000602 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000603 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000604 ast::DecorationList decos;
605 if (stride) {
606 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
607 }
608 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000609 }
610
Ben Clayton0f88b312021-05-04 17:36:31 +0000611 /// @param source the Source of the node
612 /// @param subtype the array element type
613 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000614 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000615 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000616 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000617 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000618 uint32_t n,
619 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000620 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000621 ast::DecorationList decos;
622 if (stride) {
623 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
624 }
625 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000626 }
627
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000628 /// @return the tint AST type for an array of size `N` of type `T`
629 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000630 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000631 return array(Of<T>(), N);
632 }
633
Ben Claytonbab31972021-02-08 21:16:21 +0000634 /// @param stride the array stride
635 /// @return the tint AST type for an array of size `N` of type `T`
636 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000637 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000638 return array(Of<T>(), N, stride);
639 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000640
Ben Clayton0f88b312021-05-04 17:36:31 +0000641 /// Creates a type name
642 /// @param name the name
643 /// @returns the type name
644 template <typename NAME>
645 ast::TypeName* type_name(NAME&& name) const {
646 return builder->create<ast::TypeName>(
647 builder->Sym(std::forward<NAME>(name)));
648 }
649
650 /// Creates a type name
651 /// @param source the Source of the node
652 /// @param name the name
653 /// @returns the type name
654 template <typename NAME>
655 ast::TypeName* type_name(const Source& source, NAME&& name) const {
656 return builder->create<ast::TypeName>(
657 source, builder->Sym(std::forward<NAME>(name)));
658 }
659
Ben Clayton42d1e092021-02-02 14:29:15 +0000660 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000661 /// @param name the alias name
662 /// @param type the alias type
663 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000664 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000665 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000666 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000667 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000668 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000669 }
670
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000671 /// Creates an alias type
672 /// @param source the Source of the node
673 /// @param name the alias name
674 /// @param type the alias type
675 /// @returns the alias pointer
676 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000677 ast::Alias* alias(const Source& source,
678 NAME&& name,
679 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000680 type = MaybeCreateTypename(type);
681 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000682 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000683 }
684
Ben Clayton4db10dd2021-04-16 08:47:14 +0000685 /// Creates an access control qualifier type
686 /// @param access the access control
687 /// @param type the inner type
688 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000689 ast::AccessControl* access(ast::AccessControl::Access access,
690 const ast::Type* type) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000691 type = MaybeCreateTypename(type);
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000692 return type ? builder->create<ast::AccessControl>(access, type) : nullptr;
Ben Clayton4db10dd2021-04-16 08:47:14 +0000693 }
694
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000695 /// Creates an access control qualifier type
696 /// @param source the Source of the node
697 /// @param access the access control
698 /// @param type the inner type
699 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000700 ast::AccessControl* access(const Source& source,
701 ast::AccessControl::Access access,
702 const ast::Type* type) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000703 type = MaybeCreateTypename(type);
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000704 return type ? builder->create<ast::AccessControl>(source, access, type)
705 : nullptr;
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000706 }
707
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000708 /// @param type the type of the pointer
709 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000710 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000711 ast::Pointer* pointer(ast::Type* type,
712 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000713 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000714 return builder->create<ast::Pointer>(type, storage_class);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000715 }
716
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000717 /// @param source the Source of the node
718 /// @param type the type of the pointer
719 /// @param storage_class the storage class of the pointer
720 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000721 ast::Pointer* pointer(const Source& source,
722 ast::Type* type,
723 ast::StorageClass storage_class) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000724 type = MaybeCreateTypename(type);
Ben Clayton19b03192021-05-20 15:04:08 +0000725 return builder->create<ast::Pointer>(source, type, storage_class);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000726 }
727
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000728 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000729 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000730 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000731 ast::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000732 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000733 }
734
Ben Claytone204f272021-04-22 14:40:23 +0000735 /// @param kind the kind of sampler
736 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000737 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000738 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000739 }
740
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000741 /// @param source the Source of the node
742 /// @param kind the kind of sampler
743 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000744 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000745 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000746 }
747
Ben Claytone204f272021-04-22 14:40:23 +0000748 /// @param dims the dimensionality of the texture
749 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000750 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000751 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000752 }
753
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000754 /// @param source the Source of the node
755 /// @param dims the dimensionality of the texture
756 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000757 ast::DepthTexture* depth_texture(const Source& source,
758 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000759 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000760 }
761
Ben Claytone204f272021-04-22 14:40:23 +0000762 /// @param dims the dimensionality of the texture
763 /// @param subtype the texture subtype.
764 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000765 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
766 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000767 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000768 }
769
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000770 /// @param source the Source of the node
771 /// @param dims the dimensionality of the texture
772 /// @param subtype the texture subtype.
773 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000774 ast::SampledTexture* sampled_texture(const Source& source,
775 ast::TextureDimension dims,
776 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000777 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000778 }
779
Ben Claytone204f272021-04-22 14:40:23 +0000780 /// @param dims the dimensionality of the texture
781 /// @param subtype the texture subtype.
782 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000783 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
784 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000785 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000786 }
787
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000788 /// @param source the Source of the node
789 /// @param dims the dimensionality of the texture
790 /// @param subtype the texture subtype.
791 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000792 ast::MultisampledTexture* multisampled_texture(const Source& source,
793 ast::TextureDimension dims,
794 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000795 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000796 }
797
Ben Claytone204f272021-04-22 14:40:23 +0000798 /// @param dims the dimensionality of the texture
799 /// @param format the image format of the texture
800 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000801 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
802 ast::ImageFormat format) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000803 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton19b03192021-05-20 15:04:08 +0000804 return builder->create<ast::StorageTexture>(dims, format, subtype);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000805 }
806
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000807 /// @param source the Source of the node
808 /// @param dims the dimensionality of the texture
809 /// @param format the image format of the texture
810 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000811 ast::StorageTexture* storage_texture(const Source& source,
812 ast::TextureDimension dims,
813 ast::ImageFormat format) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000814 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton19b03192021-05-20 15:04:08 +0000815 return builder->create<ast::StorageTexture>(source, dims, format,
816 subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000817 }
818
Brandon Jones7b257692021-05-17 17:40:17 +0000819 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000820 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000821 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000822 }
823
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000824 /// @param source the Source of the node
825 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000826 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000827 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000828 }
829
Ben Clayton19b03192021-05-20 15:04:08 +0000830 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
831 /// If ty is a ast::Struct or ast::Alias, the returned type is an
832 /// ast::TypeName of the given type's name, otherwise type is returned.
833 /// @param type the type
834 /// @return either type or a pointer to a new ast::TypeName
835 ast::Type* MaybeCreateTypename(ast::Type* type) const;
836
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000837 /// If ty is a ast::Struct or ast::Alias, the returned type is an
838 /// ast::TypeName of the given type's name, otherwise type is returned.
839 /// @param type the type
840 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton19b03192021-05-20 15:04:08 +0000841 const ast::Type* MaybeCreateTypename(const ast::Type* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000842
Ben Claytonf5f311e2021-04-28 14:31:23 +0000843 /// The ProgramBuilder
844 ProgramBuilder* const builder;
845
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000846 private:
847 /// CToAST<T> is specialized for various `T` types and each specialization
848 /// contains a single static `get()` method for obtaining the corresponding
849 /// AST type for the C type `T`.
850 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000851 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000852 template <typename T>
853 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000854 };
855
856 //////////////////////////////////////////////////////////////////////////////
857 // AST helper methods
858 //////////////////////////////////////////////////////////////////////////////
859
James Price65ae64d2021-04-29 12:59:14 +0000860 /// @return a new unnamed symbol
861 Symbol Sym() { return Symbols().New(); }
862
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000863 /// @param name the symbol string
864 /// @return a Symbol with the given name
865 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
866
867 /// @param sym the symbol
868 /// @return `sym`
869 Symbol Sym(Symbol sym) { return sym; }
870
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000871 /// @param expr the expression
872 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000873 template <typename T>
874 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
875 return expr;
876 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000877
Ben Claytonb8ea5912021-04-19 16:52:42 +0000878 /// Passthrough for nullptr
879 /// @return nullptr
880 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
881
Ben Claytonfe0910f2021-05-17 15:51:47 +0000882 /// @param source the source information
883 /// @param symbol the identifier symbol
884 /// @return an ast::IdentifierExpression with the given symbol
885 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
886 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000887 }
888
Ben Clayton46d78d72021-02-10 21:34:26 +0000889 /// @param symbol the identifier symbol
890 /// @return an ast::IdentifierExpression with the given symbol
891 ast::IdentifierExpression* Expr(Symbol symbol) {
892 return create<ast::IdentifierExpression>(symbol);
893 }
894
Ben Claytonfe0910f2021-05-17 15:51:47 +0000895 /// @param source the source information
896 /// @param variable the AST variable
897 /// @return an ast::IdentifierExpression with the variable's symbol
898 ast::IdentifierExpression* Expr(const Source& source,
899 ast::Variable* variable) {
900 return create<ast::IdentifierExpression>(source, variable->symbol());
901 }
902
Ben Claytonb8ea5912021-04-19 16:52:42 +0000903 /// @param variable the AST variable
904 /// @return an ast::IdentifierExpression with the variable's symbol
905 ast::IdentifierExpression* Expr(ast::Variable* variable) {
906 return create<ast::IdentifierExpression>(variable->symbol());
907 }
908
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000909 /// @param source the source information
910 /// @param name the identifier name
911 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000912 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
913 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
914 }
915
916 /// @param name the identifier name
917 /// @return an ast::IdentifierExpression with the given name
918 ast::IdentifierExpression* Expr(const char* name) {
919 return create<ast::IdentifierExpression>(Symbols().Register(name));
920 }
921
922 /// @param source the source information
923 /// @param name the identifier name
924 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000925 ast::IdentifierExpression* Expr(const Source& source,
926 const std::string& name) {
927 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
928 }
929
930 /// @param name the identifier name
931 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000932 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000933 return create<ast::IdentifierExpression>(Symbols().Register(name));
934 }
935
Ben Claytonfe0910f2021-05-17 15:51:47 +0000936 /// @param source the source information
937 /// @param value the boolean value
938 /// @return a Scalar constructor for the given value
939 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
940 return create<ast::ScalarConstructorExpression>(source, Literal(value));
941 }
942
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000943 /// @param value the boolean value
944 /// @return a Scalar constructor for the given value
945 ast::ScalarConstructorExpression* Expr(bool value) {
946 return create<ast::ScalarConstructorExpression>(Literal(value));
947 }
948
Ben Claytonfe0910f2021-05-17 15:51:47 +0000949 /// @param source the source information
950 /// @param value the float value
951 /// @return a Scalar constructor for the given value
952 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
953 return create<ast::ScalarConstructorExpression>(source, Literal(value));
954 }
955
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000956 /// @param value the float value
957 /// @return a Scalar constructor for the given value
958 ast::ScalarConstructorExpression* Expr(f32 value) {
959 return create<ast::ScalarConstructorExpression>(Literal(value));
960 }
961
Ben Claytonfe0910f2021-05-17 15:51:47 +0000962 /// @param source the source information
963 /// @param value the integer value
964 /// @return a Scalar constructor for the given value
965 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
966 return create<ast::ScalarConstructorExpression>(source, Literal(value));
967 }
968
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000969 /// @param value the integer value
970 /// @return a Scalar constructor for the given value
971 ast::ScalarConstructorExpression* Expr(i32 value) {
972 return create<ast::ScalarConstructorExpression>(Literal(value));
973 }
974
Ben Claytonfe0910f2021-05-17 15:51:47 +0000975 /// @param source the source information
976 /// @param value the unsigned int value
977 /// @return a Scalar constructor for the given value
978 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
979 return create<ast::ScalarConstructorExpression>(source, Literal(value));
980 }
981
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000982 /// @param value the unsigned int value
983 /// @return a Scalar constructor for the given value
984 ast::ScalarConstructorExpression* Expr(u32 value) {
985 return create<ast::ScalarConstructorExpression>(Literal(value));
986 }
987
988 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
989 /// `list`.
990 /// @param list the list to append too
991 /// @param arg the arg to create
992 template <typename ARG>
993 void Append(ast::ExpressionList& list, ARG&& arg) {
994 list.emplace_back(Expr(std::forward<ARG>(arg)));
995 }
996
997 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
998 /// then appends them to `list`.
999 /// @param list the list to append too
1000 /// @param arg0 the first argument
1001 /// @param args the rest of the arguments
1002 template <typename ARG0, typename... ARGS>
1003 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1004 Append(list, std::forward<ARG0>(arg0));
1005 Append(list, std::forward<ARGS>(args)...);
1006 }
1007
1008 /// @return an empty list of expressions
1009 ast::ExpressionList ExprList() { return {}; }
1010
1011 /// @param args the list of expressions
1012 /// @return the list of expressions converted to `ast::Expression`s using
1013 /// `Expr()`,
1014 template <typename... ARGS>
1015 ast::ExpressionList ExprList(ARGS&&... args) {
1016 ast::ExpressionList list;
1017 list.reserve(sizeof...(args));
1018 Append(list, std::forward<ARGS>(args)...);
1019 return list;
1020 }
1021
1022 /// @param list the list of expressions
1023 /// @return `list`
1024 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1025
1026 /// @param val the boolan value
1027 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001028 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001029
1030 /// @param val the float value
1031 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001032 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001033
1034 /// @param val the unsigned int value
1035 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001036 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001037
1038 /// @param val the integer value
1039 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001040 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001041
1042 /// @param args the arguments for the type constructor
1043 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1044 /// of `args` converted to `ast::Expression`s using `Expr()`
1045 template <typename T, typename... ARGS>
1046 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001047 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001048 }
1049
1050 /// @param type the type to construct
1051 /// @param args the arguments for the constructor
1052 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1053 /// values `args`.
1054 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001055 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001056 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001057 return create<ast::TypeConstructorExpression>(
1058 type, ExprList(std::forward<ARGS>(args)...));
1059 }
1060
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001061 /// Creates a constructor expression that constructs an object of
1062 /// `type` filled with `elem_value`. For example,
1063 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1064 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1065 /// @param type the type to construct
1066 /// @param elem_value the initial or element value (for vec and mat) to
1067 /// construct with
1068 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001069 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001070 int elem_value = 0);
1071
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001072 /// @param args the arguments for the vector constructor
1073 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1074 /// `T`, constructed with the values `args`.
1075 template <typename T, typename... ARGS>
1076 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001077 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078 }
1079
1080 /// @param args the arguments for the vector constructor
1081 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1082 /// `T`, constructed with the values `args`.
1083 template <typename T, typename... ARGS>
1084 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001085 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001086 }
1087
1088 /// @param args the arguments for the vector constructor
1089 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1090 /// `T`, constructed with the values `args`.
1091 template <typename T, typename... ARGS>
1092 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001093 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001094 }
1095
1096 /// @param args the arguments for the matrix constructor
1097 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1098 /// `T`, constructed with the values `args`.
1099 template <typename T, typename... ARGS>
1100 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001101 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001102 }
1103
1104 /// @param args the arguments for the matrix constructor
1105 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1106 /// `T`, constructed with the values `args`.
1107 template <typename T, typename... ARGS>
1108 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001109 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001110 }
1111
1112 /// @param args the arguments for the matrix constructor
1113 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1114 /// `T`, constructed with the values `args`.
1115 template <typename T, typename... ARGS>
1116 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001117 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001118 }
1119
1120 /// @param args the arguments for the matrix constructor
1121 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1122 /// `T`, constructed with the values `args`.
1123 template <typename T, typename... ARGS>
1124 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001125 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001126 }
1127
1128 /// @param args the arguments for the matrix constructor
1129 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1130 /// `T`, constructed with the values `args`.
1131 template <typename T, typename... ARGS>
1132 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001133 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001134 }
1135
1136 /// @param args the arguments for the matrix constructor
1137 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1138 /// `T`, constructed with the values `args`.
1139 template <typename T, typename... ARGS>
1140 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001141 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001142 }
1143
1144 /// @param args the arguments for the matrix constructor
1145 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1146 /// `T`, constructed with the values `args`.
1147 template <typename T, typename... ARGS>
1148 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001149 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001150 }
1151
1152 /// @param args the arguments for the matrix constructor
1153 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1154 /// `T`, constructed with the values `args`.
1155 template <typename T, typename... ARGS>
1156 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001157 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001158 }
1159
1160 /// @param args the arguments for the matrix constructor
1161 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1162 /// `T`, constructed with the values `args`.
1163 template <typename T, typename... ARGS>
1164 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001165 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001166 }
1167
1168 /// @param args the arguments for the array constructor
1169 /// @return an `ast::TypeConstructorExpression` of an array with element type
1170 /// `T`, constructed with the values `args`.
1171 template <typename T, int N = 0, typename... ARGS>
1172 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001173 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001174 }
1175
1176 /// @param subtype the array element type
1177 /// @param n the array size. 0 represents a runtime-array.
1178 /// @param args the arguments for the array constructor
1179 /// @return an `ast::TypeConstructorExpression` of an array with element type
1180 /// `subtype`, constructed with the values `args`.
1181 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001182 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001183 uint32_t n,
1184 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001185 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001186 }
1187
1188 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001189 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001190 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001191 /// @param constructor constructor expression
1192 /// @param decorations variable decorations
1193 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001194 template <typename NAME>
1195 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001196 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001197 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001198 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001199 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001200 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001201 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1202 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001203 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001204
1205 /// @param source the variable source
1206 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001207 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001208 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001209 /// @param constructor constructor expression
1210 /// @param decorations variable decorations
1211 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001212 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001213 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001214 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001215 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001216 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001217 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001218 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001219 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001220 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001221 type, false, constructor, decorations);
1222 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001223
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001224 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001225 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001226 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001228 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001229 template <typename NAME>
1230 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001231 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001232 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001233 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001234 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001235 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001236 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001237 constructor, decorations);
1238 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001239
1240 /// @param source the variable source
1241 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001242 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001243 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001244 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001245 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001246 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001247 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001248 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001249 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001250 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001251 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001252 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001253 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001254 ast::StorageClass::kNone, type, true,
1255 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001256 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001257
James Pricedaf1f1c2021-04-08 22:15:48 +00001258 /// @param name the parameter name
1259 /// @param type the parameter type
1260 /// @param decorations optional parameter decorations
1261 /// @returns a constant `ast::Variable` with the given name and type
1262 template <typename NAME>
1263 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001264 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001265 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001266 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001267 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1268 ast::StorageClass::kNone, type, true, nullptr,
1269 decorations);
1270 }
1271
1272 /// @param source the parameter source
1273 /// @param name the parameter name
1274 /// @param type the parameter type
1275 /// @param decorations optional parameter decorations
1276 /// @returns a constant `ast::Variable` with the given name and type
1277 template <typename NAME>
1278 ast::Variable* Param(const Source& source,
1279 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001280 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001281 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001282 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001283 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1284 ast::StorageClass::kNone, type, true, nullptr,
1285 decorations);
1286 }
1287
Ben Clayton42708342021-04-21 17:55:12 +00001288 /// @param name the variable name
1289 /// @param type the variable type
1290 /// @param storage the variable storage class
1291 /// @param constructor constructor expression
1292 /// @param decorations variable decorations
1293 /// @returns a new `ast::Variable`, which is automatically registered as a
1294 /// global variable with the ast::Module.
1295 template <typename NAME>
1296 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001297 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001298 ast::StorageClass storage,
1299 ast::Expression* constructor = nullptr,
1300 ast::DecorationList decorations = {}) {
1301 auto* var =
1302 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1303 AST().AddGlobalVariable(var);
1304 return var;
1305 }
1306
1307 /// @param source the variable source
1308 /// @param name the variable name
1309 /// @param type the variable type
1310 /// @param storage the variable storage class
1311 /// @param constructor constructor expression
1312 /// @param decorations variable decorations
1313 /// @returns a new `ast::Variable`, which is automatically registered as a
1314 /// global variable with the ast::Module.
1315 template <typename NAME>
1316 ast::Variable* Global(const Source& source,
1317 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001318 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001319 ast::StorageClass storage,
1320 ast::Expression* constructor = nullptr,
1321 ast::DecorationList decorations = {}) {
1322 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1323 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001324 AST().AddGlobalVariable(var);
1325 return var;
1326 }
1327
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001328 /// @param name the variable name
1329 /// @param type the variable type
1330 /// @param constructor constructor expression
1331 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001332 /// @returns a const `ast::Variable` constructed by calling Var() with the
1333 /// arguments of `args`, which is automatically registered as a global
1334 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001335 template <typename NAME>
1336 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001337 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001338 ast::Expression* constructor,
1339 ast::DecorationList decorations = {}) {
1340 auto* var = Const(std::forward<NAME>(name), type, constructor,
1341 std::move(decorations));
1342 AST().AddGlobalVariable(var);
1343 return var;
1344 }
1345
1346 /// @param source the variable source
1347 /// @param name the variable name
1348 /// @param type the variable type
1349 /// @param constructor constructor expression
1350 /// @param decorations optional variable decorations
1351 /// @returns a const `ast::Variable` constructed by calling Var() with the
1352 /// arguments of `args`, which is automatically registered as a global
1353 /// variable with the ast::Module.
1354 template <typename NAME>
1355 ast::Variable* GlobalConst(const Source& source,
1356 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001357 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001358 ast::Expression* constructor,
1359 ast::DecorationList decorations = {}) {
1360 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1361 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001362 AST().AddGlobalVariable(var);
1363 return var;
1364 }
1365
Ben Claytonfe0910f2021-05-17 15:51:47 +00001366 /// @param source the source information
1367 /// @param expr the expression to take the address of
1368 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1369 template <typename EXPR>
1370 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1371 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1372 Expr(std::forward<EXPR>(expr)));
1373 }
1374
1375 /// @param expr the expression to take the address of
1376 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1377 template <typename EXPR>
1378 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1379 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1380 Expr(std::forward<EXPR>(expr)));
1381 }
1382
1383 /// @param source the source information
1384 /// @param expr the expression to perform an indirection on
1385 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1386 template <typename EXPR>
1387 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1388 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1389 Expr(std::forward<EXPR>(expr)));
1390 }
1391
1392 /// @param expr the expression to perform an indirection on
1393 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1394 template <typename EXPR>
1395 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1396 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1397 Expr(std::forward<EXPR>(expr)));
1398 }
1399
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001400 /// @param func the function name
1401 /// @param args the function call arguments
1402 /// @returns a `ast::CallExpression` to the function `func`, with the
1403 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1404 template <typename NAME, typename... ARGS>
1405 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1406 return create<ast::CallExpression>(Expr(func),
1407 ExprList(std::forward<ARGS>(args)...));
1408 }
1409
1410 /// @param lhs the left hand argument to the addition operation
1411 /// @param rhs the right hand argument to the addition operation
1412 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1413 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001414 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001415 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1416 Expr(std::forward<LHS>(lhs)),
1417 Expr(std::forward<RHS>(rhs)));
1418 }
1419
1420 /// @param lhs the left hand argument to the subtraction operation
1421 /// @param rhs the right hand argument to the subtraction operation
1422 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1423 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001424 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001425 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1426 Expr(std::forward<LHS>(lhs)),
1427 Expr(std::forward<RHS>(rhs)));
1428 }
1429
1430 /// @param lhs the left hand argument to the multiplication operation
1431 /// @param rhs the right hand argument to the multiplication operation
1432 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1433 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001434 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001435 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1436 Expr(std::forward<LHS>(lhs)),
1437 Expr(std::forward<RHS>(rhs)));
1438 }
1439
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001440 /// @param source the source information
1441 /// @param lhs the left hand argument to the multiplication operation
1442 /// @param rhs the right hand argument to the multiplication operation
1443 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1444 template <typename LHS, typename RHS>
1445 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1446 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1447 Expr(std::forward<LHS>(lhs)),
1448 Expr(std::forward<RHS>(rhs)));
1449 }
1450
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001451 /// @param lhs the left hand argument to the division operation
1452 /// @param rhs the right hand argument to the division operation
1453 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1454 template <typename LHS, typename RHS>
1455 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1456 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1457 Expr(std::forward<LHS>(lhs)),
1458 Expr(std::forward<RHS>(rhs)));
1459 }
1460
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001461 /// @param arr the array argument for the array accessor expression
1462 /// @param idx the index argument for the array accessor expression
1463 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1464 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001465 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001466 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1467 Expr(std::forward<IDX>(idx)));
1468 }
1469
1470 /// @param obj the object for the member accessor expression
1471 /// @param idx the index argument for the array accessor expression
1472 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1473 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001474 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001475 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1476 Expr(std::forward<IDX>(idx)));
1477 }
1478
Ben Clayton42d1e092021-02-02 14:29:15 +00001479 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001480 /// @param val the offset value
1481 /// @returns the offset decoration pointer
1482 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1483 return create<ast::StructMemberOffsetDecoration>(source_, val);
1484 }
1485
Ben Claytond614dd52021-03-15 10:43:11 +00001486 /// Creates a ast::StructMemberSizeDecoration
1487 /// @param source the source information
1488 /// @param val the size value
1489 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001490 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1491 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001492 return create<ast::StructMemberSizeDecoration>(source, val);
1493 }
1494
1495 /// Creates a ast::StructMemberSizeDecoration
1496 /// @param val the size value
1497 /// @returns the size decoration pointer
1498 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1499 return create<ast::StructMemberSizeDecoration>(source_, val);
1500 }
1501
1502 /// Creates a ast::StructMemberAlignDecoration
1503 /// @param source the source information
1504 /// @param val the align value
1505 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001506 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1507 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001508 return create<ast::StructMemberAlignDecoration>(source, val);
1509 }
1510
1511 /// Creates a ast::StructMemberAlignDecoration
1512 /// @param val the align value
1513 /// @returns the align decoration pointer
1514 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1515 return create<ast::StructMemberAlignDecoration>(source_, val);
1516 }
1517
Ben Clayton42d1e092021-02-02 14:29:15 +00001518 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001519 /// @param source the source information
1520 /// @param name the function name
1521 /// @param params the function parameters
1522 /// @param type the function return type
1523 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001524 /// @param decorations the optional function decorations
1525 /// @param return_type_decorations the optional function return type
1526 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001527 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001528 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001529 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001530 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001531 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001532 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001533 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001534 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001535 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001536 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001537 auto* func =
1538 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1539 type, create<ast::BlockStatement>(body),
1540 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001541 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001542 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001543 }
1544
Ben Clayton42d1e092021-02-02 14:29:15 +00001545 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001546 /// @param name the function name
1547 /// @param params the function parameters
1548 /// @param type the function return type
1549 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001550 /// @param decorations the optional function decorations
1551 /// @param return_type_decorations the optional function return type
1552 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001553 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001554 template <typename NAME>
1555 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001556 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001557 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001558 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001559 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001560 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001561 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001562 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1563 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001564 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001565 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001566 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001567 }
1568
Ben Clayton49668bb2021-04-17 05:32:01 +00001569 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001570 /// @param source the source information
1571 /// @returns the return statement pointer
1572 ast::ReturnStatement* Return(const Source& source) {
1573 return create<ast::ReturnStatement>(source);
1574 }
1575
1576 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001577 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001578 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1579
1580 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001581 /// @param source the source information
1582 /// @param val the return value
1583 /// @returns the return statement pointer
1584 template <typename EXPR>
1585 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1586 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1587 }
1588
1589 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001590 /// @param val the return value
1591 /// @returns the return statement pointer
1592 template <typename EXPR>
1593 ast::ReturnStatement* Return(EXPR&& val) {
1594 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001595 }
1596
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001597 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001598 /// @param source the source information
1599 /// @param name the struct name
1600 /// @param members the struct members
1601 /// @param decorations the optional struct decorations
1602 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001603 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001604 ast::Struct* Structure(const Source& source,
1605 NAME&& name,
1606 ast::StructMemberList members,
1607 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001608 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001609 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001610 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001611 AST().AddConstructedType(type);
1612 return type;
1613 }
1614
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001615 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001616 /// @param name the struct name
1617 /// @param members the struct members
1618 /// @param decorations the optional struct decorations
1619 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001620 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001621 ast::Struct* Structure(NAME&& name,
1622 ast::StructMemberList members,
1623 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001624 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001625 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001626 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001627 AST().AddConstructedType(type);
1628 return type;
1629 }
1630
Ben Clayton42d1e092021-02-02 14:29:15 +00001631 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001632 /// @param source the source information
1633 /// @param name the struct member name
1634 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001635 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001636 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001637 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001638 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001639 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001640 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001641 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001642 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001643 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1644 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001645 }
1646
Ben Clayton42d1e092021-02-02 14:29:15 +00001647 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001648 /// @param name the struct member name
1649 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001650 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001651 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001652 template <typename NAME>
1653 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001654 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001655 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001656 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001657 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1658 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001659 }
1660
Ben Claytonbab31972021-02-08 21:16:21 +00001661 /// Creates a ast::StructMember with the given byte offset
1662 /// @param offset the offset to use in the StructMemberOffsetDecoration
1663 /// @param name the struct member name
1664 /// @param type the struct member type
1665 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001666 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001667 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001668 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001669 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001670 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001671 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001672 create<ast::StructMemberOffsetDecoration>(offset),
1673 });
1674 }
1675
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001676 /// Creates a ast::BlockStatement with input statements
1677 /// @param statements statements of block
1678 /// @returns the block statement pointer
1679 template <typename... Statements>
1680 ast::BlockStatement* Block(Statements&&... statements) {
1681 return create<ast::BlockStatement>(
1682 ast::StatementList{std::forward<Statements>(statements)...});
1683 }
1684
1685 /// Creates a ast::ElseStatement with input condition and body
1686 /// @param condition the else condition expression
1687 /// @param body the else body
1688 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001689 template <typename CONDITION>
1690 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1691 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1692 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001693 }
1694
1695 /// Creates a ast::IfStatement with input condition, body, and optional
1696 /// variadic else statements
1697 /// @param condition the if statement condition expression
1698 /// @param body the if statement body
1699 /// @param elseStatements optional variadic else statements
1700 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001701 template <typename CONDITION, typename... ELSE_STATEMENTS>
1702 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001703 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001704 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001705 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001706 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001707 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001708 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001709 }
1710
1711 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001712 /// @param source the source information
1713 /// @param lhs the left hand side expression initializer
1714 /// @param rhs the right hand side expression initializer
1715 /// @returns the assignment statement pointer
1716 template <typename LhsExpressionInit, typename RhsExpressionInit>
1717 ast::AssignmentStatement* Assign(const Source& source,
1718 LhsExpressionInit&& lhs,
1719 RhsExpressionInit&& rhs) {
1720 return create<ast::AssignmentStatement>(
1721 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1722 Expr(std::forward<RhsExpressionInit>(rhs)));
1723 }
1724
1725 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001726 /// @param lhs the left hand side expression initializer
1727 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001728 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001729 template <typename LhsExpressionInit, typename RhsExpressionInit>
1730 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1731 RhsExpressionInit&& rhs) {
1732 return create<ast::AssignmentStatement>(
1733 Expr(std::forward<LhsExpressionInit>(lhs)),
1734 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001735 }
1736
1737 /// Creates a ast::LoopStatement with input body and optional continuing
1738 /// @param body the loop body
1739 /// @param continuing the optional continuing block
1740 /// @returns the loop statement pointer
1741 ast::LoopStatement* Loop(ast::BlockStatement* body,
1742 ast::BlockStatement* continuing = nullptr) {
1743 return create<ast::LoopStatement>(body, continuing);
1744 }
1745
1746 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001747 /// @param source the source information
1748 /// @param var the variable to wrap in a decl statement
1749 /// @returns the variable decl statement pointer
1750 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1751 return create<ast::VariableDeclStatement>(source, var);
1752 }
1753
1754 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001755 /// @param var the variable to wrap in a decl statement
1756 /// @returns the variable decl statement pointer
1757 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1758 return create<ast::VariableDeclStatement>(var);
1759 }
1760
Antonio Maioranocea744d2021-03-25 12:55:27 +00001761 /// Creates a ast::SwitchStatement with input expression and cases
1762 /// @param condition the condition expression initializer
1763 /// @param cases case statements
1764 /// @returns the switch statement pointer
1765 template <typename ExpressionInit, typename... Cases>
1766 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1767 return create<ast::SwitchStatement>(
1768 Expr(std::forward<ExpressionInit>(condition)),
1769 ast::CaseStatementList{std::forward<Cases>(cases)...});
1770 }
1771
1772 /// Creates a ast::CaseStatement with input list of selectors, and body
1773 /// @param selectors list of selectors
1774 /// @param body the case body
1775 /// @returns the case statement pointer
1776 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1777 ast::BlockStatement* body = nullptr) {
1778 return create<ast::CaseStatement>(std::move(selectors),
1779 body ? body : Block());
1780 }
1781
1782 /// Convenient overload that takes a single selector
1783 /// @param selector a single case selector
1784 /// @param body the case body
1785 /// @returns the case statement pointer
1786 ast::CaseStatement* Case(ast::IntLiteral* selector,
1787 ast::BlockStatement* body = nullptr) {
1788 return Case(ast::CaseSelectorList{selector}, body);
1789 }
1790
1791 /// Convenience function that creates a 'default' ast::CaseStatement
1792 /// @param body the case body
1793 /// @returns the case statement pointer
1794 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1795 return Case(ast::CaseSelectorList{}, body);
1796 }
1797
James Price68f558f2021-04-06 15:51:47 +00001798 /// Creates an ast::BuiltinDecoration
1799 /// @param source the source information
1800 /// @param builtin the builtin value
1801 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001802 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001803 return create<ast::BuiltinDecoration>(source, builtin);
1804 }
1805
1806 /// Creates an ast::BuiltinDecoration
1807 /// @param builtin the builtin value
1808 /// @returns the builtin decoration pointer
1809 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1810 return create<ast::BuiltinDecoration>(source_, builtin);
1811 }
1812
1813 /// Creates an ast::LocationDecoration
1814 /// @param source the source information
1815 /// @param location the location value
1816 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001817 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001818 return create<ast::LocationDecoration>(source, location);
1819 }
1820
1821 /// Creates an ast::LocationDecoration
1822 /// @param location the location value
1823 /// @returns the location decoration pointer
1824 ast::LocationDecoration* Location(uint32_t location) {
1825 return create<ast::LocationDecoration>(source_, location);
1826 }
1827
James Pricef2f3bfc2021-05-13 20:32:32 +00001828 /// Creates an ast::OverrideDecoration with a specific constant ID
1829 /// @param source the source information
1830 /// @param id the id value
1831 /// @returns the override decoration pointer
1832 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1833 return create<ast::OverrideDecoration>(source, id);
1834 }
1835
1836 /// Creates an ast::OverrideDecoration with a specific constant ID
1837 /// @param id the optional id value
1838 /// @returns the override decoration pointer
1839 ast::OverrideDecoration* Override(uint32_t id) {
1840 return Override(source_, id);
1841 }
1842
1843 /// Creates an ast::OverrideDecoration without a constant ID
1844 /// @param source the source information
1845 /// @returns the override decoration pointer
1846 ast::OverrideDecoration* Override(const Source& source) {
1847 return create<ast::OverrideDecoration>(source);
1848 }
1849
1850 /// Creates an ast::OverrideDecoration without a constant ID
1851 /// @returns the override decoration pointer
1852 ast::OverrideDecoration* Override() { return Override(source_); }
1853
James Price68f558f2021-04-06 15:51:47 +00001854 /// Creates an ast::StageDecoration
1855 /// @param source the source information
1856 /// @param stage the pipeline stage
1857 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001858 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001859 return create<ast::StageDecoration>(source, stage);
1860 }
1861
1862 /// Creates an ast::StageDecoration
1863 /// @param stage the pipeline stage
1864 /// @returns the stage decoration pointer
1865 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1866 return create<ast::StageDecoration>(source_, stage);
1867 }
1868
James Price70f80bb2021-05-19 13:40:08 +00001869 /// Creates an ast::WorkgroupDecoration
1870 /// @param x the x dimension expression
1871 /// @returns the workgroup decoration pointer
1872 template <typename EXPR_X>
1873 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1874 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1875 }
1876
1877 /// Creates an ast::WorkgroupDecoration
1878 /// @param x the x dimension expression
1879 /// @param y the y dimension expression
1880 /// @returns the workgroup decoration pointer
1881 template <typename EXPR_X, typename EXPR_Y>
1882 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1883 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1884 nullptr);
1885 }
1886
1887 /// Creates an ast::WorkgroupDecoration
1888 /// @param x the x dimension expression
1889 /// @param y the y dimension expression
1890 /// @param z the z dimension expression
1891 /// @returns the workgroup decoration pointer
1892 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
1893 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
1894 return create<ast::WorkgroupDecoration>(
1895 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
1896 Expr(std::forward<EXPR_Z>(z)));
1897 }
1898
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001899 /// Sets the current builder source to `src`
1900 /// @param src the Source used for future create() calls
1901 void SetSource(const Source& src) {
1902 AssertNotMoved();
1903 source_ = src;
1904 }
1905
1906 /// Sets the current builder source to `loc`
1907 /// @param loc the Source used for future create() calls
1908 void SetSource(const Source::Location& loc) {
1909 AssertNotMoved();
1910 source_ = Source(loc);
1911 }
1912
Ben Clayton33352542021-01-29 16:43:41 +00001913 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001914 /// @note As the Resolver is run when the Program is built, this will only be
1915 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001916 /// @param expr the AST expression
1917 /// @return the resolved semantic type for the expression, or nullptr if the
1918 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001919 sem::Type* TypeOf(const ast::Expression* expr) const;
1920
Ben Claytonfe0910f2021-05-17 15:51:47 +00001921 /// Helper for returning the resolved semantic type of the variable `var`.
1922 /// @note As the Resolver is run when the Program is built, this will only be
1923 /// useful for the Resolver itself and tests that use their own Resolver.
1924 /// @param var the AST variable
1925 /// @return the resolved semantic type for the variable, or nullptr if the
1926 /// variable has no resolved type.
1927 sem::Type* TypeOf(const ast::Variable* var) const;
1928
Ben Claytonfbec46f2021-04-30 20:20:19 +00001929 /// Helper for returning the resolved semantic type of the AST type `type`.
1930 /// @note As the Resolver is run when the Program is built, this will only be
1931 /// useful for the Resolver itself and tests that use their own Resolver.
1932 /// @param expr the AST type
1933 /// @return the resolved semantic type for the type, or nullptr if the type
1934 /// has no resolved type.
1935 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001936
Ben Clayton169512e2021-04-17 05:52:11 +00001937 /// Wraps the ast::Literal in a statement. This is used by tests that
1938 /// construct a partial AST and require the Resolver to reach these
1939 /// nodes.
1940 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1941 /// @return the ast::Statement that wraps the ast::Statement
1942 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001943 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001944 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001945 /// nodes.
1946 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1947 /// @return the ast::Statement that wraps the ast::Expression
1948 ast::Statement* WrapInStatement(ast::Expression* expr);
1949 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001950 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001951 /// these nodes.
1952 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1953 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1954 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1955 /// Returns the statement argument. Used as a passthrough-overload by
1956 /// WrapInFunction().
1957 /// @param stmt the ast::Statement
1958 /// @return `stmt`
1959 ast::Statement* WrapInStatement(ast::Statement* stmt);
1960 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001961 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001962 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001963 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001964 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001965 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001966 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001967 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001968 }
1969 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001970 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001971 /// @returns the function
1972 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001973
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001974 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001975 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001976
1977 protected:
1978 /// Asserts that the builder has not been moved.
1979 void AssertNotMoved() const;
1980
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001981 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001982 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001983 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001984 ASTNodeAllocator ast_nodes_;
1985 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001986 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001987 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001988 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001989 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001990
1991 /// The source to use when creating AST nodes without providing a Source as
1992 /// the first argument.
1993 Source source_;
1994
Ben Clayton5f0ea112021-03-09 10:54:37 +00001995 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001996 /// program when built.
1997 bool resolve_on_build_ = true;
1998
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001999 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2000 bool moved_ = false;
2001};
2002
2003//! @cond Doxygen_Suppress
2004// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2005template <>
2006struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002007 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002008 return t->i32();
2009 }
2010};
2011template <>
2012struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002013 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002014 return t->u32();
2015 }
2016};
2017template <>
2018struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002019 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002020 return t->f32();
2021 }
2022};
2023template <>
2024struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002025 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002026 return t->bool_();
2027 }
2028};
2029template <>
2030struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002031 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002032 return t->void_();
2033 }
2034};
2035//! @endcond
2036
Ben Clayton917b14b2021-04-19 16:50:23 +00002037/// @param builder the ProgramBuilder
2038/// @returns the ProgramID of the ProgramBuilder
2039inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2040 return builder->ID();
2041}
2042
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002043} // namespace tint
2044
2045#endif // SRC_PROGRAM_BUILDER_H_