blob: d8651a9cc4a37ddc1340cbdd7d5cece16681d994 [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
21#include "src/ast/array_accessor_expression.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000022#include "src/ast/assignment_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000023#include "src/ast/binary_expression.h"
24#include "src/ast/bool_literal.h"
25#include "src/ast/call_expression.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000026#include "src/ast/case_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000027#include "src/ast/float_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000028#include "src/ast/if_statement.h"
29#include "src/ast/loop_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000030#include "src/ast/member_accessor_expression.h"
31#include "src/ast/module.h"
Antonio Maiorano03c01b52021-03-19 14:04:51 +000032#include "src/ast/return_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000033#include "src/ast/scalar_constructor_expression.h"
34#include "src/ast/sint_literal.h"
James Price68f558f2021-04-06 15:51:47 +000035#include "src/ast/stage_decoration.h"
Ben Claytonbab31972021-02-08 21:16:21 +000036#include "src/ast/stride_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000037#include "src/ast/struct_member_align_decoration.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000038#include "src/ast/struct_member_offset_decoration.h"
Ben Claytond614dd52021-03-15 10:43:11 +000039#include "src/ast/struct_member_size_decoration.h"
Antonio Maioranocea744d2021-03-25 12:55:27 +000040#include "src/ast/switch_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000041#include "src/ast/type_constructor_expression.h"
42#include "src/ast/uint_literal.h"
Antonio Maioranofd31bbd2021-03-09 10:26:57 +000043#include "src/ast/variable_decl_statement.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000044#include "src/program.h"
Ben Claytone6995de2021-04-13 23:27:27 +000045#include "src/program_id.h"
Antonio Maioranoaea9c682021-04-19 22:54:43 +000046#include "src/sem/access_control_type.h"
47#include "src/sem/alias_type.h"
48#include "src/sem/array_type.h"
49#include "src/sem/bool_type.h"
50#include "src/sem/f32_type.h"
51#include "src/sem/i32_type.h"
52#include "src/sem/matrix_type.h"
53#include "src/sem/pointer_type.h"
54#include "src/sem/struct_type.h"
55#include "src/sem/u32_type.h"
56#include "src/sem/vector_type.h"
57#include "src/sem/void_type.h"
Ben Claytona6b9a8e2021-01-26 16:57:10 +000058
59namespace tint {
60
Ben Clayton401b96b2021-02-03 17:19:59 +000061// Forward declarations
62namespace ast {
63class VariableDeclStatement;
64} // namespace ast
65
Ben Claytona6b9a8e2021-01-26 16:57:10 +000066class CloneContext;
67
68/// ProgramBuilder is a mutable builder for a Program.
69/// To construct a Program, populate the builder and then `std::move` it to a
70/// Program.
71class ProgramBuilder {
72 public:
Ben Clayton7fdfff12021-01-29 15:17:30 +000073 /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node>
74 using ASTNodeAllocator = BlockAllocator<ast::Node>;
75
Antonio Maiorano5cd71b82021-04-16 19:07:51 +000076 /// SemNodeAllocator is an alias to BlockAllocator<sem::Node>
77 using SemNodeAllocator = BlockAllocator<sem::Node>;
Ben Claytona6b9a8e2021-01-26 16:57:10 +000078
79 /// `i32` is a type alias to `int`.
80 /// Useful for passing to template methods such as `vec2<i32>()` to imitate
81 /// WGSL syntax.
82 /// Note: this is intentionally not aliased to uint32_t as we want integer
83 /// literals passed to the builder to match WGSL's integer literal types.
84 using i32 = decltype(1);
85 /// `u32` is a type alias to `unsigned int`.
86 /// Useful for passing to template methods such as `vec2<u32>()` to imitate
87 /// WGSL syntax.
88 /// Note: this is intentionally not aliased to uint32_t as we want integer
89 /// literals passed to the builder to match WGSL's integer literal types.
90 using u32 = decltype(1u);
91 /// `f32` is a type alias to `float`
92 /// Useful for passing to template methods such as `vec2<f32>()` to imitate
93 /// WGSL syntax.
94 using f32 = float;
95
96 /// Constructor
97 ProgramBuilder();
98
99 /// Move constructor
100 /// @param rhs the builder to move
101 ProgramBuilder(ProgramBuilder&& rhs);
102
103 /// Destructor
104 virtual ~ProgramBuilder();
105
106 /// Move assignment operator
107 /// @param rhs the builder to move
108 /// @return this builder
109 ProgramBuilder& operator=(ProgramBuilder&& rhs);
110
Ben Claytone43c8302021-01-29 11:59:32 +0000111 /// Wrap returns a new ProgramBuilder wrapping the Program `program` without
112 /// making a deep clone of the Program contents.
113 /// ProgramBuilder returned by Wrap() is intended to temporarily extend an
114 /// existing immutable program.
115 /// As the returned ProgramBuilder wraps `program`, `program` must not be
116 /// destructed or assigned while using the returned ProgramBuilder.
117 /// TODO(bclayton) - Evaluate whether there are safer alternatives to this
118 /// function. See crbug.com/tint/460.
119 /// @param program the immutable Program to wrap
120 /// @return the ProgramBuilder that wraps `program`
121 static ProgramBuilder Wrap(const Program* program);
122
Ben Claytone6995de2021-04-13 23:27:27 +0000123 /// @returns the unique identifier for this program
124 ProgramID ID() const { return id_; }
125
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000126 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000127 sem::Manager& Types() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000128 AssertNotMoved();
129 return types_;
130 }
131
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000132 /// @returns a reference to the program's types
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000133 const sem::Manager& Types() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000134 AssertNotMoved();
135 return types_;
136 }
137
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000138 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000139 ASTNodeAllocator& ASTNodes() {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000140 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000141 return ast_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000142 }
143
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000144 /// @returns a reference to the program's AST nodes storage
Ben Clayton7fdfff12021-01-29 15:17:30 +0000145 const ASTNodeAllocator& ASTNodes() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000146 AssertNotMoved();
Ben Clayton7fdfff12021-01-29 15:17:30 +0000147 return ast_nodes_;
148 }
149
150 /// @returns a reference to the program's semantic nodes storage
151 SemNodeAllocator& SemNodes() {
152 AssertNotMoved();
153 return sem_nodes_;
154 }
155
156 /// @returns a reference to the program's semantic nodes storage
157 const SemNodeAllocator& SemNodes() const {
158 AssertNotMoved();
159 return sem_nodes_;
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000160 }
161
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000162 /// @returns a reference to the program's AST root Module
163 ast::Module& AST() {
164 AssertNotMoved();
165 return *ast_;
166 }
167
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000168 /// @returns a reference to the program's AST root Module
169 const ast::Module& AST() const {
170 AssertNotMoved();
171 return *ast_;
172 }
173
174 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000175 sem::Info& Sem() {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000176 AssertNotMoved();
177 return sem_;
178 }
179
180 /// @returns a reference to the program's semantic info
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000181 const sem::Info& Sem() const {
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000182 AssertNotMoved();
183 return sem_;
184 }
185
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000186 /// @returns a reference to the program's SymbolTable
187 SymbolTable& Symbols() {
188 AssertNotMoved();
189 return symbols_;
190 }
191
Ben Clayton708dc2d2021-01-29 11:22:40 +0000192 /// @returns a reference to the program's SymbolTable
193 const SymbolTable& Symbols() const {
194 AssertNotMoved();
195 return symbols_;
196 }
197
Ben Clayton844217f2021-01-27 18:49:05 +0000198 /// @returns a reference to the program's diagnostics
199 diag::List& Diagnostics() {
200 AssertNotMoved();
201 return diagnostics_;
202 }
203
Ben Claytondd1b6fc2021-01-29 10:55:40 +0000204 /// @returns a reference to the program's diagnostics
205 const diag::List& Diagnostics() const {
206 AssertNotMoved();
207 return diagnostics_;
208 }
209
Ben Clayton5f0ea112021-03-09 10:54:37 +0000210 /// Controls whether the Resolver will be run on the program when it is built.
Ben Claytondd69ac32021-01-27 19:23:55 +0000211 /// @param enable the new flag value (defaults to true)
212 void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; }
213
Ben Clayton5f0ea112021-03-09 10:54:37 +0000214 /// @return true if the Resolver will be run on the program when it is
Ben Claytondd69ac32021-01-27 19:23:55 +0000215 /// built.
216 bool ResolveOnBuild() const { return resolve_on_build_; }
217
Ben Clayton844217f2021-01-27 18:49:05 +0000218 /// @returns true if the program has no error diagnostics and is not missing
219 /// information
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000220 bool IsValid() const;
221
Ben Clayton708dc2d2021-01-29 11:22:40 +0000222 /// Writes a representation of the node to the output stream
223 /// @note unlike str(), to_str() does not automatically demangle the string.
224 /// @param node the AST node
225 /// @param out the stream to write to
226 /// @param indent number of spaces to indent the node when writing
227 void to_str(const ast::Node* node, std::ostream& out, size_t indent) const {
228 node->to_str(Sem(), out, indent);
229 }
230
231 /// Returns a demangled, string representation of `node`.
232 /// @param node the AST node
233 /// @returns a string representation of the node
234 std::string str(const ast::Node* node) const;
235
Ben Clayton7fdfff12021-01-29 15:17:30 +0000236 /// Creates a new ast::Node owned by the ProgramBuilder. When the
237 /// ProgramBuilder is destructed, the ast::Node will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000238 /// @param source the Source of the node
239 /// @param args the arguments to pass to the type constructor
240 /// @returns the node pointer
241 template <typename T, typename... ARGS>
242 traits::EnableIfIsType<T, ast::Node>* create(const Source& source,
243 ARGS&&... args) {
244 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000245 return ast_nodes_.Create<T>(id_, source, std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000246 }
247
Ben Clayton7fdfff12021-01-29 15:17:30 +0000248 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
249 /// Source as set by the last call to SetSource() as the only argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000250 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000251 /// When the ProgramBuilder is destructed, the ast::Node will also be
252 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000253 /// @returns the node pointer
254 template <typename T>
255 traits::EnableIfIsType<T, ast::Node>* create() {
256 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000257 return ast_nodes_.Create<T>(id_, source_);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000258 }
259
Ben Clayton7fdfff12021-01-29 15:17:30 +0000260 /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current
261 /// Source as set by the last call to SetSource() as the first argument to the
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000262 /// constructor.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000263 /// When the ProgramBuilder is destructed, the ast::Node will also be
264 /// destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000265 /// @param arg0 the first arguments to pass to the type constructor
266 /// @param args the remaining arguments to pass to the type constructor
267 /// @returns the node pointer
268 template <typename T, typename ARG0, typename... ARGS>
269 traits::EnableIf</* T is ast::Node and ARG0 is not Source */
270 traits::IsTypeOrDerived<T, ast::Node>::value &&
271 !traits::IsTypeOrDerived<ARG0, Source>::value,
272 T>*
273 create(ARG0&& arg0, ARGS&&... args) {
274 AssertNotMoved();
Ben Claytone6995de2021-04-13 23:27:27 +0000275 return ast_nodes_.Create<T>(id_, source_, std::forward<ARG0>(arg0),
Ben Clayton7fdfff12021-01-29 15:17:30 +0000276 std::forward<ARGS>(args)...);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000277 }
278
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000279 /// Creates a new sem::Node owned by the ProgramBuilder.
280 /// When the ProgramBuilder is destructed, the sem::Node will also be
Ben Clayton7fdfff12021-01-29 15:17:30 +0000281 /// destructed.
282 /// @param args the arguments to pass to the type constructor
283 /// @returns the node pointer
284 template <typename T, typename... ARGS>
Antonio Maiorano5cd71b82021-04-16 19:07:51 +0000285 traits::EnableIfIsType<T, sem::Node>* create(ARGS&&... args) {
Ben Clayton7fdfff12021-01-29 15:17:30 +0000286 AssertNotMoved();
287 return sem_nodes_.Create<T>(std::forward<ARGS>(args)...);
288 }
289
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000290 /// Creates a new sem::Type owned by the ProgramBuilder.
Ben Clayton7fdfff12021-01-29 15:17:30 +0000291 /// When the ProgramBuilder is destructed, owned ProgramBuilder and the
292 /// returned`Type` will also be destructed.
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000293 /// Types are unique (de-aliased), and so calling create() for the same `T`
294 /// and arguments will return the same pointer.
295 /// @warning Use this method to acquire a type only if all of its type
296 /// information is provided in the constructor arguments `args`.<br>
297 /// If the type requires additional configuration after construction that
298 /// affect its fundamental type, build the type with `std::make_unique`, make
299 /// any necessary alterations and then call unique_type() instead.
300 /// @param args the arguments to pass to the type constructor
301 /// @returns the de-aliased type pointer
302 template <typename T, typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000303 traits::EnableIfIsType<T, sem::Type>* create(ARGS&&... args) {
304 static_assert(std::is_base_of<sem::Type, T>::value,
305 "T does not derive from sem::Type");
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000306 AssertNotMoved();
307 return types_.Get<T>(std::forward<ARGS>(args)...);
308 }
309
310 /// Marks this builder as moved, preventing any further use of the builder.
311 void MarkAsMoved();
312
313 //////////////////////////////////////////////////////////////////////////////
314 // TypesBuilder
315 //////////////////////////////////////////////////////////////////////////////
316
317 /// TypesBuilder holds basic `tint` types and methods for constructing
318 /// complex types.
319 class TypesBuilder {
320 public:
321 /// Constructor
322 /// @param builder the program builder
323 explicit TypesBuilder(ProgramBuilder* builder);
324
325 /// @return the tint AST type for the C type `T`.
326 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000327 sem::Type* Of() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000328 return CToAST<T>::get(this);
329 }
330
331 /// @returns a boolean type
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000332 sem::Bool* bool_() const { return builder->create<sem::Bool>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000333
334 /// @returns a f32 type
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000335 sem::F32* f32() const { return builder->create<sem::F32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000336
337 /// @returns a i32 type
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000338 sem::I32* i32() const { return builder->create<sem::I32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000339
340 /// @returns a u32 type
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000341 sem::U32* u32() const { return builder->create<sem::U32>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000342
343 /// @returns a void type
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000344 sem::Void* void_() const { return builder->create<sem::Void>(); }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000345
Antonio Maiorano25436862021-04-13 13:32:33 +0000346 /// @param type vector subtype
347 /// @return the tint AST type for a 2-element vector of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000348 sem::Vector* vec2(sem::Type* type) const {
349 return builder->create<sem::Vector>(type, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000350 }
351
352 /// @param type vector subtype
353 /// @return the tint AST type for a 3-element vector of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000354 sem::Vector* vec3(sem::Type* type) const {
355 return builder->create<sem::Vector>(type, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000356 }
357
358 /// @param type vector subtype
359 /// @return the tint AST type for a 4-element vector of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000360 sem::Type* vec4(sem::Type* type) const {
361 return builder->create<sem::Vector>(type, 4u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000362 }
363
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000364 /// @return the tint AST type for a 2-element vector of the C type `T`.
365 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000366 sem::Vector* vec2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000367 return vec2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000368 }
369
370 /// @return the tint AST type for a 3-element vector of the C type `T`.
371 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000372 sem::Vector* vec3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000373 return vec3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000374 }
375
376 /// @return the tint AST type for a 4-element vector of the C type `T`.
377 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000378 sem::Type* vec4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000379 return vec4(Of<T>());
380 }
381
382 /// @param type matrix subtype
383 /// @return the tint AST type for a 2x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000384 sem::Matrix* mat2x2(sem::Type* type) const {
385 return builder->create<sem::Matrix>(type, 2u, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000386 }
387
388 /// @param type matrix subtype
389 /// @return the tint AST type for a 2x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000390 sem::Matrix* mat2x3(sem::Type* type) const {
391 return builder->create<sem::Matrix>(type, 3u, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000392 }
393
394 /// @param type matrix subtype
395 /// @return the tint AST type for a 2x4 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000396 sem::Matrix* mat2x4(sem::Type* type) const {
397 return builder->create<sem::Matrix>(type, 4u, 2u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000398 }
399
400 /// @param type matrix subtype
401 /// @return the tint AST type for a 3x2 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000402 sem::Matrix* mat3x2(sem::Type* type) const {
403 return builder->create<sem::Matrix>(type, 2u, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000404 }
405
406 /// @param type matrix subtype
407 /// @return the tint AST type for a 3x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000408 sem::Matrix* mat3x3(sem::Type* type) const {
409 return builder->create<sem::Matrix>(type, 3u, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000410 }
411
412 /// @param type matrix subtype
413 /// @return the tint AST type for a 3x4 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000414 sem::Matrix* mat3x4(sem::Type* type) const {
415 return builder->create<sem::Matrix>(type, 4u, 3u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000416 }
417
418 /// @param type matrix subtype
419 /// @return the tint AST type for a 4x2 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000420 sem::Matrix* mat4x2(sem::Type* type) const {
421 return builder->create<sem::Matrix>(type, 2u, 4u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000422 }
423
424 /// @param type matrix subtype
425 /// @return the tint AST type for a 4x3 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000426 sem::Matrix* mat4x3(sem::Type* type) const {
427 return builder->create<sem::Matrix>(type, 3u, 4u);
Antonio Maiorano25436862021-04-13 13:32:33 +0000428 }
429
430 /// @param type matrix subtype
431 /// @return the tint AST type for a 4x4 matrix of `type`.
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000432 sem::Matrix* mat4x4(sem::Type* type) const {
433 return builder->create<sem::Matrix>(type, 4u, 4u);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000434 }
435
436 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
437 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000438 sem::Matrix* mat2x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000439 return mat2x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000440 }
441
442 /// @return the tint AST type for a 2x3 matrix of the C type `T`.
443 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000444 sem::Matrix* mat2x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000445 return mat2x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000446 }
447
448 /// @return the tint AST type for a 2x4 matrix of the C type `T`.
449 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000450 sem::Matrix* mat2x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000451 return mat2x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000452 }
453
454 /// @return the tint AST type for a 3x2 matrix of the C type `T`.
455 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000456 sem::Matrix* mat3x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000457 return mat3x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000458 }
459
460 /// @return the tint AST type for a 3x3 matrix of the C type `T`.
461 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000462 sem::Matrix* mat3x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000463 return mat3x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000464 }
465
466 /// @return the tint AST type for a 3x4 matrix of the C type `T`.
467 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000468 sem::Matrix* mat3x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000469 return mat3x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000470 }
471
472 /// @return the tint AST type for a 4x2 matrix of the C type `T`.
473 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000474 sem::Matrix* mat4x2() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000475 return mat4x2(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000476 }
477
478 /// @return the tint AST type for a 4x3 matrix of the C type `T`.
479 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000480 sem::Matrix* mat4x3() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000481 return mat4x3(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000482 }
483
484 /// @return the tint AST type for a 4x4 matrix of the C type `T`.
485 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000486 sem::Matrix* mat4x4() const {
Antonio Maiorano25436862021-04-13 13:32:33 +0000487 return mat4x4(Of<T>());
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000488 }
489
490 /// @param subtype the array element type
491 /// @param n the array size. 0 represents a runtime-array.
492 /// @return the tint AST type for a array of size `n` of type `T`
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000493 sem::ArrayType* array(sem::Type* subtype, uint32_t n = 0) const {
494 return builder->create<sem::ArrayType>(subtype, n, ast::DecorationList{});
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000495 }
496
Ben Claytonbab31972021-02-08 21:16:21 +0000497 /// @param subtype the array element type
498 /// @param n the array size. 0 represents a runtime-array.
499 /// @param stride the array stride.
500 /// @return the tint AST type for a array of size `n` of type `T`
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000501 sem::ArrayType* array(sem::Type* subtype,
502 uint32_t n,
503 uint32_t stride) const {
504 return builder->create<sem::ArrayType>(
Ben Claytonbab31972021-02-08 21:16:21 +0000505 subtype, n,
James Price95d40772021-03-11 17:39:32 +0000506 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +0000507 builder->create<ast::StrideDecoration>(stride),
508 });
509 }
510
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000511 /// @return the tint AST type for an array of size `N` of type `T`
512 template <typename T, int N = 0>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000513 sem::ArrayType* array() const {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000514 return array(Of<T>(), N);
515 }
516
Ben Claytonbab31972021-02-08 21:16:21 +0000517 /// @param stride the array stride
518 /// @return the tint AST type for an array of size `N` of type `T`
519 template <typename T, int N = 0>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000520 sem::ArrayType* array(uint32_t stride) const {
Ben Claytonbab31972021-02-08 21:16:21 +0000521 return array(Of<T>(), N, stride);
522 }
Ben Clayton4db10dd2021-04-16 08:47:14 +0000523
Ben Clayton42d1e092021-02-02 14:29:15 +0000524 /// Creates an alias type
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000525 /// @param name the alias name
526 /// @param type the alias type
527 /// @returns the alias pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000528 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000529 sem::Alias* alias(NAME&& name, sem::Type* type) const {
530 return builder->create<sem::Alias>(builder->Sym(std::forward<NAME>(name)),
531 type);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000532 }
533
Ben Clayton4db10dd2021-04-16 08:47:14 +0000534 /// Creates an access control qualifier type
535 /// @param access the access control
536 /// @param type the inner type
537 /// @returns the access control qualifier type
Ben Clayton8a8d26b2021-04-20 15:04:21 +0000538 sem::AccessControl* access(ast::AccessControl::Access access,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000539 sem::Type* type) const {
540 return builder->create<sem::AccessControl>(access, type);
Ben Clayton4db10dd2021-04-16 08:47:14 +0000541 }
542
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000543 /// @return the tint AST pointer to `type` with the given ast::StorageClass
544 /// @param type the type of the pointer
545 /// @param storage_class the storage class of the pointer
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000546 sem::Pointer* pointer(sem::Type* type,
547 ast::StorageClass storage_class) const {
548 return builder->create<sem::Pointer>(type, storage_class);
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000549 }
550
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000551 /// @return the tint AST pointer to type `T` with the given
552 /// ast::StorageClass.
553 /// @param storage_class the storage class of the pointer
554 template <typename T>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000555 sem::Pointer* pointer(ast::StorageClass storage_class) const {
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000556 return pointer(Of<T>(), storage_class);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000557 }
558
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000559 /// @param impl the struct implementation
560 /// @returns a struct pointer
Ben Clayton913a2f42021-04-20 15:21:21 +0000561 sem::StructType* struct_(ast::Struct* impl) const {
562 return builder->create<sem::StructType>(impl);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000563 }
564
565 private:
566 /// CToAST<T> is specialized for various `T` types and each specialization
567 /// contains a single static `get()` method for obtaining the corresponding
568 /// AST type for the C type `T`.
569 /// `get()` has the signature:
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000570 /// `static sem::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000571 template <typename T>
572 struct CToAST {};
573
Ben Claytonc7ca7662021-02-17 16:23:52 +0000574 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000575 };
576
577 //////////////////////////////////////////////////////////////////////////////
578 // AST helper methods
579 //////////////////////////////////////////////////////////////////////////////
580
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000581 /// @param name the symbol string
582 /// @return a Symbol with the given name
583 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
584
585 /// @param sym the symbol
586 /// @return `sym`
587 Symbol Sym(Symbol sym) { return sym; }
588
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000589 /// @param expr the expression
590 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000591 template <typename T>
592 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
593 return expr;
594 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000595
Ben Claytonb8ea5912021-04-19 16:52:42 +0000596 /// Passthrough for nullptr
597 /// @return nullptr
598 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
599
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000600 /// @param name the identifier name
601 /// @return an ast::IdentifierExpression with the given name
602 ast::IdentifierExpression* Expr(const std::string& name) {
603 return create<ast::IdentifierExpression>(Symbols().Register(name));
604 }
605
Ben Clayton46d78d72021-02-10 21:34:26 +0000606 /// @param symbol the identifier symbol
607 /// @return an ast::IdentifierExpression with the given symbol
608 ast::IdentifierExpression* Expr(Symbol symbol) {
609 return create<ast::IdentifierExpression>(symbol);
610 }
611
Ben Claytonb8ea5912021-04-19 16:52:42 +0000612 /// @param variable the AST variable
613 /// @return an ast::IdentifierExpression with the variable's symbol
614 ast::IdentifierExpression* Expr(ast::Variable* variable) {
615 return create<ast::IdentifierExpression>(variable->symbol());
616 }
617
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000618 /// @param source the source information
619 /// @param name the identifier name
620 /// @return an ast::IdentifierExpression with the given name
621 ast::IdentifierExpression* Expr(const Source& source,
622 const std::string& name) {
623 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
624 }
625
626 /// @param name the identifier name
627 /// @return an ast::IdentifierExpression with the given name
628 ast::IdentifierExpression* Expr(const char* name) {
629 return create<ast::IdentifierExpression>(Symbols().Register(name));
630 }
631
632 /// @param value the boolean value
633 /// @return a Scalar constructor for the given value
634 ast::ScalarConstructorExpression* Expr(bool value) {
635 return create<ast::ScalarConstructorExpression>(Literal(value));
636 }
637
638 /// @param value the float value
639 /// @return a Scalar constructor for the given value
640 ast::ScalarConstructorExpression* Expr(f32 value) {
641 return create<ast::ScalarConstructorExpression>(Literal(value));
642 }
643
644 /// @param value the integer value
645 /// @return a Scalar constructor for the given value
646 ast::ScalarConstructorExpression* Expr(i32 value) {
647 return create<ast::ScalarConstructorExpression>(Literal(value));
648 }
649
650 /// @param value the unsigned int value
651 /// @return a Scalar constructor for the given value
652 ast::ScalarConstructorExpression* Expr(u32 value) {
653 return create<ast::ScalarConstructorExpression>(Literal(value));
654 }
655
656 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
657 /// `list`.
658 /// @param list the list to append too
659 /// @param arg the arg to create
660 template <typename ARG>
661 void Append(ast::ExpressionList& list, ARG&& arg) {
662 list.emplace_back(Expr(std::forward<ARG>(arg)));
663 }
664
665 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
666 /// then appends them to `list`.
667 /// @param list the list to append too
668 /// @param arg0 the first argument
669 /// @param args the rest of the arguments
670 template <typename ARG0, typename... ARGS>
671 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
672 Append(list, std::forward<ARG0>(arg0));
673 Append(list, std::forward<ARGS>(args)...);
674 }
675
676 /// @return an empty list of expressions
677 ast::ExpressionList ExprList() { return {}; }
678
679 /// @param args the list of expressions
680 /// @return the list of expressions converted to `ast::Expression`s using
681 /// `Expr()`,
682 template <typename... ARGS>
683 ast::ExpressionList ExprList(ARGS&&... args) {
684 ast::ExpressionList list;
685 list.reserve(sizeof...(args));
686 Append(list, std::forward<ARGS>(args)...);
687 return list;
688 }
689
690 /// @param list the list of expressions
691 /// @return `list`
692 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
693
694 /// @param val the boolan value
695 /// @return a boolean literal with the given value
696 ast::BoolLiteral* Literal(bool val) {
697 return create<ast::BoolLiteral>(ty.bool_(), val);
698 }
699
700 /// @param val the float value
701 /// @return a float literal with the given value
702 ast::FloatLiteral* Literal(f32 val) {
703 return create<ast::FloatLiteral>(ty.f32(), val);
704 }
705
706 /// @param val the unsigned int value
707 /// @return a ast::UintLiteral with the given value
708 ast::UintLiteral* Literal(u32 val) {
709 return create<ast::UintLiteral>(ty.u32(), val);
710 }
711
712 /// @param val the integer value
713 /// @return the ast::SintLiteral with the given value
714 ast::SintLiteral* Literal(i32 val) {
715 return create<ast::SintLiteral>(ty.i32(), val);
716 }
717
718 /// @param args the arguments for the type constructor
719 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
720 /// of `args` converted to `ast::Expression`s using `Expr()`
721 template <typename T, typename... ARGS>
722 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
723 return create<ast::TypeConstructorExpression>(
724 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
725 }
726
727 /// @param type the type to construct
728 /// @param args the arguments for the constructor
729 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
730 /// values `args`.
731 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000732 ast::TypeConstructorExpression* Construct(sem::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000733 return create<ast::TypeConstructorExpression>(
734 type, ExprList(std::forward<ARGS>(args)...));
735 }
736
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000737 /// Creates a constructor expression that constructs an object of
738 /// `type` filled with `elem_value`. For example,
739 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
740 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
741 /// @param type the type to construct
742 /// @param elem_value the initial or element value (for vec and mat) to
743 /// construct with
744 /// @return the constructor expression
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000745 ast::ConstructorExpression* ConstructValueFilledWith(sem::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000746 int elem_value = 0);
747
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000748 /// @param args the arguments for the vector constructor
749 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
750 /// `T`, constructed with the values `args`.
751 template <typename T, typename... ARGS>
752 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
753 return create<ast::TypeConstructorExpression>(
754 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
755 }
756
757 /// @param args the arguments for the vector constructor
758 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
759 /// `T`, constructed with the values `args`.
760 template <typename T, typename... ARGS>
761 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
762 return create<ast::TypeConstructorExpression>(
763 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
764 }
765
766 /// @param args the arguments for the vector constructor
767 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
768 /// `T`, constructed with the values `args`.
769 template <typename T, typename... ARGS>
770 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
771 return create<ast::TypeConstructorExpression>(
772 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
773 }
774
775 /// @param args the arguments for the matrix constructor
776 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
777 /// `T`, constructed with the values `args`.
778 template <typename T, typename... ARGS>
779 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
780 return create<ast::TypeConstructorExpression>(
781 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
782 }
783
784 /// @param args the arguments for the matrix constructor
785 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
786 /// `T`, constructed with the values `args`.
787 template <typename T, typename... ARGS>
788 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
789 return create<ast::TypeConstructorExpression>(
790 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
791 }
792
793 /// @param args the arguments for the matrix constructor
794 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
795 /// `T`, constructed with the values `args`.
796 template <typename T, typename... ARGS>
797 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
798 return create<ast::TypeConstructorExpression>(
799 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
800 }
801
802 /// @param args the arguments for the matrix constructor
803 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
804 /// `T`, constructed with the values `args`.
805 template <typename T, typename... ARGS>
806 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
807 return create<ast::TypeConstructorExpression>(
808 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
809 }
810
811 /// @param args the arguments for the matrix constructor
812 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
813 /// `T`, constructed with the values `args`.
814 template <typename T, typename... ARGS>
815 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
816 return create<ast::TypeConstructorExpression>(
817 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
818 }
819
820 /// @param args the arguments for the matrix constructor
821 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
822 /// `T`, constructed with the values `args`.
823 template <typename T, typename... ARGS>
824 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
825 return create<ast::TypeConstructorExpression>(
826 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
827 }
828
829 /// @param args the arguments for the matrix constructor
830 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
831 /// `T`, constructed with the values `args`.
832 template <typename T, typename... ARGS>
833 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
834 return create<ast::TypeConstructorExpression>(
835 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
836 }
837
838 /// @param args the arguments for the matrix constructor
839 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
840 /// `T`, constructed with the values `args`.
841 template <typename T, typename... ARGS>
842 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
843 return create<ast::TypeConstructorExpression>(
844 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
845 }
846
847 /// @param args the arguments for the matrix constructor
848 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
849 /// `T`, constructed with the values `args`.
850 template <typename T, typename... ARGS>
851 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
852 return create<ast::TypeConstructorExpression>(
853 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
854 }
855
856 /// @param args the arguments for the array constructor
857 /// @return an `ast::TypeConstructorExpression` of an array with element type
858 /// `T`, constructed with the values `args`.
859 template <typename T, int N = 0, typename... ARGS>
860 ast::TypeConstructorExpression* array(ARGS&&... args) {
861 return create<ast::TypeConstructorExpression>(
862 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
863 }
864
865 /// @param subtype the array element type
866 /// @param n the array size. 0 represents a runtime-array.
867 /// @param args the arguments for the array constructor
868 /// @return an `ast::TypeConstructorExpression` of an array with element type
869 /// `subtype`, constructed with the values `args`.
870 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000871 ast::TypeConstructorExpression* array(sem::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000872 uint32_t n,
873 ARGS&&... args) {
874 return create<ast::TypeConstructorExpression>(
875 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
876 }
877
878 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000879 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000880 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000881 /// @param constructor constructor expression
882 /// @param decorations variable decorations
883 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000884 template <typename NAME>
885 ast::Variable* Var(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000886 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000887 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000888 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000889 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000890 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
891 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000892 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000893
894 /// @param source the variable source
895 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000896 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000897 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000898 /// @param constructor constructor expression
899 /// @param decorations variable decorations
900 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000901 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000902 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000903 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000904 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000905 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000906 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000907 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000908 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000909 type, false, constructor, decorations);
910 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000911
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000912 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000913 /// @param type the variable type
914 /// @param constructor optional constructor expression
915 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000916 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000917 template <typename NAME>
918 ast::Variable* Const(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000919 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000920 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000921 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000922 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000923 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000924 constructor, decorations);
925 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000926
927 /// @param source the variable source
928 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000929 /// @param type the variable type
930 /// @param constructor optional constructor expression
931 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000932 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000933 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000934 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000935 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000936 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000937 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000938 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000939 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000940 ast::StorageClass::kNone, type, true,
941 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000942 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000943
James Pricedaf1f1c2021-04-08 22:15:48 +0000944 /// @param name the parameter name
945 /// @param type the parameter type
946 /// @param decorations optional parameter decorations
947 /// @returns a constant `ast::Variable` with the given name and type
948 template <typename NAME>
949 ast::Variable* Param(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000950 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000951 ast::DecorationList decorations = {}) {
952 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
953 ast::StorageClass::kNone, type, true, nullptr,
954 decorations);
955 }
956
957 /// @param source the parameter source
958 /// @param name the parameter name
959 /// @param type the parameter type
960 /// @param decorations optional parameter decorations
961 /// @returns a constant `ast::Variable` with the given name and type
962 template <typename NAME>
963 ast::Variable* Param(const Source& source,
964 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000965 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000966 ast::DecorationList decorations = {}) {
967 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
968 ast::StorageClass::kNone, type, true, nullptr,
969 decorations);
970 }
971
Ben Clayton401b96b2021-02-03 17:19:59 +0000972 /// @param args the arguments to pass to Var()
973 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
974 /// of `args`, which is automatically registered as a global variable with the
975 /// ast::Module.
976 template <typename... ARGS>
977 ast::Variable* Global(ARGS&&... args) {
978 auto* var = Var(std::forward<ARGS>(args)...);
979 AST().AddGlobalVariable(var);
980 return var;
981 }
982
983 /// @param args the arguments to pass to Const()
984 /// @returns a const `ast::Variable` constructed by calling Var() with the
985 /// arguments of `args`, which is automatically registered as a global
986 /// variable with the ast::Module.
987 template <typename... ARGS>
988 ast::Variable* GlobalConst(ARGS&&... args) {
989 auto* var = Const(std::forward<ARGS>(args)...);
990 AST().AddGlobalVariable(var);
991 return var;
992 }
993
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000994 /// @param func the function name
995 /// @param args the function call arguments
996 /// @returns a `ast::CallExpression` to the function `func`, with the
997 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
998 template <typename NAME, typename... ARGS>
999 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1000 return create<ast::CallExpression>(Expr(func),
1001 ExprList(std::forward<ARGS>(args)...));
1002 }
1003
1004 /// @param lhs the left hand argument to the addition operation
1005 /// @param rhs the right hand argument to the addition operation
1006 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1007 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001008 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001009 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1010 Expr(std::forward<LHS>(lhs)),
1011 Expr(std::forward<RHS>(rhs)));
1012 }
1013
1014 /// @param lhs the left hand argument to the subtraction operation
1015 /// @param rhs the right hand argument to the subtraction operation
1016 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1017 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001018 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001019 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1020 Expr(std::forward<LHS>(lhs)),
1021 Expr(std::forward<RHS>(rhs)));
1022 }
1023
1024 /// @param lhs the left hand argument to the multiplication operation
1025 /// @param rhs the right hand argument to the multiplication operation
1026 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1027 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001028 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001029 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1030 Expr(std::forward<LHS>(lhs)),
1031 Expr(std::forward<RHS>(rhs)));
1032 }
1033
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001034 /// @param source the source information
1035 /// @param lhs the left hand argument to the multiplication operation
1036 /// @param rhs the right hand argument to the multiplication operation
1037 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1038 template <typename LHS, typename RHS>
1039 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1040 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1041 Expr(std::forward<LHS>(lhs)),
1042 Expr(std::forward<RHS>(rhs)));
1043 }
1044
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001045 /// @param lhs the left hand argument to the division operation
1046 /// @param rhs the right hand argument to the division operation
1047 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1048 template <typename LHS, typename RHS>
1049 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1050 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1051 Expr(std::forward<LHS>(lhs)),
1052 Expr(std::forward<RHS>(rhs)));
1053 }
1054
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001055 /// @param arr the array argument for the array accessor expression
1056 /// @param idx the index argument for the array accessor expression
1057 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1058 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001059 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001060 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1061 Expr(std::forward<IDX>(idx)));
1062 }
1063
1064 /// @param obj the object for the member accessor expression
1065 /// @param idx the index argument for the array accessor expression
1066 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1067 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001068 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001069 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1070 Expr(std::forward<IDX>(idx)));
1071 }
1072
Ben Clayton42d1e092021-02-02 14:29:15 +00001073 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001074 /// @param val the offset value
1075 /// @returns the offset decoration pointer
1076 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1077 return create<ast::StructMemberOffsetDecoration>(source_, val);
1078 }
1079
Ben Claytond614dd52021-03-15 10:43:11 +00001080 /// Creates a ast::StructMemberSizeDecoration
1081 /// @param source the source information
1082 /// @param val the size value
1083 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001084 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1085 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001086 return create<ast::StructMemberSizeDecoration>(source, val);
1087 }
1088
1089 /// Creates a ast::StructMemberSizeDecoration
1090 /// @param val the size value
1091 /// @returns the size decoration pointer
1092 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1093 return create<ast::StructMemberSizeDecoration>(source_, val);
1094 }
1095
1096 /// Creates a ast::StructMemberAlignDecoration
1097 /// @param source the source information
1098 /// @param val the align value
1099 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001100 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1101 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001102 return create<ast::StructMemberAlignDecoration>(source, val);
1103 }
1104
1105 /// Creates a ast::StructMemberAlignDecoration
1106 /// @param val the align value
1107 /// @returns the align decoration pointer
1108 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1109 return create<ast::StructMemberAlignDecoration>(source_, val);
1110 }
1111
Ben Clayton42d1e092021-02-02 14:29:15 +00001112 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001113 /// @param source the source information
1114 /// @param name the function name
1115 /// @param params the function parameters
1116 /// @param type the function return type
1117 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001118 /// @param decorations the optional function decorations
1119 /// @param return_type_decorations the optional function return type
1120 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001121 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001122 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001123 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001124 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001125 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001126 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001127 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001128 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001129 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001130 auto* func =
1131 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1132 type, create<ast::BlockStatement>(body),
1133 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001134 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001135 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001136 }
1137
Ben Clayton42d1e092021-02-02 14:29:15 +00001138 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001139 /// @param name the function name
1140 /// @param params the function parameters
1141 /// @param type the function return type
1142 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001143 /// @param decorations the optional function decorations
1144 /// @param return_type_decorations the optional function return type
1145 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001146 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001147 template <typename NAME>
1148 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001149 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001150 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001151 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001152 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001153 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001154 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1155 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001156 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001157 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001158 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001159 }
1160
Ben Clayton49668bb2021-04-17 05:32:01 +00001161 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001162 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001163 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1164
1165 /// Creates an ast::ReturnStatement with the given return value
1166 /// @param val the return value
1167 /// @returns the return statement pointer
1168 template <typename EXPR>
1169 ast::ReturnStatement* Return(EXPR&& val) {
1170 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001171 }
1172
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001173 /// Creates a ast::Struct and sem::StructType, registering the
1174 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001175 /// @param source the source information
1176 /// @param name the struct name
1177 /// @param members the struct members
1178 /// @param decorations the optional struct decorations
1179 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001180 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001181 sem::StructType* Structure(const Source& source,
1182 NAME&& name,
1183 ast::StructMemberList members,
1184 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001185 auto sym = Sym(std::forward<NAME>(name));
1186 auto* impl = create<ast::Struct>(source, sym, std::move(members),
1187 std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001188 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001189 AST().AddConstructedType(type);
1190 return type;
1191 }
1192
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001193 /// Creates a ast::Struct and sem::StructType, registering the
1194 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001195 /// @param name the struct name
1196 /// @param members the struct members
1197 /// @param decorations the optional struct decorations
1198 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001199 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001200 sem::StructType* Structure(NAME&& name,
1201 ast::StructMemberList members,
1202 ast::DecorationList decorations = {}) {
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001203 auto sym = Sym(std::forward<NAME>(name));
Ben Claytond614dd52021-03-15 10:43:11 +00001204 auto* impl =
Ben Clayton8a8d26b2021-04-20 15:04:21 +00001205 create<ast::Struct>(sym, std::move(members), std::move(decorations));
Ben Clayton913a2f42021-04-20 15:21:21 +00001206 auto* type = ty.struct_(impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001207 AST().AddConstructedType(type);
1208 return type;
1209 }
1210
Ben Clayton42d1e092021-02-02 14:29:15 +00001211 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001212 /// @param source the source information
1213 /// @param name the struct member name
1214 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001215 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001216 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001217 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001218 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001219 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001220 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001221 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001222 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1223 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001224 }
1225
Ben Clayton42d1e092021-02-02 14:29:15 +00001226 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001227 /// @param name the struct member name
1228 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001229 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001230 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001231 template <typename NAME>
1232 ast::StructMember* Member(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001233 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001234 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001235 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1236 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001237 }
1238
Ben Claytonbab31972021-02-08 21:16:21 +00001239 /// Creates a ast::StructMember with the given byte offset
1240 /// @param offset the offset to use in the StructMemberOffsetDecoration
1241 /// @param name the struct member name
1242 /// @param type the struct member type
1243 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001244 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001245 ast::StructMember* Member(uint32_t offset, NAME&& name, sem::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001246 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001247 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001248 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001249 create<ast::StructMemberOffsetDecoration>(offset),
1250 });
1251 }
1252
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001253 /// Creates a ast::BlockStatement with input statements
1254 /// @param statements statements of block
1255 /// @returns the block statement pointer
1256 template <typename... Statements>
1257 ast::BlockStatement* Block(Statements&&... statements) {
1258 return create<ast::BlockStatement>(
1259 ast::StatementList{std::forward<Statements>(statements)...});
1260 }
1261
1262 /// Creates a ast::ElseStatement with input condition and body
1263 /// @param condition the else condition expression
1264 /// @param body the else body
1265 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001266 template <typename CONDITION>
1267 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1268 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1269 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001270 }
1271
1272 /// Creates a ast::IfStatement with input condition, body, and optional
1273 /// variadic else statements
1274 /// @param condition the if statement condition expression
1275 /// @param body the if statement body
1276 /// @param elseStatements optional variadic else statements
1277 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001278 template <typename CONDITION, typename... ELSE_STATEMENTS>
1279 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001280 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001281 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001282 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001283 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001284 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001285 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001286 }
1287
1288 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001289 /// @param lhs the left hand side expression initializer
1290 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001291 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001292 template <typename LhsExpressionInit, typename RhsExpressionInit>
1293 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1294 RhsExpressionInit&& rhs) {
1295 return create<ast::AssignmentStatement>(
1296 Expr(std::forward<LhsExpressionInit>(lhs)),
1297 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001298 }
1299
1300 /// Creates a ast::LoopStatement with input body and optional continuing
1301 /// @param body the loop body
1302 /// @param continuing the optional continuing block
1303 /// @returns the loop statement pointer
1304 ast::LoopStatement* Loop(ast::BlockStatement* body,
1305 ast::BlockStatement* continuing = nullptr) {
1306 return create<ast::LoopStatement>(body, continuing);
1307 }
1308
1309 /// Creates a ast::VariableDeclStatement for the input variable
1310 /// @param var the variable to wrap in a decl statement
1311 /// @returns the variable decl statement pointer
1312 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1313 return create<ast::VariableDeclStatement>(var);
1314 }
1315
Antonio Maioranocea744d2021-03-25 12:55:27 +00001316 /// Creates a ast::SwitchStatement with input expression and cases
1317 /// @param condition the condition expression initializer
1318 /// @param cases case statements
1319 /// @returns the switch statement pointer
1320 template <typename ExpressionInit, typename... Cases>
1321 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1322 return create<ast::SwitchStatement>(
1323 Expr(std::forward<ExpressionInit>(condition)),
1324 ast::CaseStatementList{std::forward<Cases>(cases)...});
1325 }
1326
1327 /// Creates a ast::CaseStatement with input list of selectors, and body
1328 /// @param selectors list of selectors
1329 /// @param body the case body
1330 /// @returns the case statement pointer
1331 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1332 ast::BlockStatement* body = nullptr) {
1333 return create<ast::CaseStatement>(std::move(selectors),
1334 body ? body : Block());
1335 }
1336
1337 /// Convenient overload that takes a single selector
1338 /// @param selector a single case selector
1339 /// @param body the case body
1340 /// @returns the case statement pointer
1341 ast::CaseStatement* Case(ast::IntLiteral* selector,
1342 ast::BlockStatement* body = nullptr) {
1343 return Case(ast::CaseSelectorList{selector}, body);
1344 }
1345
1346 /// Convenience function that creates a 'default' ast::CaseStatement
1347 /// @param body the case body
1348 /// @returns the case statement pointer
1349 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1350 return Case(ast::CaseSelectorList{}, body);
1351 }
1352
James Price68f558f2021-04-06 15:51:47 +00001353 /// Creates an ast::BuiltinDecoration
1354 /// @param source the source information
1355 /// @param builtin the builtin value
1356 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001357 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001358 return create<ast::BuiltinDecoration>(source, builtin);
1359 }
1360
1361 /// Creates an ast::BuiltinDecoration
1362 /// @param builtin the builtin value
1363 /// @returns the builtin decoration pointer
1364 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1365 return create<ast::BuiltinDecoration>(source_, builtin);
1366 }
1367
1368 /// Creates an ast::LocationDecoration
1369 /// @param source the source information
1370 /// @param location the location value
1371 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001372 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001373 return create<ast::LocationDecoration>(source, location);
1374 }
1375
1376 /// Creates an ast::LocationDecoration
1377 /// @param location the location value
1378 /// @returns the location decoration pointer
1379 ast::LocationDecoration* Location(uint32_t location) {
1380 return create<ast::LocationDecoration>(source_, location);
1381 }
1382
1383 /// Creates an ast::StageDecoration
1384 /// @param source the source information
1385 /// @param stage the pipeline stage
1386 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001387 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001388 return create<ast::StageDecoration>(source, stage);
1389 }
1390
1391 /// Creates an ast::StageDecoration
1392 /// @param stage the pipeline stage
1393 /// @returns the stage decoration pointer
1394 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1395 return create<ast::StageDecoration>(source_, stage);
1396 }
1397
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001398 /// Sets the current builder source to `src`
1399 /// @param src the Source used for future create() calls
1400 void SetSource(const Source& src) {
1401 AssertNotMoved();
1402 source_ = src;
1403 }
1404
1405 /// Sets the current builder source to `loc`
1406 /// @param loc the Source used for future create() calls
1407 void SetSource(const Source::Location& loc) {
1408 AssertNotMoved();
1409 source_ = Source(loc);
1410 }
1411
Ben Clayton33352542021-01-29 16:43:41 +00001412 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001413 /// @note As the Resolver is run when the Program is built, this will only be
1414 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001415 /// @param expr the AST expression
1416 /// @return the resolved semantic type for the expression, or nullptr if the
1417 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001418 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001419
Ben Clayton169512e2021-04-17 05:52:11 +00001420 /// Wraps the ast::Literal in a statement. This is used by tests that
1421 /// construct a partial AST and require the Resolver to reach these
1422 /// nodes.
1423 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1424 /// @return the ast::Statement that wraps the ast::Statement
1425 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001426 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001427 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001428 /// nodes.
1429 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1430 /// @return the ast::Statement that wraps the ast::Expression
1431 ast::Statement* WrapInStatement(ast::Expression* expr);
1432 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001433 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001434 /// these nodes.
1435 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1436 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1437 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1438 /// Returns the statement argument. Used as a passthrough-overload by
1439 /// WrapInFunction().
1440 /// @param stmt the ast::Statement
1441 /// @return `stmt`
1442 ast::Statement* WrapInStatement(ast::Statement* stmt);
1443 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001444 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001445 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001446 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001447 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001448 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001449 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001450 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001451 }
1452 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001453 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001454 /// @returns the function
1455 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001456
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001457 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001458 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001459
1460 protected:
1461 /// Asserts that the builder has not been moved.
1462 void AssertNotMoved() const;
1463
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001464 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001465 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001466 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001467 ASTNodeAllocator ast_nodes_;
1468 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001469 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001470 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001471 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001472 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001473
1474 /// The source to use when creating AST nodes without providing a Source as
1475 /// the first argument.
1476 Source source_;
1477
Ben Clayton5f0ea112021-03-09 10:54:37 +00001478 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001479 /// program when built.
1480 bool resolve_on_build_ = true;
1481
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001482 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1483 bool moved_ = false;
1484};
1485
1486//! @cond Doxygen_Suppress
1487// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1488template <>
1489struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001490 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001491 return t->i32();
1492 }
1493};
1494template <>
1495struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001496 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001497 return t->u32();
1498 }
1499};
1500template <>
1501struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001502 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001503 return t->f32();
1504 }
1505};
1506template <>
1507struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001508 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001509 return t->bool_();
1510 }
1511};
1512template <>
1513struct ProgramBuilder::TypesBuilder::CToAST<void> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001514 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001515 return t->void_();
1516 }
1517};
1518//! @endcond
1519
Ben Clayton917b14b2021-04-19 16:50:23 +00001520/// @param builder the ProgramBuilder
1521/// @returns the ProgramID of the ProgramBuilder
1522inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1523 return builder->ID();
1524}
1525
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001526} // namespace tint
1527
1528#endif // SRC_PROGRAM_BUILDER_H_