blob: facd53976f602980fcbf1cc8e7576d7642c8cdef [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>
Ben Claytonb5cd10c2021-06-25 10:26:26 +000019#include <unordered_set>
Ben Claytona6b9a8e2021-01-26 16:57:10 +000020#include <utility>
21
Ben Claytone204f272021-04-22 14:40:23 +000022#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000023#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000024#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000025#include "src/ast/assignment_statement.h"
Ben Clayton8c7f0da2021-06-17 15:48:39 +000026#include "src/ast/atomic.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000028#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000029#include "src/ast/bool_literal.h"
30#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000031#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000032#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000033#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000034#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000035#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000036#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000037#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000038#include "src/ast/if_statement.h"
39#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000040#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000041#include "src/ast/member_accessor_expression.h"
42#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000043#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000044#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000045#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000046#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000047#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000048#include "src/ast/scalar_constructor_expression.h"
49#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000050#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000051#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000052#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000053#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000055#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000056#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000058#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000059#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000060#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000061#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000062#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000063#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000064#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000065#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000066#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000067#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000068#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000069#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000070#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000071#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000072#include "src/sem/f32_type.h"
73#include "src/sem/i32_type.h"
74#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000075#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000076#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000077#include "src/sem/sampled_texture_type.h"
78#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000079#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000080#include "src/sem/u32_type.h"
81#include "src/sem/vector_type.h"
82#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000083
Ben Clayton03d10722021-06-05 12:45:50 +000084#ifdef INCLUDE_TINT_TINT_H_
85#error "internal tint header being #included from tint.h"
86#endif
87
Ben Clayton401b96b2021-02-03 17:19:59 +000088// Forward declarations
Ben Claytonb5cd10c2021-06-25 10:26:26 +000089namespace tint {
Ben Clayton401b96b2021-02-03 17:19:59 +000090namespace ast {
91class VariableDeclStatement;
92} // namespace ast
Ben Claytonb5cd10c2021-06-25 10:26:26 +000093} // namespace tint
Ben Clayton401b96b2021-02-03 17:19:59 +000094
Ben Claytonb5cd10c2021-06-25 10:26:26 +000095namespace tint {
Ben Claytona6b9a8e2021-01-26 16:57:10 +000096class CloneContext;
97
98/// ProgramBuilder is a mutable builder for a Program.
99/// To construct a Program, populate the builder and then `std::move` it to a
100/// Program.
101class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +0000102 /// VarOptionals is a helper for accepting a number of optional, extra
103 /// arguments for Var() and Global().
104 struct VarOptionals {
105 template <typename... ARGS>
106 explicit VarOptionals(ARGS&&... args) {
107 Apply(std::forward<ARGS>(args)...);
108 }
109 ~VarOptionals();
110
111 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000112 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000113 ast::Expression* constructor = nullptr;
114 ast::DecorationList decorations = {};
115
116 private:
117 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000118 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000119 void Set(ast::Expression* c) { constructor = c; }
120 void Set(const ast::DecorationList& l) { decorations = l; }
121
122 template <typename FIRST, typename... ARGS>
123 void Apply(FIRST&& first, ARGS&&... args) {
124 Set(std::forward<FIRST>(first));
125 Apply(std::forward<ARGS>(args)...);
126 }
127 void Apply() {}
128 };
129
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000130 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000131 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
132 using ASTNodeAllocator = BlockAllocator<ast::Node>;
133
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000134 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
135 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000136
137 /// `i32` is a type alias to `int`.
138 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
139 /// WGSL syntax.
140 /// Note: this is intentionally not aliased to uint32_t as we want integer
141 /// literals passed to the builder to match WGSL's integer literal types.
142 using i32 = decltype(1);
143 /// `u32` is a type alias to `unsigned int`.
144 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
145 /// WGSL syntax.
146 /// Note: this is intentionally not aliased to uint32_t as we want integer
147 /// literals passed to the builder to match WGSL's integer literal types.
148 using u32 = decltype(1u);
149 /// `f32` is a type alias to `float`
150 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
151 /// WGSL syntax.
152 using f32 = float;
153
154 /// Constructor
155 ProgramBuilder();
156
157 /// Move constructor
158 /// @param rhs the builder to move
159 ProgramBuilder(ProgramBuilder&& rhs);
160
161 /// Destructor
162 virtual ~ProgramBuilder();
163
164 /// Move assignment operator
165 /// @param rhs the builder to move
166 /// @return this builder
167 ProgramBuilder& operator=(ProgramBuilder&& rhs);
168
Ben Claytone43c8302021-01-29 11:59:32 +0000169 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
170 /// making a deep clone of the Program contents.
171 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
172 /// existing immutable program.
173 /// As the returned ProgramBuilder wraps `program`, `program` must not be
174 /// destructed or assigned while using the returned ProgramBuilder.
175 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
176 /// function. See crbug.com/tint/460.
177 /// @param program the immutable Program to wrap
178 /// @return the ProgramBuilder that wraps `program`
179 static ProgramBuilder Wrap(const Program* program);
180
Ben Claytone6995de2021-04-13 23:27:27 +0000181 /// @returns the unique identifier for this program
182 ProgramID ID() const { return id_; }
183
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000184 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000185 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000186 AssertNotMoved();
187 return types_;
188 }
189
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000190 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000191 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000192 AssertNotMoved();
193 return types_;
194 }
195
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000196 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000197 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000198 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000199 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000200 }
201
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000202 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000203 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000204 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000205 return ast_nodes_;
206 }
207
208 /// @returns a reference to the program's semantic nodes storage
209 SemNodeAllocator& SemNodes() {
210 AssertNotMoved();
211 return sem_nodes_;
212 }
213
214 /// @returns a reference to the program's semantic nodes storage
215 const SemNodeAllocator& SemNodes() const {
216 AssertNotMoved();
217 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000218 }
219
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000220 /// @returns a reference to the program's AST root Module
221 ast::Module& AST() {
222 AssertNotMoved();
223 return *ast_;
224 }
225
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000226 /// @returns a reference to the program's AST root Module
227 const ast::Module& AST() const {
228 AssertNotMoved();
229 return *ast_;
230 }
231
232 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000233 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000234 AssertNotMoved();
235 return sem_;
236 }
237
238 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000239 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000240 AssertNotMoved();
241 return sem_;
242 }
243
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000244 /// @returns a reference to the program's SymbolTable
245 SymbolTable& Symbols() {
246 AssertNotMoved();
247 return symbols_;
248 }
249
Ben Clayton708dc2d2021-01-29 11:22:40 +0000250 /// @returns a reference to the program's SymbolTable
251 const SymbolTable& Symbols() const {
252 AssertNotMoved();
253 return symbols_;
254 }
255
Ben Clayton844217f2021-01-27 18:49:05 +0000256 /// @returns a reference to the program's diagnostics
257 diag::List& Diagnostics() {
258 AssertNotMoved();
259 return diagnostics_;
260 }
261
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000262 /// @returns a reference to the program's diagnostics
263 const diag::List& Diagnostics() const {
264 AssertNotMoved();
265 return diagnostics_;
266 }
267
Ben Clayton5f0ea112021-03-09 10:54:37 +0000268 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000269 /// @param enable the new flag value (defaults to true)
270 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
271
Ben Clayton5f0ea112021-03-09 10:54:37 +0000272 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000273 /// built.
274 bool ResolveOnBuild() const { return resolve_on_build_; }
275
Ben Clayton844217f2021-01-27 18:49:05 +0000276 /// @returns true if the program has no error diagnostics and is not missing
277 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000278 bool IsValid() const;
279
Ben Clayton708dc2d2021-01-29 11:22:40 +0000280 /// Writes a representation of the node to the output stream
281 /// @note unlike str(), to_str() does not automatically demangle the string.
282 /// @param node the AST node
283 /// @param out the stream to write to
284 /// @param indent number of spaces to indent the node when writing
285 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
286 node->to_str(Sem(), out, indent);
287 }
288
289 /// Returns a demangled, string representation of `node`.
290 /// @param node the AST node
291 /// @returns a string representation of the node
292 std::string str(const ast::Node* node) const;
293
Ben Clayton7fdfff12021-01-29 15:17:30 +0000294 /// Creates a new ast::Node owned by the ProgramBuilder. When the
295 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000296 /// @param source the Source of the node
297 /// @param args the arguments to pass to the type constructor
298 /// @returns the node pointer
299 template <typename T, typename... ARGS>
300 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
301 ARGS&&... args) {
302 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000303 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000304 }
305
Ben Clayton7fdfff12021-01-29 15:17:30 +0000306 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
307 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000308 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000309 /// When the ProgramBuilder is destructed, the ast::Node will also be
310 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000311 /// @returns the node pointer
312 template <typename T>
313 traits::EnableIfIsType<T, ast::Node>* create() {
314 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000315 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000316 }
317
Ben Clayton7fdfff12021-01-29 15:17:30 +0000318 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
319 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000320 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000321 /// When the ProgramBuilder is destructed, the ast::Node will also be
322 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000323 /// @param arg0 the first arguments to pass to the type constructor
324 /// @param args the remaining arguments to pass to the type constructor
325 /// @returns the node pointer
326 template <typename T, typename ARG0, typename... ARGS>
327 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
328 traits::IsTypeOrDerived<T, ast::Node>::value &&
329 !traits::IsTypeOrDerived<ARG0, Source>::value,
330 T>*
331 create(ARG0&& arg0, ARGS&&... args) {
332 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000333 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000334 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000335 }
336
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000337 /// Creates a new sem::Node owned by the ProgramBuilder.
338 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000339 /// destructed.
340 /// @param args the arguments to pass to the type constructor
341 /// @returns the node pointer
342 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000343 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
344 !traits::IsTypeOrDerived<T, sem::Type>::value,
345 T>*
346 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000347 AssertNotMoved();
348 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
349 }
350
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000351 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000352 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
353 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000354 /// Types are unique (de-aliased), and so calling create() for the same `T`
355 /// and arguments will return the same pointer.
356 /// @warning Use this method to acquire a type only if all of its type
357 /// information is provided in the constructor arguments `args`.<br>
358 /// If the type requires additional configuration after construction that
359 /// affect its fundamental type, build the type with `std::make_unique`, make
360 /// any necessary alterations and then call unique_type() instead.
361 /// @param args the arguments to pass to the type constructor
362 /// @returns the de-aliased type pointer
363 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000364 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
365 static_assert(std::is_base_of<sem::Type, T>::value,
366 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000367 AssertNotMoved();
368 return types_.Get<T>(std::forward<ARGS>(args)...);
369 }
370
371 /// Marks this builder as moved, preventing any further use of the builder.
372 void MarkAsMoved();
373
374 //////////////////////////////////////////////////////////////////////////////
375 // TypesBuilder
376 //////////////////////////////////////////////////////////////////////////////
377
378 /// TypesBuilder holds basic `tint` types and methods for constructing
379 /// complex types.
380 class TypesBuilder {
381 public:
382 /// Constructor
383 /// @param builder the program builder
384 explicit TypesBuilder(ProgramBuilder* builder);
385
386 /// @return the tint AST type for the C type `T`.
387 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000388 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000389 return CToAST<T>::get(this);
390 }
391
392 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000393 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
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 boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000397 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000398 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000399 }
400
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000401 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000402 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000403
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000404 /// @param source the Source of the node
405 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000406 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000407 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000408 }
409
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000410 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000411 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000412
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000413 /// @param source the Source of the node
414 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000415 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000416 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000417 }
418
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000419 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000420 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000421
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000422 /// @param source the Source of the node
423 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000424 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000425 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000426 }
427
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000428 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000429 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000430
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000431 /// @param source the Source of the node
432 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000433 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000434 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000435 }
436
Antonio Maiorano25436862021-04-13 13:32:33 +0000437 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000438 /// @param n vector width in elements
439 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000440 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000441 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000442 }
443
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000444 /// @param source the Source of the node
445 /// @param type vector subtype
446 /// @param n vector width in elements
447 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000448 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000449 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000450 }
451
Antonio Maiorano25436862021-04-13 13:32:33 +0000452 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000453 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000454 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000455
456 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000457 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000458 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000459
460 /// @param type vector subtype
461 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000462 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000463
464 /// @param n vector width in elements
465 /// @return the tint AST type for a `n`-element vector of `type`.
466 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000467 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000468 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000469 }
470
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000471 /// @return the tint AST type for a 2-element vector of the C type `T`.
472 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000473 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000474 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000475 }
476
477 /// @return the tint AST type for a 3-element vector of the C type `T`.
478 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000479 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000480 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000481 }
482
483 /// @return the tint AST type for a 4-element vector of the C type `T`.
484 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000485 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000486 return vec4(Of<T>());
487 }
488
489 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000490 /// @param columns number of columns for the matrix
491 /// @param rows number of rows for the matrix
492 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000493 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000494 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000495 }
496
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000497 /// @param source the Source of the node
498 /// @param type matrix subtype
499 /// @param columns number of columns for the matrix
500 /// @param rows number of rows for the matrix
501 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000502 ast::Matrix* mat(const Source& source,
503 ast::Type* type,
504 uint32_t columns,
505 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000506 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000507 }
508
Ben Claytone204f272021-04-22 14:40:23 +0000509 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000510 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000511 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000512
513 /// @param type matrix subtype
514 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000515 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000516
517 /// @param type matrix subtype
518 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000519 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000520
521 /// @param type matrix subtype
522 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000523 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000524
525 /// @param type matrix subtype
526 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000527 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000528
529 /// @param type matrix subtype
530 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000531 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000532
533 /// @param type matrix subtype
534 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000535 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000536
537 /// @param type matrix subtype
538 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000539 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000540
541 /// @param type matrix subtype
542 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000543 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000544
Ben Claytone204f272021-04-22 14:40:23 +0000545 /// @param columns number of columns for the matrix
546 /// @param rows number of rows for the matrix
547 /// @return the tint AST type for a matrix of `type`
548 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000549 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000550 return mat(Of<T>(), columns, rows);
551 }
552
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000553 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
554 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000555 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000556 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000557 }
558
559 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
560 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000561 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000562 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000563 }
564
565 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
566 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000567 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000568 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000569 }
570
571 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
572 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000573 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000574 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000575 }
576
577 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
578 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000579 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000580 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000581 }
582
583 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
584 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000585 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000586 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000587 }
588
589 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
590 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000591 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000592 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000593 }
594
595 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
596 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000597 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000598 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000599 }
600
601 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
602 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000603 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000604 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000605 }
606
607 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000608 /// @param n the array size. 0 represents a runtime-array
609 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000610 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000611 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000612 uint32_t n = 0,
613 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000614 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000615 }
616
617 /// @param source the Source of the node
618 /// @param subtype the array element type
619 /// @param n the array size. 0 represents a runtime-array
620 /// @param decos the optional decorations for the array
621 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000622 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000623 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000624 uint32_t n = 0,
625 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000626 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000627 }
628
Ben Claytonbab31972021-02-08 21:16:21 +0000629 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000630 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000631 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000632 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000633 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000634 ast::DecorationList decos;
635 if (stride) {
636 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
637 }
638 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000639 }
640
Ben Clayton0f88b312021-05-04 17:36:31 +0000641 /// @param source the Source of the node
642 /// @param subtype the array element type
643 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000644 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000645 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000646 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000647 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000648 uint32_t n,
649 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000650 ast::DecorationList decos;
651 if (stride) {
652 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
653 }
654 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000655 }
656
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000657 /// @return the tint AST type for an array of size `N` of type `T`
658 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000659 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000660 return array(Of<T>(), N);
661 }
662
Ben Claytonbab31972021-02-08 21:16:21 +0000663 /// @param stride the array stride
664 /// @return the tint AST type for an array of size `N` of type `T`
665 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000666 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000667 return array(Of<T>(), N, stride);
668 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000669
Ben Clayton0f88b312021-05-04 17:36:31 +0000670 /// Creates a type name
671 /// @param name the name
672 /// @returns the type name
673 template <typename NAME>
674 ast::TypeName* type_name(NAME&& name) const {
675 return builder->create<ast::TypeName>(
676 builder->Sym(std::forward<NAME>(name)));
677 }
678
679 /// Creates a type name
680 /// @param source the Source of the node
681 /// @param name the name
682 /// @returns the type name
683 template <typename NAME>
684 ast::TypeName* type_name(const Source& source, NAME&& name) const {
685 return builder->create<ast::TypeName>(
686 source, builder->Sym(std::forward<NAME>(name)));
687 }
688
Ben Clayton42d1e092021-02-02 14:29:15 +0000689 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000690 /// @param name the alias name
691 /// @param type the alias type
692 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000693 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000694 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000695 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000696 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000697 }
698
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000699 /// Creates an alias type
700 /// @param source the Source of the node
701 /// @param name the alias name
702 /// @param type the alias type
703 /// @returns the alias pointer
704 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000705 ast::Alias* alias(const Source& source,
706 NAME&& name,
707 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000708 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000709 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000710 }
711
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000712 /// @param type the type of the pointer
713 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000714 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000715 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000716 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000717 ast::StorageClass storage_class,
718 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000719 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000720 }
721
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000722 /// @param source the Source of the node
723 /// @param type the type of the pointer
724 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000725 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000726 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000727 ast::Pointer* pointer(const Source& source,
728 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000729 ast::StorageClass storage_class,
730 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000731 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000732 }
733
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000734 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000735 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000736 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000737 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000738 ast::Pointer* pointer(ast::StorageClass storage_class,
739 ast::Access access = ast::Access::kUndefined) const {
740 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000741 }
742
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000743 /// @param source the Source of the node
744 /// @param type the type of the atomic
745 /// @return the atomic to `type`
746 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
747 return builder->create<ast::Atomic>(source, type);
748 }
749
Ben Clayton313e6182021-06-17 19:56:14 +0000750 /// @param type the type of the atomic
751 /// @return the atomic to `type`
752 ast::Atomic* atomic(ast::Type* type) const {
753 return builder->create<ast::Atomic>(type);
754 }
755
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000756 /// @return the atomic to type `T`
757 template <typename T>
758 ast::Atomic* atomic() const {
759 return atomic(Of<T>());
760 }
761
Ben Claytone204f272021-04-22 14:40:23 +0000762 /// @param kind the kind of sampler
763 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000764 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000765 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000766 }
767
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000768 /// @param source the Source of the node
769 /// @param kind the kind of sampler
770 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000771 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000772 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000773 }
774
Ben Claytone204f272021-04-22 14:40:23 +0000775 /// @param dims the dimensionality of the texture
776 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000777 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000778 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000779 }
780
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000781 /// @param source the Source of the node
782 /// @param dims the dimensionality of the texture
783 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000784 ast::DepthTexture* depth_texture(const Source& source,
785 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000786 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000787 }
788
Ben Claytone204f272021-04-22 14:40:23 +0000789 /// @param dims the dimensionality of the texture
790 /// @param subtype the texture subtype.
791 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000792 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
793 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000794 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000795 }
796
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000797 /// @param source the Source of the node
798 /// @param dims the dimensionality of the texture
799 /// @param subtype the texture subtype.
800 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000801 ast::SampledTexture* sampled_texture(const Source& source,
802 ast::TextureDimension dims,
803 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000804 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000805 }
806
Ben Claytone204f272021-04-22 14:40:23 +0000807 /// @param dims the dimensionality of the texture
808 /// @param subtype the texture subtype.
809 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000810 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
811 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000812 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000813 }
814
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000815 /// @param source the Source of the node
816 /// @param dims the dimensionality of the texture
817 /// @param subtype the texture subtype.
818 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000819 ast::MultisampledTexture* multisampled_texture(const Source& source,
820 ast::TextureDimension dims,
821 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000822 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000823 }
824
Ben Claytone204f272021-04-22 14:40:23 +0000825 /// @param dims the dimensionality of the texture
826 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000827 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000828 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000829 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000830 ast::ImageFormat format,
831 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000832 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000833 return builder->create<ast::StorageTexture>(dims, format, subtype,
834 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000835 }
836
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000837 /// @param source the Source of the node
838 /// @param dims the dimensionality of the texture
839 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000840 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000841 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000842 ast::StorageTexture* storage_texture(const Source& source,
843 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000844 ast::ImageFormat format,
845 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000846 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000847 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
848 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000849 }
850
Brandon Jones7b257692021-05-17 17:40:17 +0000851 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000852 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000853 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000854 }
855
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000856 /// @param source the Source of the node
857 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000858 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000859 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000860 }
861
Ben Clayton19b03192021-05-20 15:04:08 +0000862 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000863 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000864 /// @param type the type
865 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000866 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000867
Ben Clayton8758f102021-06-09 14:32:14 +0000868 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000869 /// @param type the type
870 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000871 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000872
Ben Claytonf5f311e2021-04-28 14:31:23 +0000873 /// The ProgramBuilder
874 ProgramBuilder* const builder;
875
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000876 private:
877 /// CToAST<T> is specialized for various `T` types and each specialization
878 /// contains a single static `get()` method for obtaining the corresponding
879 /// AST type for the C type `T`.
880 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000881 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000882 template <typename T>
883 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000884 };
885
886 //////////////////////////////////////////////////////////////////////////////
887 // AST helper methods
888 //////////////////////////////////////////////////////////////////////////////
889
James Price65ae64d2021-04-29 12:59:14 +0000890 /// @return a new unnamed symbol
891 Symbol Sym() { return Symbols().New(); }
892
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000893 /// @param name the symbol string
894 /// @return a Symbol with the given name
895 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
896
897 /// @param sym the symbol
898 /// @return `sym`
899 Symbol Sym(Symbol sym) { return sym; }
900
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000901 /// @param expr the expression
902 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000903 template <typename T>
904 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
905 return expr;
906 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000907
Ben Claytonb8ea5912021-04-19 16:52:42 +0000908 /// Passthrough for nullptr
909 /// @return nullptr
910 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
911
Ben Claytonfe0910f2021-05-17 15:51:47 +0000912 /// @param source the source information
913 /// @param symbol the identifier symbol
914 /// @return an ast::IdentifierExpression with the given symbol
915 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
916 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000917 }
918
Ben Clayton46d78d72021-02-10 21:34:26 +0000919 /// @param symbol the identifier symbol
920 /// @return an ast::IdentifierExpression with the given symbol
921 ast::IdentifierExpression* Expr(Symbol symbol) {
922 return create<ast::IdentifierExpression>(symbol);
923 }
924
Ben Claytonfe0910f2021-05-17 15:51:47 +0000925 /// @param source the source information
926 /// @param variable the AST variable
927 /// @return an ast::IdentifierExpression with the variable's symbol
928 ast::IdentifierExpression* Expr(const Source& source,
929 ast::Variable* variable) {
930 return create<ast::IdentifierExpression>(source, variable->symbol());
931 }
932
Ben Claytonb8ea5912021-04-19 16:52:42 +0000933 /// @param variable the AST variable
934 /// @return an ast::IdentifierExpression with the variable's symbol
935 ast::IdentifierExpression* Expr(ast::Variable* variable) {
936 return create<ast::IdentifierExpression>(variable->symbol());
937 }
938
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000939 /// @param source the source information
940 /// @param name the identifier name
941 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000942 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
943 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
944 }
945
946 /// @param name the identifier name
947 /// @return an ast::IdentifierExpression with the given name
948 ast::IdentifierExpression* Expr(const char* name) {
949 return create<ast::IdentifierExpression>(Symbols().Register(name));
950 }
951
952 /// @param source the source information
953 /// @param name the identifier name
954 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000955 ast::IdentifierExpression* Expr(const Source& source,
956 const std::string& name) {
957 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
958 }
959
960 /// @param name the identifier name
961 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000962 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000963 return create<ast::IdentifierExpression>(Symbols().Register(name));
964 }
965
Ben Claytonfe0910f2021-05-17 15:51:47 +0000966 /// @param source the source information
967 /// @param value the boolean value
968 /// @return a Scalar constructor for the given value
969 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
970 return create<ast::ScalarConstructorExpression>(source, Literal(value));
971 }
972
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000973 /// @param value the boolean value
974 /// @return a Scalar constructor for the given value
975 ast::ScalarConstructorExpression* Expr(bool value) {
976 return create<ast::ScalarConstructorExpression>(Literal(value));
977 }
978
Ben Claytonfe0910f2021-05-17 15:51:47 +0000979 /// @param source the source information
980 /// @param value the float value
981 /// @return a Scalar constructor for the given value
982 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
983 return create<ast::ScalarConstructorExpression>(source, Literal(value));
984 }
985
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000986 /// @param value the float value
987 /// @return a Scalar constructor for the given value
988 ast::ScalarConstructorExpression* Expr(f32 value) {
989 return create<ast::ScalarConstructorExpression>(Literal(value));
990 }
991
Ben Claytonfe0910f2021-05-17 15:51:47 +0000992 /// @param source the source information
993 /// @param value the integer value
994 /// @return a Scalar constructor for the given value
995 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
996 return create<ast::ScalarConstructorExpression>(source, Literal(value));
997 }
998
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000999 /// @param value the integer value
1000 /// @return a Scalar constructor for the given value
1001 ast::ScalarConstructorExpression* Expr(i32 value) {
1002 return create<ast::ScalarConstructorExpression>(Literal(value));
1003 }
1004
Ben Claytonfe0910f2021-05-17 15:51:47 +00001005 /// @param source the source information
1006 /// @param value the unsigned int value
1007 /// @return a Scalar constructor for the given value
1008 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1009 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1010 }
1011
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001012 /// @param value the unsigned int value
1013 /// @return a Scalar constructor for the given value
1014 ast::ScalarConstructorExpression* Expr(u32 value) {
1015 return create<ast::ScalarConstructorExpression>(Literal(value));
1016 }
1017
1018 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1019 /// `list`.
1020 /// @param list the list to append too
1021 /// @param arg the arg to create
1022 template <typename ARG>
1023 void Append(ast::ExpressionList& list, ARG&& arg) {
1024 list.emplace_back(Expr(std::forward<ARG>(arg)));
1025 }
1026
1027 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1028 /// then appends them to `list`.
1029 /// @param list the list to append too
1030 /// @param arg0 the first argument
1031 /// @param args the rest of the arguments
1032 template <typename ARG0, typename... ARGS>
1033 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1034 Append(list, std::forward<ARG0>(arg0));
1035 Append(list, std::forward<ARGS>(args)...);
1036 }
1037
1038 /// @return an empty list of expressions
1039 ast::ExpressionList ExprList() { return {}; }
1040
1041 /// @param args the list of expressions
1042 /// @return the list of expressions converted to `ast::Expression`s using
1043 /// `Expr()`,
1044 template <typename... ARGS>
1045 ast::ExpressionList ExprList(ARGS&&... args) {
1046 ast::ExpressionList list;
1047 list.reserve(sizeof...(args));
1048 Append(list, std::forward<ARGS>(args)...);
1049 return list;
1050 }
1051
1052 /// @param list the list of expressions
1053 /// @return `list`
1054 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1055
1056 /// @param val the boolan value
1057 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001058 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001059
1060 /// @param val the float value
1061 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001062 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001063
1064 /// @param val the unsigned int value
1065 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001066 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001067
1068 /// @param val the integer value
1069 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001070 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001071
1072 /// @param args the arguments for the type constructor
1073 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1074 /// of `args` converted to `ast::Expression`s using `Expr()`
1075 template <typename T, typename... ARGS>
1076 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001077 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001078 }
1079
1080 /// @param type the type to construct
1081 /// @param args the arguments for the constructor
1082 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1083 /// values `args`.
1084 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001085 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001086 return create<ast::TypeConstructorExpression>(
1087 type, ExprList(std::forward<ARGS>(args)...));
1088 }
1089
Ben Clayton313e6182021-06-17 19:56:14 +00001090 /// @param source the source information
1091 /// @param type the type to construct
1092 /// @param args the arguments for the constructor
1093 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1094 /// values `args`.
1095 template <typename... ARGS>
1096 ast::TypeConstructorExpression* Construct(const Source& source,
1097 ast::Type* type,
1098 ARGS&&... args) {
1099 return create<ast::TypeConstructorExpression>(
1100 source, type, ExprList(std::forward<ARGS>(args)...));
1101 }
1102
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001103 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001104 /// @param type the vector type
1105 /// @param size the vector size
1106 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1107 /// type `type`, constructed with the values `args`.
1108 template <typename... ARGS>
1109 ast::TypeConstructorExpression* vec(ast::Type* type,
1110 uint32_t size,
1111 ARGS&&... args) {
1112 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1113 }
1114
1115 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001116 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1117 /// `T`, constructed with the values `args`.
1118 template <typename T, typename... ARGS>
1119 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001120 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 }
1122
1123 /// @param args the arguments for the vector constructor
1124 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1125 /// `T`, constructed with the values `args`.
1126 template <typename T, typename... ARGS>
1127 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001128 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001129 }
1130
1131 /// @param args the arguments for the vector constructor
1132 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1133 /// `T`, constructed with the values `args`.
1134 template <typename T, typename... ARGS>
1135 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001136 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 }
1138
1139 /// @param args the arguments for the matrix constructor
1140 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1141 /// `T`, constructed with the values `args`.
1142 template <typename T, typename... ARGS>
1143 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001144 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001145 }
1146
1147 /// @param args the arguments for the matrix constructor
1148 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1149 /// `T`, constructed with the values `args`.
1150 template <typename T, typename... ARGS>
1151 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001152 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001153 }
1154
1155 /// @param args the arguments for the matrix constructor
1156 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1157 /// `T`, constructed with the values `args`.
1158 template <typename T, typename... ARGS>
1159 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001160 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001161 }
1162
1163 /// @param args the arguments for the matrix constructor
1164 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1165 /// `T`, constructed with the values `args`.
1166 template <typename T, typename... ARGS>
1167 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001168 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001169 }
1170
1171 /// @param args the arguments for the matrix constructor
1172 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1173 /// `T`, constructed with the values `args`.
1174 template <typename T, typename... ARGS>
1175 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001176 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001177 }
1178
1179 /// @param args the arguments for the matrix constructor
1180 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1181 /// `T`, constructed with the values `args`.
1182 template <typename T, typename... ARGS>
1183 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001184 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001185 }
1186
1187 /// @param args the arguments for the matrix constructor
1188 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1189 /// `T`, constructed with the values `args`.
1190 template <typename T, typename... ARGS>
1191 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001192 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001193 }
1194
1195 /// @param args the arguments for the matrix constructor
1196 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1197 /// `T`, constructed with the values `args`.
1198 template <typename T, typename... ARGS>
1199 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001200 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001201 }
1202
1203 /// @param args the arguments for the matrix constructor
1204 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1205 /// `T`, constructed with the values `args`.
1206 template <typename T, typename... ARGS>
1207 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001208 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001209 }
1210
1211 /// @param args the arguments for the array constructor
1212 /// @return an `ast::TypeConstructorExpression` of an array with element type
1213 /// `T`, constructed with the values `args`.
1214 template <typename T, int N = 0, typename... ARGS>
1215 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001216 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001217 }
1218
1219 /// @param subtype the array element type
1220 /// @param n the array size. 0 represents a runtime-array.
1221 /// @param args the arguments for the array constructor
1222 /// @return an `ast::TypeConstructorExpression` of an array with element type
1223 /// `subtype`, constructed with the values `args`.
1224 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001225 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001226 uint32_t n,
1227 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001228 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001229 }
1230
1231 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001232 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001233 /// @param optional the optional variable settings.
1234 /// Can be any of the following, in any order:
1235 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001236 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001237 /// * ast::Expression* - specifies the variable's initializer expression
1238 /// * ast::DecorationList - specifies the variable's decorations
1239 /// Note that repeated arguments of the same type will use the last argument's
1240 /// value.
1241 /// @returns a `ast::Variable` with the given name, type and additional
1242 /// options
1243 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001244 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001245 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001246 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001247 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1248 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001249 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001250 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001251 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001252
1253 /// @param source the variable source
1254 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001255 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001256 /// @param optional the optional variable settings.
1257 /// Can be any of the following, in any order:
1258 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001259 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001260 /// * ast::Expression* - specifies the variable's initializer expression
1261 /// * ast::DecorationList - specifies the variable's decorations
1262 /// Note that repeated arguments of the same type will use the last argument's
1263 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001264 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001265 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001266 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001267 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001268 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001269 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001270 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1271 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001272 opts.storage, opts.access, type, false,
1273 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001274 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001275
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001276 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001277 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001278 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001279 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001280 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001281 template <typename NAME>
1282 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001283 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001284 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001285 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001286 return create<ast::Variable>(
1287 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1288 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001289 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001290
1291 /// @param source the variable source
1292 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001293 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001294 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001295 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001296 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001297 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001298 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001299 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001300 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001301 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001302 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001303 return create<ast::Variable>(
1304 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1305 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001306 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001307
James Pricedaf1f1c2021-04-08 22:15:48 +00001308 /// @param name the parameter name
1309 /// @param type the parameter type
1310 /// @param decorations optional parameter decorations
1311 /// @returns a constant `ast::Variable` with the given name and type
1312 template <typename NAME>
1313 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001314 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001315 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001316 return create<ast::Variable>(
1317 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1318 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001319 }
1320
1321 /// @param source the parameter source
1322 /// @param name the parameter name
1323 /// @param type the parameter type
1324 /// @param decorations optional parameter decorations
1325 /// @returns a constant `ast::Variable` with the given name and type
1326 template <typename NAME>
1327 ast::Variable* Param(const Source& source,
1328 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001329 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001330 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001331 return create<ast::Variable>(
1332 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1333 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001334 }
1335
Ben Clayton42708342021-04-21 17:55:12 +00001336 /// @param name the variable name
1337 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001338 /// @param optional the optional variable settings.
1339 /// Can be any of the following, in any order:
1340 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001341 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001342 /// * ast::Expression* - specifies the variable's initializer expression
1343 /// * ast::DecorationList - specifies the variable's decorations
1344 /// Note that repeated arguments of the same type will use the last argument's
1345 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001346 /// @returns a new `ast::Variable`, which is automatically registered as a
1347 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001348 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001349 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001350 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001351 OPTIONAL&&... optional) {
1352 auto* var = Var(std::forward<NAME>(name), type,
1353 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001354 AST().AddGlobalVariable(var);
1355 return var;
1356 }
1357
1358 /// @param source the variable source
1359 /// @param name the variable name
1360 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001361 /// @param optional the optional variable settings.
1362 /// Can be any of the following, in any order:
1363 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001364 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001365 /// * ast::Expression* - specifies the variable's initializer expression
1366 /// * ast::DecorationList - specifies the variable's decorations
1367 /// Note that repeated arguments of the same type will use the last argument's
1368 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001369 /// @returns a new `ast::Variable`, which is automatically registered as a
1370 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001371 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001372 ast::Variable* Global(const Source& source,
1373 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001374 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001375 OPTIONAL&&... optional) {
1376 auto* var = Var(source, std::forward<NAME>(name), type,
1377 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001378 AST().AddGlobalVariable(var);
1379 return var;
1380 }
1381
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001382 /// @param name the variable name
1383 /// @param type the variable type
1384 /// @param constructor constructor expression
1385 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001386 /// @returns a const `ast::Variable` constructed by calling Var() with the
1387 /// arguments of `args`, which is automatically registered as a global
1388 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001389 template <typename NAME>
1390 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001391 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001392 ast::Expression* constructor,
1393 ast::DecorationList decorations = {}) {
1394 auto* var = Const(std::forward<NAME>(name), type, constructor,
1395 std::move(decorations));
1396 AST().AddGlobalVariable(var);
1397 return var;
1398 }
1399
1400 /// @param source the variable source
1401 /// @param name the variable name
1402 /// @param type the variable type
1403 /// @param constructor constructor expression
1404 /// @param decorations optional variable decorations
1405 /// @returns a const `ast::Variable` constructed by calling Var() with the
1406 /// arguments of `args`, which is automatically registered as a global
1407 /// variable with the ast::Module.
1408 template <typename NAME>
1409 ast::Variable* GlobalConst(const Source& source,
1410 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001411 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001412 ast::Expression* constructor,
1413 ast::DecorationList decorations = {}) {
1414 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1415 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001416 AST().AddGlobalVariable(var);
1417 return var;
1418 }
1419
Ben Claytonfe0910f2021-05-17 15:51:47 +00001420 /// @param source the source information
1421 /// @param expr the expression to take the address of
1422 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1423 template <typename EXPR>
1424 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1425 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1426 Expr(std::forward<EXPR>(expr)));
1427 }
1428
1429 /// @param expr the expression to take the address of
1430 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1431 template <typename EXPR>
1432 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1433 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1434 Expr(std::forward<EXPR>(expr)));
1435 }
1436
1437 /// @param source the source information
1438 /// @param expr the expression to perform an indirection on
1439 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1440 template <typename EXPR>
1441 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1442 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1443 Expr(std::forward<EXPR>(expr)));
1444 }
1445
1446 /// @param expr the expression to perform an indirection on
1447 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1448 template <typename EXPR>
1449 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1450 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1451 Expr(std::forward<EXPR>(expr)));
1452 }
1453
Antonio Maiorano14b34032021-06-09 20:17:59 +00001454 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001455 /// @param func the function name
1456 /// @param args the function call arguments
1457 /// @returns a `ast::CallExpression` to the function `func`, with the
1458 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1459 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001460 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1461 return create<ast::CallExpression>(source, Expr(func),
1462 ExprList(std::forward<ARGS>(args)...));
1463 }
1464
1465 /// @param func the function name
1466 /// @param args the function call arguments
1467 /// @returns a `ast::CallExpression` to the function `func`, with the
1468 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1469 template <typename NAME,
1470 typename... ARGS,
1471 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001472 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1473 return create<ast::CallExpression>(Expr(func),
1474 ExprList(std::forward<ARGS>(args)...));
1475 }
1476
Ben Clayton7fe01062021-06-11 13:22:27 +00001477 /// @param expr the expression to ignore
1478 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1479 /// passed the single `expr` argument
1480 template <typename EXPR>
1481 ast::CallStatement* Ignore(EXPR&& expr) {
1482 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1483 }
1484
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001485 /// @param lhs the left hand argument to the addition operation
1486 /// @param rhs the right hand argument to the addition operation
1487 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1488 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001489 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001490 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1491 Expr(std::forward<LHS>(lhs)),
1492 Expr(std::forward<RHS>(rhs)));
1493 }
1494
James Pricec32e8f62021-06-22 20:08:29 +00001495 /// @param lhs the left hand argument to the and operation
1496 /// @param rhs the right hand argument to the and operation
1497 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1498 template <typename LHS, typename RHS>
1499 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1500 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1501 Expr(std::forward<LHS>(lhs)),
1502 Expr(std::forward<RHS>(rhs)));
1503 }
1504
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001505 /// @param lhs the left hand argument to the subtraction operation
1506 /// @param rhs the right hand argument to the subtraction operation
1507 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1508 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001509 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001510 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1511 Expr(std::forward<LHS>(lhs)),
1512 Expr(std::forward<RHS>(rhs)));
1513 }
1514
1515 /// @param lhs the left hand argument to the multiplication operation
1516 /// @param rhs the right hand argument to the multiplication operation
1517 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1518 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001519 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001520 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1521 Expr(std::forward<LHS>(lhs)),
1522 Expr(std::forward<RHS>(rhs)));
1523 }
1524
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001525 /// @param source the source information
1526 /// @param lhs the left hand argument to the multiplication operation
1527 /// @param rhs the right hand argument to the multiplication operation
1528 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1529 template <typename LHS, typename RHS>
1530 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1531 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1532 Expr(std::forward<LHS>(lhs)),
1533 Expr(std::forward<RHS>(rhs)));
1534 }
1535
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001536 /// @param lhs the left hand argument to the division operation
1537 /// @param rhs the right hand argument to the division operation
1538 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1539 template <typename LHS, typename RHS>
1540 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1541 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1542 Expr(std::forward<LHS>(lhs)),
1543 Expr(std::forward<RHS>(rhs)));
1544 }
1545
Ben Clayton0597a2b2021-06-16 09:19:36 +00001546 /// @param source the source information
1547 /// @param arr the array argument for the array accessor expression
1548 /// @param idx the index argument for the array accessor expression
1549 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1550 template <typename ARR, typename IDX>
1551 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1552 ARR&& arr,
1553 IDX&& idx) {
1554 return create<ast::ArrayAccessorExpression>(
1555 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1556 }
1557
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001558 /// @param arr the array argument for the array accessor expression
1559 /// @param idx the index argument for the array accessor expression
1560 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1561 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001562 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001563 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1564 Expr(std::forward<IDX>(idx)));
1565 }
1566
1567 /// @param obj the object for the member accessor expression
1568 /// @param idx the index argument for the array accessor expression
1569 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1570 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001571 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001572 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1573 Expr(std::forward<IDX>(idx)));
1574 }
1575
Ben Clayton42d1e092021-02-02 14:29:15 +00001576 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001577 /// @param val the offset value
1578 /// @returns the offset decoration pointer
1579 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1580 return create<ast::StructMemberOffsetDecoration>(source_, val);
1581 }
1582
Ben Claytond614dd52021-03-15 10:43:11 +00001583 /// Creates a ast::StructMemberSizeDecoration
1584 /// @param source the source information
1585 /// @param val the size value
1586 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001587 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1588 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001589 return create<ast::StructMemberSizeDecoration>(source, val);
1590 }
1591
1592 /// Creates a ast::StructMemberSizeDecoration
1593 /// @param val the size value
1594 /// @returns the size decoration pointer
1595 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1596 return create<ast::StructMemberSizeDecoration>(source_, val);
1597 }
1598
1599 /// Creates a ast::StructMemberAlignDecoration
1600 /// @param source the source information
1601 /// @param val the align value
1602 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001603 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1604 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001605 return create<ast::StructMemberAlignDecoration>(source, val);
1606 }
1607
1608 /// Creates a ast::StructMemberAlignDecoration
1609 /// @param val the align value
1610 /// @returns the align decoration pointer
1611 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1612 return create<ast::StructMemberAlignDecoration>(source_, val);
1613 }
1614
Ben Clayton42d1e092021-02-02 14:29:15 +00001615 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001616 /// @param source the source information
1617 /// @param name the function name
1618 /// @param params the function parameters
1619 /// @param type the function return type
1620 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001621 /// @param decorations the optional function decorations
1622 /// @param return_type_decorations the optional function return type
1623 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001624 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001625 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001626 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001627 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001628 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001629 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001630 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001631 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001632 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001633 auto* func =
1634 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1635 type, create<ast::BlockStatement>(body),
1636 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001637 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001638 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001639 }
1640
Ben Clayton42d1e092021-02-02 14:29:15 +00001641 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001642 /// @param name the function name
1643 /// @param params the function parameters
1644 /// @param type the function return type
1645 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001646 /// @param decorations the optional function decorations
1647 /// @param return_type_decorations the optional function return type
1648 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001649 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001650 template <typename NAME>
1651 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001652 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001653 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001654 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001655 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001656 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001657 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1658 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001659 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001660 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001661 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001662 }
1663
Ben Clayton49668bb2021-04-17 05:32:01 +00001664 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001665 /// @param source the source information
1666 /// @returns the return statement pointer
1667 ast::ReturnStatement* Return(const Source& source) {
1668 return create<ast::ReturnStatement>(source);
1669 }
1670
1671 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001672 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001673 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1674
1675 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001676 /// @param source the source information
1677 /// @param val the return value
1678 /// @returns the return statement pointer
1679 template <typename EXPR>
1680 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1681 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1682 }
1683
1684 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001685 /// @param val the return value
1686 /// @returns the return statement pointer
1687 template <typename EXPR>
1688 ast::ReturnStatement* Return(EXPR&& val) {
1689 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001690 }
1691
Ben Clayton950809f2021-06-09 14:32:14 +00001692 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001693 /// @param source the source information
1694 /// @param name the alias name
1695 /// @param type the alias target type
1696 /// @returns the alias type
1697 template <typename NAME>
1698 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1699 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001700 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001701 return out;
1702 }
1703
Ben Clayton950809f2021-06-09 14:32:14 +00001704 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001705 /// @param name the alias name
1706 /// @param type the alias target type
1707 /// @returns the alias type
1708 template <typename NAME>
1709 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1710 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001711 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001712 return out;
1713 }
1714
Ben Clayton950809f2021-06-09 14:32:14 +00001715 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001716 /// @param source the source information
1717 /// @param name the struct name
1718 /// @param members the struct members
1719 /// @param decorations the optional struct decorations
1720 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001721 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001722 ast::Struct* Structure(const Source& source,
1723 NAME&& name,
1724 ast::StructMemberList members,
1725 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001726 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001727 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001728 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001729 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001730 return type;
1731 }
1732
Ben Clayton950809f2021-06-09 14:32:14 +00001733 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001734 /// @param name the struct name
1735 /// @param members the struct members
1736 /// @param decorations the optional struct decorations
1737 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001738 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001739 ast::Struct* Structure(NAME&& name,
1740 ast::StructMemberList members,
1741 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001742 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001743 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001744 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001745 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001746 return type;
1747 }
1748
Ben Clayton42d1e092021-02-02 14:29:15 +00001749 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001750 /// @param source the source information
1751 /// @param name the struct member name
1752 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001753 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001754 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001755 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001756 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001757 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001758 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001759 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001760 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1761 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001762 }
1763
Ben Clayton42d1e092021-02-02 14:29:15 +00001764 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001765 /// @param name the struct member name
1766 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001767 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001768 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001769 template <typename NAME>
1770 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001771 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001772 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001773 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1774 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001775 }
1776
Ben Claytonbab31972021-02-08 21:16:21 +00001777 /// Creates a ast::StructMember with the given byte offset
1778 /// @param offset the offset to use in the StructMemberOffsetDecoration
1779 /// @param name the struct member name
1780 /// @param type the struct member type
1781 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001782 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001783 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001784 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001785 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001786 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001787 create<ast::StructMemberOffsetDecoration>(offset),
1788 });
1789 }
1790
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001791 /// Creates a ast::BlockStatement with input statements
1792 /// @param statements statements of block
1793 /// @returns the block statement pointer
1794 template <typename... Statements>
1795 ast::BlockStatement* Block(Statements&&... statements) {
1796 return create<ast::BlockStatement>(
1797 ast::StatementList{std::forward<Statements>(statements)...});
1798 }
1799
1800 /// Creates a ast::ElseStatement with input condition and body
1801 /// @param condition the else condition expression
1802 /// @param body the else body
1803 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001804 template <typename CONDITION>
1805 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1806 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1807 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001808 }
1809
1810 /// Creates a ast::IfStatement with input condition, body, and optional
1811 /// variadic else statements
1812 /// @param condition the if statement condition expression
1813 /// @param body the if statement body
1814 /// @param elseStatements optional variadic else statements
1815 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001816 template <typename CONDITION, typename... ELSE_STATEMENTS>
1817 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001818 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001819 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001820 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001821 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001822 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001823 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001824 }
1825
1826 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001827 /// @param source the source information
1828 /// @param lhs the left hand side expression initializer
1829 /// @param rhs the right hand side expression initializer
1830 /// @returns the assignment statement pointer
1831 template <typename LhsExpressionInit, typename RhsExpressionInit>
1832 ast::AssignmentStatement* Assign(const Source& source,
1833 LhsExpressionInit&& lhs,
1834 RhsExpressionInit&& rhs) {
1835 return create<ast::AssignmentStatement>(
1836 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1837 Expr(std::forward<RhsExpressionInit>(rhs)));
1838 }
1839
1840 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001841 /// @param lhs the left hand side expression initializer
1842 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001843 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001844 template <typename LhsExpressionInit, typename RhsExpressionInit>
1845 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1846 RhsExpressionInit&& rhs) {
1847 return create<ast::AssignmentStatement>(
1848 Expr(std::forward<LhsExpressionInit>(lhs)),
1849 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001850 }
1851
1852 /// Creates a ast::LoopStatement with input body and optional continuing
1853 /// @param body the loop body
1854 /// @param continuing the optional continuing block
1855 /// @returns the loop statement pointer
1856 ast::LoopStatement* Loop(ast::BlockStatement* body,
1857 ast::BlockStatement* continuing = nullptr) {
1858 return create<ast::LoopStatement>(body, continuing);
1859 }
1860
1861 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001862 /// @param source the source information
1863 /// @param var the variable to wrap in a decl statement
1864 /// @returns the variable decl statement pointer
1865 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1866 return create<ast::VariableDeclStatement>(source, var);
1867 }
1868
1869 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001870 /// @param var the variable to wrap in a decl statement
1871 /// @returns the variable decl statement pointer
1872 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1873 return create<ast::VariableDeclStatement>(var);
1874 }
1875
Antonio Maioranocea744d2021-03-25 12:55:27 +00001876 /// Creates a ast::SwitchStatement with input expression and cases
1877 /// @param condition the condition expression initializer
1878 /// @param cases case statements
1879 /// @returns the switch statement pointer
1880 template <typename ExpressionInit, typename... Cases>
1881 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1882 return create<ast::SwitchStatement>(
1883 Expr(std::forward<ExpressionInit>(condition)),
1884 ast::CaseStatementList{std::forward<Cases>(cases)...});
1885 }
1886
1887 /// Creates a ast::CaseStatement with input list of selectors, and body
1888 /// @param selectors list of selectors
1889 /// @param body the case body
1890 /// @returns the case statement pointer
1891 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1892 ast::BlockStatement* body = nullptr) {
1893 return create<ast::CaseStatement>(std::move(selectors),
1894 body ? body : Block());
1895 }
1896
1897 /// Convenient overload that takes a single selector
1898 /// @param selector a single case selector
1899 /// @param body the case body
1900 /// @returns the case statement pointer
1901 ast::CaseStatement* Case(ast::IntLiteral* selector,
1902 ast::BlockStatement* body = nullptr) {
1903 return Case(ast::CaseSelectorList{selector}, body);
1904 }
1905
1906 /// Convenience function that creates a 'default' ast::CaseStatement
1907 /// @param body the case body
1908 /// @returns the case statement pointer
1909 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1910 return Case(ast::CaseSelectorList{}, body);
1911 }
1912
James Price68f558f2021-04-06 15:51:47 +00001913 /// Creates an ast::BuiltinDecoration
1914 /// @param source the source information
1915 /// @param builtin the builtin value
1916 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001917 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001918 return create<ast::BuiltinDecoration>(source, builtin);
1919 }
1920
1921 /// Creates an ast::BuiltinDecoration
1922 /// @param builtin the builtin value
1923 /// @returns the builtin decoration pointer
1924 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1925 return create<ast::BuiltinDecoration>(source_, builtin);
1926 }
1927
1928 /// Creates an ast::LocationDecoration
1929 /// @param source the source information
1930 /// @param location the location value
1931 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001932 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001933 return create<ast::LocationDecoration>(source, location);
1934 }
1935
1936 /// Creates an ast::LocationDecoration
1937 /// @param location the location value
1938 /// @returns the location decoration pointer
1939 ast::LocationDecoration* Location(uint32_t location) {
1940 return create<ast::LocationDecoration>(source_, location);
1941 }
1942
James Pricef2f3bfc2021-05-13 20:32:32 +00001943 /// Creates an ast::OverrideDecoration with a specific constant ID
1944 /// @param source the source information
1945 /// @param id the id value
1946 /// @returns the override decoration pointer
1947 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1948 return create<ast::OverrideDecoration>(source, id);
1949 }
1950
1951 /// Creates an ast::OverrideDecoration with a specific constant ID
1952 /// @param id the optional id value
1953 /// @returns the override decoration pointer
1954 ast::OverrideDecoration* Override(uint32_t id) {
1955 return Override(source_, id);
1956 }
1957
1958 /// Creates an ast::OverrideDecoration without a constant ID
1959 /// @param source the source information
1960 /// @returns the override decoration pointer
1961 ast::OverrideDecoration* Override(const Source& source) {
1962 return create<ast::OverrideDecoration>(source);
1963 }
1964
1965 /// Creates an ast::OverrideDecoration without a constant ID
1966 /// @returns the override decoration pointer
1967 ast::OverrideDecoration* Override() { return Override(source_); }
1968
James Price68f558f2021-04-06 15:51:47 +00001969 /// Creates an ast::StageDecoration
1970 /// @param source the source information
1971 /// @param stage the pipeline stage
1972 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001973 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001974 return create<ast::StageDecoration>(source, stage);
1975 }
1976
1977 /// Creates an ast::StageDecoration
1978 /// @param stage the pipeline stage
1979 /// @returns the stage decoration pointer
1980 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1981 return create<ast::StageDecoration>(source_, stage);
1982 }
1983
James Price70f80bb2021-05-19 13:40:08 +00001984 /// Creates an ast::WorkgroupDecoration
1985 /// @param x the x dimension expression
1986 /// @returns the workgroup decoration pointer
1987 template <typename EXPR_X>
1988 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
1989 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
1990 }
1991
1992 /// Creates an ast::WorkgroupDecoration
1993 /// @param x the x dimension expression
1994 /// @param y the y dimension expression
1995 /// @returns the workgroup decoration pointer
1996 template <typename EXPR_X, typename EXPR_Y>
1997 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
1998 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
1999 nullptr);
2000 }
2001
2002 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002003 /// @param source the source information
2004 /// @param x the x dimension expression
2005 /// @param y the y dimension expression
2006 /// @param z the z dimension expression
2007 /// @returns the workgroup decoration pointer
2008 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2009 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2010 EXPR_X&& x,
2011 EXPR_Y&& y,
2012 EXPR_Z&& z) {
2013 return create<ast::WorkgroupDecoration>(
2014 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2015 Expr(std::forward<EXPR_Z>(z)));
2016 }
2017
2018 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002019 /// @param x the x dimension expression
2020 /// @param y the y dimension expression
2021 /// @param z the z dimension expression
2022 /// @returns the workgroup decoration pointer
2023 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2024 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2025 return create<ast::WorkgroupDecoration>(
2026 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2027 Expr(std::forward<EXPR_Z>(z)));
2028 }
2029
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002030 /// Sets the current builder source to `src`
2031 /// @param src the Source used for future create() calls
2032 void SetSource(const Source& src) {
2033 AssertNotMoved();
2034 source_ = src;
2035 }
2036
2037 /// Sets the current builder source to `loc`
2038 /// @param loc the Source used for future create() calls
2039 void SetSource(const Source::Location& loc) {
2040 AssertNotMoved();
2041 source_ = Source(loc);
2042 }
2043
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002044 /// Marks that the given transform has been applied to this program.
2045 /// @param transform the transform that has been applied
2046 void SetTransformApplied(const CastableBase* transform) {
2047 transforms_applied_.emplace(&transform->TypeInfo());
2048 }
2049
2050 /// Marks that the given transform `T` has been applied to this program.
2051 template <typename T>
2052 void SetTransformApplied() {
2053 transforms_applied_.emplace(&TypeInfo::Of<T>());
2054 }
2055
2056 /// Marks that the transforms with the given TypeInfos have been applied to
2057 /// this program.
2058 /// @param transforms the set of transform TypeInfos that has been applied
2059 void SetTransformApplied(
2060 const std::unordered_set<const TypeInfo*>& transforms) {
2061 for (auto* transform : transforms) {
2062 transforms_applied_.emplace(transform);
2063 }
2064 }
2065
2066 /// @returns true if the transform of type `T` was applied.
2067 template <typename T>
2068 bool HasTransformApplied() {
2069 return transforms_applied_.count(&TypeInfo::Of<T>());
2070 }
2071
2072 /// @return the TypeInfo pointers of all transforms that have been applied to
2073 /// this program.
2074 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2075 return transforms_applied_;
2076 }
2077
Ben Clayton33352542021-01-29 16:43:41 +00002078 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002079 /// @note As the Resolver is run when the Program is built, this will only be
2080 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002081 /// @param expr the AST expression
2082 /// @return the resolved semantic type for the expression, or nullptr if the
2083 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002084 sem::Type* TypeOf(const ast::Expression* expr) const;
2085
Ben Claytonfe0910f2021-05-17 15:51:47 +00002086 /// Helper for returning the resolved semantic type of the variable `var`.
2087 /// @note As the Resolver is run when the Program is built, this will only be
2088 /// useful for the Resolver itself and tests that use their own Resolver.
2089 /// @param var the AST variable
2090 /// @return the resolved semantic type for the variable, or nullptr if the
2091 /// variable has no resolved type.
2092 sem::Type* TypeOf(const ast::Variable* var) const;
2093
Ben Claytonfbec46f2021-04-30 20:20:19 +00002094 /// Helper for returning the resolved semantic type of the AST type `type`.
2095 /// @note As the Resolver is run when the Program is built, this will only be
2096 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002097 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002098 /// @return the resolved semantic type for the type, or nullptr if the type
2099 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002100 const sem::Type* TypeOf(const ast::Type* type) const;
2101
2102 /// Helper for returning the resolved semantic type of the AST type
2103 /// declaration `type_decl`.
2104 /// @note As the Resolver is run when the Program is built, this will only be
2105 /// useful for the Resolver itself and tests that use their own Resolver.
2106 /// @param type_decl the AST type declaration
2107 /// @return the resolved semantic type for the type declaration, or nullptr if
2108 /// the type declaration has no resolved type.
2109 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002110
Ben Clayton169512e2021-04-17 05:52:11 +00002111 /// Wraps the ast::Literal in a statement. This is used by tests that
2112 /// construct a partial AST and require the Resolver to reach these
2113 /// nodes.
2114 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2115 /// @return the ast::Statement that wraps the ast::Statement
2116 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002117 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002118 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002119 /// nodes.
2120 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2121 /// @return the ast::Statement that wraps the ast::Expression
2122 ast::Statement* WrapInStatement(ast::Expression* expr);
2123 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002124 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002125 /// these nodes.
2126 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2127 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2128 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2129 /// Returns the statement argument. Used as a passthrough-overload by
2130 /// WrapInFunction().
2131 /// @param stmt the ast::Statement
2132 /// @return `stmt`
2133 ast::Statement* WrapInStatement(ast::Statement* stmt);
2134 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002135 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002136 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002137 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002138 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002139 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002140 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002141 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002142 }
2143 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002144 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002145 /// @returns the function
2146 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002147
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002148 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002149 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002150
2151 protected:
2152 /// Asserts that the builder has not been moved.
2153 void AssertNotMoved() const;
2154
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002155 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002156 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002157 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002158 ASTNodeAllocator ast_nodes_;
2159 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002160 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002161 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002162 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002163 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002164 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002165
2166 /// The source to use when creating AST nodes without providing a Source as
2167 /// the first argument.
2168 Source source_;
2169
Ben Clayton5f0ea112021-03-09 10:54:37 +00002170 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002171 /// program when built.
2172 bool resolve_on_build_ = true;
2173
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002174 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2175 bool moved_ = false;
2176};
2177
2178//! @cond Doxygen_Suppress
2179// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2180template <>
2181struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002182 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002183 return t->i32();
2184 }
2185};
2186template <>
2187struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002188 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002189 return t->u32();
2190 }
2191};
2192template <>
2193struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002194 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002195 return t->f32();
2196 }
2197};
2198template <>
2199struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002200 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002201 return t->bool_();
2202 }
2203};
2204template <>
2205struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002206 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002207 return t->void_();
2208 }
2209};
2210//! @endcond
2211
Ben Clayton917b14b2021-04-19 16:50:23 +00002212/// @param builder the ProgramBuilder
2213/// @returns the ProgramID of the ProgramBuilder
2214inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2215 return builder->ID();
2216}
2217
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002218} // namespace tint
2219
2220#endif // SRC_PROGRAM_BUILDER_H_