blob: f0b0e059c8a2ed40b6ecd70403a93593f49de6a5 [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 Claytond9603282021-06-29 12:37:45 +000028#include "src/ast/bitcast_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000029#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030#include "src/ast/bool_literal.h"
31#include "src/ast/call_expression.h"
Ben Clayton7fe01062021-06-11 13:22:27 +000032#include "src/ast/call_statement.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000033#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000034#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000035#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000036#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000037#include "src/ast/float_literal.h"
Ben Clayton65cd2592021-07-02 19:27:42 +000038#include "src/ast/for_loop_statement.h"
Ben Claytona922e652021-04-21 13:37:22 +000039#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000040#include "src/ast/if_statement.h"
James Price989a8e42021-06-28 23:04:43 +000041#include "src/ast/interpolate_decoration.h"
James Price508d2492021-07-12 12:12:32 +000042#include "src/ast/invariant_decoration.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000043#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000044#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/member_accessor_expression.h"
46#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000047#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000048#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000049#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000050#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000051#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000052#include "src/ast/scalar_constructor_expression.h"
53#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000054#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000055#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000056#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000057#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000058#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000059#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000060#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000061#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000062#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000063#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000064#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000065#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000066#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000067#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000068#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000069#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000070#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000071#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000072#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000073#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000074#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000075#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000076#include "src/sem/f32_type.h"
77#include "src/sem/i32_type.h"
78#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000079#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000080#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000081#include "src/sem/sampled_texture_type.h"
82#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000083#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000084#include "src/sem/u32_type.h"
85#include "src/sem/vector_type.h"
86#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000087
Ben Clayton03d10722021-06-05 12:45:50 +000088#ifdef INCLUDE_TINT_TINT_H_
89#error "internal tint header being #included from tint.h"
90#endif
91
Ben Clayton401b96b2021-02-03 17:19:59 +000092// Forward declarations
Ben Claytonb5cd10c2021-06-25 10:26:26 +000093namespace tint {
Ben Clayton401b96b2021-02-03 17:19:59 +000094namespace ast {
95class VariableDeclStatement;
96} // namespace ast
Ben Claytonb5cd10c2021-06-25 10:26:26 +000097} // namespace tint
Ben Clayton401b96b2021-02-03 17:19:59 +000098
Ben Claytonb5cd10c2021-06-25 10:26:26 +000099namespace tint {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000100class CloneContext;
101
102/// ProgramBuilder is a mutable builder for a Program.
103/// To construct a Program, populate the builder and then `std::move` it to a
104/// Program.
105class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +0000106 /// VarOptionals is a helper for accepting a number of optional, extra
107 /// arguments for Var() and Global().
108 struct VarOptionals {
109 template <typename... ARGS>
110 explicit VarOptionals(ARGS&&... args) {
111 Apply(std::forward<ARGS>(args)...);
112 }
113 ~VarOptionals();
114
115 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000116 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000117 ast::Expression* constructor = nullptr;
118 ast::DecorationList decorations = {};
119
120 private:
121 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000122 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000123 void Set(ast::Expression* c) { constructor = c; }
124 void Set(const ast::DecorationList& l) { decorations = l; }
125
126 template <typename FIRST, typename... ARGS>
127 void Apply(FIRST&& first, ARGS&&... args) {
128 Set(std::forward<FIRST>(first));
129 Apply(std::forward<ARGS>(args)...);
130 }
131 void Apply() {}
132 };
133
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000134 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000135 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
136 using ASTNodeAllocator = BlockAllocator<ast::Node>;
137
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000138 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
139 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000140
141 /// `i32` is a type alias to `int`.
142 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
143 /// WGSL syntax.
144 /// Note: this is intentionally not aliased to uint32_t as we want integer
145 /// literals passed to the builder to match WGSL's integer literal types.
146 using i32 = decltype(1);
147 /// `u32` is a type alias to `unsigned int`.
148 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
149 /// WGSL syntax.
150 /// Note: this is intentionally not aliased to uint32_t as we want integer
151 /// literals passed to the builder to match WGSL's integer literal types.
152 using u32 = decltype(1u);
153 /// `f32` is a type alias to `float`
154 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
155 /// WGSL syntax.
156 using f32 = float;
157
158 /// Constructor
159 ProgramBuilder();
160
161 /// Move constructor
162 /// @param rhs the builder to move
163 ProgramBuilder(ProgramBuilder&& rhs);
164
165 /// Destructor
166 virtual ~ProgramBuilder();
167
168 /// Move assignment operator
169 /// @param rhs the builder to move
170 /// @return this builder
171 ProgramBuilder& operator=(ProgramBuilder&& rhs);
172
Ben Claytone43c8302021-01-29 11:59:32 +0000173 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
174 /// making a deep clone of the Program contents.
175 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
176 /// existing immutable program.
177 /// As the returned ProgramBuilder wraps `program`, `program` must not be
178 /// destructed or assigned while using the returned ProgramBuilder.
179 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
180 /// function. See crbug.com/tint/460.
181 /// @param program the immutable Program to wrap
182 /// @return the ProgramBuilder that wraps `program`
183 static ProgramBuilder Wrap(const Program* program);
184
Ben Claytone6995de2021-04-13 23:27:27 +0000185 /// @returns the unique identifier for this program
186 ProgramID ID() const { return id_; }
187
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000188 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000189 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000190 AssertNotMoved();
191 return types_;
192 }
193
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000194 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000195 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000196 AssertNotMoved();
197 return types_;
198 }
199
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000200 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000201 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000202 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000203 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000204 }
205
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000206 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000207 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000208 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000209 return ast_nodes_;
210 }
211
212 /// @returns a reference to the program's semantic nodes storage
213 SemNodeAllocator& SemNodes() {
214 AssertNotMoved();
215 return sem_nodes_;
216 }
217
218 /// @returns a reference to the program's semantic nodes storage
219 const SemNodeAllocator& SemNodes() const {
220 AssertNotMoved();
221 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000222 }
223
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000224 /// @returns a reference to the program's AST root Module
225 ast::Module& AST() {
226 AssertNotMoved();
227 return *ast_;
228 }
229
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000230 /// @returns a reference to the program's AST root Module
231 const ast::Module& AST() const {
232 AssertNotMoved();
233 return *ast_;
234 }
235
236 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000237 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000238 AssertNotMoved();
239 return sem_;
240 }
241
242 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000243 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000244 AssertNotMoved();
245 return sem_;
246 }
247
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000248 /// @returns a reference to the program's SymbolTable
249 SymbolTable& Symbols() {
250 AssertNotMoved();
251 return symbols_;
252 }
253
Ben Clayton708dc2d2021-01-29 11:22:40 +0000254 /// @returns a reference to the program's SymbolTable
255 const SymbolTable& Symbols() const {
256 AssertNotMoved();
257 return symbols_;
258 }
259
Ben Clayton844217f2021-01-27 18:49:05 +0000260 /// @returns a reference to the program's diagnostics
261 diag::List& Diagnostics() {
262 AssertNotMoved();
263 return diagnostics_;
264 }
265
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000266 /// @returns a reference to the program's diagnostics
267 const diag::List& Diagnostics() const {
268 AssertNotMoved();
269 return diagnostics_;
270 }
271
Ben Clayton5f0ea112021-03-09 10:54:37 +0000272 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000273 /// @param enable the new flag value (defaults to true)
274 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
275
Ben Clayton5f0ea112021-03-09 10:54:37 +0000276 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000277 /// built.
278 bool ResolveOnBuild() const { return resolve_on_build_; }
279
Ben Clayton844217f2021-01-27 18:49:05 +0000280 /// @returns true if the program has no error diagnostics and is not missing
281 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000282 bool IsValid() const;
283
Ben Clayton708dc2d2021-01-29 11:22:40 +0000284 /// Writes a representation of the node to the output stream
285 /// @note unlike str(), to_str() does not automatically demangle the string.
286 /// @param node the AST node
287 /// @param out the stream to write to
288 /// @param indent number of spaces to indent the node when writing
289 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
290 node->to_str(Sem(), out, indent);
291 }
292
293 /// Returns a demangled, string representation of `node`.
294 /// @param node the AST node
295 /// @returns a string representation of the node
296 std::string str(const ast::Node* node) const;
297
Ben Clayton7fdfff12021-01-29 15:17:30 +0000298 /// Creates a new ast::Node owned by the ProgramBuilder. When the
299 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000300 /// @param source the Source of the node
301 /// @param args the arguments to pass to the type constructor
302 /// @returns the node pointer
303 template <typename T, typename... ARGS>
304 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
305 ARGS&&... args) {
306 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000307 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000308 }
309
Ben Clayton7fdfff12021-01-29 15:17:30 +0000310 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
311 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000312 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000313 /// When the ProgramBuilder is destructed, the ast::Node will also be
314 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000315 /// @returns the node pointer
316 template <typename T>
317 traits::EnableIfIsType<T, ast::Node>* create() {
318 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000319 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000320 }
321
Ben Clayton7fdfff12021-01-29 15:17:30 +0000322 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
323 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000324 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000325 /// When the ProgramBuilder is destructed, the ast::Node will also be
326 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000327 /// @param arg0 the first arguments to pass to the type constructor
328 /// @param args the remaining arguments to pass to the type constructor
329 /// @returns the node pointer
330 template <typename T, typename ARG0, typename... ARGS>
331 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
332 traits::IsTypeOrDerived<T, ast::Node>::value &&
333 !traits::IsTypeOrDerived<ARG0, Source>::value,
334 T>*
335 create(ARG0&& arg0, ARGS&&... args) {
336 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000337 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000338 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000339 }
340
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000341 /// Creates a new sem::Node owned by the ProgramBuilder.
342 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000343 /// destructed.
344 /// @param args the arguments to pass to the type constructor
345 /// @returns the node pointer
346 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000347 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
348 !traits::IsTypeOrDerived<T, sem::Type>::value,
349 T>*
350 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000351 AssertNotMoved();
352 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
353 }
354
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000355 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000356 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
357 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000358 /// Types are unique (de-aliased), and so calling create() for the same `T`
359 /// and arguments will return the same pointer.
360 /// @warning Use this method to acquire a type only if all of its type
361 /// information is provided in the constructor arguments `args`.<br>
362 /// If the type requires additional configuration after construction that
363 /// affect its fundamental type, build the type with `std::make_unique`, make
364 /// any necessary alterations and then call unique_type() instead.
365 /// @param args the arguments to pass to the type constructor
366 /// @returns the de-aliased type pointer
367 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000368 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
369 static_assert(std::is_base_of<sem::Type, T>::value,
370 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000371 AssertNotMoved();
372 return types_.Get<T>(std::forward<ARGS>(args)...);
373 }
374
375 /// Marks this builder as moved, preventing any further use of the builder.
376 void MarkAsMoved();
377
378 //////////////////////////////////////////////////////////////////////////////
379 // TypesBuilder
380 //////////////////////////////////////////////////////////////////////////////
381
382 /// TypesBuilder holds basic `tint` types and methods for constructing
383 /// complex types.
384 class TypesBuilder {
385 public:
386 /// Constructor
387 /// @param builder the program builder
388 explicit TypesBuilder(ProgramBuilder* builder);
389
390 /// @return the tint AST type for the C type `T`.
391 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000392 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000393 return CToAST<T>::get(this);
394 }
395
396 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000397 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000398
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000399 /// @param source the Source of the node
400 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000401 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000402 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000403 }
404
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000405 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000406 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000407
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000408 /// @param source the Source of the node
409 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000410 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000411 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000412 }
413
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000414 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000415 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000416
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000417 /// @param source the Source of the node
418 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000419 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000420 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000421 }
422
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000423 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000424 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000425
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000426 /// @param source the Source of the node
427 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000428 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000429 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000430 }
431
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000432 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000433 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000434
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000435 /// @param source the Source of the node
436 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000437 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000438 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000439 }
440
Antonio Maiorano25436862021-04-13 13:32:33 +0000441 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000442 /// @param n vector width in elements
443 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000444 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000445 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000446 }
447
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000448 /// @param source the Source of the node
449 /// @param type vector subtype
450 /// @param n vector width in elements
451 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000452 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000453 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000454 }
455
Antonio Maiorano25436862021-04-13 13:32:33 +0000456 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000457 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000458 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000459
460 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000461 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000462 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000463
464 /// @param type vector subtype
465 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000466 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000467
468 /// @param n vector width in elements
469 /// @return the tint AST type for a `n`-element vector of `type`.
470 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000471 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000472 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000473 }
474
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000475 /// @return the tint AST type for a 2-element vector of the C type `T`.
476 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000477 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000478 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000479 }
480
481 /// @return the tint AST type for a 3-element vector of the C type `T`.
482 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000483 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000484 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000485 }
486
487 /// @return the tint AST type for a 4-element vector of the C type `T`.
488 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000489 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000490 return vec4(Of<T>());
491 }
492
493 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000494 /// @param columns number of columns for the matrix
495 /// @param rows number of rows for the matrix
496 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000497 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000498 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000499 }
500
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000501 /// @param source the Source of the node
502 /// @param type matrix subtype
503 /// @param columns number of columns for the matrix
504 /// @param rows number of rows for the matrix
505 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000506 ast::Matrix* mat(const Source& source,
507 ast::Type* type,
508 uint32_t columns,
509 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000510 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000511 }
512
Ben Claytone204f272021-04-22 14:40:23 +0000513 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000514 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000515 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000516
517 /// @param type matrix subtype
518 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000519 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000520
521 /// @param type matrix subtype
522 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000523 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000524
525 /// @param type matrix subtype
526 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000527 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000528
529 /// @param type matrix subtype
530 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000531 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000532
533 /// @param type matrix subtype
534 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000535 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000536
537 /// @param type matrix subtype
538 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000539 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000540
541 /// @param type matrix subtype
542 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000543 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000544
545 /// @param type matrix subtype
546 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000547 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000548
Ben Claytone204f272021-04-22 14:40:23 +0000549 /// @param columns number of columns for the matrix
550 /// @param rows number of rows for the matrix
551 /// @return the tint AST type for a matrix of `type`
552 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000553 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000554 return mat(Of<T>(), columns, rows);
555 }
556
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000557 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
558 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000559 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000560 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000561 }
562
563 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
564 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000565 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000566 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000567 }
568
569 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
570 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000571 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000572 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000573 }
574
575 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
576 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000577 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000578 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000579 }
580
581 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
582 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000583 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000584 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000585 }
586
587 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
588 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000589 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000590 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000591 }
592
593 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
594 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000595 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000596 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000597 }
598
599 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
600 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000601 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000602 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000603 }
604
605 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
606 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000607 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000608 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000609 }
610
611 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000612 /// @param n the array size. 0 represents a runtime-array
613 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000614 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000615 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000616 uint32_t n = 0,
617 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000618 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000619 }
620
621 /// @param source the Source of the node
622 /// @param subtype the array element type
623 /// @param n the array size. 0 represents a runtime-array
624 /// @param decos the optional decorations for the array
625 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000626 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000627 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000628 uint32_t n = 0,
629 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000630 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000631 }
632
Ben Claytonbab31972021-02-08 21:16:21 +0000633 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000634 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000635 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000636 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000637 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000638 ast::DecorationList decos;
639 if (stride) {
640 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
641 }
642 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000643 }
644
Ben Clayton0f88b312021-05-04 17:36:31 +0000645 /// @param source the Source of the node
646 /// @param subtype the array element type
647 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000648 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000649 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000650 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000651 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000652 uint32_t n,
653 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000654 ast::DecorationList decos;
655 if (stride) {
656 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
657 }
658 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000659 }
660
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000661 /// @return the tint AST type for an array of size `N` of type `T`
662 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000663 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000664 return array(Of<T>(), N);
665 }
666
Ben Claytonbab31972021-02-08 21:16:21 +0000667 /// @param stride the array stride
668 /// @return the tint AST type for an array of size `N` of type `T`
669 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000670 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000671 return array(Of<T>(), N, stride);
672 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000673
Ben Clayton0f88b312021-05-04 17:36:31 +0000674 /// Creates a type name
675 /// @param name the name
676 /// @returns the type name
677 template <typename NAME>
678 ast::TypeName* type_name(NAME&& name) const {
679 return builder->create<ast::TypeName>(
680 builder->Sym(std::forward<NAME>(name)));
681 }
682
683 /// Creates a type name
684 /// @param source the Source of the node
685 /// @param name the name
686 /// @returns the type name
687 template <typename NAME>
688 ast::TypeName* type_name(const Source& source, NAME&& name) const {
689 return builder->create<ast::TypeName>(
690 source, builder->Sym(std::forward<NAME>(name)));
691 }
692
Ben Clayton42d1e092021-02-02 14:29:15 +0000693 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000694 /// @param name the alias name
695 /// @param type the alias type
696 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000697 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000698 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000699 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000700 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000701 }
702
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000703 /// Creates an alias type
704 /// @param source the Source of the node
705 /// @param name the alias name
706 /// @param type the alias type
707 /// @returns the alias pointer
708 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000709 ast::Alias* alias(const Source& source,
710 NAME&& name,
711 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000712 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000713 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000714 }
715
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000716 /// @param type the type of the pointer
717 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000718 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000719 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000720 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000721 ast::StorageClass storage_class,
722 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000723 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000724 }
725
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000726 /// @param source the Source of the node
727 /// @param type the type of the pointer
728 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000729 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000730 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000731 ast::Pointer* pointer(const Source& source,
732 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000733 ast::StorageClass storage_class,
734 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000735 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000736 }
737
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000738 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000739 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000740 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000741 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000742 ast::Pointer* pointer(ast::StorageClass storage_class,
743 ast::Access access = ast::Access::kUndefined) const {
744 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000745 }
746
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000747 /// @param source the Source of the node
748 /// @param type the type of the atomic
749 /// @return the atomic to `type`
750 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
751 return builder->create<ast::Atomic>(source, type);
752 }
753
Ben Clayton313e6182021-06-17 19:56:14 +0000754 /// @param type the type of the atomic
755 /// @return the atomic to `type`
756 ast::Atomic* atomic(ast::Type* type) const {
757 return builder->create<ast::Atomic>(type);
758 }
759
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000760 /// @return the atomic to type `T`
761 template <typename T>
762 ast::Atomic* atomic() const {
763 return atomic(Of<T>());
764 }
765
Ben Claytone204f272021-04-22 14:40:23 +0000766 /// @param kind the kind of sampler
767 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000768 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000769 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000770 }
771
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000772 /// @param source the Source of the node
773 /// @param kind the kind of sampler
774 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000775 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000776 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000777 }
778
Ben Claytone204f272021-04-22 14:40:23 +0000779 /// @param dims the dimensionality of the texture
780 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000781 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000782 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000783 }
784
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000785 /// @param source the Source of the node
786 /// @param dims the dimensionality of the texture
787 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000788 ast::DepthTexture* depth_texture(const Source& source,
789 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000790 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000791 }
792
Ben Claytone204f272021-04-22 14:40:23 +0000793 /// @param dims the dimensionality of the texture
794 /// @param subtype the texture subtype.
795 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000796 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
797 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000798 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000799 }
800
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000801 /// @param source the Source of the node
802 /// @param dims the dimensionality of the texture
803 /// @param subtype the texture subtype.
804 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000805 ast::SampledTexture* sampled_texture(const Source& source,
806 ast::TextureDimension dims,
807 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000808 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000809 }
810
Ben Claytone204f272021-04-22 14:40:23 +0000811 /// @param dims the dimensionality of the texture
812 /// @param subtype the texture subtype.
813 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000814 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
815 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000816 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000817 }
818
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000819 /// @param source the Source of the node
820 /// @param dims the dimensionality of the texture
821 /// @param subtype the texture subtype.
822 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000823 ast::MultisampledTexture* multisampled_texture(const Source& source,
824 ast::TextureDimension dims,
825 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000826 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000827 }
828
Ben Claytone204f272021-04-22 14:40:23 +0000829 /// @param dims the dimensionality of the texture
830 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000831 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000832 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000833 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000834 ast::ImageFormat format,
835 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000836 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000837 return builder->create<ast::StorageTexture>(dims, format, subtype,
838 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000839 }
840
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000841 /// @param source the Source of the node
842 /// @param dims the dimensionality of the texture
843 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000844 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000845 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000846 ast::StorageTexture* storage_texture(const Source& source,
847 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000848 ast::ImageFormat format,
849 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000850 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000851 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
852 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000853 }
854
Brandon Jones7b257692021-05-17 17:40:17 +0000855 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000856 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000857 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000858 }
859
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000860 /// @param source the Source of the node
861 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000862 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000863 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000864 }
865
Ben Clayton19b03192021-05-20 15:04:08 +0000866 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000867 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000868 /// @param type the type
869 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000870 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000871
Ben Clayton8758f102021-06-09 14:32:14 +0000872 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000873 /// @param type the type
874 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000875 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000876
Ben Claytonf5f311e2021-04-28 14:31:23 +0000877 /// The ProgramBuilder
878 ProgramBuilder* const builder;
879
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000880 private:
881 /// CToAST<T> is specialized for various `T` types and each specialization
882 /// contains a single static `get()` method for obtaining the corresponding
883 /// AST type for the C type `T`.
884 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000885 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000886 template <typename T>
887 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000888 };
889
890 //////////////////////////////////////////////////////////////////////////////
891 // AST helper methods
892 //////////////////////////////////////////////////////////////////////////////
893
James Price65ae64d2021-04-29 12:59:14 +0000894 /// @return a new unnamed symbol
895 Symbol Sym() { return Symbols().New(); }
896
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000897 /// @param name the symbol string
898 /// @return a Symbol with the given name
899 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
900
901 /// @param sym the symbol
902 /// @return `sym`
903 Symbol Sym(Symbol sym) { return sym; }
904
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000905 /// @param expr the expression
906 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000907 template <typename T>
908 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
909 return expr;
910 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000911
Ben Claytonb8ea5912021-04-19 16:52:42 +0000912 /// Passthrough for nullptr
913 /// @return nullptr
914 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
915
Ben Claytonfe0910f2021-05-17 15:51:47 +0000916 /// @param source the source information
917 /// @param symbol the identifier symbol
918 /// @return an ast::IdentifierExpression with the given symbol
919 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
920 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000921 }
922
Ben Clayton46d78d72021-02-10 21:34:26 +0000923 /// @param symbol the identifier symbol
924 /// @return an ast::IdentifierExpression with the given symbol
925 ast::IdentifierExpression* Expr(Symbol symbol) {
926 return create<ast::IdentifierExpression>(symbol);
927 }
928
Ben Claytonfe0910f2021-05-17 15:51:47 +0000929 /// @param source the source information
930 /// @param variable the AST variable
931 /// @return an ast::IdentifierExpression with the variable's symbol
932 ast::IdentifierExpression* Expr(const Source& source,
933 ast::Variable* variable) {
934 return create<ast::IdentifierExpression>(source, variable->symbol());
935 }
936
Ben Claytonb8ea5912021-04-19 16:52:42 +0000937 /// @param variable the AST variable
938 /// @return an ast::IdentifierExpression with the variable's symbol
939 ast::IdentifierExpression* Expr(ast::Variable* variable) {
940 return create<ast::IdentifierExpression>(variable->symbol());
941 }
942
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000943 /// @param source the source information
944 /// @param name the identifier name
945 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000946 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
947 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
948 }
949
950 /// @param name the identifier name
951 /// @return an ast::IdentifierExpression with the given name
952 ast::IdentifierExpression* Expr(const char* name) {
953 return create<ast::IdentifierExpression>(Symbols().Register(name));
954 }
955
956 /// @param source the source information
957 /// @param name the identifier name
958 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000959 ast::IdentifierExpression* Expr(const Source& source,
960 const std::string& name) {
961 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
962 }
963
964 /// @param name the identifier name
965 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000966 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000967 return create<ast::IdentifierExpression>(Symbols().Register(name));
968 }
969
Ben Claytonfe0910f2021-05-17 15:51:47 +0000970 /// @param source the source information
971 /// @param value the boolean value
972 /// @return a Scalar constructor for the given value
973 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
974 return create<ast::ScalarConstructorExpression>(source, Literal(value));
975 }
976
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000977 /// @param value the boolean value
978 /// @return a Scalar constructor for the given value
979 ast::ScalarConstructorExpression* Expr(bool value) {
980 return create<ast::ScalarConstructorExpression>(Literal(value));
981 }
982
Ben Claytonfe0910f2021-05-17 15:51:47 +0000983 /// @param source the source information
984 /// @param value the float value
985 /// @return a Scalar constructor for the given value
986 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
987 return create<ast::ScalarConstructorExpression>(source, Literal(value));
988 }
989
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000990 /// @param value the float value
991 /// @return a Scalar constructor for the given value
992 ast::ScalarConstructorExpression* Expr(f32 value) {
993 return create<ast::ScalarConstructorExpression>(Literal(value));
994 }
995
Ben Claytonfe0910f2021-05-17 15:51:47 +0000996 /// @param source the source information
997 /// @param value the integer value
998 /// @return a Scalar constructor for the given value
999 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1000 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1001 }
1002
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001003 /// @param value the integer value
1004 /// @return a Scalar constructor for the given value
1005 ast::ScalarConstructorExpression* Expr(i32 value) {
1006 return create<ast::ScalarConstructorExpression>(Literal(value));
1007 }
1008
Ben Claytonfe0910f2021-05-17 15:51:47 +00001009 /// @param source the source information
1010 /// @param value the unsigned int value
1011 /// @return a Scalar constructor for the given value
1012 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1013 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1014 }
1015
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001016 /// @param value the unsigned int value
1017 /// @return a Scalar constructor for the given value
1018 ast::ScalarConstructorExpression* Expr(u32 value) {
1019 return create<ast::ScalarConstructorExpression>(Literal(value));
1020 }
1021
1022 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1023 /// `list`.
1024 /// @param list the list to append too
1025 /// @param arg the arg to create
1026 template <typename ARG>
1027 void Append(ast::ExpressionList& list, ARG&& arg) {
1028 list.emplace_back(Expr(std::forward<ARG>(arg)));
1029 }
1030
1031 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1032 /// then appends them to `list`.
1033 /// @param list the list to append too
1034 /// @param arg0 the first argument
1035 /// @param args the rest of the arguments
1036 template <typename ARG0, typename... ARGS>
1037 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1038 Append(list, std::forward<ARG0>(arg0));
1039 Append(list, std::forward<ARGS>(args)...);
1040 }
1041
1042 /// @return an empty list of expressions
1043 ast::ExpressionList ExprList() { return {}; }
1044
1045 /// @param args the list of expressions
1046 /// @return the list of expressions converted to `ast::Expression`s using
1047 /// `Expr()`,
1048 template <typename... ARGS>
1049 ast::ExpressionList ExprList(ARGS&&... args) {
1050 ast::ExpressionList list;
1051 list.reserve(sizeof...(args));
1052 Append(list, std::forward<ARGS>(args)...);
1053 return list;
1054 }
1055
1056 /// @param list the list of expressions
1057 /// @return `list`
1058 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1059
1060 /// @param val the boolan value
1061 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001062 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001063
1064 /// @param val the float value
1065 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001066 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001067
1068 /// @param val the unsigned int value
1069 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001070 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001071
1072 /// @param val the integer value
1073 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001074 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001075
1076 /// @param args the arguments for the type constructor
1077 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1078 /// of `args` converted to `ast::Expression`s using `Expr()`
1079 template <typename T, typename... ARGS>
1080 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001081 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001082 }
1083
1084 /// @param type the type to construct
1085 /// @param args the arguments for the constructor
1086 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1087 /// values `args`.
1088 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001089 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001090 return create<ast::TypeConstructorExpression>(
1091 type, ExprList(std::forward<ARGS>(args)...));
1092 }
1093
Ben Clayton313e6182021-06-17 19:56:14 +00001094 /// @param source the source information
1095 /// @param type the type to construct
1096 /// @param args the arguments for the constructor
1097 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1098 /// values `args`.
1099 template <typename... ARGS>
1100 ast::TypeConstructorExpression* Construct(const Source& source,
1101 ast::Type* type,
1102 ARGS&&... args) {
1103 return create<ast::TypeConstructorExpression>(
1104 source, type, ExprList(std::forward<ARGS>(args)...));
1105 }
1106
Ben Claytond9603282021-06-29 12:37:45 +00001107 /// @param expr the expression for the bitcast
1108 /// @return an `ast::BitcastExpression` of type `ty`, with the values of
1109 /// `expr` converted to `ast::Expression`s using `Expr()`
1110 template <typename T, typename EXPR>
1111 ast::BitcastExpression* Bitcast(EXPR&& expr) {
1112 return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
1113 }
1114
1115 /// @param type the type to cast to
1116 /// @param expr the expression for the bitcast
1117 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1118 /// `expr`.
1119 template <typename EXPR>
1120 ast::BitcastExpression* Bitcast(ast::Type* type, EXPR&& expr) {
1121 return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
1122 }
1123
1124 /// @param source the source information
1125 /// @param type the type to cast to
1126 /// @param expr the expression for the bitcast
1127 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1128 /// `expr`.
1129 template <typename EXPR>
1130 ast::BitcastExpression* Bitcast(const Source& source,
1131 ast::Type* type,
1132 EXPR&& expr) {
1133 return create<ast::BitcastExpression>(source, type,
1134 Expr(std::forward<EXPR>(expr)));
1135 }
1136
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001137 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001138 /// @param type the vector type
1139 /// @param size the vector size
1140 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1141 /// type `type`, constructed with the values `args`.
1142 template <typename... ARGS>
1143 ast::TypeConstructorExpression* vec(ast::Type* type,
1144 uint32_t size,
1145 ARGS&&... args) {
1146 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1147 }
1148
1149 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001150 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1151 /// `T`, constructed with the values `args`.
1152 template <typename T, typename... ARGS>
1153 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001154 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001155 }
1156
1157 /// @param args the arguments for the vector constructor
1158 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1159 /// `T`, constructed with the values `args`.
1160 template <typename T, typename... ARGS>
1161 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001162 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001163 }
1164
1165 /// @param args the arguments for the vector constructor
1166 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1167 /// `T`, constructed with the values `args`.
1168 template <typename T, typename... ARGS>
1169 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001170 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001171 }
1172
1173 /// @param args the arguments for the matrix constructor
1174 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1175 /// `T`, constructed with the values `args`.
1176 template <typename T, typename... ARGS>
1177 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001178 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001179 }
1180
1181 /// @param args the arguments for the matrix constructor
1182 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1183 /// `T`, constructed with the values `args`.
1184 template <typename T, typename... ARGS>
1185 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001186 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001187 }
1188
1189 /// @param args the arguments for the matrix constructor
1190 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1191 /// `T`, constructed with the values `args`.
1192 template <typename T, typename... ARGS>
1193 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001194 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001195 }
1196
1197 /// @param args the arguments for the matrix constructor
1198 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1199 /// `T`, constructed with the values `args`.
1200 template <typename T, typename... ARGS>
1201 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001202 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001203 }
1204
1205 /// @param args the arguments for the matrix constructor
1206 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1207 /// `T`, constructed with the values `args`.
1208 template <typename T, typename... ARGS>
1209 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001210 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001211 }
1212
1213 /// @param args the arguments for the matrix constructor
1214 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1215 /// `T`, constructed with the values `args`.
1216 template <typename T, typename... ARGS>
1217 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001218 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001219 }
1220
1221 /// @param args the arguments for the matrix constructor
1222 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1223 /// `T`, constructed with the values `args`.
1224 template <typename T, typename... ARGS>
1225 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001226 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227 }
1228
1229 /// @param args the arguments for the matrix constructor
1230 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1231 /// `T`, constructed with the values `args`.
1232 template <typename T, typename... ARGS>
1233 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001234 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001235 }
1236
1237 /// @param args the arguments for the matrix constructor
1238 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1239 /// `T`, constructed with the values `args`.
1240 template <typename T, typename... ARGS>
1241 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001242 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001243 }
1244
1245 /// @param args the arguments for the array constructor
1246 /// @return an `ast::TypeConstructorExpression` of an array with element type
1247 /// `T`, constructed with the values `args`.
1248 template <typename T, int N = 0, typename... ARGS>
1249 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001250 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001251 }
1252
1253 /// @param subtype the array element type
1254 /// @param n the array size. 0 represents a runtime-array.
1255 /// @param args the arguments for the array constructor
1256 /// @return an `ast::TypeConstructorExpression` of an array with element type
1257 /// `subtype`, constructed with the values `args`.
1258 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001259 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001260 uint32_t n,
1261 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001262 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001263 }
1264
1265 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001266 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001267 /// @param optional the optional variable settings.
1268 /// Can be any of the following, in any order:
1269 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001270 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001271 /// * ast::Expression* - specifies the variable's initializer expression
1272 /// * ast::DecorationList - specifies the variable's decorations
1273 /// Note that repeated arguments of the same type will use the last argument's
1274 /// value.
1275 /// @returns a `ast::Variable` with the given name, type and additional
1276 /// options
1277 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001278 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001279 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001280 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001281 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1282 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001283 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001284 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001285 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001286
1287 /// @param source the variable source
1288 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001289 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001290 /// @param optional the optional variable settings.
1291 /// Can be any of the following, in any order:
1292 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001293 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001294 /// * ast::Expression* - specifies the variable's initializer expression
1295 /// * ast::DecorationList - specifies the variable's decorations
1296 /// Note that repeated arguments of the same type will use the last argument's
1297 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001298 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001299 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001300 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001301 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001302 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001303 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001304 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1305 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001306 opts.storage, opts.access, type, false,
1307 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001308 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001309
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001310 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001311 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001312 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001313 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001314 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001315 template <typename NAME>
1316 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001317 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001318 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001319 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001320 return create<ast::Variable>(
1321 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1322 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001323 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001324
1325 /// @param source the variable source
1326 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001327 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001328 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001329 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001330 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001331 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001332 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001333 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001334 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001335 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001336 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001337 return create<ast::Variable>(
1338 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1339 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001340 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001341
James Pricedaf1f1c2021-04-08 22:15:48 +00001342 /// @param name the parameter name
1343 /// @param type the parameter type
1344 /// @param decorations optional parameter decorations
1345 /// @returns a constant `ast::Variable` with the given name and type
1346 template <typename NAME>
1347 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001348 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001349 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001350 return create<ast::Variable>(
1351 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1352 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001353 }
1354
1355 /// @param source the parameter source
1356 /// @param name the parameter name
1357 /// @param type the parameter type
1358 /// @param decorations optional parameter decorations
1359 /// @returns a constant `ast::Variable` with the given name and type
1360 template <typename NAME>
1361 ast::Variable* Param(const Source& source,
1362 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001363 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001364 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001365 return create<ast::Variable>(
1366 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1367 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001368 }
1369
Ben Clayton42708342021-04-21 17:55:12 +00001370 /// @param name the variable name
1371 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001372 /// @param optional the optional variable settings.
1373 /// Can be any of the following, in any order:
1374 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001375 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001376 /// * ast::Expression* - specifies the variable's initializer expression
1377 /// * ast::DecorationList - specifies the variable's decorations
1378 /// Note that repeated arguments of the same type will use the last argument's
1379 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001380 /// @returns a new `ast::Variable`, which is automatically registered as a
1381 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001382 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001383 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001384 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001385 OPTIONAL&&... optional) {
1386 auto* var = Var(std::forward<NAME>(name), type,
1387 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001388 AST().AddGlobalVariable(var);
1389 return var;
1390 }
1391
1392 /// @param source the variable source
1393 /// @param name the variable name
1394 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001395 /// @param optional the optional variable settings.
1396 /// Can be any of the following, in any order:
1397 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001398 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001399 /// * ast::Expression* - specifies the variable's initializer expression
1400 /// * ast::DecorationList - specifies the variable's decorations
1401 /// Note that repeated arguments of the same type will use the last argument's
1402 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001403 /// @returns a new `ast::Variable`, which is automatically registered as a
1404 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001405 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001406 ast::Variable* Global(const Source& source,
1407 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001408 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001409 OPTIONAL&&... optional) {
1410 auto* var = Var(source, std::forward<NAME>(name), type,
1411 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001412 AST().AddGlobalVariable(var);
1413 return var;
1414 }
1415
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001416 /// @param name the variable name
1417 /// @param type the variable type
1418 /// @param constructor constructor expression
1419 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001420 /// @returns a const `ast::Variable` constructed by calling Var() with the
1421 /// arguments of `args`, which is automatically registered as a global
1422 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001423 template <typename NAME>
1424 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001425 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001426 ast::Expression* constructor,
1427 ast::DecorationList decorations = {}) {
1428 auto* var = Const(std::forward<NAME>(name), type, constructor,
1429 std::move(decorations));
1430 AST().AddGlobalVariable(var);
1431 return var;
1432 }
1433
1434 /// @param source the variable source
1435 /// @param name the variable name
1436 /// @param type the variable type
1437 /// @param constructor constructor expression
1438 /// @param decorations optional variable decorations
1439 /// @returns a const `ast::Variable` constructed by calling Var() with the
1440 /// arguments of `args`, which is automatically registered as a global
1441 /// variable with the ast::Module.
1442 template <typename NAME>
1443 ast::Variable* GlobalConst(const Source& source,
1444 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001445 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001446 ast::Expression* constructor,
1447 ast::DecorationList decorations = {}) {
1448 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1449 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001450 AST().AddGlobalVariable(var);
1451 return var;
1452 }
1453
Ben Claytonfe0910f2021-05-17 15:51:47 +00001454 /// @param source the source information
1455 /// @param expr the expression to take the address of
1456 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1457 template <typename EXPR>
1458 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1459 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1460 Expr(std::forward<EXPR>(expr)));
1461 }
1462
1463 /// @param expr the expression to take the address of
1464 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1465 template <typename EXPR>
1466 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1467 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1468 Expr(std::forward<EXPR>(expr)));
1469 }
1470
1471 /// @param source the source information
1472 /// @param expr the expression to perform an indirection on
1473 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1474 template <typename EXPR>
1475 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1476 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1477 Expr(std::forward<EXPR>(expr)));
1478 }
1479
1480 /// @param expr the expression to perform an indirection on
1481 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1482 template <typename EXPR>
1483 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1484 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1485 Expr(std::forward<EXPR>(expr)));
1486 }
1487
Antonio Maiorano14b34032021-06-09 20:17:59 +00001488 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001489 /// @param func the function name
1490 /// @param args the function call arguments
1491 /// @returns a `ast::CallExpression` to the function `func`, with the
1492 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1493 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001494 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1495 return create<ast::CallExpression>(source, Expr(func),
1496 ExprList(std::forward<ARGS>(args)...));
1497 }
1498
1499 /// @param func the function name
1500 /// @param args the function call arguments
1501 /// @returns a `ast::CallExpression` to the function `func`, with the
1502 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1503 template <typename NAME,
1504 typename... ARGS,
1505 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001506 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1507 return create<ast::CallExpression>(Expr(func),
1508 ExprList(std::forward<ARGS>(args)...));
1509 }
1510
Ben Clayton7fe01062021-06-11 13:22:27 +00001511 /// @param expr the expression to ignore
1512 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1513 /// passed the single `expr` argument
1514 template <typename EXPR>
1515 ast::CallStatement* Ignore(EXPR&& expr) {
1516 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1517 }
1518
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001519 /// @param lhs the left hand argument to the addition operation
1520 /// @param rhs the right hand argument to the addition operation
1521 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1522 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001523 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001524 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1525 Expr(std::forward<LHS>(lhs)),
1526 Expr(std::forward<RHS>(rhs)));
1527 }
1528
James Pricec32e8f62021-06-22 20:08:29 +00001529 /// @param lhs the left hand argument to the and operation
1530 /// @param rhs the right hand argument to the and operation
1531 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1532 template <typename LHS, typename RHS>
1533 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1534 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1535 Expr(std::forward<LHS>(lhs)),
1536 Expr(std::forward<RHS>(rhs)));
1537 }
1538
Ben Claytond9603282021-06-29 12:37:45 +00001539 /// @param lhs the left hand argument to the or operation
1540 /// @param rhs the right hand argument to the or operation
1541 /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
1542 template <typename LHS, typename RHS>
1543 ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
1544 return create<ast::BinaryExpression>(ast::BinaryOp::kOr,
1545 Expr(std::forward<LHS>(lhs)),
1546 Expr(std::forward<RHS>(rhs)));
1547 }
1548
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001549 /// @param lhs the left hand argument to the subtraction operation
1550 /// @param rhs the right hand argument to the subtraction operation
1551 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1552 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001553 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001554 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1555 Expr(std::forward<LHS>(lhs)),
1556 Expr(std::forward<RHS>(rhs)));
1557 }
1558
1559 /// @param lhs the left hand argument to the multiplication operation
1560 /// @param rhs the right hand argument to the multiplication operation
1561 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1562 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001563 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001564 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1565 Expr(std::forward<LHS>(lhs)),
1566 Expr(std::forward<RHS>(rhs)));
1567 }
1568
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001569 /// @param source the source information
1570 /// @param lhs the left hand argument to the multiplication operation
1571 /// @param rhs the right hand argument to the multiplication operation
1572 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1573 template <typename LHS, typename RHS>
1574 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1575 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1576 Expr(std::forward<LHS>(lhs)),
1577 Expr(std::forward<RHS>(rhs)));
1578 }
1579
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001580 /// @param lhs the left hand argument to the division operation
1581 /// @param rhs the right hand argument to the division operation
1582 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1583 template <typename LHS, typename RHS>
1584 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1585 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1586 Expr(std::forward<LHS>(lhs)),
1587 Expr(std::forward<RHS>(rhs)));
1588 }
1589
Ben Claytond9603282021-06-29 12:37:45 +00001590 /// @param lhs the left hand argument to the bit shift right operation
1591 /// @param rhs the right hand argument to the bit shift right operation
1592 /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
1593 template <typename LHS, typename RHS>
1594 ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
1595 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftRight,
1596 Expr(std::forward<LHS>(lhs)),
1597 Expr(std::forward<RHS>(rhs)));
1598 }
1599
1600 /// @param lhs the left hand argument to the bit shift left operation
1601 /// @param rhs the right hand argument to the bit shift left operation
1602 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
1603 template <typename LHS, typename RHS>
1604 ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
1605 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftLeft,
1606 Expr(std::forward<LHS>(lhs)),
1607 Expr(std::forward<RHS>(rhs)));
1608 }
1609
Ben Clayton0597a2b2021-06-16 09:19:36 +00001610 /// @param source the source information
1611 /// @param arr the array argument for the array accessor expression
1612 /// @param idx the index argument for the array accessor expression
1613 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1614 template <typename ARR, typename IDX>
1615 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1616 ARR&& arr,
1617 IDX&& idx) {
1618 return create<ast::ArrayAccessorExpression>(
1619 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1620 }
1621
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001622 /// @param arr the array argument for the array accessor expression
1623 /// @param idx the index argument for the array accessor expression
1624 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1625 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001626 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001627 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1628 Expr(std::forward<IDX>(idx)));
1629 }
1630
1631 /// @param obj the object for the member accessor expression
1632 /// @param idx the index argument for the array accessor expression
1633 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1634 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001635 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001636 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1637 Expr(std::forward<IDX>(idx)));
1638 }
1639
Ben Clayton42d1e092021-02-02 14:29:15 +00001640 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001641 /// @param val the offset value
1642 /// @returns the offset decoration pointer
1643 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1644 return create<ast::StructMemberOffsetDecoration>(source_, val);
1645 }
1646
Ben Claytond614dd52021-03-15 10:43:11 +00001647 /// Creates a ast::StructMemberSizeDecoration
1648 /// @param source the source information
1649 /// @param val the size value
1650 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001651 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1652 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001653 return create<ast::StructMemberSizeDecoration>(source, val);
1654 }
1655
1656 /// Creates a ast::StructMemberSizeDecoration
1657 /// @param val the size value
1658 /// @returns the size decoration pointer
1659 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1660 return create<ast::StructMemberSizeDecoration>(source_, val);
1661 }
1662
1663 /// Creates a ast::StructMemberAlignDecoration
1664 /// @param source the source information
1665 /// @param val the align value
1666 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001667 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1668 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001669 return create<ast::StructMemberAlignDecoration>(source, val);
1670 }
1671
1672 /// Creates a ast::StructMemberAlignDecoration
1673 /// @param val the align value
1674 /// @returns the align decoration pointer
1675 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1676 return create<ast::StructMemberAlignDecoration>(source_, val);
1677 }
1678
Ben Clayton42d1e092021-02-02 14:29:15 +00001679 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001680 /// @param source the source information
1681 /// @param name the function name
1682 /// @param params the function parameters
1683 /// @param type the function return type
1684 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001685 /// @param decorations the optional function decorations
1686 /// @param return_type_decorations the optional function return type
1687 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001688 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001689 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001690 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001691 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001692 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001693 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001694 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001695 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001696 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001697 auto* func =
1698 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1699 type, create<ast::BlockStatement>(body),
1700 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001701 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001702 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001703 }
1704
Ben Clayton42d1e092021-02-02 14:29:15 +00001705 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001706 /// @param name the function name
1707 /// @param params the function parameters
1708 /// @param type the function return type
1709 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001710 /// @param decorations the optional function decorations
1711 /// @param return_type_decorations the optional function return type
1712 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001713 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001714 template <typename NAME>
1715 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001716 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001717 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001718 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001719 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001720 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001721 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1722 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001723 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001724 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001725 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001726 }
1727
Ben Clayton49668bb2021-04-17 05:32:01 +00001728 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001729 /// @param source the source information
1730 /// @returns the return statement pointer
1731 ast::ReturnStatement* Return(const Source& source) {
1732 return create<ast::ReturnStatement>(source);
1733 }
1734
1735 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001736 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001737 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1738
1739 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001740 /// @param source the source information
1741 /// @param val the return value
1742 /// @returns the return statement pointer
1743 template <typename EXPR>
1744 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1745 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1746 }
1747
1748 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001749 /// @param val the return value
1750 /// @returns the return statement pointer
1751 template <typename EXPR>
1752 ast::ReturnStatement* Return(EXPR&& val) {
1753 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001754 }
1755
Ben Clayton950809f2021-06-09 14:32:14 +00001756 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001757 /// @param source the source information
1758 /// @param name the alias name
1759 /// @param type the alias target type
1760 /// @returns the alias type
1761 template <typename NAME>
1762 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1763 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001764 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001765 return out;
1766 }
1767
Ben Clayton950809f2021-06-09 14:32:14 +00001768 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001769 /// @param name the alias name
1770 /// @param type the alias target type
1771 /// @returns the alias type
1772 template <typename NAME>
1773 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1774 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001775 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001776 return out;
1777 }
1778
Ben Clayton950809f2021-06-09 14:32:14 +00001779 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001780 /// @param source the source information
1781 /// @param name the struct name
1782 /// @param members the struct members
1783 /// @param decorations the optional struct decorations
1784 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001785 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001786 ast::Struct* Structure(const Source& source,
1787 NAME&& name,
1788 ast::StructMemberList members,
1789 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001790 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001791 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001792 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001793 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001794 return type;
1795 }
1796
Ben Clayton950809f2021-06-09 14:32:14 +00001797 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001798 /// @param name the struct name
1799 /// @param members the struct members
1800 /// @param decorations the optional struct decorations
1801 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001802 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001803 ast::Struct* Structure(NAME&& name,
1804 ast::StructMemberList members,
1805 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001806 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001807 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001808 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001809 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001810 return type;
1811 }
1812
Ben Clayton42d1e092021-02-02 14:29:15 +00001813 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001814 /// @param source the source information
1815 /// @param name the struct member name
1816 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001817 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001818 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001819 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001820 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001821 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001822 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001823 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001824 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1825 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001826 }
1827
Ben Clayton42d1e092021-02-02 14:29:15 +00001828 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001829 /// @param name the struct member name
1830 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001831 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001832 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001833 template <typename NAME>
1834 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001835 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001836 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001837 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1838 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001839 }
1840
Ben Claytonbab31972021-02-08 21:16:21 +00001841 /// Creates a ast::StructMember with the given byte offset
1842 /// @param offset the offset to use in the StructMemberOffsetDecoration
1843 /// @param name the struct member name
1844 /// @param type the struct member type
1845 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001846 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001847 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001848 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001849 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001850 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001851 create<ast::StructMemberOffsetDecoration>(offset),
1852 });
1853 }
1854
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001855 /// Creates a ast::BlockStatement with input statements
1856 /// @param statements statements of block
1857 /// @returns the block statement pointer
1858 template <typename... Statements>
1859 ast::BlockStatement* Block(Statements&&... statements) {
1860 return create<ast::BlockStatement>(
1861 ast::StatementList{std::forward<Statements>(statements)...});
1862 }
1863
1864 /// Creates a ast::ElseStatement with input condition and body
1865 /// @param condition the else condition expression
1866 /// @param body the else body
1867 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001868 template <typename CONDITION>
1869 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1870 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1871 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001872 }
1873
1874 /// Creates a ast::IfStatement with input condition, body, and optional
1875 /// variadic else statements
1876 /// @param condition the if statement condition expression
1877 /// @param body the if statement body
1878 /// @param elseStatements optional variadic else statements
1879 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001880 template <typename CONDITION, typename... ELSE_STATEMENTS>
1881 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001882 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001883 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001884 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001885 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001886 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001887 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001888 }
1889
1890 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001891 /// @param source the source information
1892 /// @param lhs the left hand side expression initializer
1893 /// @param rhs the right hand side expression initializer
1894 /// @returns the assignment statement pointer
1895 template <typename LhsExpressionInit, typename RhsExpressionInit>
1896 ast::AssignmentStatement* Assign(const Source& source,
1897 LhsExpressionInit&& lhs,
1898 RhsExpressionInit&& rhs) {
1899 return create<ast::AssignmentStatement>(
1900 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1901 Expr(std::forward<RhsExpressionInit>(rhs)));
1902 }
1903
1904 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001905 /// @param lhs the left hand side expression initializer
1906 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001907 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001908 template <typename LhsExpressionInit, typename RhsExpressionInit>
1909 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1910 RhsExpressionInit&& rhs) {
1911 return create<ast::AssignmentStatement>(
1912 Expr(std::forward<LhsExpressionInit>(lhs)),
1913 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001914 }
1915
1916 /// Creates a ast::LoopStatement with input body and optional continuing
1917 /// @param body the loop body
1918 /// @param continuing the optional continuing block
1919 /// @returns the loop statement pointer
1920 ast::LoopStatement* Loop(ast::BlockStatement* body,
1921 ast::BlockStatement* continuing = nullptr) {
1922 return create<ast::LoopStatement>(body, continuing);
1923 }
1924
Ben Clayton65cd2592021-07-02 19:27:42 +00001925 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1926 /// condition and continuing.
1927 /// @param source the source information
1928 /// @param init the optional loop initializer
1929 /// @param cond the optional loop condition
1930 /// @param cont the optional loop continuing
1931 /// @param body the loop body
1932 /// @returns the for loop statement pointer
1933 template <typename COND>
1934 ast::ForLoopStatement* For(const Source& source,
1935 ast::Statement* init,
1936 COND&& cond,
1937 ast::Statement* cont,
1938 ast::BlockStatement* body) {
1939 return create<ast::ForLoopStatement>(
1940 source, init, Expr(std::forward<COND>(cond)), cont, body);
1941 }
1942
1943 /// Creates a ast::ForLoopStatement with input body and optional initializer,
1944 /// condition and continuing.
1945 /// @param init the optional loop initializer
1946 /// @param cond the optional loop condition
1947 /// @param cont the optional loop continuing
1948 /// @param body the loop body
1949 /// @returns the for loop statement pointer
1950 template <typename COND>
1951 ast::ForLoopStatement* For(ast::Statement* init,
1952 COND&& cond,
1953 ast::Statement* cont,
1954 ast::BlockStatement* body) {
1955 return create<ast::ForLoopStatement>(init, Expr(std::forward<COND>(cond)),
1956 cont, body);
1957 }
1958
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001959 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001960 /// @param source the source information
1961 /// @param var the variable to wrap in a decl statement
1962 /// @returns the variable decl statement pointer
1963 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1964 return create<ast::VariableDeclStatement>(source, var);
1965 }
1966
1967 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001968 /// @param var the variable to wrap in a decl statement
1969 /// @returns the variable decl statement pointer
1970 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1971 return create<ast::VariableDeclStatement>(var);
1972 }
1973
Antonio Maioranocea744d2021-03-25 12:55:27 +00001974 /// Creates a ast::SwitchStatement with input expression and cases
1975 /// @param condition the condition expression initializer
1976 /// @param cases case statements
1977 /// @returns the switch statement pointer
1978 template <typename ExpressionInit, typename... Cases>
1979 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1980 return create<ast::SwitchStatement>(
1981 Expr(std::forward<ExpressionInit>(condition)),
1982 ast::CaseStatementList{std::forward<Cases>(cases)...});
1983 }
1984
1985 /// Creates a ast::CaseStatement with input list of selectors, and body
1986 /// @param selectors list of selectors
1987 /// @param body the case body
1988 /// @returns the case statement pointer
1989 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1990 ast::BlockStatement* body = nullptr) {
1991 return create<ast::CaseStatement>(std::move(selectors),
1992 body ? body : Block());
1993 }
1994
1995 /// Convenient overload that takes a single selector
1996 /// @param selector a single case selector
1997 /// @param body the case body
1998 /// @returns the case statement pointer
1999 ast::CaseStatement* Case(ast::IntLiteral* selector,
2000 ast::BlockStatement* body = nullptr) {
2001 return Case(ast::CaseSelectorList{selector}, body);
2002 }
2003
2004 /// Convenience function that creates a 'default' ast::CaseStatement
2005 /// @param body the case body
2006 /// @returns the case statement pointer
2007 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
2008 return Case(ast::CaseSelectorList{}, body);
2009 }
2010
James Price68f558f2021-04-06 15:51:47 +00002011 /// Creates an ast::BuiltinDecoration
2012 /// @param source the source information
2013 /// @param builtin the builtin value
2014 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002015 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00002016 return create<ast::BuiltinDecoration>(source, builtin);
2017 }
2018
2019 /// Creates an ast::BuiltinDecoration
2020 /// @param builtin the builtin value
2021 /// @returns the builtin decoration pointer
2022 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
2023 return create<ast::BuiltinDecoration>(source_, builtin);
2024 }
2025
James Price989a8e42021-06-28 23:04:43 +00002026 /// Creates an ast::InterpolateDecoration
2027 /// @param source the source information
2028 /// @param type the interpolation type
2029 /// @param sampling the interpolation sampling
2030 /// @returns the interpolate decoration pointer
2031 ast::InterpolateDecoration* Interpolate(const Source& source,
2032 ast::InterpolationType type,
2033 ast::InterpolationSampling sampling) {
2034 return create<ast::InterpolateDecoration>(source, type, sampling);
2035 }
2036
2037 /// Creates an ast::InterpolateDecoration
2038 /// @param type the interpolation type
2039 /// @param sampling the interpolation sampling
2040 /// @returns the interpolate decoration pointer
2041 ast::InterpolateDecoration* Interpolate(ast::InterpolationType type,
2042 ast::InterpolationSampling sampling) {
2043 return create<ast::InterpolateDecoration>(source_, type, sampling);
2044 }
2045
James Price508d2492021-07-12 12:12:32 +00002046 /// Creates an ast::InvariantDecoration
2047 /// @param source the source information
2048 /// @returns the invariant decoration pointer
2049 ast::InvariantDecoration* Invariant(const Source& source) {
2050 return create<ast::InvariantDecoration>(source);
2051 }
2052
2053 /// Creates an ast::InvariantDecoration
2054 /// @returns the invariant decoration pointer
2055 ast::InvariantDecoration* Invariant() {
2056 return create<ast::InvariantDecoration>(source_);
2057 }
2058
James Price68f558f2021-04-06 15:51:47 +00002059 /// Creates an ast::LocationDecoration
2060 /// @param source the source information
2061 /// @param location the location value
2062 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002063 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00002064 return create<ast::LocationDecoration>(source, location);
2065 }
2066
2067 /// Creates an ast::LocationDecoration
2068 /// @param location the location value
2069 /// @returns the location decoration pointer
2070 ast::LocationDecoration* Location(uint32_t location) {
2071 return create<ast::LocationDecoration>(source_, location);
2072 }
2073
James Pricef2f3bfc2021-05-13 20:32:32 +00002074 /// Creates an ast::OverrideDecoration with a specific constant ID
2075 /// @param source the source information
2076 /// @param id the id value
2077 /// @returns the override decoration pointer
2078 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
2079 return create<ast::OverrideDecoration>(source, id);
2080 }
2081
2082 /// Creates an ast::OverrideDecoration with a specific constant ID
2083 /// @param id the optional id value
2084 /// @returns the override decoration pointer
2085 ast::OverrideDecoration* Override(uint32_t id) {
2086 return Override(source_, id);
2087 }
2088
2089 /// Creates an ast::OverrideDecoration without a constant ID
2090 /// @param source the source information
2091 /// @returns the override decoration pointer
2092 ast::OverrideDecoration* Override(const Source& source) {
2093 return create<ast::OverrideDecoration>(source);
2094 }
2095
2096 /// Creates an ast::OverrideDecoration without a constant ID
2097 /// @returns the override decoration pointer
2098 ast::OverrideDecoration* Override() { return Override(source_); }
2099
James Price68f558f2021-04-06 15:51:47 +00002100 /// Creates an ast::StageDecoration
2101 /// @param source the source information
2102 /// @param stage the pipeline stage
2103 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002104 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00002105 return create<ast::StageDecoration>(source, stage);
2106 }
2107
2108 /// Creates an ast::StageDecoration
2109 /// @param stage the pipeline stage
2110 /// @returns the stage decoration pointer
2111 ast::StageDecoration* Stage(ast::PipelineStage stage) {
2112 return create<ast::StageDecoration>(source_, stage);
2113 }
2114
James Price70f80bb2021-05-19 13:40:08 +00002115 /// Creates an ast::WorkgroupDecoration
2116 /// @param x the x dimension expression
2117 /// @returns the workgroup decoration pointer
2118 template <typename EXPR_X>
2119 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
2120 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
2121 }
2122
2123 /// Creates an ast::WorkgroupDecoration
2124 /// @param x the x dimension expression
2125 /// @param y the y dimension expression
2126 /// @returns the workgroup decoration pointer
2127 template <typename EXPR_X, typename EXPR_Y>
2128 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
2129 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
2130 nullptr);
2131 }
2132
2133 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002134 /// @param source the source information
2135 /// @param x the x dimension expression
2136 /// @param y the y dimension expression
2137 /// @param z the z dimension expression
2138 /// @returns the workgroup decoration pointer
2139 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2140 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2141 EXPR_X&& x,
2142 EXPR_Y&& y,
2143 EXPR_Z&& z) {
2144 return create<ast::WorkgroupDecoration>(
2145 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2146 Expr(std::forward<EXPR_Z>(z)));
2147 }
2148
2149 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002150 /// @param x the x dimension expression
2151 /// @param y the y dimension expression
2152 /// @param z the z dimension expression
2153 /// @returns the workgroup decoration pointer
2154 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2155 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2156 return create<ast::WorkgroupDecoration>(
2157 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2158 Expr(std::forward<EXPR_Z>(z)));
2159 }
2160
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002161 /// Sets the current builder source to `src`
2162 /// @param src the Source used for future create() calls
2163 void SetSource(const Source& src) {
2164 AssertNotMoved();
2165 source_ = src;
2166 }
2167
2168 /// Sets the current builder source to `loc`
2169 /// @param loc the Source used for future create() calls
2170 void SetSource(const Source::Location& loc) {
2171 AssertNotMoved();
2172 source_ = Source(loc);
2173 }
2174
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002175 /// Marks that the given transform has been applied to this program.
2176 /// @param transform the transform that has been applied
2177 void SetTransformApplied(const CastableBase* transform) {
2178 transforms_applied_.emplace(&transform->TypeInfo());
2179 }
2180
2181 /// Marks that the given transform `T` has been applied to this program.
2182 template <typename T>
2183 void SetTransformApplied() {
2184 transforms_applied_.emplace(&TypeInfo::Of<T>());
2185 }
2186
2187 /// Marks that the transforms with the given TypeInfos have been applied to
2188 /// this program.
2189 /// @param transforms the set of transform TypeInfos that has been applied
2190 void SetTransformApplied(
2191 const std::unordered_set<const TypeInfo*>& transforms) {
2192 for (auto* transform : transforms) {
2193 transforms_applied_.emplace(transform);
2194 }
2195 }
2196
2197 /// @returns true if the transform of type `T` was applied.
2198 template <typename T>
2199 bool HasTransformApplied() {
2200 return transforms_applied_.count(&TypeInfo::Of<T>());
2201 }
2202
2203 /// @return the TypeInfo pointers of all transforms that have been applied to
2204 /// this program.
2205 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2206 return transforms_applied_;
2207 }
2208
Ben Clayton33352542021-01-29 16:43:41 +00002209 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002210 /// @note As the Resolver is run when the Program is built, this will only be
2211 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002212 /// @param expr the AST expression
2213 /// @return the resolved semantic type for the expression, or nullptr if the
2214 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002215 sem::Type* TypeOf(const ast::Expression* expr) const;
2216
Ben Claytonfe0910f2021-05-17 15:51:47 +00002217 /// Helper for returning the resolved semantic type of the variable `var`.
2218 /// @note As the Resolver is run when the Program is built, this will only be
2219 /// useful for the Resolver itself and tests that use their own Resolver.
2220 /// @param var the AST variable
2221 /// @return the resolved semantic type for the variable, or nullptr if the
2222 /// variable has no resolved type.
2223 sem::Type* TypeOf(const ast::Variable* var) const;
2224
Ben Claytonfbec46f2021-04-30 20:20:19 +00002225 /// Helper for returning the resolved semantic type of the AST type `type`.
2226 /// @note As the Resolver is run when the Program is built, this will only be
2227 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002228 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002229 /// @return the resolved semantic type for the type, or nullptr if the type
2230 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002231 const sem::Type* TypeOf(const ast::Type* type) const;
2232
2233 /// Helper for returning the resolved semantic type of the AST type
2234 /// declaration `type_decl`.
2235 /// @note As the Resolver is run when the Program is built, this will only be
2236 /// useful for the Resolver itself and tests that use their own Resolver.
2237 /// @param type_decl the AST type declaration
2238 /// @return the resolved semantic type for the type declaration, or nullptr if
2239 /// the type declaration has no resolved type.
2240 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002241
Ben Clayton169512e2021-04-17 05:52:11 +00002242 /// Wraps the ast::Literal in a statement. This is used by tests that
2243 /// construct a partial AST and require the Resolver to reach these
2244 /// nodes.
2245 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2246 /// @return the ast::Statement that wraps the ast::Statement
2247 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002248 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002249 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002250 /// nodes.
2251 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2252 /// @return the ast::Statement that wraps the ast::Expression
2253 ast::Statement* WrapInStatement(ast::Expression* expr);
2254 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002255 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002256 /// these nodes.
2257 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2258 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2259 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2260 /// Returns the statement argument. Used as a passthrough-overload by
2261 /// WrapInFunction().
2262 /// @param stmt the ast::Statement
2263 /// @return `stmt`
2264 ast::Statement* WrapInStatement(ast::Statement* stmt);
2265 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002266 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002267 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002268 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002269 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002270 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002271 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002272 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002273 }
2274 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002275 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002276 /// @returns the function
2277 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002278
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002279 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002280 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002281
2282 protected:
2283 /// Asserts that the builder has not been moved.
2284 void AssertNotMoved() const;
2285
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002286 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002287 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002288 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002289 ASTNodeAllocator ast_nodes_;
2290 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002291 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002292 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002293 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002294 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002295 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002296
2297 /// The source to use when creating AST nodes without providing a Source as
2298 /// the first argument.
2299 Source source_;
2300
Ben Clayton5f0ea112021-03-09 10:54:37 +00002301 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002302 /// program when built.
2303 bool resolve_on_build_ = true;
2304
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002305 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2306 bool moved_ = false;
2307};
2308
2309//! @cond Doxygen_Suppress
2310// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2311template <>
2312struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002313 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002314 return t->i32();
2315 }
2316};
2317template <>
2318struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002319 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002320 return t->u32();
2321 }
2322};
2323template <>
2324struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002325 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002326 return t->f32();
2327 }
2328};
2329template <>
2330struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002331 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002332 return t->bool_();
2333 }
2334};
2335template <>
2336struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002337 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002338 return t->void_();
2339 }
2340};
2341//! @endcond
2342
Ben Clayton917b14b2021-04-19 16:50:23 +00002343/// @param builder the ProgramBuilder
2344/// @returns the ProgramID of the ProgramBuilder
2345inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2346 return builder->ID();
2347}
2348
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002349} // namespace tint
2350
2351#endif // SRC_PROGRAM_BUILDER_H_