blob: e5dbe148c20dd8b6fd773370bc250d973b8eb303 [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
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000538 sem::AccessControl* access(ast::AccessControl access,
539 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
559 /// @param name the struct name
560 /// @param impl the struct implementation
561 /// @returns a struct pointer
Ben Clayton1d618b12021-04-09 16:19:48 +0000562 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000563 sem::StructType* struct_(NAME&& name, ast::Struct* impl) const {
564 return builder->create<sem::StructType>(
Ben Clayton1d618b12021-04-09 16:19:48 +0000565 builder->Sym(std::forward<NAME>(name)), impl);
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000566 }
567
568 private:
569 /// CToAST<T> is specialized for various `T` types and each specialization
570 /// contains a single static `get()` method for obtaining the corresponding
571 /// AST type for the C type `T`.
572 /// `get()` has the signature:
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000573 /// `static sem::Type* get(Types* t)`
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000574 template <typename T>
575 struct CToAST {};
576
Ben Claytonc7ca7662021-02-17 16:23:52 +0000577 ProgramBuilder* const builder;
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000578 };
579
580 //////////////////////////////////////////////////////////////////////////////
581 // AST helper methods
582 //////////////////////////////////////////////////////////////////////////////
583
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000584 /// @param name the symbol string
585 /// @return a Symbol with the given name
586 Symbol Sym(const std::string& name) { return Symbols().Register(name); }
587
588 /// @param sym the symbol
589 /// @return `sym`
590 Symbol Sym(Symbol sym) { return sym; }
591
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000592 /// @param expr the expression
593 /// @return expr
Ben Clayton6d612ad2021-02-24 14:15:02 +0000594 template <typename T>
595 traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) {
596 return expr;
597 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000598
Ben Claytonb8ea5912021-04-19 16:52:42 +0000599 /// Passthrough for nullptr
600 /// @return nullptr
601 ast::IdentifierExpression* Expr(std::nullptr_t) { return nullptr; }
602
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000603 /// @param name the identifier name
604 /// @return an ast::IdentifierExpression with the given name
605 ast::IdentifierExpression* Expr(const std::string& name) {
606 return create<ast::IdentifierExpression>(Symbols().Register(name));
607 }
608
Ben Clayton46d78d72021-02-10 21:34:26 +0000609 /// @param symbol the identifier symbol
610 /// @return an ast::IdentifierExpression with the given symbol
611 ast::IdentifierExpression* Expr(Symbol symbol) {
612 return create<ast::IdentifierExpression>(symbol);
613 }
614
Ben Claytonb8ea5912021-04-19 16:52:42 +0000615 /// @param variable the AST variable
616 /// @return an ast::IdentifierExpression with the variable's symbol
617 ast::IdentifierExpression* Expr(ast::Variable* variable) {
618 return create<ast::IdentifierExpression>(variable->symbol());
619 }
620
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000621 /// @param source the source information
622 /// @param name the identifier name
623 /// @return an ast::IdentifierExpression with the given name
624 ast::IdentifierExpression* Expr(const Source& source,
625 const std::string& name) {
626 return create<ast::IdentifierExpression>(source, Symbols().Register(name));
627 }
628
629 /// @param name the identifier name
630 /// @return an ast::IdentifierExpression with the given name
631 ast::IdentifierExpression* Expr(const char* name) {
632 return create<ast::IdentifierExpression>(Symbols().Register(name));
633 }
634
635 /// @param value the boolean value
636 /// @return a Scalar constructor for the given value
637 ast::ScalarConstructorExpression* Expr(bool value) {
638 return create<ast::ScalarConstructorExpression>(Literal(value));
639 }
640
641 /// @param value the float value
642 /// @return a Scalar constructor for the given value
643 ast::ScalarConstructorExpression* Expr(f32 value) {
644 return create<ast::ScalarConstructorExpression>(Literal(value));
645 }
646
647 /// @param value the integer value
648 /// @return a Scalar constructor for the given value
649 ast::ScalarConstructorExpression* Expr(i32 value) {
650 return create<ast::ScalarConstructorExpression>(Literal(value));
651 }
652
653 /// @param value the unsigned int value
654 /// @return a Scalar constructor for the given value
655 ast::ScalarConstructorExpression* Expr(u32 value) {
656 return create<ast::ScalarConstructorExpression>(Literal(value));
657 }
658
659 /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to
660 /// `list`.
661 /// @param list the list to append too
662 /// @param arg the arg to create
663 template <typename ARG>
664 void Append(ast::ExpressionList& list, ARG&& arg) {
665 list.emplace_back(Expr(std::forward<ARG>(arg)));
666 }
667
668 /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`,
669 /// then appends them to `list`.
670 /// @param list the list to append too
671 /// @param arg0 the first argument
672 /// @param args the rest of the arguments
673 template <typename ARG0, typename... ARGS>
674 void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) {
675 Append(list, std::forward<ARG0>(arg0));
676 Append(list, std::forward<ARGS>(args)...);
677 }
678
679 /// @return an empty list of expressions
680 ast::ExpressionList ExprList() { return {}; }
681
682 /// @param args the list of expressions
683 /// @return the list of expressions converted to `ast::Expression`s using
684 /// `Expr()`,
685 template <typename... ARGS>
686 ast::ExpressionList ExprList(ARGS&&... args) {
687 ast::ExpressionList list;
688 list.reserve(sizeof...(args));
689 Append(list, std::forward<ARGS>(args)...);
690 return list;
691 }
692
693 /// @param list the list of expressions
694 /// @return `list`
695 ast::ExpressionList ExprList(ast::ExpressionList list) { return list; }
696
697 /// @param val the boolan value
698 /// @return a boolean literal with the given value
699 ast::BoolLiteral* Literal(bool val) {
700 return create<ast::BoolLiteral>(ty.bool_(), val);
701 }
702
703 /// @param val the float value
704 /// @return a float literal with the given value
705 ast::FloatLiteral* Literal(f32 val) {
706 return create<ast::FloatLiteral>(ty.f32(), val);
707 }
708
709 /// @param val the unsigned int value
710 /// @return a ast::UintLiteral with the given value
711 ast::UintLiteral* Literal(u32 val) {
712 return create<ast::UintLiteral>(ty.u32(), val);
713 }
714
715 /// @param val the integer value
716 /// @return the ast::SintLiteral with the given value
717 ast::SintLiteral* Literal(i32 val) {
718 return create<ast::SintLiteral>(ty.i32(), val);
719 }
720
721 /// @param args the arguments for the type constructor
722 /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values
723 /// of `args` converted to `ast::Expression`s using `Expr()`
724 template <typename T, typename... ARGS>
725 ast::TypeConstructorExpression* Construct(ARGS&&... args) {
726 return create<ast::TypeConstructorExpression>(
727 ty.Of<T>(), ExprList(std::forward<ARGS>(args)...));
728 }
729
730 /// @param type the type to construct
731 /// @param args the arguments for the constructor
732 /// @return an `ast::TypeConstructorExpression` of `type` constructed with the
733 /// values `args`.
734 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000735 ast::TypeConstructorExpression* Construct(sem::Type* type, ARGS&&... args) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000736 return create<ast::TypeConstructorExpression>(
737 type, ExprList(std::forward<ARGS>(args)...));
738 }
739
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000740 /// Creates a constructor expression that constructs an object of
741 /// `type` filled with `elem_value`. For example,
742 /// ConstructValueFilledWith(ty.mat3x4<float>(), 5) returns a
743 /// TypeConstructorExpression for a Mat3x4 filled with 5.0f values.
744 /// @param type the type to construct
745 /// @param elem_value the initial or element value (for vec and mat) to
746 /// construct with
747 /// @return the constructor expression
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000748 ast::ConstructorExpression* ConstructValueFilledWith(sem::Type* type,
Antonio Maiorano39a65a12021-03-31 12:46:52 +0000749 int elem_value = 0);
750
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000751 /// @param args the arguments for the vector constructor
752 /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type
753 /// `T`, constructed with the values `args`.
754 template <typename T, typename... ARGS>
755 ast::TypeConstructorExpression* vec2(ARGS&&... args) {
756 return create<ast::TypeConstructorExpression>(
757 ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...));
758 }
759
760 /// @param args the arguments for the vector constructor
761 /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type
762 /// `T`, constructed with the values `args`.
763 template <typename T, typename... ARGS>
764 ast::TypeConstructorExpression* vec3(ARGS&&... args) {
765 return create<ast::TypeConstructorExpression>(
766 ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...));
767 }
768
769 /// @param args the arguments for the vector constructor
770 /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type
771 /// `T`, constructed with the values `args`.
772 template <typename T, typename... ARGS>
773 ast::TypeConstructorExpression* vec4(ARGS&&... args) {
774 return create<ast::TypeConstructorExpression>(
775 ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...));
776 }
777
778 /// @param args the arguments for the matrix constructor
779 /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type
780 /// `T`, constructed with the values `args`.
781 template <typename T, typename... ARGS>
782 ast::TypeConstructorExpression* mat2x2(ARGS&&... args) {
783 return create<ast::TypeConstructorExpression>(
784 ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...));
785 }
786
787 /// @param args the arguments for the matrix constructor
788 /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type
789 /// `T`, constructed with the values `args`.
790 template <typename T, typename... ARGS>
791 ast::TypeConstructorExpression* mat2x3(ARGS&&... args) {
792 return create<ast::TypeConstructorExpression>(
793 ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...));
794 }
795
796 /// @param args the arguments for the matrix constructor
797 /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type
798 /// `T`, constructed with the values `args`.
799 template <typename T, typename... ARGS>
800 ast::TypeConstructorExpression* mat2x4(ARGS&&... args) {
801 return create<ast::TypeConstructorExpression>(
802 ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...));
803 }
804
805 /// @param args the arguments for the matrix constructor
806 /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type
807 /// `T`, constructed with the values `args`.
808 template <typename T, typename... ARGS>
809 ast::TypeConstructorExpression* mat3x2(ARGS&&... args) {
810 return create<ast::TypeConstructorExpression>(
811 ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...));
812 }
813
814 /// @param args the arguments for the matrix constructor
815 /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type
816 /// `T`, constructed with the values `args`.
817 template <typename T, typename... ARGS>
818 ast::TypeConstructorExpression* mat3x3(ARGS&&... args) {
819 return create<ast::TypeConstructorExpression>(
820 ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...));
821 }
822
823 /// @param args the arguments for the matrix constructor
824 /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type
825 /// `T`, constructed with the values `args`.
826 template <typename T, typename... ARGS>
827 ast::TypeConstructorExpression* mat3x4(ARGS&&... args) {
828 return create<ast::TypeConstructorExpression>(
829 ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...));
830 }
831
832 /// @param args the arguments for the matrix constructor
833 /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type
834 /// `T`, constructed with the values `args`.
835 template <typename T, typename... ARGS>
836 ast::TypeConstructorExpression* mat4x2(ARGS&&... args) {
837 return create<ast::TypeConstructorExpression>(
838 ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...));
839 }
840
841 /// @param args the arguments for the matrix constructor
842 /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type
843 /// `T`, constructed with the values `args`.
844 template <typename T, typename... ARGS>
845 ast::TypeConstructorExpression* mat4x3(ARGS&&... args) {
846 return create<ast::TypeConstructorExpression>(
847 ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...));
848 }
849
850 /// @param args the arguments for the matrix constructor
851 /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type
852 /// `T`, constructed with the values `args`.
853 template <typename T, typename... ARGS>
854 ast::TypeConstructorExpression* mat4x4(ARGS&&... args) {
855 return create<ast::TypeConstructorExpression>(
856 ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...));
857 }
858
859 /// @param args the arguments for the array constructor
860 /// @return an `ast::TypeConstructorExpression` of an array with element type
861 /// `T`, constructed with the values `args`.
862 template <typename T, int N = 0, typename... ARGS>
863 ast::TypeConstructorExpression* array(ARGS&&... args) {
864 return create<ast::TypeConstructorExpression>(
865 ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...));
866 }
867
868 /// @param subtype the array element type
869 /// @param n the array size. 0 represents a runtime-array.
870 /// @param args the arguments for the array constructor
871 /// @return an `ast::TypeConstructorExpression` of an array with element type
872 /// `subtype`, constructed with the values `args`.
873 template <typename... ARGS>
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000874 ast::TypeConstructorExpression* array(sem::Type* subtype,
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000875 uint32_t n,
876 ARGS&&... args) {
877 return create<ast::TypeConstructorExpression>(
878 ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...));
879 }
880
881 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000882 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000883 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000884 /// @param constructor constructor expression
885 /// @param decorations variable decorations
886 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000887 template <typename NAME>
888 ast::Variable* Var(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000889 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000890 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000891 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000892 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000893 return create<ast::Variable>(Sym(std::forward<NAME>(name)), storage, type,
894 false, constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000895 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000896
897 /// @param source the variable source
898 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000899 /// @param type the variable type
Ben Clayton37571bc2021-02-16 23:57:01 +0000900 /// @param storage the variable storage class
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000901 /// @param constructor constructor expression
902 /// @param decorations variable decorations
903 /// @returns a `ast::Variable` with the given name, storage and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000904 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000905 ast::Variable* Var(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000906 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000907 sem::Type* type,
Ben Clayton37571bc2021-02-16 23:57:01 +0000908 ast::StorageClass storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000909 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000910 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000911 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)), storage,
Ben Clayton46d78d72021-02-10 21:34:26 +0000912 type, false, constructor, decorations);
913 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000914
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000915 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000916 /// @param type the variable type
917 /// @param constructor optional constructor expression
918 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000919 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000920 template <typename NAME>
921 ast::Variable* Const(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000922 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000923 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000924 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000925 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000926 ast::StorageClass::kNone, type, true,
Ben Clayton46d78d72021-02-10 21:34:26 +0000927 constructor, decorations);
928 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000929
930 /// @param source the variable source
931 /// @param name the variable name
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000932 /// @param type the variable type
933 /// @param constructor optional constructor expression
934 /// @param decorations optional variable decorations
James Pricedaf1f1c2021-04-08 22:15:48 +0000935 /// @returns a constant `ast::Variable` with the given name and type
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000936 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000937 ast::Variable* Const(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000938 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000939 sem::Type* type,
Ben Clayton46d78d72021-02-10 21:34:26 +0000940 ast::Expression* constructor = nullptr,
James Price95d40772021-03-11 17:39:32 +0000941 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +0000942 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
Ben Clayton81a29fe2021-02-17 00:26:52 +0000943 ast::StorageClass::kNone, type, true,
944 constructor, decorations);
Ben Clayton46d78d72021-02-10 21:34:26 +0000945 }
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000946
James Pricedaf1f1c2021-04-08 22:15:48 +0000947 /// @param name the parameter name
948 /// @param type the parameter type
949 /// @param decorations optional parameter decorations
950 /// @returns a constant `ast::Variable` with the given name and type
951 template <typename NAME>
952 ast::Variable* Param(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000953 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000954 ast::DecorationList decorations = {}) {
955 return create<ast::Variable>(Sym(std::forward<NAME>(name)),
956 ast::StorageClass::kNone, type, true, nullptr,
957 decorations);
958 }
959
960 /// @param source the parameter source
961 /// @param name the parameter name
962 /// @param type the parameter type
963 /// @param decorations optional parameter decorations
964 /// @returns a constant `ast::Variable` with the given name and type
965 template <typename NAME>
966 ast::Variable* Param(const Source& source,
967 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +0000968 sem::Type* type,
James Pricedaf1f1c2021-04-08 22:15:48 +0000969 ast::DecorationList decorations = {}) {
970 return create<ast::Variable>(source, Sym(std::forward<NAME>(name)),
971 ast::StorageClass::kNone, type, true, nullptr,
972 decorations);
973 }
974
Ben Clayton401b96b2021-02-03 17:19:59 +0000975 /// @param args the arguments to pass to Var()
976 /// @returns a `ast::Variable` constructed by calling Var() with the arguments
977 /// of `args`, which is automatically registered as a global variable with the
978 /// ast::Module.
979 template <typename... ARGS>
980 ast::Variable* Global(ARGS&&... args) {
981 auto* var = Var(std::forward<ARGS>(args)...);
982 AST().AddGlobalVariable(var);
983 return var;
984 }
985
986 /// @param args the arguments to pass to Const()
987 /// @returns a const `ast::Variable` constructed by calling Var() with the
988 /// arguments of `args`, which is automatically registered as a global
989 /// variable with the ast::Module.
990 template <typename... ARGS>
991 ast::Variable* GlobalConst(ARGS&&... args) {
992 auto* var = Const(std::forward<ARGS>(args)...);
993 AST().AddGlobalVariable(var);
994 return var;
995 }
996
Ben Claytona6b9a8e2021-01-26 16:57:10 +0000997 /// @param func the function name
998 /// @param args the function call arguments
999 /// @returns a `ast::CallExpression` to the function `func`, with the
1000 /// arguments of `args` converted to `ast::Expression`s using `Expr()`.
1001 template <typename NAME, typename... ARGS>
1002 ast::CallExpression* Call(NAME&& func, ARGS&&... args) {
1003 return create<ast::CallExpression>(Expr(func),
1004 ExprList(std::forward<ARGS>(args)...));
1005 }
1006
1007 /// @param lhs the left hand argument to the addition operation
1008 /// @param rhs the right hand argument to the addition operation
1009 /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs`
1010 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001011 ast::BinaryExpression* Add(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001012 return create<ast::BinaryExpression>(ast::BinaryOp::kAdd,
1013 Expr(std::forward<LHS>(lhs)),
1014 Expr(std::forward<RHS>(rhs)));
1015 }
1016
1017 /// @param lhs the left hand argument to the subtraction operation
1018 /// @param rhs the right hand argument to the subtraction operation
1019 /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs`
1020 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001021 ast::BinaryExpression* Sub(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001022 return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract,
1023 Expr(std::forward<LHS>(lhs)),
1024 Expr(std::forward<RHS>(rhs)));
1025 }
1026
1027 /// @param lhs the left hand argument to the multiplication operation
1028 /// @param rhs the right hand argument to the multiplication operation
1029 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1030 template <typename LHS, typename RHS>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001031 ast::BinaryExpression* Mul(LHS&& lhs, RHS&& rhs) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001032 return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply,
1033 Expr(std::forward<LHS>(lhs)),
1034 Expr(std::forward<RHS>(rhs)));
1035 }
1036
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001037 /// @param source the source information
1038 /// @param lhs the left hand argument to the multiplication operation
1039 /// @param rhs the right hand argument to the multiplication operation
1040 /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs`
1041 template <typename LHS, typename RHS>
1042 ast::BinaryExpression* Mul(const Source& source, LHS&& lhs, RHS&& rhs) {
1043 return create<ast::BinaryExpression>(source, ast::BinaryOp::kMultiply,
1044 Expr(std::forward<LHS>(lhs)),
1045 Expr(std::forward<RHS>(rhs)));
1046 }
1047
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001048 /// @param lhs the left hand argument to the division operation
1049 /// @param rhs the right hand argument to the division operation
1050 /// @returns a `ast::BinaryExpression` dividing `lhs` by `rhs`
1051 template <typename LHS, typename RHS>
1052 ast::Expression* Div(LHS&& lhs, RHS&& rhs) {
1053 return create<ast::BinaryExpression>(ast::BinaryOp::kDivide,
1054 Expr(std::forward<LHS>(lhs)),
1055 Expr(std::forward<RHS>(rhs)));
1056 }
1057
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001058 /// @param arr the array argument for the array accessor expression
1059 /// @param idx the index argument for the array accessor expression
1060 /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx`
1061 template <typename ARR, typename IDX>
Antonio Maiorano61dabab2021-04-01 19:58:37 +00001062 ast::ArrayAccessorExpression* IndexAccessor(ARR&& arr, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001063 return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)),
1064 Expr(std::forward<IDX>(idx)));
1065 }
1066
1067 /// @param obj the object for the member accessor expression
1068 /// @param idx the index argument for the array accessor expression
1069 /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx`
1070 template <typename OBJ, typename IDX>
Ben Clayton6d612ad2021-02-24 14:15:02 +00001071 ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001072 return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)),
1073 Expr(std::forward<IDX>(idx)));
1074 }
1075
Ben Clayton42d1e092021-02-02 14:29:15 +00001076 /// Creates a ast::StructMemberOffsetDecoration
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001077 /// @param val the offset value
1078 /// @returns the offset decoration pointer
1079 ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) {
1080 return create<ast::StructMemberOffsetDecoration>(source_, val);
1081 }
1082
Ben Claytond614dd52021-03-15 10:43:11 +00001083 /// Creates a ast::StructMemberSizeDecoration
1084 /// @param source the source information
1085 /// @param val the size value
1086 /// @returns the size decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001087 ast::StructMemberSizeDecoration* MemberSize(const Source& source,
1088 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001089 return create<ast::StructMemberSizeDecoration>(source, val);
1090 }
1091
1092 /// Creates a ast::StructMemberSizeDecoration
1093 /// @param val the size value
1094 /// @returns the size decoration pointer
1095 ast::StructMemberSizeDecoration* MemberSize(uint32_t val) {
1096 return create<ast::StructMemberSizeDecoration>(source_, val);
1097 }
1098
1099 /// Creates a ast::StructMemberAlignDecoration
1100 /// @param source the source information
1101 /// @param val the align value
1102 /// @returns the align decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001103 ast::StructMemberAlignDecoration* MemberAlign(const Source& source,
1104 uint32_t val) {
Ben Claytond614dd52021-03-15 10:43:11 +00001105 return create<ast::StructMemberAlignDecoration>(source, val);
1106 }
1107
1108 /// Creates a ast::StructMemberAlignDecoration
1109 /// @param val the align value
1110 /// @returns the align decoration pointer
1111 ast::StructMemberAlignDecoration* MemberAlign(uint32_t val) {
1112 return create<ast::StructMemberAlignDecoration>(source_, val);
1113 }
1114
Ben Clayton42d1e092021-02-02 14:29:15 +00001115 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001116 /// @param source the source information
1117 /// @param name the function name
1118 /// @param params the function parameters
1119 /// @param type the function return type
1120 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001121 /// @param decorations the optional function decorations
1122 /// @param return_type_decorations the optional function return type
1123 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001124 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001125 template <typename NAME>
Antonio Maiorano101f4632021-04-07 20:35:11 +00001126 ast::Function* Func(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001127 NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001128 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001129 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001130 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001131 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001132 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001133 auto* func =
1134 create<ast::Function>(source, Sym(std::forward<NAME>(name)), params,
1135 type, create<ast::BlockStatement>(body),
1136 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001137 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001138 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001139 }
1140
Ben Clayton42d1e092021-02-02 14:29:15 +00001141 /// Creates an ast::Function and registers it with the ast::Module.
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001142 /// @param name the function name
1143 /// @param params the function parameters
1144 /// @param type the function return type
1145 /// @param body the function body
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001146 /// @param decorations the optional function decorations
1147 /// @param return_type_decorations the optional function return type
1148 /// decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001149 /// @returns the function pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001150 template <typename NAME>
1151 ast::Function* Func(NAME&& name,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001152 ast::VariableList params,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001153 sem::Type* type,
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001154 ast::StatementList body,
Ben Clayton4f154a82021-04-06 18:15:17 +00001155 ast::DecorationList decorations = {},
James Pricefeecbe02021-03-15 17:01:34 +00001156 ast::DecorationList return_type_decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001157 auto* func = create<ast::Function>(Sym(std::forward<NAME>(name)), params,
1158 type, create<ast::BlockStatement>(body),
James Pricefeecbe02021-03-15 17:01:34 +00001159 decorations, return_type_decorations);
James Price3eaa4502021-02-09 21:26:21 +00001160 AST().AddFunction(func);
Ben Clayton42d1e092021-02-02 14:29:15 +00001161 return func;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001162 }
1163
Ben Clayton49668bb2021-04-17 05:32:01 +00001164 /// Creates an ast::ReturnStatement with no return value
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001165 /// @returns the return statement pointer
Ben Clayton49668bb2021-04-17 05:32:01 +00001166 ast::ReturnStatement* Return() { return create<ast::ReturnStatement>(); }
1167
1168 /// Creates an ast::ReturnStatement with the given return value
1169 /// @param val the return value
1170 /// @returns the return statement pointer
1171 template <typename EXPR>
1172 ast::ReturnStatement* Return(EXPR&& val) {
1173 return create<ast::ReturnStatement>(Expr(std::forward<EXPR>(val)));
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001174 }
1175
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001176 /// Creates a ast::Struct and sem::StructType, registering the
1177 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001178 /// @param source the source information
1179 /// @param name the struct name
1180 /// @param members the struct members
1181 /// @param decorations the optional struct decorations
1182 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001183 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001184 sem::StructType* Structure(const Source& source,
1185 NAME&& name,
1186 ast::StructMemberList members,
1187 ast::DecorationList decorations = {}) {
Ben Claytond614dd52021-03-15 10:43:11 +00001188 auto* impl =
1189 create<ast::Struct>(source, std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001190 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001191 AST().AddConstructedType(type);
1192 return type;
1193 }
1194
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001195 /// Creates a ast::Struct and sem::StructType, registering the
1196 /// sem::StructType with the AST().ConstructedTypes().
Ben Claytond614dd52021-03-15 10:43:11 +00001197 /// @param name the struct name
1198 /// @param members the struct members
1199 /// @param decorations the optional struct decorations
1200 /// @returns the struct type
Ben Clayton1d618b12021-04-09 16:19:48 +00001201 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001202 sem::StructType* Structure(NAME&& name,
1203 ast::StructMemberList members,
1204 ast::DecorationList decorations = {}) {
Ben Claytond614dd52021-03-15 10:43:11 +00001205 auto* impl =
1206 create<ast::Struct>(std::move(members), std::move(decorations));
Ben Clayton1d618b12021-04-09 16:19:48 +00001207 auto* type = ty.struct_(Sym(std::forward<NAME>(name)), impl);
Ben Claytond614dd52021-03-15 10:43:11 +00001208 AST().AddConstructedType(type);
1209 return type;
1210 }
1211
Ben Clayton42d1e092021-02-02 14:29:15 +00001212 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001213 /// @param source the source information
1214 /// @param name the struct member name
1215 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001216 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001217 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001218 template <typename NAME>
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001219 ast::StructMember* Member(const Source& source,
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001220 NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001221 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001222 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001223 return create<ast::StructMember>(source, Sym(std::forward<NAME>(name)),
1224 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001225 }
1226
Ben Clayton42d1e092021-02-02 14:29:15 +00001227 /// Creates a ast::StructMember
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001228 /// @param name the struct member name
1229 /// @param type the struct member type
Ben Claytond614dd52021-03-15 10:43:11 +00001230 /// @param decorations the optional struct member decorations
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001231 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001232 template <typename NAME>
1233 ast::StructMember* Member(NAME&& name,
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001234 sem::Type* type,
Ben Claytond614dd52021-03-15 10:43:11 +00001235 ast::DecorationList decorations = {}) {
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001236 return create<ast::StructMember>(source_, Sym(std::forward<NAME>(name)),
1237 type, std::move(decorations));
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001238 }
1239
Ben Claytonbab31972021-02-08 21:16:21 +00001240 /// Creates a ast::StructMember with the given byte offset
1241 /// @param offset the offset to use in the StructMemberOffsetDecoration
1242 /// @param name the struct member name
1243 /// @param type the struct member type
1244 /// @returns the struct member pointer
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001245 template <typename NAME>
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001246 ast::StructMember* Member(uint32_t offset, NAME&& name, sem::Type* type) {
Ben Claytonbab31972021-02-08 21:16:21 +00001247 return create<ast::StructMember>(
Ben Clayton1b8d9f22021-04-07 11:16:01 +00001248 source_, Sym(std::forward<NAME>(name)), type,
James Price95d40772021-03-11 17:39:32 +00001249 ast::DecorationList{
Ben Claytonbab31972021-02-08 21:16:21 +00001250 create<ast::StructMemberOffsetDecoration>(offset),
1251 });
1252 }
1253
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001254 /// Creates a ast::BlockStatement with input statements
1255 /// @param statements statements of block
1256 /// @returns the block statement pointer
1257 template <typename... Statements>
1258 ast::BlockStatement* Block(Statements&&... statements) {
1259 return create<ast::BlockStatement>(
1260 ast::StatementList{std::forward<Statements>(statements)...});
1261 }
1262
1263 /// Creates a ast::ElseStatement with input condition and body
1264 /// @param condition the else condition expression
1265 /// @param body the else body
1266 /// @returns the else statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001267 template <typename CONDITION>
1268 ast::ElseStatement* Else(CONDITION&& condition, ast::BlockStatement* body) {
1269 return create<ast::ElseStatement>(Expr(std::forward<CONDITION>(condition)),
1270 body);
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001271 }
1272
1273 /// Creates a ast::IfStatement with input condition, body, and optional
1274 /// variadic else statements
1275 /// @param condition the if statement condition expression
1276 /// @param body the if statement body
1277 /// @param elseStatements optional variadic else statements
1278 /// @returns the if statement pointer
Ben Claytonb8ea5912021-04-19 16:52:42 +00001279 template <typename CONDITION, typename... ELSE_STATEMENTS>
1280 ast::IfStatement* If(CONDITION&& condition,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001281 ast::BlockStatement* body,
Ben Claytonb8ea5912021-04-19 16:52:42 +00001282 ELSE_STATEMENTS&&... elseStatements) {
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001283 return create<ast::IfStatement>(
Ben Claytonb8ea5912021-04-19 16:52:42 +00001284 Expr(std::forward<CONDITION>(condition)), body,
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001285 ast::ElseStatementList{
Ben Claytonb8ea5912021-04-19 16:52:42 +00001286 std::forward<ELSE_STATEMENTS>(elseStatements)...});
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001287 }
1288
1289 /// Creates a ast::AssignmentStatement with input lhs and rhs expressions
Antonio Maioranocea744d2021-03-25 12:55:27 +00001290 /// @param lhs the left hand side expression initializer
1291 /// @param rhs the right hand side expression initializer
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001292 /// @returns the assignment statement pointer
Antonio Maioranocea744d2021-03-25 12:55:27 +00001293 template <typename LhsExpressionInit, typename RhsExpressionInit>
1294 ast::AssignmentStatement* Assign(LhsExpressionInit&& lhs,
1295 RhsExpressionInit&& rhs) {
1296 return create<ast::AssignmentStatement>(
1297 Expr(std::forward<LhsExpressionInit>(lhs)),
1298 Expr(std::forward<RhsExpressionInit>(rhs)));
Antonio Maioranofd31bbd2021-03-09 10:26:57 +00001299 }
1300
1301 /// Creates a ast::LoopStatement with input body and optional continuing
1302 /// @param body the loop body
1303 /// @param continuing the optional continuing block
1304 /// @returns the loop statement pointer
1305 ast::LoopStatement* Loop(ast::BlockStatement* body,
1306 ast::BlockStatement* continuing = nullptr) {
1307 return create<ast::LoopStatement>(body, continuing);
1308 }
1309
1310 /// Creates a ast::VariableDeclStatement for the input variable
1311 /// @param var the variable to wrap in a decl statement
1312 /// @returns the variable decl statement pointer
1313 ast::VariableDeclStatement* Decl(ast::Variable* var) {
1314 return create<ast::VariableDeclStatement>(var);
1315 }
1316
Antonio Maioranocea744d2021-03-25 12:55:27 +00001317 /// Creates a ast::SwitchStatement with input expression and cases
1318 /// @param condition the condition expression initializer
1319 /// @param cases case statements
1320 /// @returns the switch statement pointer
1321 template <typename ExpressionInit, typename... Cases>
1322 ast::SwitchStatement* Switch(ExpressionInit&& condition, Cases&&... cases) {
1323 return create<ast::SwitchStatement>(
1324 Expr(std::forward<ExpressionInit>(condition)),
1325 ast::CaseStatementList{std::forward<Cases>(cases)...});
1326 }
1327
1328 /// Creates a ast::CaseStatement with input list of selectors, and body
1329 /// @param selectors list of selectors
1330 /// @param body the case body
1331 /// @returns the case statement pointer
1332 ast::CaseStatement* Case(ast::CaseSelectorList selectors,
1333 ast::BlockStatement* body = nullptr) {
1334 return create<ast::CaseStatement>(std::move(selectors),
1335 body ? body : Block());
1336 }
1337
1338 /// Convenient overload that takes a single selector
1339 /// @param selector a single case selector
1340 /// @param body the case body
1341 /// @returns the case statement pointer
1342 ast::CaseStatement* Case(ast::IntLiteral* selector,
1343 ast::BlockStatement* body = nullptr) {
1344 return Case(ast::CaseSelectorList{selector}, body);
1345 }
1346
1347 /// Convenience function that creates a 'default' ast::CaseStatement
1348 /// @param body the case body
1349 /// @returns the case statement pointer
1350 ast::CaseStatement* DefaultCase(ast::BlockStatement* body = nullptr) {
1351 return Case(ast::CaseSelectorList{}, body);
1352 }
1353
James Price68f558f2021-04-06 15:51:47 +00001354 /// Creates an ast::BuiltinDecoration
1355 /// @param source the source information
1356 /// @param builtin the builtin value
1357 /// @returns the builtin decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001358 ast::BuiltinDecoration* Builtin(const Source& source, ast::Builtin builtin) {
James Price68f558f2021-04-06 15:51:47 +00001359 return create<ast::BuiltinDecoration>(source, builtin);
1360 }
1361
1362 /// Creates an ast::BuiltinDecoration
1363 /// @param builtin the builtin value
1364 /// @returns the builtin decoration pointer
1365 ast::BuiltinDecoration* Builtin(ast::Builtin builtin) {
1366 return create<ast::BuiltinDecoration>(source_, builtin);
1367 }
1368
1369 /// Creates an ast::LocationDecoration
1370 /// @param source the source information
1371 /// @param location the location value
1372 /// @returns the location decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001373 ast::LocationDecoration* Location(const Source& source, uint32_t location) {
James Price68f558f2021-04-06 15:51:47 +00001374 return create<ast::LocationDecoration>(source, location);
1375 }
1376
1377 /// Creates an ast::LocationDecoration
1378 /// @param location the location value
1379 /// @returns the location decoration pointer
1380 ast::LocationDecoration* Location(uint32_t location) {
1381 return create<ast::LocationDecoration>(source_, location);
1382 }
1383
1384 /// Creates an ast::StageDecoration
1385 /// @param source the source information
1386 /// @param stage the pipeline stage
1387 /// @returns the stage decoration pointer
Antonio Maiorano101f4632021-04-07 20:35:11 +00001388 ast::StageDecoration* Stage(const Source& source, ast::PipelineStage stage) {
James Price68f558f2021-04-06 15:51:47 +00001389 return create<ast::StageDecoration>(source, stage);
1390 }
1391
1392 /// Creates an ast::StageDecoration
1393 /// @param stage the pipeline stage
1394 /// @returns the stage decoration pointer
1395 ast::StageDecoration* Stage(ast::PipelineStage stage) {
1396 return create<ast::StageDecoration>(source_, stage);
1397 }
1398
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001399 /// Sets the current builder source to `src`
1400 /// @param src the Source used for future create() calls
1401 void SetSource(const Source& src) {
1402 AssertNotMoved();
1403 source_ = src;
1404 }
1405
1406 /// Sets the current builder source to `loc`
1407 /// @param loc the Source used for future create() calls
1408 void SetSource(const Source::Location& loc) {
1409 AssertNotMoved();
1410 source_ = Source(loc);
1411 }
1412
Ben Clayton33352542021-01-29 16:43:41 +00001413 /// Helper for returning the resolved semantic type of the expression `expr`.
Ben Clayton5f0ea112021-03-09 10:54:37 +00001414 /// @note As the Resolver is run when the Program is built, this will only be
1415 /// useful for the Resolver itself and tests that use their own Resolver.
Ben Clayton33352542021-01-29 16:43:41 +00001416 /// @param expr the AST expression
1417 /// @return the resolved semantic type for the expression, or nullptr if the
1418 /// expression has no resolved type.
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001419 sem::Type* TypeOf(ast::Expression* expr) const;
Ben Clayton33352542021-01-29 16:43:41 +00001420
Ben Clayton169512e2021-04-17 05:52:11 +00001421 /// Wraps the ast::Literal in a statement. This is used by tests that
1422 /// construct a partial AST and require the Resolver to reach these
1423 /// nodes.
1424 /// @param lit the ast::Literal to be wrapped by an ast::Statement
1425 /// @return the ast::Statement that wraps the ast::Statement
1426 ast::Statement* WrapInStatement(ast::Literal* lit);
Ben Clayton401b96b2021-02-03 17:19:59 +00001427 /// Wraps the ast::Expression in a statement. This is used by tests that
Ben Clayton5f0ea112021-03-09 10:54:37 +00001428 /// construct a partial AST and require the Resolver to reach these
Ben Clayton401b96b2021-02-03 17:19:59 +00001429 /// nodes.
1430 /// @param expr the ast::Expression to be wrapped by an ast::Statement
1431 /// @return the ast::Statement that wraps the ast::Expression
1432 ast::Statement* WrapInStatement(ast::Expression* expr);
1433 /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by
Ben Clayton5f0ea112021-03-09 10:54:37 +00001434 /// tests that construct a partial AST and require the Resolver to reach
Ben Clayton401b96b2021-02-03 17:19:59 +00001435 /// these nodes.
1436 /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement
1437 /// @return the ast::VariableDeclStatement that wraps the ast::Variable
1438 ast::VariableDeclStatement* WrapInStatement(ast::Variable* v);
1439 /// Returns the statement argument. Used as a passthrough-overload by
1440 /// WrapInFunction().
1441 /// @param stmt the ast::Statement
1442 /// @return `stmt`
1443 ast::Statement* WrapInStatement(ast::Statement* stmt);
1444 /// Wraps the list of arguments in a simple function so that each is reachable
Ben Clayton5f0ea112021-03-09 10:54:37 +00001445 /// by the Resolver.
Ben Clayton401b96b2021-02-03 17:19:59 +00001446 /// @param args a mix of ast::Expression, ast::Statement, ast::Variables.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001447 /// @returns the function
Ben Clayton401b96b2021-02-03 17:19:59 +00001448 template <typename... ARGS>
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001449 ast::Function* WrapInFunction(ARGS&&... args) {
Ben Clayton401b96b2021-02-03 17:19:59 +00001450 ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...};
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001451 return WrapInFunction(std::move(stmts));
Ben Clayton401b96b2021-02-03 17:19:59 +00001452 }
1453 /// @param stmts a list of ast::Statement that will be wrapped by a function,
Ben Clayton5f0ea112021-03-09 10:54:37 +00001454 /// so that each statement is reachable by the Resolver.
Antonio Maiorano03c01b52021-03-19 14:04:51 +00001455 /// @returns the function
1456 ast::Function* WrapInFunction(ast::StatementList stmts);
Ben Clayton401b96b2021-02-03 17:19:59 +00001457
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001458 /// The builder types
Ben Claytonc7ca7662021-02-17 16:23:52 +00001459 TypesBuilder const ty{this};
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001460
1461 protected:
1462 /// Asserts that the builder has not been moved.
1463 void AssertNotMoved() const;
1464
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001465 private:
Ben Claytone6995de2021-04-13 23:27:27 +00001466 ProgramID id_;
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001467 sem::Manager types_;
Ben Clayton7fdfff12021-01-29 15:17:30 +00001468 ASTNodeAllocator ast_nodes_;
1469 SemNodeAllocator sem_nodes_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001470 ast::Module* ast_;
Antonio Maiorano5cd71b82021-04-16 19:07:51 +00001471 sem::Info sem_;
Ben Clayton13ef87c2021-04-15 18:20:03 +00001472 SymbolTable symbols_{id_};
Ben Clayton844217f2021-01-27 18:49:05 +00001473 diag::List diagnostics_;
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001474
1475 /// The source to use when creating AST nodes without providing a Source as
1476 /// the first argument.
1477 Source source_;
1478
Ben Clayton5f0ea112021-03-09 10:54:37 +00001479 /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the
Ben Claytondd69ac32021-01-27 19:23:55 +00001480 /// program when built.
1481 bool resolve_on_build_ = true;
1482
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001483 /// Set by MarkAsMoved(). Once set, no methods may be called on this builder.
1484 bool moved_ = false;
1485};
1486
1487//! @cond Doxygen_Suppress
1488// Various template specializations for ProgramBuilder::TypesBuilder::CToAST.
1489template <>
1490struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001491 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001492 return t->i32();
1493 }
1494};
1495template <>
1496struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001497 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001498 return t->u32();
1499 }
1500};
1501template <>
1502struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001503 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001504 return t->f32();
1505 }
1506};
1507template <>
1508struct ProgramBuilder::TypesBuilder::CToAST<bool> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001509 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001510 return t->bool_();
1511 }
1512};
1513template <>
1514struct ProgramBuilder::TypesBuilder::CToAST<void> {
Antonio Maiorano3751fd22021-04-19 22:51:23 +00001515 static sem::Type* get(const ProgramBuilder::TypesBuilder* t) {
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001516 return t->void_();
1517 }
1518};
1519//! @endcond
1520
Ben Clayton917b14b2021-04-19 16:50:23 +00001521/// @param builder the ProgramBuilder
1522/// @returns the ProgramID of the ProgramBuilder
1523inline ProgramID ProgramIDOf(const ProgramBuilder* builder) {
1524 return builder->ID();
1525}
1526
Ben Claytona6b9a8e2021-01-26 16:57:10 +00001527} // namespace tint
1528
1529#endif // SRC_PROGRAM_BUILDER_H_