blob: b5bd0764aa941a94a0ab727e7b76f7a92adabd8d [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 Claytona922e652021-04-21 13:37:22 +000038#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000039#include "src/ast/if_statement.h"
James Price989a8e42021-06-28 23:04:43 +000040#include "src/ast/interpolate_decoration.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000041#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000042#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000043#include "src/ast/member_accessor_expression.h"
44#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000045#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000046#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000047#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000048#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000049#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000050#include "src/ast/scalar_constructor_expression.h"
51#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000052#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000053#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000054#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000055#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000056#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000057#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000058#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000059#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000060#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000061#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000062#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000063#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000064#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000065#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000066#include "src/ast/void.h"
James Price70f80bb2021-05-19 13:40:08 +000067#include "src/ast/workgroup_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000068#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000069#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000070#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000071#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000072#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000073#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000074#include "src/sem/f32_type.h"
75#include "src/sem/i32_type.h"
76#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000077#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000078#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000079#include "src/sem/sampled_texture_type.h"
80#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000081#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000082#include "src/sem/u32_type.h"
83#include "src/sem/vector_type.h"
84#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000085
Ben Clayton03d10722021-06-05 12:45:50 +000086#ifdef INCLUDE_TINT_TINT_H_
87#error "internal tint header being #included from tint.h"
88#endif
89
Ben Clayton401b96b2021-02-03 17:19:59 +000090// Forward declarations
Ben Claytonb5cd10c2021-06-25 10:26:26 +000091namespace tint {
Ben Clayton401b96b2021-02-03 17:19:59 +000092namespace ast {
93class VariableDeclStatement;
94} // namespace ast
Ben Claytonb5cd10c2021-06-25 10:26:26 +000095} // namespace tint
Ben Clayton401b96b2021-02-03 17:19:59 +000096
Ben Claytonb5cd10c2021-06-25 10:26:26 +000097namespace tint {
Ben Claytona6b9a8e2021-01-26 16:57:10 +000098class CloneContext;
99
100/// ProgramBuilder is a mutable builder for a Program.
101/// To construct a Program, populate the builder and then `std::move` it to a
102/// Program.
103class ProgramBuilder {
Ben Clayton620d77e2021-06-04 19:55:08 +0000104 /// VarOptionals is a helper for accepting a number of optional, extra
105 /// arguments for Var() and Global().
106 struct VarOptionals {
107 template <typename... ARGS>
108 explicit VarOptionals(ARGS&&... args) {
109 Apply(std::forward<ARGS>(args)...);
110 }
111 ~VarOptionals();
112
113 ast::StorageClass storage = ast::StorageClass::kNone;
Ben Clayton93e8f522021-06-04 20:41:47 +0000114 ast::Access access = ast::Access::kUndefined;
Ben Clayton620d77e2021-06-04 19:55:08 +0000115 ast::Expression* constructor = nullptr;
116 ast::DecorationList decorations = {};
117
118 private:
119 void Set(ast::StorageClass sc) { storage = sc; }
Ben Clayton93e8f522021-06-04 20:41:47 +0000120 void Set(ast::Access ac) { access = ac; }
Ben Clayton620d77e2021-06-04 19:55:08 +0000121 void Set(ast::Expression* c) { constructor = c; }
122 void Set(const ast::DecorationList& l) { decorations = l; }
123
124 template <typename FIRST, typename... ARGS>
125 void Apply(FIRST&& first, ARGS&&... args) {
126 Set(std::forward<FIRST>(first));
127 Apply(std::forward<ARGS>(args)...);
128 }
129 void Apply() {}
130 };
131
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000132 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +0000133 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
134 using ASTNodeAllocator = BlockAllocator<ast::Node>;
135
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000136 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
137 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000138
139 /// `i32` is a type alias to `int`.
140 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
141 /// WGSL syntax.
142 /// Note: this is intentionally not aliased to uint32_t as we want integer
143 /// literals passed to the builder to match WGSL's integer literal types.
144 using i32 = decltype(1);
145 /// `u32` is a type alias to `unsigned int`.
146 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
147 /// WGSL syntax.
148 /// Note: this is intentionally not aliased to uint32_t as we want integer
149 /// literals passed to the builder to match WGSL's integer literal types.
150 using u32 = decltype(1u);
151 /// `f32` is a type alias to `float`
152 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
153 /// WGSL syntax.
154 using f32 = float;
155
156 /// Constructor
157 ProgramBuilder();
158
159 /// Move constructor
160 /// @param rhs the builder to move
161 ProgramBuilder(ProgramBuilder&& rhs);
162
163 /// Destructor
164 virtual ~ProgramBuilder();
165
166 /// Move assignment operator
167 /// @param rhs the builder to move
168 /// @return this builder
169 ProgramBuilder& operator=(ProgramBuilder&& rhs);
170
Ben Claytone43c8302021-01-29 11:59:32 +0000171 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
172 /// making a deep clone of the Program contents.
173 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
174 /// existing immutable program.
175 /// As the returned ProgramBuilder wraps `program`, `program` must not be
176 /// destructed or assigned while using the returned ProgramBuilder.
177 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
178 /// function. See crbug.com/tint/460.
179 /// @param program the immutable Program to wrap
180 /// @return the ProgramBuilder that wraps `program`
181 static ProgramBuilder Wrap(const Program* program);
182
Ben Claytone6995de2021-04-13 23:27:27 +0000183 /// @returns the unique identifier for this program
184 ProgramID ID() const { return id_; }
185
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000186 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000187 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000188 AssertNotMoved();
189 return types_;
190 }
191
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000192 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000193 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000194 AssertNotMoved();
195 return types_;
196 }
197
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000198 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000199 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000200 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000201 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000202 }
203
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000204 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000205 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000206 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000207 return ast_nodes_;
208 }
209
210 /// @returns a reference to the program's semantic nodes storage
211 SemNodeAllocator& SemNodes() {
212 AssertNotMoved();
213 return sem_nodes_;
214 }
215
216 /// @returns a reference to the program's semantic nodes storage
217 const SemNodeAllocator& SemNodes() const {
218 AssertNotMoved();
219 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000220 }
221
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000222 /// @returns a reference to the program's AST root Module
223 ast::Module& AST() {
224 AssertNotMoved();
225 return *ast_;
226 }
227
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000228 /// @returns a reference to the program's AST root Module
229 const ast::Module& AST() const {
230 AssertNotMoved();
231 return *ast_;
232 }
233
234 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000235 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000236 AssertNotMoved();
237 return sem_;
238 }
239
240 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000241 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000242 AssertNotMoved();
243 return sem_;
244 }
245
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000246 /// @returns a reference to the program's SymbolTable
247 SymbolTable& Symbols() {
248 AssertNotMoved();
249 return symbols_;
250 }
251
Ben Clayton708dc2d2021-01-29 11:22:40 +0000252 /// @returns a reference to the program's SymbolTable
253 const SymbolTable& Symbols() const {
254 AssertNotMoved();
255 return symbols_;
256 }
257
Ben Clayton844217f2021-01-27 18:49:05 +0000258 /// @returns a reference to the program's diagnostics
259 diag::List& Diagnostics() {
260 AssertNotMoved();
261 return diagnostics_;
262 }
263
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000264 /// @returns a reference to the program's diagnostics
265 const diag::List& Diagnostics() const {
266 AssertNotMoved();
267 return diagnostics_;
268 }
269
Ben Clayton5f0ea112021-03-09 10:54:37 +0000270 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000271 /// @param enable the new flag value (defaults to true)
272 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
273
Ben Clayton5f0ea112021-03-09 10:54:37 +0000274 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000275 /// built.
276 bool ResolveOnBuild() const { return resolve_on_build_; }
277
Ben Clayton844217f2021-01-27 18:49:05 +0000278 /// @returns true if the program has no error diagnostics and is not missing
279 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000280 bool IsValid() const;
281
Ben Clayton708dc2d2021-01-29 11:22:40 +0000282 /// Writes a representation of the node to the output stream
283 /// @note unlike str(), to_str() does not automatically demangle the string.
284 /// @param node the AST node
285 /// @param out the stream to write to
286 /// @param indent number of spaces to indent the node when writing
287 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
288 node->to_str(Sem(), out, indent);
289 }
290
291 /// Returns a demangled, string representation of `node`.
292 /// @param node the AST node
293 /// @returns a string representation of the node
294 std::string str(const ast::Node* node) const;
295
Ben Clayton7fdfff12021-01-29 15:17:30 +0000296 /// Creates a new ast::Node owned by the ProgramBuilder. When the
297 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000298 /// @param source the Source of the node
299 /// @param args the arguments to pass to the type constructor
300 /// @returns the node pointer
301 template <typename T, typename... ARGS>
302 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
303 ARGS&&... args) {
304 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000305 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000306 }
307
Ben Clayton7fdfff12021-01-29 15:17:30 +0000308 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
309 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000310 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000311 /// When the ProgramBuilder is destructed, the ast::Node will also be
312 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000313 /// @returns the node pointer
314 template <typename T>
315 traits::EnableIfIsType<T, ast::Node>* create() {
316 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000317 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000318 }
319
Ben Clayton7fdfff12021-01-29 15:17:30 +0000320 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
321 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000322 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000323 /// When the ProgramBuilder is destructed, the ast::Node will also be
324 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000325 /// @param arg0 the first arguments to pass to the type constructor
326 /// @param args the remaining arguments to pass to the type constructor
327 /// @returns the node pointer
328 template <typename T, typename ARG0, typename... ARGS>
329 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
330 traits::IsTypeOrDerived<T, ast::Node>::value &&
331 !traits::IsTypeOrDerived<ARG0, Source>::value,
332 T>*
333 create(ARG0&& arg0, ARGS&&... args) {
334 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000335 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000336 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000337 }
338
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000339 /// Creates a new sem::Node owned by the ProgramBuilder.
340 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000341 /// destructed.
342 /// @param args the arguments to pass to the type constructor
343 /// @returns the node pointer
344 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000345 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
346 !traits::IsTypeOrDerived<T, sem::Type>::value,
347 T>*
348 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000349 AssertNotMoved();
350 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
351 }
352
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000353 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000354 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
355 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000356 /// Types are unique (de-aliased), and so calling create() for the same `T`
357 /// and arguments will return the same pointer.
358 /// @warning Use this method to acquire a type only if all of its type
359 /// information is provided in the constructor arguments `args`.<br>
360 /// If the type requires additional configuration after construction that
361 /// affect its fundamental type, build the type with `std::make_unique`, make
362 /// any necessary alterations and then call unique_type() instead.
363 /// @param args the arguments to pass to the type constructor
364 /// @returns the de-aliased type pointer
365 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000366 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
367 static_assert(std::is_base_of<sem::Type, T>::value,
368 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000369 AssertNotMoved();
370 return types_.Get<T>(std::forward<ARGS>(args)...);
371 }
372
373 /// Marks this builder as moved, preventing any further use of the builder.
374 void MarkAsMoved();
375
376 //////////////////////////////////////////////////////////////////////////////
377 // TypesBuilder
378 //////////////////////////////////////////////////////////////////////////////
379
380 /// TypesBuilder holds basic `tint` types and methods for constructing
381 /// complex types.
382 class TypesBuilder {
383 public:
384 /// Constructor
385 /// @param builder the program builder
386 explicit TypesBuilder(ProgramBuilder* builder);
387
388 /// @return the tint AST type for the C type `T`.
389 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000390 ast::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000391 return CToAST<T>::get(this);
392 }
393
394 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000395 ast::Bool* bool_() const { return builder->create<ast::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000396
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000397 /// @param source the Source of the node
398 /// @returns a boolean type
Ben Clayton19d32052021-05-20 15:10:48 +0000399 ast::Bool* bool_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000400 return builder->create<ast::Bool>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000401 }
402
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000403 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000404 ast::F32* f32() const { return builder->create<ast::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000405
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000406 /// @param source the Source of the node
407 /// @returns a f32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000408 ast::F32* f32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000409 return builder->create<ast::F32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000410 }
411
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000412 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000413 ast::I32* i32() const { return builder->create<ast::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000414
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000415 /// @param source the Source of the node
416 /// @returns a i32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000417 ast::I32* i32(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000418 return builder->create<ast::I32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000419 }
420
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000421 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000422 ast::U32* u32() const { return builder->create<ast::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000423
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000424 /// @param source the Source of the node
425 /// @returns a u32 type
Ben Clayton19d32052021-05-20 15:10:48 +0000426 ast::U32* u32(const Source& source) const {
Ben Clayton58dec172021-05-19 17:47:11 +0000427 return builder->create<ast::U32>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000428 }
429
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000430 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000431 ast::Void* void_() const { return builder->create<ast::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000432
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000433 /// @param source the Source of the node
434 /// @returns a void type
Ben Clayton19d32052021-05-20 15:10:48 +0000435 ast::Void* void_(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000436 return builder->create<ast::Void>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000437 }
438
Antonio Maiorano25436862021-04-13 13:32:33 +0000439 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000440 /// @param n vector width in elements
441 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000442 ast::Vector* vec(ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000443 return builder->create<ast::Vector>(type, n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000444 }
445
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000446 /// @param source the Source of the node
447 /// @param type vector subtype
448 /// @param n vector width in elements
449 /// @return the tint AST type for a `n`-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000450 ast::Vector* vec(const Source& source, ast::Type* type, uint32_t n) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000451 return builder->create<ast::Vector>(source, type, n);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000452 }
453
Antonio Maiorano25436862021-04-13 13:32:33 +0000454 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000455 /// @return the tint AST type for a 2-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000456 ast::Vector* vec2(ast::Type* type) const { return vec(type, 2u); }
Ben Claytone204f272021-04-22 14:40:23 +0000457
458 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000459 /// @return the tint AST type for a 3-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000460 ast::Vector* vec3(ast::Type* type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000461
462 /// @param type vector subtype
463 /// @return the tint AST type for a 4-element vector of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000464 ast::Vector* vec4(ast::Type* type) const { return vec(type, 4u); }
Ben Claytone204f272021-04-22 14:40:23 +0000465
466 /// @param n vector width in elements
467 /// @return the tint AST type for a `n`-element vector of `type`.
468 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000469 ast::Vector* vec(uint32_t n) const {
Ben Claytone204f272021-04-22 14:40:23 +0000470 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000471 }
472
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000473 /// @return the tint AST type for a 2-element vector of the C type `T`.
474 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000475 ast::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000476 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000477 }
478
479 /// @return the tint AST type for a 3-element vector of the C type `T`.
480 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000481 ast::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000482 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000483 }
484
485 /// @return the tint AST type for a 4-element vector of the C type `T`.
486 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000487 ast::Vector* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000488 return vec4(Of<T>());
489 }
490
491 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000492 /// @param columns number of columns for the matrix
493 /// @param rows number of rows for the matrix
494 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000495 ast::Matrix* mat(ast::Type* type, uint32_t columns, uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000496 return builder->create<ast::Matrix>(type, rows, columns);
Ben Claytone204f272021-04-22 14:40:23 +0000497 }
498
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000499 /// @param source the Source of the node
500 /// @param type matrix subtype
501 /// @param columns number of columns for the matrix
502 /// @param rows number of rows for the matrix
503 /// @return the tint AST type for a matrix of `type`
Ben Clayton19d32052021-05-20 15:10:48 +0000504 ast::Matrix* mat(const Source& source,
505 ast::Type* type,
506 uint32_t columns,
507 uint32_t rows) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000508 return builder->create<ast::Matrix>(source, type, rows, columns);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000509 }
510
Ben Claytone204f272021-04-22 14:40:23 +0000511 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000512 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000513 ast::Matrix* mat2x2(ast::Type* type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000514
515 /// @param type matrix subtype
516 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000517 ast::Matrix* mat2x3(ast::Type* type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000518
519 /// @param type matrix subtype
520 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000521 ast::Matrix* mat2x4(ast::Type* type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000522
523 /// @param type matrix subtype
524 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000525 ast::Matrix* mat3x2(ast::Type* type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000526
527 /// @param type matrix subtype
528 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000529 ast::Matrix* mat3x3(ast::Type* type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000530
531 /// @param type matrix subtype
532 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000533 ast::Matrix* mat3x4(ast::Type* type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000534
535 /// @param type matrix subtype
536 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000537 ast::Matrix* mat4x2(ast::Type* type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000538
539 /// @param type matrix subtype
540 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000541 ast::Matrix* mat4x3(ast::Type* type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000542
543 /// @param type matrix subtype
544 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton19d32052021-05-20 15:10:48 +0000545 ast::Matrix* mat4x4(ast::Type* type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000546
Ben Claytone204f272021-04-22 14:40:23 +0000547 /// @param columns number of columns for the matrix
548 /// @param rows number of rows for the matrix
549 /// @return the tint AST type for a matrix of `type`
550 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000551 ast::Matrix* mat(uint32_t columns, uint32_t rows) const {
Ben Claytone204f272021-04-22 14:40:23 +0000552 return mat(Of<T>(), columns, rows);
553 }
554
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000555 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
556 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000557 ast::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000558 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559 }
560
561 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
562 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000563 ast::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000564 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000565 }
566
567 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
568 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000569 ast::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000570 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 }
572
573 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
574 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000575 ast::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000576 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000577 }
578
579 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
580 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000581 ast::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000582 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000583 }
584
585 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
586 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000587 ast::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000588 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000589 }
590
591 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
592 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000593 ast::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000594 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000595 }
596
597 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
598 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000599 ast::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000600 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000601 }
602
603 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
604 template <typename T>
Ben Clayton19d32052021-05-20 15:10:48 +0000605 ast::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000606 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000607 }
608
609 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000610 /// @param n the array size. 0 represents a runtime-array
611 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000612 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000613 ast::Array* array(ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000614 uint32_t n = 0,
615 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000616 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000617 }
618
619 /// @param source the Source of the node
620 /// @param subtype the array element type
621 /// @param n the array size. 0 represents a runtime-array
622 /// @param decos the optional decorations for the array
623 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000624 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000625 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000626 uint32_t n = 0,
627 ast::DecorationList decos = {}) const {
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000628 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000629 }
630
Ben Claytonbab31972021-02-08 21:16:21 +0000631 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000632 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000633 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000634 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton19b03192021-05-20 15:04:08 +0000635 ast::Array* array(ast::Type* subtype, uint32_t n, uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000636 ast::DecorationList decos;
637 if (stride) {
638 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
639 }
640 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000641 }
642
Ben Clayton0f88b312021-05-04 17:36:31 +0000643 /// @param source the Source of the node
644 /// @param subtype the array element type
645 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000646 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000647 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000648 ast::Array* array(const Source& source,
Ben Clayton19b03192021-05-20 15:04:08 +0000649 ast::Type* subtype,
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000650 uint32_t n,
651 uint32_t stride) const {
Ben Claytoncbbe5762021-05-10 17:25:21 +0000652 ast::DecorationList decos;
653 if (stride) {
654 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
655 }
656 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000657 }
658
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000659 /// @return the tint AST type for an array of size `N` of type `T`
660 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000661 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000662 return array(Of<T>(), N);
663 }
664
Ben Claytonbab31972021-02-08 21:16:21 +0000665 /// @param stride the array stride
666 /// @return the tint AST type for an array of size `N` of type `T`
667 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000668 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000669 return array(Of<T>(), N, stride);
670 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000671
Ben Clayton0f88b312021-05-04 17:36:31 +0000672 /// Creates a type name
673 /// @param name the name
674 /// @returns the type name
675 template <typename NAME>
676 ast::TypeName* type_name(NAME&& name) const {
677 return builder->create<ast::TypeName>(
678 builder->Sym(std::forward<NAME>(name)));
679 }
680
681 /// Creates a type name
682 /// @param source the Source of the node
683 /// @param name the name
684 /// @returns the type name
685 template <typename NAME>
686 ast::TypeName* type_name(const Source& source, NAME&& name) const {
687 return builder->create<ast::TypeName>(
688 source, builder->Sym(std::forward<NAME>(name)));
689 }
690
Ben Clayton42d1e092021-02-02 14:29:15 +0000691 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000692 /// @param name the alias name
693 /// @param type the alias type
694 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000695 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000696 ast::Alias* alias(NAME&& name, ast::Type* type) const {
Ben Claytone204f272021-04-22 14:40:23 +0000697 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000698 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000699 }
700
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000701 /// Creates an alias type
702 /// @param source the Source of the node
703 /// @param name the alias name
704 /// @param type the alias type
705 /// @returns the alias pointer
706 template <typename NAME>
Ben Clayton19d32052021-05-20 15:10:48 +0000707 ast::Alias* alias(const Source& source,
708 NAME&& name,
709 ast::Type* type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000710 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000711 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000712 }
713
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000714 /// @param type the type of the pointer
715 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000716 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000717 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000718 ast::Pointer* pointer(ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000719 ast::StorageClass storage_class,
720 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000721 return builder->create<ast::Pointer>(type, storage_class, access);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000722 }
723
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000724 /// @param source the Source of the node
725 /// @param type the type of the pointer
726 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000727 /// @param access the optional access control of the pointer
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000728 /// @return the pointer to `type` with the given ast::StorageClass
Ben Clayton19d32052021-05-20 15:10:48 +0000729 ast::Pointer* pointer(const Source& source,
730 ast::Type* type,
Ben Clayton18588542021-06-04 22:17:37 +0000731 ast::StorageClass storage_class,
732 ast::Access access = ast::Access::kUndefined) const {
Ben Clayton18588542021-06-04 22:17:37 +0000733 return builder->create<ast::Pointer>(source, type, storage_class, access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000734 }
735
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000736 /// @param storage_class the storage class of the pointer
Ben Clayton18588542021-06-04 22:17:37 +0000737 /// @param access the optional access control of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000738 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000739 template <typename T>
Ben Clayton18588542021-06-04 22:17:37 +0000740 ast::Pointer* pointer(ast::StorageClass storage_class,
741 ast::Access access = ast::Access::kUndefined) const {
742 return pointer(Of<T>(), storage_class, access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000743 }
744
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000745 /// @param source the Source of the node
746 /// @param type the type of the atomic
747 /// @return the atomic to `type`
748 ast::Atomic* atomic(const Source& source, ast::Type* type) const {
749 return builder->create<ast::Atomic>(source, type);
750 }
751
Ben Clayton313e6182021-06-17 19:56:14 +0000752 /// @param type the type of the atomic
753 /// @return the atomic to `type`
754 ast::Atomic* atomic(ast::Type* type) const {
755 return builder->create<ast::Atomic>(type);
756 }
757
Ben Clayton8c7f0da2021-06-17 15:48:39 +0000758 /// @return the atomic to type `T`
759 template <typename T>
760 ast::Atomic* atomic() const {
761 return atomic(Of<T>());
762 }
763
Ben Claytone204f272021-04-22 14:40:23 +0000764 /// @param kind the kind of sampler
765 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000766 ast::Sampler* sampler(ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000767 return builder->create<ast::Sampler>(kind);
Ben Claytone204f272021-04-22 14:40:23 +0000768 }
769
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000770 /// @param source the Source of the node
771 /// @param kind the kind of sampler
772 /// @returns the sampler
Ben Clayton19d32052021-05-20 15:10:48 +0000773 ast::Sampler* sampler(const Source& source, ast::SamplerKind kind) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000774 return builder->create<ast::Sampler>(source, kind);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000775 }
776
Ben Claytone204f272021-04-22 14:40:23 +0000777 /// @param dims the dimensionality of the texture
778 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000779 ast::DepthTexture* depth_texture(ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000780 return builder->create<ast::DepthTexture>(dims);
Ben Claytone204f272021-04-22 14:40:23 +0000781 }
782
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000783 /// @param source the Source of the node
784 /// @param dims the dimensionality of the texture
785 /// @returns the depth texture
Ben Clayton19d32052021-05-20 15:10:48 +0000786 ast::DepthTexture* depth_texture(const Source& source,
787 ast::TextureDimension dims) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000788 return builder->create<ast::DepthTexture>(source, dims);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000789 }
790
Ben Claytone204f272021-04-22 14:40:23 +0000791 /// @param dims the dimensionality of the texture
792 /// @param subtype the texture subtype.
793 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000794 ast::SampledTexture* sampled_texture(ast::TextureDimension dims,
795 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000796 return builder->create<ast::SampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000797 }
798
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000799 /// @param source the Source of the node
800 /// @param dims the dimensionality of the texture
801 /// @param subtype the texture subtype.
802 /// @returns the sampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000803 ast::SampledTexture* sampled_texture(const Source& source,
804 ast::TextureDimension dims,
805 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000806 return builder->create<ast::SampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000807 }
808
Ben Claytone204f272021-04-22 14:40:23 +0000809 /// @param dims the dimensionality of the texture
810 /// @param subtype the texture subtype.
811 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000812 ast::MultisampledTexture* multisampled_texture(ast::TextureDimension dims,
813 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000814 return builder->create<ast::MultisampledTexture>(dims, subtype);
Ben Claytone204f272021-04-22 14:40:23 +0000815 }
816
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000817 /// @param source the Source of the node
818 /// @param dims the dimensionality of the texture
819 /// @param subtype the texture subtype.
820 /// @returns the multisampled texture
Ben Clayton19d32052021-05-20 15:10:48 +0000821 ast::MultisampledTexture* multisampled_texture(const Source& source,
822 ast::TextureDimension dims,
823 ast::Type* subtype) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000824 return builder->create<ast::MultisampledTexture>(source, dims, subtype);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000825 }
826
Ben Claytone204f272021-04-22 14:40:23 +0000827 /// @param dims the dimensionality of the texture
828 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000829 /// @param access the access control of the texture
Ben Claytone204f272021-04-22 14:40:23 +0000830 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000831 ast::StorageTexture* storage_texture(ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000832 ast::ImageFormat format,
833 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000834 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000835 return builder->create<ast::StorageTexture>(dims, format, subtype,
836 access);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000837 }
838
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000839 /// @param source the Source of the node
840 /// @param dims the dimensionality of the texture
841 /// @param format the image format of the texture
Ben Clayton93e8f522021-06-04 20:41:47 +0000842 /// @param access the access control of the texture
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000843 /// @returns the storage texture
Ben Clayton19d32052021-05-20 15:10:48 +0000844 ast::StorageTexture* storage_texture(const Source& source,
845 ast::TextureDimension dims,
Ben Clayton93e8f522021-06-04 20:41:47 +0000846 ast::ImageFormat format,
847 ast::Access access) const {
Ben Clayton55ecfc42021-05-20 14:42:28 +0000848 auto* subtype = ast::StorageTexture::SubtypeFor(format, *builder);
Ben Clayton93e8f522021-06-04 20:41:47 +0000849 return builder->create<ast::StorageTexture>(source, dims, format, subtype,
850 access);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000851 }
852
Brandon Jones7b257692021-05-17 17:40:17 +0000853 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000854 ast::ExternalTexture* external_texture() const {
Ben Clayton19b03192021-05-20 15:04:08 +0000855 return builder->create<ast::ExternalTexture>();
Brandon Jones7b257692021-05-17 17:40:17 +0000856 }
857
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000858 /// @param source the Source of the node
859 /// @returns the external texture
Ben Clayton19d32052021-05-20 15:10:48 +0000860 ast::ExternalTexture* external_texture(const Source& source) const {
Ben Clayton19b03192021-05-20 15:04:08 +0000861 return builder->create<ast::ExternalTexture>(source);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000862 }
863
Ben Clayton19b03192021-05-20 15:04:08 +0000864 /// [DEPRECATED]: TODO(crbug.com/tint/745): Migrate to const AST pointers.
Ben Clayton8758f102021-06-09 14:32:14 +0000865 /// Constructs a TypeName for the type declaration.
Ben Clayton19b03192021-05-20 15:04:08 +0000866 /// @param type the type
867 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000868 ast::TypeName* Of(ast::TypeDecl* type) const;
Ben Clayton19b03192021-05-20 15:04:08 +0000869
Ben Clayton8758f102021-06-09 14:32:14 +0000870 /// Constructs a TypeName for the type declaration.
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000871 /// @param type the type
872 /// @return either type or a pointer to a new ast::TypeName
Ben Clayton8758f102021-06-09 14:32:14 +0000873 const ast::TypeName* Of(const ast::TypeDecl* type) const;
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000874
Ben Claytonf5f311e2021-04-28 14:31:23 +0000875 /// The ProgramBuilder
876 ProgramBuilder* const builder;
877
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000878 private:
879 /// CToAST<T> is specialized for various `T` types and each specialization
880 /// contains a single static `get()` method for obtaining the corresponding
881 /// AST type for the C type `T`.
882 /// `get()` has the signature:
Ben Clayton19d32052021-05-20 15:10:48 +0000883 /// `static ast::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000884 template <typename T>
885 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000886 };
887
888 //////////////////////////////////////////////////////////////////////////////
889 // AST helper methods
890 //////////////////////////////////////////////////////////////////////////////
891
James Price65ae64d2021-04-29 12:59:14 +0000892 /// @return a new unnamed symbol
893 Symbol Sym() { return Symbols().New(); }
894
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000895 /// @param name the symbol string
896 /// @return a Symbol with the given name
897 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
898
899 /// @param sym the symbol
900 /// @return `sym`
901 Symbol Sym(Symbol sym) { return sym; }
902
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000903 /// @param expr the expression
904 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000905 template <typename T>
906 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
907 return expr;
908 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000909
Ben Claytonb8ea5912021-04-19 16:52:42 +0000910 /// Passthrough for nullptr
911 /// @return nullptr
912 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
913
Ben Claytonfe0910f2021-05-17 15:51:47 +0000914 /// @param source the source information
915 /// @param symbol the identifier symbol
916 /// @return an ast::IdentifierExpression with the given symbol
917 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
918 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000919 }
920
Ben Clayton46d78d72021-02-10 21:34:26 +0000921 /// @param symbol the identifier symbol
922 /// @return an ast::IdentifierExpression with the given symbol
923 ast::IdentifierExpression* Expr(Symbol symbol) {
924 return create<ast::IdentifierExpression>(symbol);
925 }
926
Ben Claytonfe0910f2021-05-17 15:51:47 +0000927 /// @param source the source information
928 /// @param variable the AST variable
929 /// @return an ast::IdentifierExpression with the variable's symbol
930 ast::IdentifierExpression* Expr(const Source& source,
931 ast::Variable* variable) {
932 return create<ast::IdentifierExpression>(source, variable->symbol());
933 }
934
Ben Claytonb8ea5912021-04-19 16:52:42 +0000935 /// @param variable the AST variable
936 /// @return an ast::IdentifierExpression with the variable's symbol
937 ast::IdentifierExpression* Expr(ast::Variable* variable) {
938 return create<ast::IdentifierExpression>(variable->symbol());
939 }
940
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000941 /// @param source the source information
942 /// @param name the identifier name
943 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000944 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
945 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
946 }
947
948 /// @param name the identifier name
949 /// @return an ast::IdentifierExpression with the given name
950 ast::IdentifierExpression* Expr(const char* name) {
951 return create<ast::IdentifierExpression>(Symbols().Register(name));
952 }
953
954 /// @param source the source information
955 /// @param name the identifier name
956 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000957 ast::IdentifierExpression* Expr(const Source& source,
958 const std::string& name) {
959 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
960 }
961
962 /// @param name the identifier name
963 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000964 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000965 return create<ast::IdentifierExpression>(Symbols().Register(name));
966 }
967
Ben Claytonfe0910f2021-05-17 15:51:47 +0000968 /// @param source the source information
969 /// @param value the boolean value
970 /// @return a Scalar constructor for the given value
971 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
972 return create<ast::ScalarConstructorExpression>(source, Literal(value));
973 }
974
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000975 /// @param value the boolean value
976 /// @return a Scalar constructor for the given value
977 ast::ScalarConstructorExpression* Expr(bool value) {
978 return create<ast::ScalarConstructorExpression>(Literal(value));
979 }
980
Ben Claytonfe0910f2021-05-17 15:51:47 +0000981 /// @param source the source information
982 /// @param value the float value
983 /// @return a Scalar constructor for the given value
984 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
985 return create<ast::ScalarConstructorExpression>(source, Literal(value));
986 }
987
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000988 /// @param value the float value
989 /// @return a Scalar constructor for the given value
990 ast::ScalarConstructorExpression* Expr(f32 value) {
991 return create<ast::ScalarConstructorExpression>(Literal(value));
992 }
993
Ben Claytonfe0910f2021-05-17 15:51:47 +0000994 /// @param source the source information
995 /// @param value the integer value
996 /// @return a Scalar constructor for the given value
997 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
998 return create<ast::ScalarConstructorExpression>(source, Literal(value));
999 }
1000
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001001 /// @param value the integer value
1002 /// @return a Scalar constructor for the given value
1003 ast::ScalarConstructorExpression* Expr(i32 value) {
1004 return create<ast::ScalarConstructorExpression>(Literal(value));
1005 }
1006
Ben Claytonfe0910f2021-05-17 15:51:47 +00001007 /// @param source the source information
1008 /// @param value the unsigned int value
1009 /// @return a Scalar constructor for the given value
1010 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1011 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1012 }
1013
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001014 /// @param value the unsigned int value
1015 /// @return a Scalar constructor for the given value
1016 ast::ScalarConstructorExpression* Expr(u32 value) {
1017 return create<ast::ScalarConstructorExpression>(Literal(value));
1018 }
1019
1020 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1021 /// `list`.
1022 /// @param list the list to append too
1023 /// @param arg the arg to create
1024 template <typename ARG>
1025 void Append(ast::ExpressionList& list, ARG&& arg) {
1026 list.emplace_back(Expr(std::forward<ARG>(arg)));
1027 }
1028
1029 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1030 /// then appends them to `list`.
1031 /// @param list the list to append too
1032 /// @param arg0 the first argument
1033 /// @param args the rest of the arguments
1034 template <typename ARG0, typename... ARGS>
1035 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1036 Append(list, std::forward<ARG0>(arg0));
1037 Append(list, std::forward<ARGS>(args)...);
1038 }
1039
1040 /// @return an empty list of expressions
1041 ast::ExpressionList ExprList() { return {}; }
1042
1043 /// @param args the list of expressions
1044 /// @return the list of expressions converted to `ast::Expression`s using
1045 /// `Expr()`,
1046 template <typename... ARGS>
1047 ast::ExpressionList ExprList(ARGS&&... args) {
1048 ast::ExpressionList list;
1049 list.reserve(sizeof...(args));
1050 Append(list, std::forward<ARGS>(args)...);
1051 return list;
1052 }
1053
1054 /// @param list the list of expressions
1055 /// @return `list`
1056 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1057
1058 /// @param val the boolan value
1059 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001060 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001061
1062 /// @param val the float value
1063 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001064 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001065
1066 /// @param val the unsigned int value
1067 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001068 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001069
1070 /// @param val the integer value
1071 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001072 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001073
1074 /// @param args the arguments for the type constructor
1075 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1076 /// of `args` converted to `ast::Expression`s using `Expr()`
1077 template <typename T, typename... ARGS>
1078 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001079 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001080 }
1081
1082 /// @param type the type to construct
1083 /// @param args the arguments for the constructor
1084 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1085 /// values `args`.
1086 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001087 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001088 return create<ast::TypeConstructorExpression>(
1089 type, ExprList(std::forward<ARGS>(args)...));
1090 }
1091
Ben Clayton313e6182021-06-17 19:56:14 +00001092 /// @param source the source information
1093 /// @param type the type to construct
1094 /// @param args the arguments for the constructor
1095 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1096 /// values `args`.
1097 template <typename... ARGS>
1098 ast::TypeConstructorExpression* Construct(const Source& source,
1099 ast::Type* type,
1100 ARGS&&... args) {
1101 return create<ast::TypeConstructorExpression>(
1102 source, type, ExprList(std::forward<ARGS>(args)...));
1103 }
1104
Ben Claytond9603282021-06-29 12:37:45 +00001105 /// @param expr the expression for the bitcast
1106 /// @return an `ast::BitcastExpression` of type `ty`, with the values of
1107 /// `expr` converted to `ast::Expression`s using `Expr()`
1108 template <typename T, typename EXPR>
1109 ast::BitcastExpression* Bitcast(EXPR&& expr) {
1110 return Bitcast(ty.Of<T>(), std::forward<EXPR>(expr));
1111 }
1112
1113 /// @param type the type to cast to
1114 /// @param expr the expression for the bitcast
1115 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1116 /// `expr`.
1117 template <typename EXPR>
1118 ast::BitcastExpression* Bitcast(ast::Type* type, EXPR&& expr) {
1119 return create<ast::BitcastExpression>(type, Expr(std::forward<EXPR>(expr)));
1120 }
1121
1122 /// @param source the source information
1123 /// @param type the type to cast to
1124 /// @param expr the expression for the bitcast
1125 /// @return an `ast::BitcastExpression` of `type` constructed with the values
1126 /// `expr`.
1127 template <typename EXPR>
1128 ast::BitcastExpression* Bitcast(const Source& source,
1129 ast::Type* type,
1130 EXPR&& expr) {
1131 return create<ast::BitcastExpression>(source, type,
1132 Expr(std::forward<EXPR>(expr)));
1133 }
1134
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001135 /// @param args the arguments for the vector constructor
Antonio Maiorano05072732021-06-18 14:59:51 +00001136 /// @param type the vector type
1137 /// @param size the vector size
1138 /// @return an `ast::TypeConstructorExpression` of a `size`-element vector of
1139 /// type `type`, constructed with the values `args`.
1140 template <typename... ARGS>
1141 ast::TypeConstructorExpression* vec(ast::Type* type,
1142 uint32_t size,
1143 ARGS&&... args) {
1144 return Construct(ty.vec(type, size), std::forward<ARGS>(args)...);
1145 }
1146
1147 /// @param args the arguments for the vector constructor
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001148 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1149 /// `T`, constructed with the values `args`.
1150 template <typename T, typename... ARGS>
1151 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001152 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001153 }
1154
1155 /// @param args the arguments for the vector constructor
1156 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1157 /// `T`, constructed with the values `args`.
1158 template <typename T, typename... ARGS>
1159 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001160 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001161 }
1162
1163 /// @param args the arguments for the vector constructor
1164 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1165 /// `T`, constructed with the values `args`.
1166 template <typename T, typename... ARGS>
1167 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001168 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001169 }
1170
1171 /// @param args the arguments for the matrix constructor
1172 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1173 /// `T`, constructed with the values `args`.
1174 template <typename T, typename... ARGS>
1175 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001176 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001177 }
1178
1179 /// @param args the arguments for the matrix constructor
1180 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1181 /// `T`, constructed with the values `args`.
1182 template <typename T, typename... ARGS>
1183 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001184 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001185 }
1186
1187 /// @param args the arguments for the matrix constructor
1188 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1189 /// `T`, constructed with the values `args`.
1190 template <typename T, typename... ARGS>
1191 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001192 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001193 }
1194
1195 /// @param args the arguments for the matrix constructor
1196 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1197 /// `T`, constructed with the values `args`.
1198 template <typename T, typename... ARGS>
1199 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001200 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001201 }
1202
1203 /// @param args the arguments for the matrix constructor
1204 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1205 /// `T`, constructed with the values `args`.
1206 template <typename T, typename... ARGS>
1207 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001208 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001209 }
1210
1211 /// @param args the arguments for the matrix constructor
1212 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1213 /// `T`, constructed with the values `args`.
1214 template <typename T, typename... ARGS>
1215 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001216 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001217 }
1218
1219 /// @param args the arguments for the matrix constructor
1220 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1221 /// `T`, constructed with the values `args`.
1222 template <typename T, typename... ARGS>
1223 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001224 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001225 }
1226
1227 /// @param args the arguments for the matrix constructor
1228 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1229 /// `T`, constructed with the values `args`.
1230 template <typename T, typename... ARGS>
1231 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001232 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001233 }
1234
1235 /// @param args the arguments for the matrix constructor
1236 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1237 /// `T`, constructed with the values `args`.
1238 template <typename T, typename... ARGS>
1239 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001240 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001241 }
1242
1243 /// @param args the arguments for the array constructor
1244 /// @return an `ast::TypeConstructorExpression` of an array with element type
1245 /// `T`, constructed with the values `args`.
1246 template <typename T, int N = 0, typename... ARGS>
1247 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001248 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001249 }
1250
1251 /// @param subtype the array element type
1252 /// @param n the array size. 0 represents a runtime-array.
1253 /// @param args the arguments for the array constructor
1254 /// @return an `ast::TypeConstructorExpression` of an array with element type
1255 /// `subtype`, constructed with the values `args`.
1256 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001257 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001258 uint32_t n,
1259 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001260 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001261 }
1262
1263 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001264 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001265 /// @param optional the optional variable settings.
1266 /// Can be any of the following, in any order:
1267 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001268 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001269 /// * ast::Expression* - specifies the variable's initializer expression
1270 /// * ast::DecorationList - specifies the variable's decorations
1271 /// Note that repeated arguments of the same type will use the last argument's
1272 /// value.
1273 /// @returns a `ast::Variable` with the given name, type and additional
1274 /// options
1275 template <typename NAME, typename... OPTIONAL>
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001276 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001277 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001278 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001279 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1280 return create<ast::Variable>(Sym(std::forward<NAME>(name)), opts.storage,
Ben Clayton93e8f522021-06-04 20:41:47 +00001281 opts.access, type, false, opts.constructor,
Ben Clayton620d77e2021-06-04 19:55:08 +00001282 std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001283 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001284
1285 /// @param source the variable source
1286 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001287 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001288 /// @param optional the optional variable settings.
1289 /// Can be any of the following, in any order:
1290 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001291 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001292 /// * ast::Expression* - specifies the variable's initializer expression
1293 /// * ast::DecorationList - specifies the variable's decorations
1294 /// Note that repeated arguments of the same type will use the last argument's
1295 /// value.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001296 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton620d77e2021-06-04 19:55:08 +00001297 template <typename NAME, typename... OPTIONAL>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001298 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001299 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001300 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001301 OPTIONAL&&... optional) {
Ben Clayton620d77e2021-06-04 19:55:08 +00001302 VarOptionals opts(std::forward<OPTIONAL>(optional)...);
1303 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton93e8f522021-06-04 20:41:47 +00001304 opts.storage, opts.access, type, false,
1305 opts.constructor, std::move(opts.decorations));
Ben Clayton46d78d72021-02-10 21:34:26 +00001306 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001307
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001308 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001309 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001310 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001311 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001312 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001313 template <typename NAME>
1314 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001315 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001316 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001317 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001318 return create<ast::Variable>(
1319 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1320 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001321 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001322
1323 /// @param source the variable source
1324 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001325 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001326 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001327 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001328 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001329 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001330 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001331 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001332 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001333 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001334 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001335 return create<ast::Variable>(
1336 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1337 ast::Access::kUndefined, type, true, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001338 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001339
James Pricedaf1f1c2021-04-08 22:15:48 +00001340 /// @param name the parameter name
1341 /// @param type the parameter type
1342 /// @param decorations optional parameter decorations
1343 /// @returns a constant `ast::Variable` with the given name and type
1344 template <typename NAME>
1345 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001346 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001347 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001348 return create<ast::Variable>(
1349 Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1350 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001351 }
1352
1353 /// @param source the parameter source
1354 /// @param name the parameter name
1355 /// @param type the parameter type
1356 /// @param decorations optional parameter decorations
1357 /// @returns a constant `ast::Variable` with the given name and type
1358 template <typename NAME>
1359 ast::Variable* Param(const Source& source,
1360 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001361 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001362 ast::DecorationList decorations = {}) {
Ben Clayton93e8f522021-06-04 20:41:47 +00001363 return create<ast::Variable>(
1364 source, Sym(std::forward<NAME>(name)), ast::StorageClass::kNone,
1365 ast::Access::kUndefined, type, true, nullptr, decorations);
James Pricedaf1f1c2021-04-08 22:15:48 +00001366 }
1367
Ben Clayton42708342021-04-21 17:55:12 +00001368 /// @param name the variable name
1369 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001370 /// @param optional the optional variable settings.
1371 /// Can be any of the following, in any order:
1372 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001373 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001374 /// * ast::Expression* - specifies the variable's initializer expression
1375 /// * ast::DecorationList - specifies the variable's decorations
1376 /// Note that repeated arguments of the same type will use the last argument's
1377 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001378 /// @returns a new `ast::Variable`, which is automatically registered as a
1379 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001380 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001381 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001382 const ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001383 OPTIONAL&&... optional) {
1384 auto* var = Var(std::forward<NAME>(name), type,
1385 std::forward<OPTIONAL>(optional)...);
Ben Clayton42708342021-04-21 17:55:12 +00001386 AST().AddGlobalVariable(var);
1387 return var;
1388 }
1389
1390 /// @param source the variable source
1391 /// @param name the variable name
1392 /// @param type the variable type
Ben Clayton620d77e2021-06-04 19:55:08 +00001393 /// @param optional the optional variable settings.
1394 /// Can be any of the following, in any order:
1395 /// * ast::StorageClass - specifies the variable storage class
Ben Clayton93e8f522021-06-04 20:41:47 +00001396 /// * ast::Access - specifies the variable's access control
Ben Clayton620d77e2021-06-04 19:55:08 +00001397 /// * ast::Expression* - specifies the variable's initializer expression
1398 /// * ast::DecorationList - specifies the variable's decorations
1399 /// Note that repeated arguments of the same type will use the last argument's
1400 /// value.
Ben Clayton42708342021-04-21 17:55:12 +00001401 /// @returns a new `ast::Variable`, which is automatically registered as a
1402 /// global variable with the ast::Module.
Ben Clayton620d77e2021-06-04 19:55:08 +00001403 template <typename NAME, typename... OPTIONAL>
Ben Clayton42708342021-04-21 17:55:12 +00001404 ast::Variable* Global(const Source& source,
1405 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001406 ast::Type* type,
Ben Clayton620d77e2021-06-04 19:55:08 +00001407 OPTIONAL&&... optional) {
1408 auto* var = Var(source, std::forward<NAME>(name), type,
1409 std::forward<OPTIONAL>(optional)...);
Ben Clayton401b96b2021-02-03 17:19:59 +00001410 AST().AddGlobalVariable(var);
1411 return var;
1412 }
1413
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001414 /// @param name the variable name
1415 /// @param type the variable type
1416 /// @param constructor constructor expression
1417 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001418 /// @returns a const `ast::Variable` constructed by calling Var() with the
1419 /// arguments of `args`, which is automatically registered as a global
1420 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001421 template <typename NAME>
1422 ast::Variable* GlobalConst(NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001423 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001424 ast::Expression* constructor,
1425 ast::DecorationList decorations = {}) {
1426 auto* var = Const(std::forward<NAME>(name), type, constructor,
1427 std::move(decorations));
1428 AST().AddGlobalVariable(var);
1429 return var;
1430 }
1431
1432 /// @param source the variable source
1433 /// @param name the variable name
1434 /// @param type the variable type
1435 /// @param constructor constructor expression
1436 /// @param decorations optional variable decorations
1437 /// @returns a const `ast::Variable` constructed by calling Var() with the
1438 /// arguments of `args`, which is automatically registered as a global
1439 /// variable with the ast::Module.
1440 template <typename NAME>
1441 ast::Variable* GlobalConst(const Source& source,
1442 NAME&& name,
Ben Clayton19d32052021-05-20 15:10:48 +00001443 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001444 ast::Expression* constructor,
1445 ast::DecorationList decorations = {}) {
1446 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1447 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001448 AST().AddGlobalVariable(var);
1449 return var;
1450 }
1451
Ben Claytonfe0910f2021-05-17 15:51:47 +00001452 /// @param source the source information
1453 /// @param expr the expression to take the address of
1454 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1455 template <typename EXPR>
1456 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1457 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1458 Expr(std::forward<EXPR>(expr)));
1459 }
1460
1461 /// @param expr the expression to take the address of
1462 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1463 template <typename EXPR>
1464 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1465 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1466 Expr(std::forward<EXPR>(expr)));
1467 }
1468
1469 /// @param source the source information
1470 /// @param expr the expression to perform an indirection on
1471 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1472 template <typename EXPR>
1473 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1474 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1475 Expr(std::forward<EXPR>(expr)));
1476 }
1477
1478 /// @param expr the expression to perform an indirection on
1479 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1480 template <typename EXPR>
1481 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1482 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1483 Expr(std::forward<EXPR>(expr)));
1484 }
1485
Antonio Maiorano14b34032021-06-09 20:17:59 +00001486 /// @param source the source information
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001487 /// @param func the function name
1488 /// @param args the function call arguments
1489 /// @returns a `ast::CallExpression` to the function `func`, with the
1490 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1491 template <typename NAME, typename... ARGS>
Antonio Maiorano14b34032021-06-09 20:17:59 +00001492 ast::CallExpression* Call(const Source& source, NAME&& func, ARGS&&... args) {
1493 return create<ast::CallExpression>(source, Expr(func),
1494 ExprList(std::forward<ARGS>(args)...));
1495 }
1496
1497 /// @param func the function name
1498 /// @param args the function call arguments
1499 /// @returns a `ast::CallExpression` to the function `func`, with the
1500 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1501 template <typename NAME,
1502 typename... ARGS,
1503 traits::EnableIfIsNotType<traits::Decay<NAME>, Source>* = nullptr>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001504 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1505 return create<ast::CallExpression>(Expr(func),
1506 ExprList(std::forward<ARGS>(args)...));
1507 }
1508
Ben Clayton7fe01062021-06-11 13:22:27 +00001509 /// @param expr the expression to ignore
1510 /// @returns a `ast::CallStatement` that calls the `ignore` intrinsic which is
1511 /// passed the single `expr` argument
1512 template <typename EXPR>
1513 ast::CallStatement* Ignore(EXPR&& expr) {
1514 return create<ast::CallStatement>(Call("ignore", Expr(expr)));
1515 }
1516
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001517 /// @param lhs the left hand argument to the addition operation
1518 /// @param rhs the right hand argument to the addition operation
1519 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1520 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001521 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001522 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1523 Expr(std::forward<LHS>(lhs)),
1524 Expr(std::forward<RHS>(rhs)));
1525 }
1526
James Pricec32e8f62021-06-22 20:08:29 +00001527 /// @param lhs the left hand argument to the and operation
1528 /// @param rhs the right hand argument to the and operation
1529 /// @returns a `ast::BinaryExpression` bitwise anding `lhs` and `rhs`
1530 template <typename LHS, typename RHS>
1531 ast::BinaryExpression* And(LHS&& lhs, RHS&& rhs) {
1532 return create<ast::BinaryExpression>(ast::BinaryOp::kAnd,
1533 Expr(std::forward<LHS>(lhs)),
1534 Expr(std::forward<RHS>(rhs)));
1535 }
1536
Ben Claytond9603282021-06-29 12:37:45 +00001537 /// @param lhs the left hand argument to the or operation
1538 /// @param rhs the right hand argument to the or operation
1539 /// @returns a `ast::BinaryExpression` bitwise or-ing `lhs` and `rhs`
1540 template <typename LHS, typename RHS>
1541 ast::BinaryExpression* Or(LHS&& lhs, RHS&& rhs) {
1542 return create<ast::BinaryExpression>(ast::BinaryOp::kOr,
1543 Expr(std::forward<LHS>(lhs)),
1544 Expr(std::forward<RHS>(rhs)));
1545 }
1546
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001547 /// @param lhs the left hand argument to the subtraction operation
1548 /// @param rhs the right hand argument to the subtraction operation
1549 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1550 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001551 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001552 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1553 Expr(std::forward<LHS>(lhs)),
1554 Expr(std::forward<RHS>(rhs)));
1555 }
1556
1557 /// @param lhs the left hand argument to the multiplication operation
1558 /// @param rhs the right hand argument to the multiplication operation
1559 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1560 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001561 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001562 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1563 Expr(std::forward<LHS>(lhs)),
1564 Expr(std::forward<RHS>(rhs)));
1565 }
1566
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001567 /// @param source the source information
1568 /// @param lhs the left hand argument to the multiplication operation
1569 /// @param rhs the right hand argument to the multiplication operation
1570 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1571 template <typename LHS, typename RHS>
1572 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1573 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1574 Expr(std::forward<LHS>(lhs)),
1575 Expr(std::forward<RHS>(rhs)));
1576 }
1577
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001578 /// @param lhs the left hand argument to the division operation
1579 /// @param rhs the right hand argument to the division operation
1580 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1581 template <typename LHS, typename RHS>
1582 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1583 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1584 Expr(std::forward<LHS>(lhs)),
1585 Expr(std::forward<RHS>(rhs)));
1586 }
1587
Ben Claytond9603282021-06-29 12:37:45 +00001588 /// @param lhs the left hand argument to the bit shift right operation
1589 /// @param rhs the right hand argument to the bit shift right operation
1590 /// @returns a `ast::BinaryExpression` bit shifting right `lhs` by `rhs`
1591 template <typename LHS, typename RHS>
1592 ast::BinaryExpression* Shr(LHS&& lhs, RHS&& rhs) {
1593 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftRight,
1594 Expr(std::forward<LHS>(lhs)),
1595 Expr(std::forward<RHS>(rhs)));
1596 }
1597
1598 /// @param lhs the left hand argument to the bit shift left operation
1599 /// @param rhs the right hand argument to the bit shift left operation
1600 /// @returns a `ast::BinaryExpression` bit shifting left `lhs` by `rhs`
1601 template <typename LHS, typename RHS>
1602 ast::BinaryExpression* Shl(LHS&& lhs, RHS&& rhs) {
1603 return create<ast::BinaryExpression>(ast::BinaryOp::kShiftLeft,
1604 Expr(std::forward<LHS>(lhs)),
1605 Expr(std::forward<RHS>(rhs)));
1606 }
1607
Ben Clayton0597a2b2021-06-16 09:19:36 +00001608 /// @param source the source information
1609 /// @param arr the array argument for the array accessor expression
1610 /// @param idx the index argument for the array accessor expression
1611 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1612 template <typename ARR, typename IDX>
1613 ast::ArrayAccessorExpression* IndexAccessor(const Source& source,
1614 ARR&& arr,
1615 IDX&& idx) {
1616 return create<ast::ArrayAccessorExpression>(
1617 source, Expr(std::forward<ARR>(arr)), Expr(std::forward<IDX>(idx)));
1618 }
1619
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001620 /// @param arr the array argument for the array accessor expression
1621 /// @param idx the index argument for the array accessor expression
1622 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1623 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001624 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001625 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1626 Expr(std::forward<IDX>(idx)));
1627 }
1628
1629 /// @param obj the object for the member accessor expression
1630 /// @param idx the index argument for the array accessor expression
1631 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1632 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001633 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001634 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1635 Expr(std::forward<IDX>(idx)));
1636 }
1637
Ben Clayton42d1e092021-02-02 14:29:15 +00001638 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001639 /// @param val the offset value
1640 /// @returns the offset decoration pointer
1641 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1642 return create<ast::StructMemberOffsetDecoration>(source_, val);
1643 }
1644
Ben Claytond614dd52021-03-15 10:43:11 +00001645 /// Creates a ast::StructMemberSizeDecoration
1646 /// @param source the source information
1647 /// @param val the size value
1648 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001649 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1650 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001651 return create<ast::StructMemberSizeDecoration>(source, val);
1652 }
1653
1654 /// Creates a ast::StructMemberSizeDecoration
1655 /// @param val the size value
1656 /// @returns the size decoration pointer
1657 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1658 return create<ast::StructMemberSizeDecoration>(source_, val);
1659 }
1660
1661 /// Creates a ast::StructMemberAlignDecoration
1662 /// @param source the source information
1663 /// @param val the align value
1664 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001665 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1666 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001667 return create<ast::StructMemberAlignDecoration>(source, val);
1668 }
1669
1670 /// Creates a ast::StructMemberAlignDecoration
1671 /// @param val the align value
1672 /// @returns the align decoration pointer
1673 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1674 return create<ast::StructMemberAlignDecoration>(source_, val);
1675 }
1676
Ben Clayton42d1e092021-02-02 14:29:15 +00001677 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001678 /// @param source the source information
1679 /// @param name the function name
1680 /// @param params the function parameters
1681 /// @param type the function return type
1682 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001683 /// @param decorations the optional function decorations
1684 /// @param return_type_decorations the optional function return type
1685 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001686 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001687 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001688 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001689 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001690 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001691 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001692 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001693 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001694 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001695 auto* func =
1696 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1697 type, create<ast::BlockStatement>(body),
1698 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001699 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001700 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001701 }
1702
Ben Clayton42d1e092021-02-02 14:29:15 +00001703 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001704 /// @param name the function name
1705 /// @param params the function parameters
1706 /// @param type the function return type
1707 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001708 /// @param decorations the optional function decorations
1709 /// @param return_type_decorations the optional function return type
1710 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001711 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001712 template <typename NAME>
1713 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001714 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001715 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001716 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001717 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001718 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001719 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1720 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001721 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001722 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001723 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001724 }
1725
Ben Clayton49668bb2021-04-17 05:32:01 +00001726 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001727 /// @param source the source information
1728 /// @returns the return statement pointer
1729 ast::ReturnStatement* Return(const Source& source) {
1730 return create<ast::ReturnStatement>(source);
1731 }
1732
1733 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001734 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001735 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1736
1737 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001738 /// @param source the source information
1739 /// @param val the return value
1740 /// @returns the return statement pointer
1741 template <typename EXPR>
1742 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1743 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1744 }
1745
1746 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001747 /// @param val the return value
1748 /// @returns the return statement pointer
1749 template <typename EXPR>
1750 ast::ReturnStatement* Return(EXPR&& val) {
1751 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001752 }
1753
Ben Clayton950809f2021-06-09 14:32:14 +00001754 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001755 /// @param source the source information
1756 /// @param name the alias name
1757 /// @param type the alias target type
1758 /// @returns the alias type
1759 template <typename NAME>
1760 ast::Alias* Alias(const Source& source, NAME&& name, ast::Type* type) {
1761 auto* out = ty.alias(source, std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001762 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001763 return out;
1764 }
1765
Ben Clayton950809f2021-06-09 14:32:14 +00001766 /// Creates a ast::Alias registering it with the AST().TypeDecls().
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001767 /// @param name the alias name
1768 /// @param type the alias target type
1769 /// @returns the alias type
1770 template <typename NAME>
1771 ast::Alias* Alias(NAME&& name, ast::Type* type) {
1772 auto* out = ty.alias(std::forward<NAME>(name), type);
Ben Clayton950809f2021-06-09 14:32:14 +00001773 AST().AddTypeDecl(out);
Ben Clayton6e7d24d2021-06-09 14:32:14 +00001774 return out;
1775 }
1776
Ben Clayton950809f2021-06-09 14:32:14 +00001777 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001778 /// @param source the source information
1779 /// @param name the struct name
1780 /// @param members the struct members
1781 /// @param decorations the optional struct decorations
1782 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001783 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001784 ast::Struct* Structure(const Source& source,
1785 NAME&& name,
1786 ast::StructMemberList members,
1787 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001788 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001789 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001790 std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001791 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001792 return type;
1793 }
1794
Ben Clayton950809f2021-06-09 14:32:14 +00001795 /// Creates a ast::Struct registering it with the AST().TypeDecls().
Ben Claytond614dd52021-03-15 10:43:11 +00001796 /// @param name the struct name
1797 /// @param members the struct members
1798 /// @param decorations the optional struct decorations
1799 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001800 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001801 ast::Struct* Structure(NAME&& name,
1802 ast::StructMemberList members,
1803 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001804 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001805 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001806 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton950809f2021-06-09 14:32:14 +00001807 AST().AddTypeDecl(type);
Ben Claytond614dd52021-03-15 10:43:11 +00001808 return type;
1809 }
1810
Ben Clayton42d1e092021-02-02 14:29:15 +00001811 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001812 /// @param source the source information
1813 /// @param name the struct member name
1814 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001815 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001816 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001817 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001818 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001819 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001820 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001821 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001822 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1823 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001824 }
1825
Ben Clayton42d1e092021-02-02 14:29:15 +00001826 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001827 /// @param name the struct member name
1828 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001829 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001830 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001831 template <typename NAME>
1832 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001833 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001834 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001835 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1836 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001837 }
1838
Ben Claytonbab31972021-02-08 21:16:21 +00001839 /// Creates a ast::StructMember with the given byte offset
1840 /// @param offset the offset to use in the StructMemberOffsetDecoration
1841 /// @param name the struct member name
1842 /// @param type the struct member type
1843 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001844 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001845 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001846 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001847 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001848 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001849 create<ast::StructMemberOffsetDecoration>(offset),
1850 });
1851 }
1852
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001853 /// Creates a ast::BlockStatement with input statements
1854 /// @param statements statements of block
1855 /// @returns the block statement pointer
1856 template <typename... Statements>
1857 ast::BlockStatement* Block(Statements&&... statements) {
1858 return create<ast::BlockStatement>(
1859 ast::StatementList{std::forward<Statements>(statements)...});
1860 }
1861
1862 /// Creates a ast::ElseStatement with input condition and body
1863 /// @param condition the else condition expression
1864 /// @param body the else body
1865 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001866 template <typename CONDITION>
1867 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1868 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1869 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001870 }
1871
1872 /// Creates a ast::IfStatement with input condition, body, and optional
1873 /// variadic else statements
1874 /// @param condition the if statement condition expression
1875 /// @param body the if statement body
1876 /// @param elseStatements optional variadic else statements
1877 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001878 template <typename CONDITION, typename... ELSE_STATEMENTS>
1879 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001880 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001881 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001882 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001883 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001884 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001885 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001886 }
1887
1888 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001889 /// @param source the source information
1890 /// @param lhs the left hand side expression initializer
1891 /// @param rhs the right hand side expression initializer
1892 /// @returns the assignment statement pointer
1893 template <typename LhsExpressionInit, typename RhsExpressionInit>
1894 ast::AssignmentStatement* Assign(const Source& source,
1895 LhsExpressionInit&& lhs,
1896 RhsExpressionInit&& rhs) {
1897 return create<ast::AssignmentStatement>(
1898 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1899 Expr(std::forward<RhsExpressionInit>(rhs)));
1900 }
1901
1902 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001903 /// @param lhs the left hand side expression initializer
1904 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001905 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001906 template <typename LhsExpressionInit, typename RhsExpressionInit>
1907 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1908 RhsExpressionInit&& rhs) {
1909 return create<ast::AssignmentStatement>(
1910 Expr(std::forward<LhsExpressionInit>(lhs)),
1911 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001912 }
1913
1914 /// Creates a ast::LoopStatement with input body and optional continuing
1915 /// @param body the loop body
1916 /// @param continuing the optional continuing block
1917 /// @returns the loop statement pointer
1918 ast::LoopStatement* Loop(ast::BlockStatement* body,
1919 ast::BlockStatement* continuing = nullptr) {
1920 return create<ast::LoopStatement>(body, continuing);
1921 }
1922
1923 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001924 /// @param source the source information
1925 /// @param var the variable to wrap in a decl statement
1926 /// @returns the variable decl statement pointer
1927 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1928 return create<ast::VariableDeclStatement>(source, var);
1929 }
1930
1931 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001932 /// @param var the variable to wrap in a decl statement
1933 /// @returns the variable decl statement pointer
1934 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1935 return create<ast::VariableDeclStatement>(var);
1936 }
1937
Antonio Maioranocea744d2021-03-25 12:55:27 +00001938 /// Creates a ast::SwitchStatement with input expression and cases
1939 /// @param condition the condition expression initializer
1940 /// @param cases case statements
1941 /// @returns the switch statement pointer
1942 template <typename ExpressionInit, typename... Cases>
1943 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1944 return create<ast::SwitchStatement>(
1945 Expr(std::forward<ExpressionInit>(condition)),
1946 ast::CaseStatementList{std::forward<Cases>(cases)...});
1947 }
1948
1949 /// Creates a ast::CaseStatement with input list of selectors, and body
1950 /// @param selectors list of selectors
1951 /// @param body the case body
1952 /// @returns the case statement pointer
1953 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1954 ast::BlockStatement* body = nullptr) {
1955 return create<ast::CaseStatement>(std::move(selectors),
1956 body ? body : Block());
1957 }
1958
1959 /// Convenient overload that takes a single selector
1960 /// @param selector a single case selector
1961 /// @param body the case body
1962 /// @returns the case statement pointer
1963 ast::CaseStatement* Case(ast::IntLiteral* selector,
1964 ast::BlockStatement* body = nullptr) {
1965 return Case(ast::CaseSelectorList{selector}, body);
1966 }
1967
1968 /// Convenience function that creates a 'default' ast::CaseStatement
1969 /// @param body the case body
1970 /// @returns the case statement pointer
1971 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1972 return Case(ast::CaseSelectorList{}, body);
1973 }
1974
James Price68f558f2021-04-06 15:51:47 +00001975 /// Creates an ast::BuiltinDecoration
1976 /// @param source the source information
1977 /// @param builtin the builtin value
1978 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001979 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001980 return create<ast::BuiltinDecoration>(source, builtin);
1981 }
1982
1983 /// Creates an ast::BuiltinDecoration
1984 /// @param builtin the builtin value
1985 /// @returns the builtin decoration pointer
1986 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1987 return create<ast::BuiltinDecoration>(source_, builtin);
1988 }
1989
James Price989a8e42021-06-28 23:04:43 +00001990 /// Creates an ast::InterpolateDecoration
1991 /// @param source the source information
1992 /// @param type the interpolation type
1993 /// @param sampling the interpolation sampling
1994 /// @returns the interpolate decoration pointer
1995 ast::InterpolateDecoration* Interpolate(const Source& source,
1996 ast::InterpolationType type,
1997 ast::InterpolationSampling sampling) {
1998 return create<ast::InterpolateDecoration>(source, type, sampling);
1999 }
2000
2001 /// Creates an ast::InterpolateDecoration
2002 /// @param type the interpolation type
2003 /// @param sampling the interpolation sampling
2004 /// @returns the interpolate decoration pointer
2005 ast::InterpolateDecoration* Interpolate(ast::InterpolationType type,
2006 ast::InterpolationSampling sampling) {
2007 return create<ast::InterpolateDecoration>(source_, type, sampling);
2008 }
2009
James Price68f558f2021-04-06 15:51:47 +00002010 /// Creates an ast::LocationDecoration
2011 /// @param source the source information
2012 /// @param location the location value
2013 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002014 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00002015 return create<ast::LocationDecoration>(source, location);
2016 }
2017
2018 /// Creates an ast::LocationDecoration
2019 /// @param location the location value
2020 /// @returns the location decoration pointer
2021 ast::LocationDecoration* Location(uint32_t location) {
2022 return create<ast::LocationDecoration>(source_, location);
2023 }
2024
James Pricef2f3bfc2021-05-13 20:32:32 +00002025 /// Creates an ast::OverrideDecoration with a specific constant ID
2026 /// @param source the source information
2027 /// @param id the id value
2028 /// @returns the override decoration pointer
2029 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
2030 return create<ast::OverrideDecoration>(source, id);
2031 }
2032
2033 /// Creates an ast::OverrideDecoration with a specific constant ID
2034 /// @param id the optional id value
2035 /// @returns the override decoration pointer
2036 ast::OverrideDecoration* Override(uint32_t id) {
2037 return Override(source_, id);
2038 }
2039
2040 /// Creates an ast::OverrideDecoration without a constant ID
2041 /// @param source the source information
2042 /// @returns the override decoration pointer
2043 ast::OverrideDecoration* Override(const Source& source) {
2044 return create<ast::OverrideDecoration>(source);
2045 }
2046
2047 /// Creates an ast::OverrideDecoration without a constant ID
2048 /// @returns the override decoration pointer
2049 ast::OverrideDecoration* Override() { return Override(source_); }
2050
James Price68f558f2021-04-06 15:51:47 +00002051 /// Creates an ast::StageDecoration
2052 /// @param source the source information
2053 /// @param stage the pipeline stage
2054 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00002055 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00002056 return create<ast::StageDecoration>(source, stage);
2057 }
2058
2059 /// Creates an ast::StageDecoration
2060 /// @param stage the pipeline stage
2061 /// @returns the stage decoration pointer
2062 ast::StageDecoration* Stage(ast::PipelineStage stage) {
2063 return create<ast::StageDecoration>(source_, stage);
2064 }
2065
James Price70f80bb2021-05-19 13:40:08 +00002066 /// Creates an ast::WorkgroupDecoration
2067 /// @param x the x dimension expression
2068 /// @returns the workgroup decoration pointer
2069 template <typename EXPR_X>
2070 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x) {
2071 return WorkgroupSize(std::forward<EXPR_X>(x), nullptr, nullptr);
2072 }
2073
2074 /// Creates an ast::WorkgroupDecoration
2075 /// @param x the x dimension expression
2076 /// @param y the y dimension expression
2077 /// @returns the workgroup decoration pointer
2078 template <typename EXPR_X, typename EXPR_Y>
2079 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y) {
2080 return WorkgroupSize(std::forward<EXPR_X>(x), std::forward<EXPR_Y>(y),
2081 nullptr);
2082 }
2083
2084 /// Creates an ast::WorkgroupDecoration
Ben Clayton241c16d2021-06-09 18:53:57 +00002085 /// @param source the source information
2086 /// @param x the x dimension expression
2087 /// @param y the y dimension expression
2088 /// @param z the z dimension expression
2089 /// @returns the workgroup decoration pointer
2090 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2091 ast::WorkgroupDecoration* WorkgroupSize(const Source& source,
2092 EXPR_X&& x,
2093 EXPR_Y&& y,
2094 EXPR_Z&& z) {
2095 return create<ast::WorkgroupDecoration>(
2096 source, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2097 Expr(std::forward<EXPR_Z>(z)));
2098 }
2099
2100 /// Creates an ast::WorkgroupDecoration
James Price70f80bb2021-05-19 13:40:08 +00002101 /// @param x the x dimension expression
2102 /// @param y the y dimension expression
2103 /// @param z the z dimension expression
2104 /// @returns the workgroup decoration pointer
2105 template <typename EXPR_X, typename EXPR_Y, typename EXPR_Z>
2106 ast::WorkgroupDecoration* WorkgroupSize(EXPR_X&& x, EXPR_Y&& y, EXPR_Z&& z) {
2107 return create<ast::WorkgroupDecoration>(
2108 source_, Expr(std::forward<EXPR_X>(x)), Expr(std::forward<EXPR_Y>(y)),
2109 Expr(std::forward<EXPR_Z>(z)));
2110 }
2111
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002112 /// Sets the current builder source to `src`
2113 /// @param src the Source used for future create() calls
2114 void SetSource(const Source& src) {
2115 AssertNotMoved();
2116 source_ = src;
2117 }
2118
2119 /// Sets the current builder source to `loc`
2120 /// @param loc the Source used for future create() calls
2121 void SetSource(const Source::Location& loc) {
2122 AssertNotMoved();
2123 source_ = Source(loc);
2124 }
2125
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002126 /// Marks that the given transform has been applied to this program.
2127 /// @param transform the transform that has been applied
2128 void SetTransformApplied(const CastableBase* transform) {
2129 transforms_applied_.emplace(&transform->TypeInfo());
2130 }
2131
2132 /// Marks that the given transform `T` has been applied to this program.
2133 template <typename T>
2134 void SetTransformApplied() {
2135 transforms_applied_.emplace(&TypeInfo::Of<T>());
2136 }
2137
2138 /// Marks that the transforms with the given TypeInfos have been applied to
2139 /// this program.
2140 /// @param transforms the set of transform TypeInfos that has been applied
2141 void SetTransformApplied(
2142 const std::unordered_set<const TypeInfo*>& transforms) {
2143 for (auto* transform : transforms) {
2144 transforms_applied_.emplace(transform);
2145 }
2146 }
2147
2148 /// @returns true if the transform of type `T` was applied.
2149 template <typename T>
2150 bool HasTransformApplied() {
2151 return transforms_applied_.count(&TypeInfo::Of<T>());
2152 }
2153
2154 /// @return the TypeInfo pointers of all transforms that have been applied to
2155 /// this program.
2156 std::unordered_set<const TypeInfo*> TransformsApplied() const {
2157 return transforms_applied_;
2158 }
2159
Ben Clayton33352542021-01-29 16:43:41 +00002160 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00002161 /// @note As the Resolver is run when the Program is built, this will only be
2162 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00002163 /// @param expr the AST expression
2164 /// @return the resolved semantic type for the expression, or nullptr if the
2165 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00002166 sem::Type* TypeOf(const ast::Expression* expr) const;
2167
Ben Claytonfe0910f2021-05-17 15:51:47 +00002168 /// Helper for returning the resolved semantic type of the variable `var`.
2169 /// @note As the Resolver is run when the Program is built, this will only be
2170 /// useful for the Resolver itself and tests that use their own Resolver.
2171 /// @param var the AST variable
2172 /// @return the resolved semantic type for the variable, or nullptr if the
2173 /// variable has no resolved type.
2174 sem::Type* TypeOf(const ast::Variable* var) const;
2175
Ben Claytonfbec46f2021-04-30 20:20:19 +00002176 /// Helper for returning the resolved semantic type of the AST type `type`.
2177 /// @note As the Resolver is run when the Program is built, this will only be
2178 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton8758f102021-06-09 14:32:14 +00002179 /// @param type the AST type
Ben Claytonfbec46f2021-04-30 20:20:19 +00002180 /// @return the resolved semantic type for the type, or nullptr if the type
2181 /// has no resolved type.
Ben Clayton8758f102021-06-09 14:32:14 +00002182 const sem::Type* TypeOf(const ast::Type* type) const;
2183
2184 /// Helper for returning the resolved semantic type of the AST type
2185 /// declaration `type_decl`.
2186 /// @note As the Resolver is run when the Program is built, this will only be
2187 /// useful for the Resolver itself and tests that use their own Resolver.
2188 /// @param type_decl the AST type declaration
2189 /// @return the resolved semantic type for the type declaration, or nullptr if
2190 /// the type declaration has no resolved type.
2191 const sem::Type* TypeOf(const ast::TypeDecl* type_decl) const;
Ben Clayton33352542021-01-29 16:43:41 +00002192
Ben Clayton169512e2021-04-17 05:52:11 +00002193 /// Wraps the ast::Literal in a statement. This is used by tests that
2194 /// construct a partial AST and require the Resolver to reach these
2195 /// nodes.
2196 /// @param lit the ast::Literal to be wrapped by an ast::Statement
2197 /// @return the ast::Statement that wraps the ast::Statement
2198 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00002199 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00002200 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00002201 /// nodes.
2202 /// @param expr the ast::Expression to be wrapped by an ast::Statement
2203 /// @return the ast::Statement that wraps the ast::Expression
2204 ast::Statement* WrapInStatement(ast::Expression* expr);
2205 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00002206 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00002207 /// these nodes.
2208 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
2209 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
2210 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
2211 /// Returns the statement argument. Used as a passthrough-overload by
2212 /// WrapInFunction().
2213 /// @param stmt the ast::Statement
2214 /// @return `stmt`
2215 ast::Statement* WrapInStatement(ast::Statement* stmt);
2216 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00002217 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00002218 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002219 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00002220 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002221 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00002222 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002223 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00002224 }
2225 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00002226 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00002227 /// @returns the function
2228 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00002229
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002230 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00002231 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002232
2233 protected:
2234 /// Asserts that the builder has not been moved.
2235 void AssertNotMoved() const;
2236
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002237 private:
Ben Claytone6995de2021-04-13 23:27:27 +00002238 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00002239 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00002240 ASTNodeAllocator ast_nodes_;
2241 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002242 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00002243 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002244 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002245 diag::List diagnostics_;
Ben Claytonb5cd10c2021-06-25 10:26:26 +00002246 std::unordered_set<const TypeInfo*> transforms_applied_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002247
2248 /// The source to use when creating AST nodes without providing a Source as
2249 /// the first argument.
2250 Source source_;
2251
Ben Clayton5f0ea112021-03-09 10:54:37 +00002252 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002253 /// program when built.
2254 bool resolve_on_build_ = true;
2255
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002256 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2257 bool moved_ = false;
2258};
2259
2260//! @cond Doxygen_Suppress
2261// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2262template <>
2263struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002264 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002265 return t->i32();
2266 }
2267};
2268template <>
2269struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002270 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002271 return t->u32();
2272 }
2273};
2274template <>
2275struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton19d32052021-05-20 15:10:48 +00002276 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002277 return t->f32();
2278 }
2279};
2280template <>
2281struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton19d32052021-05-20 15:10:48 +00002282 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002283 return t->bool_();
2284 }
2285};
2286template <>
2287struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton19d32052021-05-20 15:10:48 +00002288 static ast::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002289 return t->void_();
2290 }
2291};
2292//! @endcond
2293
Ben Clayton917b14b2021-04-19 16:50:23 +00002294/// @param builder the ProgramBuilder
2295/// @returns the ProgramID of the ProgramBuilder
2296inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2297 return builder->ID();
2298}
2299
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002300} // namespace tint
2301
2302#endif // SRC_PROGRAM_BUILDER_H_