blob: 74367bcbeb097079782a4071c7636e3a1804a57c [file] [log] [blame]
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001// Copyright 2021 The Tint Authors.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SRC_PROGRAM_BUILDER_H_
16#define SRC_PROGRAM_BUILDER_H_
17
18#include <string>
19#include <utility>
20
Ben Claytone204f272021-04-22 14:40:23 +000021#include "src/ast/alias.h"
Ben Clayton7241a502021-04-22 14:32:53 +000022#include "src/ast/array.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000024#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000025#include "src/ast/binary_expression.h"
Ben Claytona922e652021-04-21 13:37:22 +000026#include "src/ast/bool.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/bool_literal.h"
28#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000029#include "src/ast/case_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000030#include "src/ast/depth_texture.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000031#include "src/ast/external_texture.h"
Ben Claytona922e652021-04-21 13:37:22 +000032#include "src/ast/f32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/float_literal.h"
Ben Claytona922e652021-04-21 13:37:22 +000034#include "src/ast/i32.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000035#include "src/ast/if_statement.h"
36#include "src/ast/loop_statement.h"
Ben Claytonfdb91fd2021-04-22 14:30:53 +000037#include "src/ast/matrix.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/member_accessor_expression.h"
39#include "src/ast/module.h"
Ben Claytone204f272021-04-22 14:40:23 +000040#include "src/ast/multisampled_texture.h"
James Pricef2f3bfc2021-05-13 20:32:32 +000041#include "src/ast/override_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000042#include "src/ast/pointer.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000043#include "src/ast/return_statement.h"
Ben Claytone204f272021-04-22 14:40:23 +000044#include "src/ast/sampled_texture.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000045#include "src/ast/scalar_constructor_expression.h"
46#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000047#include "src/ast/stage_decoration.h"
Ben Claytone204f272021-04-22 14:40:23 +000048#include "src/ast/storage_texture.h"
Ben Claytonbab31972021-02-08 21:16:21 +000049#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000050#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000051#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000052#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000053#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000054#include "src/ast/type_constructor_expression.h"
Ben Clayton0f88b312021-05-04 17:36:31 +000055#include "src/ast/type_name.h"
Ben Claytona922e652021-04-21 13:37:22 +000056#include "src/ast/u32.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000057#include "src/ast/uint_literal.h"
Ben Claytonfe0910f2021-05-17 15:51:47 +000058#include "src/ast/unary_op_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000059#include "src/ast/variable_decl_statement.h"
Ben Claytone30ffeb2021-04-22 14:24:43 +000060#include "src/ast/vector.h"
Ben Claytona922e652021-04-21 13:37:22 +000061#include "src/ast/void.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000062#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000063#include "src/program_id.h"
Ben Clayton4cd5eea2021-05-07 20:58:34 +000064#include "src/sem/array.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000065#include "src/sem/bool_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000066#include "src/sem/depth_texture_type.h"
Antonio Maiorano4b16a162021-04-27 17:32:37 +000067#include "src/sem/external_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000068#include "src/sem/f32_type.h"
69#include "src/sem/i32_type.h"
70#include "src/sem/matrix_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000071#include "src/sem/multisampled_texture_type.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000072#include "src/sem/pointer_type.h"
Ben Claytone204f272021-04-22 14:40:23 +000073#include "src/sem/sampled_texture_type.h"
74#include "src/sem/storage_texture_type.h"
Ben Claytonba6ab5e2021-05-07 14:49:34 +000075#include "src/sem/struct.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000076#include "src/sem/u32_type.h"
77#include "src/sem/vector_type.h"
78#include "src/sem/void_type.h"
Ben Claytona922e652021-04-21 13:37:22 +000079#include "src/typepair.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000080
81namespace tint {
82
Ben Clayton401b96b2021-02-03 17:19:59 +000083// Forward declarations
84namespace ast {
85class VariableDeclStatement;
86} // namespace ast
87
Ben Claytona6b9a8e2021-01-26 16:57:10 +000088class CloneContext;
89
90/// ProgramBuilder is a mutable builder for a Program.
91/// To construct a Program, populate the builder and then `std::move` it to a
92/// Program.
93class ProgramBuilder {
94 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000095 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
96 using ASTNodeAllocator = BlockAllocator<ast::Node>;
97
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000098 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
99 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000100
101 /// `i32` is a type alias to `int`.
102 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
103 /// WGSL syntax.
104 /// Note: this is intentionally not aliased to uint32_t as we want integer
105 /// literals passed to the builder to match WGSL's integer literal types.
106 using i32 = decltype(1);
107 /// `u32` is a type alias to `unsigned int`.
108 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
109 /// WGSL syntax.
110 /// Note: this is intentionally not aliased to uint32_t as we want integer
111 /// literals passed to the builder to match WGSL's integer literal types.
112 using u32 = decltype(1u);
113 /// `f32` is a type alias to `float`
114 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
115 /// WGSL syntax.
116 using f32 = float;
117
118 /// Constructor
119 ProgramBuilder();
120
121 /// Move constructor
122 /// @param rhs the builder to move
123 ProgramBuilder(ProgramBuilder&& rhs);
124
125 /// Destructor
126 virtual ~ProgramBuilder();
127
128 /// Move assignment operator
129 /// @param rhs the builder to move
130 /// @return this builder
131 ProgramBuilder& operator=(ProgramBuilder&& rhs);
132
Ben Claytone43c8302021-01-29 11:59:32 +0000133 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
134 /// making a deep clone of the Program contents.
135 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
136 /// existing immutable program.
137 /// As the returned ProgramBuilder wraps `program`, `program` must not be
138 /// destructed or assigned while using the returned ProgramBuilder.
139 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
140 /// function. See crbug.com/tint/460.
141 /// @param program the immutable Program to wrap
142 /// @return the ProgramBuilder that wraps `program`
143 static ProgramBuilder Wrap(const Program* program);
144
Ben Claytone6995de2021-04-13 23:27:27 +0000145 /// @returns the unique identifier for this program
146 ProgramID ID() const { return id_; }
147
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000148 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000149 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000150 AssertNotMoved();
151 return types_;
152 }
153
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000154 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000155 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000156 AssertNotMoved();
157 return types_;
158 }
159
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000160 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000161 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000162 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000163 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000164 }
165
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000166 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000167 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000168 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000169 return ast_nodes_;
170 }
171
172 /// @returns a reference to the program's semantic nodes storage
173 SemNodeAllocator& SemNodes() {
174 AssertNotMoved();
175 return sem_nodes_;
176 }
177
178 /// @returns a reference to the program's semantic nodes storage
179 const SemNodeAllocator& SemNodes() const {
180 AssertNotMoved();
181 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000182 }
183
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000184 /// @returns a reference to the program's AST root Module
185 ast::Module& AST() {
186 AssertNotMoved();
187 return *ast_;
188 }
189
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000190 /// @returns a reference to the program's AST root Module
191 const ast::Module& AST() const {
192 AssertNotMoved();
193 return *ast_;
194 }
195
196 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000197 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000198 AssertNotMoved();
199 return sem_;
200 }
201
202 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000203 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000204 AssertNotMoved();
205 return sem_;
206 }
207
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000208 /// @returns a reference to the program's SymbolTable
209 SymbolTable& Symbols() {
210 AssertNotMoved();
211 return symbols_;
212 }
213
Ben Clayton708dc2d2021-01-29 11:22:40 +0000214 /// @returns a reference to the program's SymbolTable
215 const SymbolTable& Symbols() const {
216 AssertNotMoved();
217 return symbols_;
218 }
219
Ben Clayton844217f2021-01-27 18:49:05 +0000220 /// @returns a reference to the program's diagnostics
221 diag::List& Diagnostics() {
222 AssertNotMoved();
223 return diagnostics_;
224 }
225
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000226 /// @returns a reference to the program's diagnostics
227 const diag::List& Diagnostics() const {
228 AssertNotMoved();
229 return diagnostics_;
230 }
231
Ben Clayton5f0ea112021-03-09 10:54:37 +0000232 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000233 /// @param enable the new flag value (defaults to true)
234 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
235
Ben Clayton5f0ea112021-03-09 10:54:37 +0000236 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000237 /// built.
238 bool ResolveOnBuild() const { return resolve_on_build_; }
239
Ben Clayton844217f2021-01-27 18:49:05 +0000240 /// @returns true if the program has no error diagnostics and is not missing
241 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000242 bool IsValid() const;
243
Ben Clayton708dc2d2021-01-29 11:22:40 +0000244 /// Writes a representation of the node to the output stream
245 /// @note unlike str(), to_str() does not automatically demangle the string.
246 /// @param node the AST node
247 /// @param out the stream to write to
248 /// @param indent number of spaces to indent the node when writing
249 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
250 node->to_str(Sem(), out, indent);
251 }
252
253 /// Returns a demangled, string representation of `node`.
254 /// @param node the AST node
255 /// @returns a string representation of the node
256 std::string str(const ast::Node* node) const;
257
Ben Clayton7fdfff12021-01-29 15:17:30 +0000258 /// Creates a new ast::Node owned by the ProgramBuilder. When the
259 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000260 /// @param source the Source of the node
261 /// @param args the arguments to pass to the type constructor
262 /// @returns the node pointer
263 template <typename T, typename... ARGS>
264 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
265 ARGS&&... args) {
266 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000267 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000268 }
269
Ben Clayton7fdfff12021-01-29 15:17:30 +0000270 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
271 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000272 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000273 /// When the ProgramBuilder is destructed, the ast::Node will also be
274 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000275 /// @returns the node pointer
276 template <typename T>
277 traits::EnableIfIsType<T, ast::Node>* create() {
278 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000279 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000280 }
281
Ben Clayton7fdfff12021-01-29 15:17:30 +0000282 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
283 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000284 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000285 /// When the ProgramBuilder is destructed, the ast::Node will also be
286 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000287 /// @param arg0 the first arguments to pass to the type constructor
288 /// @param args the remaining arguments to pass to the type constructor
289 /// @returns the node pointer
290 template <typename T, typename ARG0, typename... ARGS>
291 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
292 traits::IsTypeOrDerived<T, ast::Node>::value &&
293 !traits::IsTypeOrDerived<ARG0, Source>::value,
294 T>*
295 create(ARG0&& arg0, ARGS&&... args) {
296 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000297 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000298 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000299 }
300
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000301 /// Creates a new sem::Node owned by the ProgramBuilder.
302 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000303 /// destructed.
304 /// @param args the arguments to pass to the type constructor
305 /// @returns the node pointer
306 template <typename T, typename... ARGS>
Ben Clayton58750ea2021-05-06 15:52:33 +0000307 traits::EnableIf<traits::IsTypeOrDerived<T, sem::Node>::value &&
308 !traits::IsTypeOrDerived<T, sem::Type>::value,
309 T>*
310 create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000311 AssertNotMoved();
312 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
313 }
314
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000315 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000316 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
317 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000318 /// Types are unique (de-aliased), and so calling create() for the same `T`
319 /// and arguments will return the same pointer.
320 /// @warning Use this method to acquire a type only if all of its type
321 /// information is provided in the constructor arguments `args`.<br>
322 /// If the type requires additional configuration after construction that
323 /// affect its fundamental type, build the type with `std::make_unique`, make
324 /// any necessary alterations and then call unique_type() instead.
325 /// @param args the arguments to pass to the type constructor
326 /// @returns the de-aliased type pointer
327 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000328 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
329 static_assert(std::is_base_of<sem::Type, T>::value,
330 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000331 AssertNotMoved();
332 return types_.Get<T>(std::forward<ARGS>(args)...);
333 }
334
335 /// Marks this builder as moved, preventing any further use of the builder.
336 void MarkAsMoved();
337
338 //////////////////////////////////////////////////////////////////////////////
339 // TypesBuilder
340 //////////////////////////////////////////////////////////////////////////////
341
342 /// TypesBuilder holds basic `tint` types and methods for constructing
343 /// complex types.
344 class TypesBuilder {
345 public:
346 /// Constructor
347 /// @param builder the program builder
348 explicit TypesBuilder(ProgramBuilder* builder);
349
350 /// @return the tint AST type for the C type `T`.
351 template <typename T>
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000352 typ::Type Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000353 return CToAST<T>::get(this);
354 }
355
356 /// @returns a boolean type
Ben Claytona922e652021-04-21 13:37:22 +0000357 typ::Bool bool_() const {
358 return {builder->create<ast::Bool>(), builder->create<sem::Bool>()};
359 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000360
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000361 /// @param source the Source of the node
362 /// @returns a boolean type
363 typ::Bool bool_(const Source& source) const {
364 return {builder->create<ast::Bool>(source), builder->create<sem::Bool>()};
365 }
366
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000367 /// @returns a f32 type
Ben Claytona922e652021-04-21 13:37:22 +0000368 typ::F32 f32() const {
369 return {builder->create<ast::F32>(), builder->create<sem::F32>()};
370 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000371
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000372 /// @param source the Source of the node
373 /// @returns a f32 type
374 typ::F32 f32(const Source& source) const {
375 return {builder->create<ast::F32>(source), builder->create<sem::F32>()};
376 }
377
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000378 /// @returns a i32 type
Ben Claytona922e652021-04-21 13:37:22 +0000379 typ::I32 i32() const {
380 return {builder->create<ast::I32>(), builder->create<sem::I32>()};
381 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000382
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000383 /// @param source the Source of the node
384 /// @returns a i32 type
385 typ::I32 i32(const Source& source) const {
386 return {builder->create<ast::I32>(source), builder->create<sem::I32>()};
387 }
388
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000389 /// @returns a u32 type
Ben Claytona922e652021-04-21 13:37:22 +0000390 typ::U32 u32() const {
391 return {builder->create<ast::U32>(), builder->create<sem::U32>()};
392 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000393
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000394 /// @param source the Source of the node
395 /// @returns a u32 type
396 typ::U32 u32(const Source& source) const {
397 return {builder->create<ast::U32>(source), builder->create<sem::U32>()};
398 }
399
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000400 /// @returns a void type
Ben Claytona922e652021-04-21 13:37:22 +0000401 typ::Void void_() const {
402 return {builder->create<ast::Void>(), builder->create<sem::Void>()};
403 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000404
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000405 /// @param source the Source of the node
406 /// @returns a void type
407 typ::Void void_(const Source& source) const {
408 return {builder->create<ast::Void>(source), builder->create<sem::Void>()};
409 }
410
Antonio Maiorano25436862021-04-13 13:32:33 +0000411 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000412 /// @param n vector width in elements
413 /// @return the tint AST type for a `n`-element vector of `type`.
414 typ::Vector vec(typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000415 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000416 return {type.ast ? builder->create<ast::Vector>(type, n) : nullptr,
417 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano25436862021-04-13 13:32:33 +0000418 }
419
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000420 /// @param source the Source of the node
421 /// @param type vector subtype
422 /// @param n vector width in elements
423 /// @return the tint AST type for a `n`-element vector of `type`.
424 typ::Vector vec(const Source& source, typ::Type type, uint32_t n) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000425 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000426 return {
427 type.ast ? builder->create<ast::Vector>(source, type, n) : nullptr,
428 type.sem ? builder->create<sem::Vector>(type, n) : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000429 }
430
Antonio Maiorano25436862021-04-13 13:32:33 +0000431 /// @param type vector subtype
Ben Claytone204f272021-04-22 14:40:23 +0000432 /// @return the tint AST type for a 2-element vector of `type`.
433 typ::Vector vec2(typ::Type type) const { return vec(type, 2u); }
434
435 /// @param type vector subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000436 /// @return the tint AST type for a 3-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000437 typ::Vector vec3(typ::Type type) const { return vec(type, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000438
439 /// @param type vector subtype
440 /// @return the tint AST type for a 4-element vector of `type`.
Ben Claytone204f272021-04-22 14:40:23 +0000441 typ::Vector vec4(typ::Type type) const { return vec(type, 4u); }
442
443 /// @param n vector width in elements
444 /// @return the tint AST type for a `n`-element vector of `type`.
445 template <typename T>
446 typ::Vector vec(uint32_t n) const {
447 return vec(Of<T>(), n);
Antonio Maiorano25436862021-04-13 13:32:33 +0000448 }
449
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000450 /// @return the tint AST type for a 2-element vector of the C type `T`.
451 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000452 typ::Vector vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000453 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000454 }
455
456 /// @return the tint AST type for a 3-element vector of the C type `T`.
457 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000458 typ::Vector vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000459 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000460 }
461
462 /// @return the tint AST type for a 4-element vector of the C type `T`.
463 template <typename T>
Ben Claytone30ffeb2021-04-22 14:24:43 +0000464 typ::Vector vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000465 return vec4(Of<T>());
466 }
467
468 /// @param type matrix subtype
Ben Claytone204f272021-04-22 14:40:23 +0000469 /// @param columns number of columns for the matrix
470 /// @param rows number of rows for the matrix
471 /// @return the tint AST type for a matrix of `type`
472 typ::Matrix mat(typ::Type type, uint32_t columns, uint32_t rows) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000473 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000474 return {type.ast ? builder->create<ast::Matrix>(type, rows, columns)
475 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000476 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000477 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000478 }
479
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000480 /// @param source the Source of the node
481 /// @param type matrix subtype
482 /// @param columns number of columns for the matrix
483 /// @param rows number of rows for the matrix
484 /// @return the tint AST type for a matrix of `type`
485 typ::Matrix mat(const Source& source,
486 typ::Type type,
487 uint32_t columns,
488 uint32_t rows) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000489 return {type.ast
490 ? builder->create<ast::Matrix>(source, type, rows, columns)
491 : nullptr,
Ben Clayton6c1cf652021-05-05 16:48:32 +0000492 type.sem ? builder->create<sem::Matrix>(vec(type, rows), columns)
Ben Clayton95c2e952021-04-28 12:58:13 +0000493 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000494 }
495
Ben Claytone204f272021-04-22 14:40:23 +0000496 /// @param type matrix subtype
Antonio Maiorano25436862021-04-13 13:32:33 +0000497 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000498 typ::Matrix mat2x2(typ::Type type) const { return mat(type, 2u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000499
500 /// @param type matrix subtype
501 /// @return the tint AST type for a 2x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000502 typ::Matrix mat2x3(typ::Type type) const { return mat(type, 2u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000503
504 /// @param type matrix subtype
505 /// @return the tint AST type for a 2x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000506 typ::Matrix mat2x4(typ::Type type) const { return mat(type, 2u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000507
508 /// @param type matrix subtype
509 /// @return the tint AST type for a 3x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000510 typ::Matrix mat3x2(typ::Type type) const { return mat(type, 3u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000511
512 /// @param type matrix subtype
513 /// @return the tint AST type for a 3x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000514 typ::Matrix mat3x3(typ::Type type) const { return mat(type, 3u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000515
516 /// @param type matrix subtype
517 /// @return the tint AST type for a 3x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000518 typ::Matrix mat3x4(typ::Type type) const { return mat(type, 3u, 4u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000519
520 /// @param type matrix subtype
521 /// @return the tint AST type for a 4x2 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000522 typ::Matrix mat4x2(typ::Type type) const { return mat(type, 4u, 2u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000523
524 /// @param type matrix subtype
525 /// @return the tint AST type for a 4x3 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000526 typ::Matrix mat4x3(typ::Type type) const { return mat(type, 4u, 3u); }
Antonio Maiorano25436862021-04-13 13:32:33 +0000527
528 /// @param type matrix subtype
529 /// @return the tint AST type for a 4x4 matrix of `type`.
Ben Clayton95c2e952021-04-28 12:58:13 +0000530 typ::Matrix mat4x4(typ::Type type) const { return mat(type, 4u, 4u); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000531
Ben Claytone204f272021-04-22 14:40:23 +0000532 /// @param columns number of columns for the matrix
533 /// @param rows number of rows for the matrix
534 /// @return the tint AST type for a matrix of `type`
535 template <typename T>
536 typ::Matrix mat(uint32_t columns, uint32_t rows) const {
537 return mat(Of<T>(), columns, rows);
538 }
539
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000540 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
541 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000542 typ::Matrix mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000543 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000544 }
545
546 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
547 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000548 typ::Matrix mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000549 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000550 }
551
552 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
553 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000554 typ::Matrix mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000555 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000556 }
557
558 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
559 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000560 typ::Matrix mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000561 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000562 }
563
564 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
565 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000566 typ::Matrix mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000567 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000568 }
569
570 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
571 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000572 typ::Matrix mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000573 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000574 }
575
576 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
577 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000578 typ::Matrix mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000579 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000580 }
581
582 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
583 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000584 typ::Matrix mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000585 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000586 }
587
588 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
589 template <typename T>
Ben Claytonfdb91fd2021-04-22 14:30:53 +0000590 typ::Matrix mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000591 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000592 }
593
594 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000595 /// @param n the array size. 0 represents a runtime-array
596 /// @param decos the optional decorations for the array
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000597 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000598 ast::Array* array(typ::Type subtype,
599 uint32_t n = 0,
600 ast::DecorationList decos = {}) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000601 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000602 return builder->create<ast::Array>(subtype, n, decos);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000603 }
604
605 /// @param source the Source of the node
606 /// @param subtype the array element type
607 /// @param n the array size. 0 represents a runtime-array
608 /// @param decos the optional decorations for the array
609 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000610 ast::Array* array(const Source& source,
611 typ::Type subtype,
612 uint32_t n = 0,
613 ast::DecorationList decos = {}) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000614 subtype = MaybeCreateTypename(subtype);
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000615 return builder->create<ast::Array>(source, subtype, n, decos);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000616 }
617
Ben Claytonbab31972021-02-08 21:16:21 +0000618 /// @param subtype the array element type
Ben Claytone204f272021-04-22 14:40:23 +0000619 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000620 /// @param stride the array stride. 0 represents implicit stride
Ben Claytonbab31972021-02-08 21:16:21 +0000621 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000622 ast::Array* array(typ::Type subtype, uint32_t n, uint32_t stride) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000623 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000624 ast::DecorationList decos;
625 if (stride) {
626 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
627 }
628 return array(subtype, n, std::move(decos));
Ben Claytonbab31972021-02-08 21:16:21 +0000629 }
630
Ben Clayton0f88b312021-05-04 17:36:31 +0000631 /// @param source the Source of the node
632 /// @param subtype the array element type
633 /// @param n the array size. 0 represents a runtime-array
Ben Claytoncbbe5762021-05-10 17:25:21 +0000634 /// @param stride the array stride. 0 represents implicit stride
Ben Clayton0f88b312021-05-04 17:36:31 +0000635 /// @return the tint AST type for a array of size `n` of type `T`
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000636 ast::Array* array(const Source& source,
637 typ::Type subtype,
638 uint32_t n,
639 uint32_t stride) const {
Ben Clayton0f88b312021-05-04 17:36:31 +0000640 subtype = MaybeCreateTypename(subtype);
Ben Claytoncbbe5762021-05-10 17:25:21 +0000641 ast::DecorationList decos;
642 if (stride) {
643 decos.emplace_back(builder->create<ast::StrideDecoration>(stride));
644 }
645 return array(source, subtype, n, std::move(decos));
Ben Clayton0f88b312021-05-04 17:36:31 +0000646 }
647
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000648 /// @return the tint AST type for an array of size `N` of type `T`
649 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000650 ast::Array* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000651 return array(Of<T>(), N);
652 }
653
Ben Claytonbab31972021-02-08 21:16:21 +0000654 /// @param stride the array stride
655 /// @return the tint AST type for an array of size `N` of type `T`
656 template <typename T, int N = 0>
Ben Clayton4cd5eea2021-05-07 20:58:34 +0000657 ast::Array* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000658 return array(Of<T>(), N, stride);
659 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000660
Ben Clayton0f88b312021-05-04 17:36:31 +0000661 /// Creates a type name
662 /// @param name the name
663 /// @returns the type name
664 template <typename NAME>
665 ast::TypeName* type_name(NAME&& name) const {
666 return builder->create<ast::TypeName>(
667 builder->Sym(std::forward<NAME>(name)));
668 }
669
670 /// Creates a type name
671 /// @param source the Source of the node
672 /// @param name the name
673 /// @returns the type name
674 template <typename NAME>
675 ast::TypeName* type_name(const Source& source, NAME&& name) const {
676 return builder->create<ast::TypeName>(
677 source, builder->Sym(std::forward<NAME>(name)));
678 }
679
Ben Clayton42d1e092021-02-02 14:29:15 +0000680 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000681 /// @param name the alias name
682 /// @param type the alias type
683 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000684 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000685 ast::Alias* alias(NAME&& name, typ::Type type) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000686 type = MaybeCreateTypename(type);
Ben Claytone204f272021-04-22 14:40:23 +0000687 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000688 return builder->create<ast::Alias>(sym, type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000689 }
690
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000691 /// Creates an alias type
692 /// @param source the Source of the node
693 /// @param name the alias name
694 /// @param type the alias type
695 /// @returns the alias pointer
696 template <typename NAME>
Ben Claytona34fa0e2021-05-10 17:38:01 +0000697 ast::Alias* alias(const Source& source, NAME&& name, typ::Type type) const {
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000698 type = MaybeCreateTypename(type);
699 auto sym = builder->Sym(std::forward<NAME>(name));
Ben Claytona34fa0e2021-05-10 17:38:01 +0000700 return builder->create<ast::Alias>(source, sym, type);
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000701 }
702
Ben Clayton4db10dd2021-04-16 08:47:14 +0000703 /// Creates an access control qualifier type
704 /// @param access the access control
705 /// @param type the inner type
706 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000707 ast::AccessControl* access(ast::AccessControl::Access access,
708 const ast::Type* type) const {
709 type = MaybeCreateTypename(type).ast;
710 return type ? builder->create<ast::AccessControl>(access, type) : nullptr;
Ben Clayton4db10dd2021-04-16 08:47:14 +0000711 }
712
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000713 /// Creates an access control qualifier type
714 /// @param source the Source of the node
715 /// @param access the access control
716 /// @param type the inner type
717 /// @returns the access control qualifier type
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000718 ast::AccessControl* access(const Source& source,
719 ast::AccessControl::Access access,
720 const ast::Type* type) const {
721 type = MaybeCreateTypename(type).ast;
722 return type ? builder->create<ast::AccessControl>(source, access, type)
723 : nullptr;
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000724 }
725
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000726 /// @param type the type of the pointer
727 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000728 /// @return the pointer to `type` with the given ast::StorageClass
729 typ::Pointer pointer(typ::Type type,
730 ast::StorageClass storage_class) const {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000731 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000732 return {type.ast ? builder->create<ast::Pointer>(type, storage_class)
733 : nullptr,
734 type.sem ? builder->create<sem::Pointer>(type, storage_class)
735 : nullptr};
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000736 }
737
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000738 /// @param source the Source of the node
739 /// @param type the type of the pointer
740 /// @param storage_class the storage class of the pointer
741 /// @return the pointer to `type` with the given ast::StorageClass
742 typ::Pointer pointer(const Source& source,
743 typ::Type type,
744 ast::StorageClass storage_class) const {
745 type = MaybeCreateTypename(type);
Ben Clayton95c2e952021-04-28 12:58:13 +0000746 return {type.ast
747 ? builder->create<ast::Pointer>(source, type, storage_class)
748 : nullptr,
749 type.sem ? builder->create<sem::Pointer>(type, storage_class)
750 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000751 }
752
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000753 /// @param storage_class the storage class of the pointer
Ben Claytone204f272021-04-22 14:40:23 +0000754 /// @return the pointer to type `T` with the given ast::StorageClass.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000755 template <typename T>
Ben Claytone204f272021-04-22 14:40:23 +0000756 typ::Pointer pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000757 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000758 }
759
Ben Claytone204f272021-04-22 14:40:23 +0000760 /// @param kind the kind of sampler
761 /// @returns the sampler
762 typ::Sampler sampler(ast::SamplerKind kind) const {
763 return {builder->create<ast::Sampler>(kind),
764 builder->create<sem::Sampler>(kind)};
765 }
766
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000767 /// @param source the Source of the node
768 /// @param kind the kind of sampler
769 /// @returns the sampler
770 typ::Sampler sampler(const Source& source, ast::SamplerKind kind) const {
771 return {builder->create<ast::Sampler>(source, kind),
772 builder->create<sem::Sampler>(kind)};
773 }
774
Ben Claytone204f272021-04-22 14:40:23 +0000775 /// @param dims the dimensionality of the texture
776 /// @returns the depth texture
777 typ::DepthTexture depth_texture(ast::TextureDimension dims) const {
778 return {builder->create<ast::DepthTexture>(dims),
779 builder->create<sem::DepthTexture>(dims)};
780 }
781
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000782 /// @param source the Source of the node
783 /// @param dims the dimensionality of the texture
784 /// @returns the depth texture
785 typ::DepthTexture depth_texture(const Source& source,
786 ast::TextureDimension dims) const {
787 return {builder->create<ast::DepthTexture>(source, dims),
788 builder->create<sem::DepthTexture>(dims)};
789 }
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
794 typ::SampledTexture sampled_texture(ast::TextureDimension dims,
795 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000796 return {subtype.ast ? builder->create<ast::SampledTexture>(dims, subtype)
797 : nullptr,
798 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
799 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000800 }
801
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000802 /// @param source the Source of the node
803 /// @param dims the dimensionality of the texture
804 /// @param subtype the texture subtype.
805 /// @returns the sampled texture
806 typ::SampledTexture sampled_texture(const Source& source,
807 ast::TextureDimension dims,
808 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000809 return {subtype.ast
810 ? builder->create<ast::SampledTexture>(source, dims, subtype)
811 : nullptr,
812 subtype.sem ? builder->create<sem::SampledTexture>(dims, subtype)
813 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000814 }
815
Ben Claytone204f272021-04-22 14:40:23 +0000816 /// @param dims the dimensionality of the texture
817 /// @param subtype the texture subtype.
818 /// @returns the multisampled texture
819 typ::MultisampledTexture multisampled_texture(ast::TextureDimension dims,
820 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000821 return {
822 subtype.ast ? builder->create<ast::MultisampledTexture>(dims, subtype)
823 : nullptr,
824 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
825 : nullptr};
Ben Claytone204f272021-04-22 14:40:23 +0000826 }
827
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000828 /// @param source the Source of the node
829 /// @param dims the dimensionality of the texture
830 /// @param subtype the texture subtype.
831 /// @returns the multisampled texture
832 typ::MultisampledTexture multisampled_texture(const Source& source,
833 ast::TextureDimension dims,
834 typ::Type subtype) const {
Ben Clayton95c2e952021-04-28 12:58:13 +0000835 return {
836 subtype.ast
837 ? builder->create<ast::MultisampledTexture>(source, dims, subtype)
838 : nullptr,
839 subtype.sem ? builder->create<sem::MultisampledTexture>(dims, subtype)
840 : nullptr};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000841 }
842
Ben Claytone204f272021-04-22 14:40:23 +0000843 /// @param dims the dimensionality of the texture
844 /// @param format the image format of the texture
845 /// @returns the storage texture
846 typ::StorageTexture storage_texture(ast::TextureDimension dims,
847 ast::ImageFormat format) const {
848 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
849 auto* sem_subtype =
850 sem::StorageTexture::SubtypeFor(format, builder->Types());
851 return {builder->create<ast::StorageTexture>(dims, format, ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000852 builder->create<sem::StorageTexture>(
853 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000854 }
855
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000856 /// @param source the Source of the node
857 /// @param dims the dimensionality of the texture
858 /// @param format the image format of the texture
859 /// @returns the storage texture
860 typ::StorageTexture storage_texture(const Source& source,
861 ast::TextureDimension dims,
862 ast::ImageFormat format) const {
863 auto* ast_subtype = ast::StorageTexture::SubtypeFor(format, *builder);
864 auto* sem_subtype =
865 sem::StorageTexture::SubtypeFor(format, builder->Types());
866 return {builder->create<ast::StorageTexture>(source, dims, format,
867 ast_subtype),
Antonio Maioranodc4e6c12021-05-14 17:51:13 +0000868 builder->create<sem::StorageTexture>(
869 dims, format, ast::AccessControl::kInvalid, sem_subtype)};
Antonio Maiorano4b16a162021-04-27 17:32:37 +0000870 }
871
872 /// @param source the Source of the node
873 /// @returns the external texture
874 typ::ExternalTexture external_texture(const Source& source) const {
875 return {builder->create<ast::ExternalTexture>(source),
876 builder->create<sem::ExternalTexture>()};
877 }
878
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000879 /// If ty is a ast::Struct or ast::Alias, the returned type is an
880 /// ast::TypeName of the given type's name, otherwise type is returned.
881 /// @param type the type
882 /// @return either type or a pointer to a new ast::TypeName
883 typ::Type MaybeCreateTypename(typ::Type type) const;
884
Ben Claytonf5f311e2021-04-28 14:31:23 +0000885 /// The ProgramBuilder
886 ProgramBuilder* const builder;
887
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000888 private:
889 /// CToAST<T> is specialized for various `T` types and each specialization
890 /// contains a single static `get()` method for obtaining the corresponding
891 /// AST type for the C type `T`.
892 /// `get()` has the signature:
Ben Clayton3b3cf5f2021-04-23 15:41:34 +0000893 /// `static typ::Type get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000894 template <typename T>
895 struct CToAST {};
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000896 };
897
898 //////////////////////////////////////////////////////////////////////////////
899 // AST helper methods
900 //////////////////////////////////////////////////////////////////////////////
901
James Price65ae64d2021-04-29 12:59:14 +0000902 /// @return a new unnamed symbol
903 Symbol Sym() { return Symbols().New(); }
904
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000905 /// @param name the symbol string
906 /// @return a Symbol with the given name
907 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
908
909 /// @param sym the symbol
910 /// @return `sym`
911 Symbol Sym(Symbol sym) { return sym; }
912
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000913 /// @param expr the expression
914 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000915 template <typename T>
916 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
917 return expr;
918 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000919
Ben Claytonb8ea5912021-04-19 16:52:42 +0000920 /// Passthrough for nullptr
921 /// @return nullptr
922 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
923
Ben Claytonfe0910f2021-05-17 15:51:47 +0000924 /// @param source the source information
925 /// @param symbol the identifier symbol
926 /// @return an ast::IdentifierExpression with the given symbol
927 ast::IdentifierExpression* Expr(const Source& source, Symbol symbol) {
928 return create<ast::IdentifierExpression>(source, symbol);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000929 }
930
Ben Clayton46d78d72021-02-10 21:34:26 +0000931 /// @param symbol the identifier symbol
932 /// @return an ast::IdentifierExpression with the given symbol
933 ast::IdentifierExpression* Expr(Symbol symbol) {
934 return create<ast::IdentifierExpression>(symbol);
935 }
936
Ben Claytonfe0910f2021-05-17 15:51:47 +0000937 /// @param source the source information
938 /// @param variable the AST variable
939 /// @return an ast::IdentifierExpression with the variable's symbol
940 ast::IdentifierExpression* Expr(const Source& source,
941 ast::Variable* variable) {
942 return create<ast::IdentifierExpression>(source, variable->symbol());
943 }
944
Ben Claytonb8ea5912021-04-19 16:52:42 +0000945 /// @param variable the AST variable
946 /// @return an ast::IdentifierExpression with the variable's symbol
947 ast::IdentifierExpression* Expr(ast::Variable* variable) {
948 return create<ast::IdentifierExpression>(variable->symbol());
949 }
950
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000951 /// @param source the source information
952 /// @param name the identifier name
953 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000954 ast::IdentifierExpression* Expr(const Source& source, const char* name) {
955 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
956 }
957
958 /// @param name the identifier name
959 /// @return an ast::IdentifierExpression with the given name
960 ast::IdentifierExpression* Expr(const char* name) {
961 return create<ast::IdentifierExpression>(Symbols().Register(name));
962 }
963
964 /// @param source the source information
965 /// @param name the identifier name
966 /// @return an ast::IdentifierExpression with the given name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000967 ast::IdentifierExpression* Expr(const Source& source,
968 const std::string& name) {
969 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
970 }
971
972 /// @param name the identifier name
973 /// @return an ast::IdentifierExpression with the given name
Ben Claytonfe0910f2021-05-17 15:51:47 +0000974 ast::IdentifierExpression* Expr(const std::string& name) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000975 return create<ast::IdentifierExpression>(Symbols().Register(name));
976 }
977
Ben Claytonfe0910f2021-05-17 15:51:47 +0000978 /// @param source the source information
979 /// @param value the boolean value
980 /// @return a Scalar constructor for the given value
981 ast::ScalarConstructorExpression* Expr(const Source& source, bool value) {
982 return create<ast::ScalarConstructorExpression>(source, Literal(value));
983 }
984
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000985 /// @param value the boolean value
986 /// @return a Scalar constructor for the given value
987 ast::ScalarConstructorExpression* Expr(bool value) {
988 return create<ast::ScalarConstructorExpression>(Literal(value));
989 }
990
Ben Claytonfe0910f2021-05-17 15:51:47 +0000991 /// @param source the source information
992 /// @param value the float value
993 /// @return a Scalar constructor for the given value
994 ast::ScalarConstructorExpression* Expr(const Source& source, f32 value) {
995 return create<ast::ScalarConstructorExpression>(source, Literal(value));
996 }
997
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000998 /// @param value the float value
999 /// @return a Scalar constructor for the given value
1000 ast::ScalarConstructorExpression* Expr(f32 value) {
1001 return create<ast::ScalarConstructorExpression>(Literal(value));
1002 }
1003
Ben Claytonfe0910f2021-05-17 15:51:47 +00001004 /// @param source the source information
1005 /// @param value the integer value
1006 /// @return a Scalar constructor for the given value
1007 ast::ScalarConstructorExpression* Expr(const Source& source, i32 value) {
1008 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1009 }
1010
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001011 /// @param value the integer value
1012 /// @return a Scalar constructor for the given value
1013 ast::ScalarConstructorExpression* Expr(i32 value) {
1014 return create<ast::ScalarConstructorExpression>(Literal(value));
1015 }
1016
Ben Claytonfe0910f2021-05-17 15:51:47 +00001017 /// @param source the source information
1018 /// @param value the unsigned int value
1019 /// @return a Scalar constructor for the given value
1020 ast::ScalarConstructorExpression* Expr(const Source& source, u32 value) {
1021 return create<ast::ScalarConstructorExpression>(source, Literal(value));
1022 }
1023
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001024 /// @param value the unsigned int value
1025 /// @return a Scalar constructor for the given value
1026 ast::ScalarConstructorExpression* Expr(u32 value) {
1027 return create<ast::ScalarConstructorExpression>(Literal(value));
1028 }
1029
1030 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
1031 /// `list`.
1032 /// @param list the list to append too
1033 /// @param arg the arg to create
1034 template <typename ARG>
1035 void Append(ast::ExpressionList& list, ARG&& arg) {
1036 list.emplace_back(Expr(std::forward<ARG>(arg)));
1037 }
1038
1039 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
1040 /// then appends them to `list`.
1041 /// @param list the list to append too
1042 /// @param arg0 the first argument
1043 /// @param args the rest of the arguments
1044 template <typename ARG0, typename... ARGS>
1045 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
1046 Append(list, std::forward<ARG0>(arg0));
1047 Append(list, std::forward<ARGS>(args)...);
1048 }
1049
1050 /// @return an empty list of expressions
1051 ast::ExpressionList ExprList() { return {}; }
1052
1053 /// @param args the list of expressions
1054 /// @return the list of expressions converted to `ast::Expression`s using
1055 /// `Expr()`,
1056 template <typename... ARGS>
1057 ast::ExpressionList ExprList(ARGS&&... args) {
1058 ast::ExpressionList list;
1059 list.reserve(sizeof...(args));
1060 Append(list, std::forward<ARGS>(args)...);
1061 return list;
1062 }
1063
1064 /// @param list the list of expressions
1065 /// @return `list`
1066 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
1067
1068 /// @param val the boolan value
1069 /// @return a boolean literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001070 ast::BoolLiteral* Literal(bool val) { return create<ast::BoolLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001071
1072 /// @param val the float value
1073 /// @return a float literal with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001074 ast::FloatLiteral* Literal(f32 val) { return create<ast::FloatLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001075
1076 /// @param val the unsigned int value
1077 /// @return a ast::UintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001078 ast::UintLiteral* Literal(u32 val) { return create<ast::UintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001079
1080 /// @param val the integer value
1081 /// @return the ast::SintLiteral with the given value
Ben Clayton109b18f2021-04-28 13:50:43 +00001082 ast::SintLiteral* Literal(i32 val) { return create<ast::SintLiteral>(val); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001083
1084 /// @param args the arguments for the type constructor
1085 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
1086 /// of `args` converted to `ast::Expression`s using `Expr()`
1087 template <typename T, typename... ARGS>
1088 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001089 return Construct(ty.Of<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001090 }
1091
1092 /// @param type the type to construct
1093 /// @param args the arguments for the constructor
1094 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
1095 /// values `args`.
1096 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001097 ast::TypeConstructorExpression* Construct(ast::Type* type, ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001098 type = ty.MaybeCreateTypename(type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001099 return create<ast::TypeConstructorExpression>(
1100 type, ExprList(std::forward<ARGS>(args)...));
1101 }
1102
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001103 /// Creates a constructor expression that constructs an object of
1104 /// `type` filled with `elem_value`. For example,
1105 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
1106 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
1107 /// @param type the type to construct
1108 /// @param elem_value the initial or element value (for vec and mat) to
1109 /// construct with
1110 /// @return the constructor expression
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001111 ast::ConstructorExpression* ConstructValueFilledWith(const ast::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +00001112 int elem_value = 0);
1113
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001114 /// @param args the arguments for the vector constructor
1115 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
1116 /// `T`, constructed with the values `args`.
1117 template <typename T, typename... ARGS>
1118 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001119 return Construct(ty.vec2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001120 }
1121
1122 /// @param args the arguments for the vector constructor
1123 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
1124 /// `T`, constructed with the values `args`.
1125 template <typename T, typename... ARGS>
1126 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001127 return Construct(ty.vec3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001128 }
1129
1130 /// @param args the arguments for the vector constructor
1131 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
1132 /// `T`, constructed with the values `args`.
1133 template <typename T, typename... ARGS>
1134 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001135 return Construct(ty.vec4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001136 }
1137
1138 /// @param args the arguments for the matrix constructor
1139 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
1140 /// `T`, constructed with the values `args`.
1141 template <typename T, typename... ARGS>
1142 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001143 return Construct(ty.mat2x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001144 }
1145
1146 /// @param args the arguments for the matrix constructor
1147 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
1148 /// `T`, constructed with the values `args`.
1149 template <typename T, typename... ARGS>
1150 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001151 return Construct(ty.mat2x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001152 }
1153
1154 /// @param args the arguments for the matrix constructor
1155 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
1156 /// `T`, constructed with the values `args`.
1157 template <typename T, typename... ARGS>
1158 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001159 return Construct(ty.mat2x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001160 }
1161
1162 /// @param args the arguments for the matrix constructor
1163 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
1164 /// `T`, constructed with the values `args`.
1165 template <typename T, typename... ARGS>
1166 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001167 return Construct(ty.mat3x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001168 }
1169
1170 /// @param args the arguments for the matrix constructor
1171 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
1172 /// `T`, constructed with the values `args`.
1173 template <typename T, typename... ARGS>
1174 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001175 return Construct(ty.mat3x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001176 }
1177
1178 /// @param args the arguments for the matrix constructor
1179 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
1180 /// `T`, constructed with the values `args`.
1181 template <typename T, typename... ARGS>
1182 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001183 return Construct(ty.mat3x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001184 }
1185
1186 /// @param args the arguments for the matrix constructor
1187 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
1188 /// `T`, constructed with the values `args`.
1189 template <typename T, typename... ARGS>
1190 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001191 return Construct(ty.mat4x2<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001192 }
1193
1194 /// @param args the arguments for the matrix constructor
1195 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
1196 /// `T`, constructed with the values `args`.
1197 template <typename T, typename... ARGS>
1198 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001199 return Construct(ty.mat4x3<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001200 }
1201
1202 /// @param args the arguments for the matrix constructor
1203 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
1204 /// `T`, constructed with the values `args`.
1205 template <typename T, typename... ARGS>
1206 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001207 return Construct(ty.mat4x4<T>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001208 }
1209
1210 /// @param args the arguments for the array constructor
1211 /// @return an `ast::TypeConstructorExpression` of an array with element type
1212 /// `T`, constructed with the values `args`.
1213 template <typename T, int N = 0, typename... ARGS>
1214 ast::TypeConstructorExpression* array(ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001215 return Construct(ty.array<T, N>(), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216 }
1217
1218 /// @param subtype the array element type
1219 /// @param n the array size. 0 represents a runtime-array.
1220 /// @param args the arguments for the array constructor
1221 /// @return an `ast::TypeConstructorExpression` of an array with element type
1222 /// `subtype`, constructed with the values `args`.
1223 template <typename... ARGS>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001224 ast::TypeConstructorExpression* array(ast::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001225 uint32_t n,
1226 ARGS&&... args) {
Ben Clayton0f88b312021-05-04 17:36:31 +00001227 return Construct(ty.array(subtype, n), std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228 }
1229
1230 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001232 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001233 /// @param constructor constructor expression
1234 /// @param decorations variable decorations
1235 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001236 template <typename NAME>
1237 ast::Variable* Var(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001238 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001239 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001240 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001241 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001242 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001243 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
1244 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001245 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001246
1247 /// @param source the variable source
1248 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001249 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +00001250 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001251 /// @param constructor constructor expression
1252 /// @param decorations variable decorations
1253 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001254 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001255 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001256 NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001257 const ast::Type* type,
Ben Claytonf6c84e42021-05-12 21:08:22 +00001258 ast::StorageClass storage = ast::StorageClass::kNone,
Ben Clayton46d78d72021-02-10 21:34:26 +00001259 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +00001260 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001261 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001262 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +00001263 type, false, constructor, decorations);
1264 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001265
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001266 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001267 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001268 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001269 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001270 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001271 template <typename NAME>
1272 ast::Variable* Const(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001273 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001274 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001275 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001276 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001277 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001278 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +00001279 constructor, decorations);
1280 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001281
1282 /// @param source the variable source
1283 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001284 /// @param type the variable type
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001285 /// @param constructor constructor expression
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001286 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +00001287 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001288 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001289 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001290 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001291 ast::Type* type,
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001292 ast::Expression* constructor,
James Price95d40772021-03-11 17:39:32 +00001293 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001294 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001295 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +00001296 ast::StorageClass::kNone, type, true,
1297 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +00001298 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001299
James Pricedaf1f1c2021-04-08 22:15:48 +00001300 /// @param name the parameter name
1301 /// @param type the parameter type
1302 /// @param decorations optional parameter decorations
1303 /// @returns a constant `ast::Variable` with the given name and type
1304 template <typename NAME>
1305 ast::Variable* Param(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001306 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001307 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001308 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001309 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
1310 ast::StorageClass::kNone, type, true, nullptr,
1311 decorations);
1312 }
1313
1314 /// @param source the parameter source
1315 /// @param name the parameter name
1316 /// @param type the parameter type
1317 /// @param decorations optional parameter decorations
1318 /// @returns a constant `ast::Variable` with the given name and type
1319 template <typename NAME>
1320 ast::Variable* Param(const Source& source,
1321 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001322 ast::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +00001323 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001324 type = ty.MaybeCreateTypename(type);
James Pricedaf1f1c2021-04-08 22:15:48 +00001325 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
1326 ast::StorageClass::kNone, type, true, nullptr,
1327 decorations);
1328 }
1329
Ben Clayton42708342021-04-21 17:55:12 +00001330 /// @param name the variable name
1331 /// @param type the variable type
1332 /// @param storage the variable storage class
1333 /// @param constructor constructor expression
1334 /// @param decorations variable decorations
1335 /// @returns a new `ast::Variable`, which is automatically registered as a
1336 /// global variable with the ast::Module.
1337 template <typename NAME>
1338 ast::Variable* Global(NAME&& name,
Antonio Maioranodc4e6c12021-05-14 17:51:13 +00001339 const ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001340 ast::StorageClass storage,
1341 ast::Expression* constructor = nullptr,
1342 ast::DecorationList decorations = {}) {
1343 auto* var =
1344 Var(std::forward<NAME>(name), type, storage, constructor, decorations);
1345 AST().AddGlobalVariable(var);
1346 return var;
1347 }
1348
1349 /// @param source the variable source
1350 /// @param name the variable name
1351 /// @param type the variable type
1352 /// @param storage the variable storage class
1353 /// @param constructor constructor expression
1354 /// @param decorations variable decorations
1355 /// @returns a new `ast::Variable`, which is automatically registered as a
1356 /// global variable with the ast::Module.
1357 template <typename NAME>
1358 ast::Variable* Global(const Source& source,
1359 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001360 ast::Type* type,
Ben Clayton42708342021-04-21 17:55:12 +00001361 ast::StorageClass storage,
1362 ast::Expression* constructor = nullptr,
1363 ast::DecorationList decorations = {}) {
1364 auto* var = Var(source, std::forward<NAME>(name), type, storage,
1365 constructor, decorations);
Ben Clayton401b96b2021-02-03 17:19:59 +00001366 AST().AddGlobalVariable(var);
1367 return var;
1368 }
1369
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001370 /// @param name the variable name
1371 /// @param type the variable type
1372 /// @param constructor constructor expression
1373 /// @param decorations optional variable decorations
Ben Clayton401b96b2021-02-03 17:19:59 +00001374 /// @returns a const `ast::Variable` constructed by calling Var() with the
1375 /// arguments of `args`, which is automatically registered as a global
1376 /// variable with the ast::Module.
Antonio Maioranoe0448d32021-05-10 20:42:56 +00001377 template <typename NAME>
1378 ast::Variable* GlobalConst(NAME&& name,
1379 typ::Type type,
1380 ast::Expression* constructor,
1381 ast::DecorationList decorations = {}) {
1382 auto* var = Const(std::forward<NAME>(name), type, constructor,
1383 std::move(decorations));
1384 AST().AddGlobalVariable(var);
1385 return var;
1386 }
1387
1388 /// @param source the variable source
1389 /// @param name the variable name
1390 /// @param type the variable type
1391 /// @param constructor constructor expression
1392 /// @param decorations optional variable decorations
1393 /// @returns a const `ast::Variable` constructed by calling Var() with the
1394 /// arguments of `args`, which is automatically registered as a global
1395 /// variable with the ast::Module.
1396 template <typename NAME>
1397 ast::Variable* GlobalConst(const Source& source,
1398 NAME&& name,
1399 typ::Type type,
1400 ast::Expression* constructor,
1401 ast::DecorationList decorations = {}) {
1402 auto* var = Const(source, std::forward<NAME>(name), type, constructor,
1403 std::move(decorations));
Ben Clayton401b96b2021-02-03 17:19:59 +00001404 AST().AddGlobalVariable(var);
1405 return var;
1406 }
1407
Ben Claytonfe0910f2021-05-17 15:51:47 +00001408 /// @param source the source information
1409 /// @param expr the expression to take the address of
1410 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1411 template <typename EXPR>
1412 ast::UnaryOpExpression* AddressOf(const Source& source, EXPR&& expr) {
1413 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kAddressOf,
1414 Expr(std::forward<EXPR>(expr)));
1415 }
1416
1417 /// @param expr the expression to take the address of
1418 /// @return an ast::UnaryOpExpression that takes the address of `expr`
1419 template <typename EXPR>
1420 ast::UnaryOpExpression* AddressOf(EXPR&& expr) {
1421 return create<ast::UnaryOpExpression>(ast::UnaryOp::kAddressOf,
1422 Expr(std::forward<EXPR>(expr)));
1423 }
1424
1425 /// @param source the source information
1426 /// @param expr the expression to perform an indirection on
1427 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1428 template <typename EXPR>
1429 ast::UnaryOpExpression* Deref(const Source& source, EXPR&& expr) {
1430 return create<ast::UnaryOpExpression>(source, ast::UnaryOp::kIndirection,
1431 Expr(std::forward<EXPR>(expr)));
1432 }
1433
1434 /// @param expr the expression to perform an indirection on
1435 /// @return an ast::UnaryOpExpression that dereferences the pointer `expr`
1436 template <typename EXPR>
1437 ast::UnaryOpExpression* Deref(EXPR&& expr) {
1438 return create<ast::UnaryOpExpression>(ast::UnaryOp::kIndirection,
1439 Expr(std::forward<EXPR>(expr)));
1440 }
1441
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001442 /// @param func the function name
1443 /// @param args the function call arguments
1444 /// @returns a `ast::CallExpression` to the function `func`, with the
1445 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1446 template <typename NAME, typename... ARGS>
1447 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1448 return create<ast::CallExpression>(Expr(func),
1449 ExprList(std::forward<ARGS>(args)...));
1450 }
1451
1452 /// @param lhs the left hand argument to the addition operation
1453 /// @param rhs the right hand argument to the addition operation
1454 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1455 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001456 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001457 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1458 Expr(std::forward<LHS>(lhs)),
1459 Expr(std::forward<RHS>(rhs)));
1460 }
1461
1462 /// @param lhs the left hand argument to the subtraction operation
1463 /// @param rhs the right hand argument to the subtraction operation
1464 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1465 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001466 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001467 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1468 Expr(std::forward<LHS>(lhs)),
1469 Expr(std::forward<RHS>(rhs)));
1470 }
1471
1472 /// @param lhs the left hand argument to the multiplication operation
1473 /// @param rhs the right hand argument to the multiplication operation
1474 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1475 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001476 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001477 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1478 Expr(std::forward<LHS>(lhs)),
1479 Expr(std::forward<RHS>(rhs)));
1480 }
1481
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001482 /// @param source the source information
1483 /// @param lhs the left hand argument to the multiplication operation
1484 /// @param rhs the right hand argument to the multiplication operation
1485 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1486 template <typename LHS, typename RHS>
1487 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1488 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1489 Expr(std::forward<LHS>(lhs)),
1490 Expr(std::forward<RHS>(rhs)));
1491 }
1492
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001493 /// @param lhs the left hand argument to the division operation
1494 /// @param rhs the right hand argument to the division operation
1495 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1496 template <typename LHS, typename RHS>
1497 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1498 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1499 Expr(std::forward<LHS>(lhs)),
1500 Expr(std::forward<RHS>(rhs)));
1501 }
1502
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001503 /// @param arr the array argument for the array accessor expression
1504 /// @param idx the index argument for the array accessor expression
1505 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1506 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001507 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001508 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1509 Expr(std::forward<IDX>(idx)));
1510 }
1511
1512 /// @param obj the object for the member accessor expression
1513 /// @param idx the index argument for the array accessor expression
1514 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1515 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001516 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001517 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1518 Expr(std::forward<IDX>(idx)));
1519 }
1520
Ben Clayton42d1e092021-02-02 14:29:15 +00001521 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001522 /// @param val the offset value
1523 /// @returns the offset decoration pointer
1524 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1525 return create<ast::StructMemberOffsetDecoration>(source_, val);
1526 }
1527
Ben Claytond614dd52021-03-15 10:43:11 +00001528 /// Creates a ast::StructMemberSizeDecoration
1529 /// @param source the source information
1530 /// @param val the size value
1531 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001532 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1533 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001534 return create<ast::StructMemberSizeDecoration>(source, val);
1535 }
1536
1537 /// Creates a ast::StructMemberSizeDecoration
1538 /// @param val the size value
1539 /// @returns the size decoration pointer
1540 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1541 return create<ast::StructMemberSizeDecoration>(source_, val);
1542 }
1543
1544 /// Creates a ast::StructMemberAlignDecoration
1545 /// @param source the source information
1546 /// @param val the align value
1547 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001548 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1549 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001550 return create<ast::StructMemberAlignDecoration>(source, val);
1551 }
1552
1553 /// Creates a ast::StructMemberAlignDecoration
1554 /// @param val the align value
1555 /// @returns the align decoration pointer
1556 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1557 return create<ast::StructMemberAlignDecoration>(source_, val);
1558 }
1559
Ben Clayton42d1e092021-02-02 14:29:15 +00001560 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001561 /// @param source the source information
1562 /// @param name the function name
1563 /// @param params the function parameters
1564 /// @param type the function return type
1565 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001566 /// @param decorations the optional function decorations
1567 /// @param return_type_decorations the optional function return type
1568 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001569 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001570 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001571 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001572 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001573 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001574 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001575 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001576 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001577 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001578 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001579 auto* func =
1580 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1581 type, create<ast::BlockStatement>(body),
1582 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001583 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001584 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001585 }
1586
Ben Clayton42d1e092021-02-02 14:29:15 +00001587 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001588 /// @param name the function name
1589 /// @param params the function parameters
1590 /// @param type the function return type
1591 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001592 /// @param decorations the optional function decorations
1593 /// @param return_type_decorations the optional function return type
1594 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001595 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001596 template <typename NAME>
1597 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001598 ast::VariableList params,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001599 ast::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001600 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001601 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001602 ast::DecorationList return_type_decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001603 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001604 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1605 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001606 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001607 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001608 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001609 }
1610
Ben Clayton49668bb2021-04-17 05:32:01 +00001611 /// Creates an ast::ReturnStatement with no return value
Ben Clayton43073d82021-04-22 13:50:53 +00001612 /// @param source the source information
1613 /// @returns the return statement pointer
1614 ast::ReturnStatement* Return(const Source& source) {
1615 return create<ast::ReturnStatement>(source);
1616 }
1617
1618 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001619 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001620 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1621
1622 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton43073d82021-04-22 13:50:53 +00001623 /// @param source the source information
1624 /// @param val the return value
1625 /// @returns the return statement pointer
1626 template <typename EXPR>
1627 ast::ReturnStatement* Return(const Source& source, EXPR&& val) {
1628 return create<ast::ReturnStatement>(source, Expr(std::forward<EXPR>(val)));
1629 }
1630
1631 /// Creates an ast::ReturnStatement with the given return value
Ben Clayton49668bb2021-04-17 05:32:01 +00001632 /// @param val the return value
1633 /// @returns the return statement pointer
1634 template <typename EXPR>
1635 ast::ReturnStatement* Return(EXPR&& val) {
1636 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001637 }
1638
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001639 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001640 /// @param source the source information
1641 /// @param name the struct name
1642 /// @param members the struct members
1643 /// @param decorations the optional struct decorations
1644 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001645 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001646 ast::Struct* Structure(const Source& source,
1647 NAME&& name,
1648 ast::StructMemberList members,
1649 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001650 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001651 auto* type = create<ast::Struct>(source, sym, std::move(members),
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001652 std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001653 AST().AddConstructedType(type);
1654 return type;
1655 }
1656
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001657 /// Creates a ast::Struct registering it with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001658 /// @param name the struct name
1659 /// @param members the struct members
1660 /// @param decorations the optional struct decorations
1661 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001662 template <typename NAME>
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001663 ast::Struct* Structure(NAME&& name,
1664 ast::StructMemberList members,
1665 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001666 auto sym = Sym(std::forward<NAME>(name));
Ben Claytonba6ab5e2021-05-07 14:49:34 +00001667 auto* type =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001668 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Claytond614dd52021-03-15 10:43:11 +00001669 AST().AddConstructedType(type);
1670 return type;
1671 }
1672
Ben Clayton42d1e092021-02-02 14:29:15 +00001673 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001674 /// @param source the source information
1675 /// @param name the struct member name
1676 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001677 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001678 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001679 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001680 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001681 NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001682 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001683 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001684 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001685 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1686 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001687 }
1688
Ben Clayton42d1e092021-02-02 14:29:15 +00001689 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001690 /// @param name the struct member name
1691 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001692 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001693 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001694 template <typename NAME>
1695 ast::StructMember* Member(NAME&& name,
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001696 ast::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001697 ast::DecorationList decorations = {}) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001698 type = ty.MaybeCreateTypename(type);
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001699 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1700 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001701 }
1702
Ben Claytonbab31972021-02-08 21:16:21 +00001703 /// Creates a ast::StructMember with the given byte offset
1704 /// @param offset the offset to use in the StructMemberOffsetDecoration
1705 /// @param name the struct member name
1706 /// @param type the struct member type
1707 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001708 template <typename NAME>
Ben Clayton02ebf0d2021-05-05 09:09:41 +00001709 ast::StructMember* Member(uint32_t offset, NAME&& name, ast::Type* type) {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00001710 type = ty.MaybeCreateTypename(type);
Ben Claytonbab31972021-02-08 21:16:21 +00001711 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001712 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001713 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001714 create<ast::StructMemberOffsetDecoration>(offset),
1715 });
1716 }
1717
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001718 /// Creates a ast::BlockStatement with input statements
1719 /// @param statements statements of block
1720 /// @returns the block statement pointer
1721 template <typename... Statements>
1722 ast::BlockStatement* Block(Statements&&... statements) {
1723 return create<ast::BlockStatement>(
1724 ast::StatementList{std::forward<Statements>(statements)...});
1725 }
1726
1727 /// Creates a ast::ElseStatement with input condition and body
1728 /// @param condition the else condition expression
1729 /// @param body the else body
1730 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001731 template <typename CONDITION>
1732 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1733 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1734 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001735 }
1736
1737 /// Creates a ast::IfStatement with input condition, body, and optional
1738 /// variadic else statements
1739 /// @param condition the if statement condition expression
1740 /// @param body the if statement body
1741 /// @param elseStatements optional variadic else statements
1742 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001743 template <typename CONDITION, typename... ELSE_STATEMENTS>
1744 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001745 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001746 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001747 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001748 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001749 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001750 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001751 }
1752
1753 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Ben Clayton43073d82021-04-22 13:50:53 +00001754 /// @param source the source information
1755 /// @param lhs the left hand side expression initializer
1756 /// @param rhs the right hand side expression initializer
1757 /// @returns the assignment statement pointer
1758 template <typename LhsExpressionInit, typename RhsExpressionInit>
1759 ast::AssignmentStatement* Assign(const Source& source,
1760 LhsExpressionInit&& lhs,
1761 RhsExpressionInit&& rhs) {
1762 return create<ast::AssignmentStatement>(
1763 source, Expr(std::forward<LhsExpressionInit>(lhs)),
1764 Expr(std::forward<RhsExpressionInit>(rhs)));
1765 }
1766
1767 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001768 /// @param lhs the left hand side expression initializer
1769 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001770 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001771 template <typename LhsExpressionInit, typename RhsExpressionInit>
1772 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1773 RhsExpressionInit&& rhs) {
1774 return create<ast::AssignmentStatement>(
1775 Expr(std::forward<LhsExpressionInit>(lhs)),
1776 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001777 }
1778
1779 /// Creates a ast::LoopStatement with input body and optional continuing
1780 /// @param body the loop body
1781 /// @param continuing the optional continuing block
1782 /// @returns the loop statement pointer
1783 ast::LoopStatement* Loop(ast::BlockStatement* body,
1784 ast::BlockStatement* continuing = nullptr) {
1785 return create<ast::LoopStatement>(body, continuing);
1786 }
1787
1788 /// Creates a ast::VariableDeclStatement for the input variable
Ben Clayton43073d82021-04-22 13:50:53 +00001789 /// @param source the source information
1790 /// @param var the variable to wrap in a decl statement
1791 /// @returns the variable decl statement pointer
1792 ast::VariableDeclStatement* Decl(const Source& source, ast::Variable* var) {
1793 return create<ast::VariableDeclStatement>(source, var);
1794 }
1795
1796 /// Creates a ast::VariableDeclStatement for the input variable
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001797 /// @param var the variable to wrap in a decl statement
1798 /// @returns the variable decl statement pointer
1799 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1800 return create<ast::VariableDeclStatement>(var);
1801 }
1802
Antonio Maioranocea744d2021-03-25 12:55:27 +00001803 /// Creates a ast::SwitchStatement with input expression and cases
1804 /// @param condition the condition expression initializer
1805 /// @param cases case statements
1806 /// @returns the switch statement pointer
1807 template <typename ExpressionInit, typename... Cases>
1808 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1809 return create<ast::SwitchStatement>(
1810 Expr(std::forward<ExpressionInit>(condition)),
1811 ast::CaseStatementList{std::forward<Cases>(cases)...});
1812 }
1813
1814 /// Creates a ast::CaseStatement with input list of selectors, and body
1815 /// @param selectors list of selectors
1816 /// @param body the case body
1817 /// @returns the case statement pointer
1818 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1819 ast::BlockStatement* body = nullptr) {
1820 return create<ast::CaseStatement>(std::move(selectors),
1821 body ? body : Block());
1822 }
1823
1824 /// Convenient overload that takes a single selector
1825 /// @param selector a single case selector
1826 /// @param body the case body
1827 /// @returns the case statement pointer
1828 ast::CaseStatement* Case(ast::IntLiteral* selector,
1829 ast::BlockStatement* body = nullptr) {
1830 return Case(ast::CaseSelectorList{selector}, body);
1831 }
1832
1833 /// Convenience function that creates a 'default' ast::CaseStatement
1834 /// @param body the case body
1835 /// @returns the case statement pointer
1836 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1837 return Case(ast::CaseSelectorList{}, body);
1838 }
1839
James Price68f558f2021-04-06 15:51:47 +00001840 /// Creates an ast::BuiltinDecoration
1841 /// @param source the source information
1842 /// @param builtin the builtin value
1843 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001844 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001845 return create<ast::BuiltinDecoration>(source, builtin);
1846 }
1847
1848 /// Creates an ast::BuiltinDecoration
1849 /// @param builtin the builtin value
1850 /// @returns the builtin decoration pointer
1851 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1852 return create<ast::BuiltinDecoration>(source_, builtin);
1853 }
1854
1855 /// Creates an ast::LocationDecoration
1856 /// @param source the source information
1857 /// @param location the location value
1858 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001859 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001860 return create<ast::LocationDecoration>(source, location);
1861 }
1862
1863 /// Creates an ast::LocationDecoration
1864 /// @param location the location value
1865 /// @returns the location decoration pointer
1866 ast::LocationDecoration* Location(uint32_t location) {
1867 return create<ast::LocationDecoration>(source_, location);
1868 }
1869
James Pricef2f3bfc2021-05-13 20:32:32 +00001870 /// Creates an ast::OverrideDecoration with a specific constant ID
1871 /// @param source the source information
1872 /// @param id the id value
1873 /// @returns the override decoration pointer
1874 ast::OverrideDecoration* Override(const Source& source, uint32_t id) {
1875 return create<ast::OverrideDecoration>(source, id);
1876 }
1877
1878 /// Creates an ast::OverrideDecoration with a specific constant ID
1879 /// @param id the optional id value
1880 /// @returns the override decoration pointer
1881 ast::OverrideDecoration* Override(uint32_t id) {
1882 return Override(source_, id);
1883 }
1884
1885 /// Creates an ast::OverrideDecoration without a constant ID
1886 /// @param source the source information
1887 /// @returns the override decoration pointer
1888 ast::OverrideDecoration* Override(const Source& source) {
1889 return create<ast::OverrideDecoration>(source);
1890 }
1891
1892 /// Creates an ast::OverrideDecoration without a constant ID
1893 /// @returns the override decoration pointer
1894 ast::OverrideDecoration* Override() { return Override(source_); }
1895
James Price68f558f2021-04-06 15:51:47 +00001896 /// Creates an ast::StageDecoration
1897 /// @param source the source information
1898 /// @param stage the pipeline stage
1899 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001900 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001901 return create<ast::StageDecoration>(source, stage);
1902 }
1903
1904 /// Creates an ast::StageDecoration
1905 /// @param stage the pipeline stage
1906 /// @returns the stage decoration pointer
1907 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1908 return create<ast::StageDecoration>(source_, stage);
1909 }
1910
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001911 /// Sets the current builder source to `src`
1912 /// @param src the Source used for future create() calls
1913 void SetSource(const Source& src) {
1914 AssertNotMoved();
1915 source_ = src;
1916 }
1917
1918 /// Sets the current builder source to `loc`
1919 /// @param loc the Source used for future create() calls
1920 void SetSource(const Source::Location& loc) {
1921 AssertNotMoved();
1922 source_ = Source(loc);
1923 }
1924
Ben Clayton33352542021-01-29 16:43:41 +00001925 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001926 /// @note As the Resolver is run when the Program is built, this will only be
1927 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001928 /// @param expr the AST expression
1929 /// @return the resolved semantic type for the expression, or nullptr if the
1930 /// expression has no resolved type.
Ben Claytonfbec46f2021-04-30 20:20:19 +00001931 sem::Type* TypeOf(const ast::Expression* expr) const;
1932
Ben Claytonfe0910f2021-05-17 15:51:47 +00001933 /// Helper for returning the resolved semantic type of the variable `var`.
1934 /// @note As the Resolver is run when the Program is built, this will only be
1935 /// useful for the Resolver itself and tests that use their own Resolver.
1936 /// @param var the AST variable
1937 /// @return the resolved semantic type for the variable, or nullptr if the
1938 /// variable has no resolved type.
1939 sem::Type* TypeOf(const ast::Variable* var) const;
1940
Ben Claytonfbec46f2021-04-30 20:20:19 +00001941 /// Helper for returning the resolved semantic type of the AST type `type`.
1942 /// @note As the Resolver is run when the Program is built, this will only be
1943 /// useful for the Resolver itself and tests that use their own Resolver.
1944 /// @param expr the AST type
1945 /// @return the resolved semantic type for the type, or nullptr if the type
1946 /// has no resolved type.
1947 const sem::Type* TypeOf(const ast::Type* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001948
Ben Clayton169512e2021-04-17 05:52:11 +00001949 /// Wraps the ast::Literal in a statement. This is used by tests that
1950 /// construct a partial AST and require the Resolver to reach these
1951 /// nodes.
1952 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1953 /// @return the ast::Statement that wraps the ast::Statement
1954 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001955 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001956 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001957 /// nodes.
1958 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1959 /// @return the ast::Statement that wraps the ast::Expression
1960 ast::Statement* WrapInStatement(ast::Expression* expr);
1961 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001962 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001963 /// these nodes.
1964 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1965 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1966 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1967 /// Returns the statement argument. Used as a passthrough-overload by
1968 /// WrapInFunction().
1969 /// @param stmt the ast::Statement
1970 /// @return `stmt`
1971 ast::Statement* WrapInStatement(ast::Statement* stmt);
1972 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001973 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001974 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001975 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001976 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001977 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001978 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001979 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001980 }
1981 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001982 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001983 /// @returns the function
1984 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001985
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001986 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001987 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001988
1989 protected:
1990 /// Asserts that the builder has not been moved.
1991 void AssertNotMoved() const;
1992
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001993 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001994 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001995 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001996 ASTNodeAllocator ast_nodes_;
1997 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001998 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001999 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00002000 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00002001 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002002
2003 /// The source to use when creating AST nodes without providing a Source as
2004 /// the first argument.
2005 Source source_;
2006
Ben Clayton5f0ea112021-03-09 10:54:37 +00002007 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00002008 /// program when built.
2009 bool resolve_on_build_ = true;
2010
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002011 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
2012 bool moved_ = false;
2013};
2014
2015//! @cond Doxygen_Suppress
2016// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
2017template <>
2018struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002019 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002020 return t->i32();
2021 }
2022};
2023template <>
2024struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002025 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002026 return t->u32();
2027 }
2028};
2029template <>
2030struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002031 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002032 return t->f32();
2033 }
2034};
2035template <>
2036struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002037 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002038 return t->bool_();
2039 }
2040};
2041template <>
2042struct ProgramBuilder::TypesBuilder::CToAST<void> {
Ben Clayton3b3cf5f2021-04-23 15:41:34 +00002043 static typ::Type get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002044 return t->void_();
2045 }
2046};
2047//! @endcond
2048
Ben Clayton917b14b2021-04-19 16:50:23 +00002049/// @param builder the ProgramBuilder
2050/// @returns the ProgramID of the ProgramBuilder
2051inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
2052 return builder->ID();
2053}
2054
Ben Claytona6b9a8e2021-01-26 16:57:10 +00002055} // namespace tint
2056
2057#endif // SRC_PROGRAM_BUILDER_H_