Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1 | // 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 Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 22 | #include "src/ast/assignment_statement.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 23 | #include "src/ast/binary_expression.h" |
| 24 | #include "src/ast/bool_literal.h" |
| 25 | #include "src/ast/call_expression.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 26 | #include "src/ast/float_literal.h" |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 27 | #include "src/ast/if_statement.h" |
| 28 | #include "src/ast/loop_statement.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 29 | #include "src/ast/member_accessor_expression.h" |
| 30 | #include "src/ast/module.h" |
| 31 | #include "src/ast/scalar_constructor_expression.h" |
| 32 | #include "src/ast/sint_literal.h" |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 33 | #include "src/ast/stride_decoration.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 34 | #include "src/ast/struct_member_offset_decoration.h" |
| 35 | #include "src/ast/type_constructor_expression.h" |
| 36 | #include "src/ast/uint_literal.h" |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 37 | #include "src/ast/variable_decl_statement.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 38 | #include "src/program.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 39 | #include "src/type/alias_type.h" |
| 40 | #include "src/type/array_type.h" |
| 41 | #include "src/type/bool_type.h" |
| 42 | #include "src/type/f32_type.h" |
| 43 | #include "src/type/i32_type.h" |
| 44 | #include "src/type/matrix_type.h" |
| 45 | #include "src/type/pointer_type.h" |
| 46 | #include "src/type/struct_type.h" |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 47 | #include "src/type/u32_type.h" |
| 48 | #include "src/type/vector_type.h" |
| 49 | #include "src/type/void_type.h" |
| 50 | |
| 51 | namespace tint { |
| 52 | |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 53 | // Forward declarations |
| 54 | namespace ast { |
| 55 | class VariableDeclStatement; |
| 56 | } // namespace ast |
| 57 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 58 | class CloneContext; |
| 59 | |
| 60 | /// ProgramBuilder is a mutable builder for a Program. |
| 61 | /// To construct a Program, populate the builder and then `std::move` it to a |
| 62 | /// Program. |
| 63 | class ProgramBuilder { |
| 64 | public: |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 65 | /// ASTNodeAllocator is an alias to BlockAllocator<ast::Node> |
| 66 | using ASTNodeAllocator = BlockAllocator<ast::Node>; |
| 67 | |
| 68 | /// SemNodeAllocator is an alias to BlockAllocator<semantic::Node> |
| 69 | using SemNodeAllocator = BlockAllocator<semantic::Node>; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 70 | |
| 71 | /// `i32` is a type alias to `int`. |
| 72 | /// Useful for passing to template methods such as `vec2<i32>()` to imitate |
| 73 | /// WGSL syntax. |
| 74 | /// Note: this is intentionally not aliased to uint32_t as we want integer |
| 75 | /// literals passed to the builder to match WGSL's integer literal types. |
| 76 | using i32 = decltype(1); |
| 77 | /// `u32` is a type alias to `unsigned int`. |
| 78 | /// Useful for passing to template methods such as `vec2<u32>()` to imitate |
| 79 | /// WGSL syntax. |
| 80 | /// Note: this is intentionally not aliased to uint32_t as we want integer |
| 81 | /// literals passed to the builder to match WGSL's integer literal types. |
| 82 | using u32 = decltype(1u); |
| 83 | /// `f32` is a type alias to `float` |
| 84 | /// Useful for passing to template methods such as `vec2<f32>()` to imitate |
| 85 | /// WGSL syntax. |
| 86 | using f32 = float; |
| 87 | |
| 88 | /// Constructor |
| 89 | ProgramBuilder(); |
| 90 | |
| 91 | /// Move constructor |
| 92 | /// @param rhs the builder to move |
| 93 | ProgramBuilder(ProgramBuilder&& rhs); |
| 94 | |
| 95 | /// Destructor |
| 96 | virtual ~ProgramBuilder(); |
| 97 | |
| 98 | /// Move assignment operator |
| 99 | /// @param rhs the builder to move |
| 100 | /// @return this builder |
| 101 | ProgramBuilder& operator=(ProgramBuilder&& rhs); |
| 102 | |
Ben Clayton | e43c830 | 2021-01-29 11:59:32 +0000 | [diff] [blame] | 103 | /// Wrap returns a new ProgramBuilder wrapping the Program `program` without |
| 104 | /// making a deep clone of the Program contents. |
| 105 | /// ProgramBuilder returned by Wrap() is intended to temporarily extend an |
| 106 | /// existing immutable program. |
| 107 | /// As the returned ProgramBuilder wraps `program`, `program` must not be |
| 108 | /// destructed or assigned while using the returned ProgramBuilder. |
| 109 | /// TODO(bclayton) - Evaluate whether there are safer alternatives to this |
| 110 | /// function. See crbug.com/tint/460. |
| 111 | /// @param program the immutable Program to wrap |
| 112 | /// @return the ProgramBuilder that wraps `program` |
| 113 | static ProgramBuilder Wrap(const Program* program); |
| 114 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 115 | /// @returns a reference to the program's types |
| 116 | type::Manager& Types() { |
| 117 | AssertNotMoved(); |
| 118 | return types_; |
| 119 | } |
| 120 | |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 121 | /// @returns a reference to the program's types |
| 122 | const type::Manager& Types() const { |
| 123 | AssertNotMoved(); |
| 124 | return types_; |
| 125 | } |
| 126 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 127 | /// @returns a reference to the program's AST nodes storage |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 128 | ASTNodeAllocator& ASTNodes() { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 129 | AssertNotMoved(); |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 130 | return ast_nodes_; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 133 | /// @returns a reference to the program's AST nodes storage |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 134 | const ASTNodeAllocator& ASTNodes() const { |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 135 | AssertNotMoved(); |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 136 | return ast_nodes_; |
| 137 | } |
| 138 | |
| 139 | /// @returns a reference to the program's semantic nodes storage |
| 140 | SemNodeAllocator& SemNodes() { |
| 141 | AssertNotMoved(); |
| 142 | return sem_nodes_; |
| 143 | } |
| 144 | |
| 145 | /// @returns a reference to the program's semantic nodes storage |
| 146 | const SemNodeAllocator& SemNodes() const { |
| 147 | AssertNotMoved(); |
| 148 | return sem_nodes_; |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 151 | /// @returns a reference to the program's AST root Module |
| 152 | ast::Module& AST() { |
| 153 | AssertNotMoved(); |
| 154 | return *ast_; |
| 155 | } |
| 156 | |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 157 | /// @returns a reference to the program's AST root Module |
| 158 | const ast::Module& AST() const { |
| 159 | AssertNotMoved(); |
| 160 | return *ast_; |
| 161 | } |
| 162 | |
| 163 | /// @returns a reference to the program's semantic info |
| 164 | semantic::Info& Sem() { |
| 165 | AssertNotMoved(); |
| 166 | return sem_; |
| 167 | } |
| 168 | |
| 169 | /// @returns a reference to the program's semantic info |
| 170 | const semantic::Info& Sem() const { |
| 171 | AssertNotMoved(); |
| 172 | return sem_; |
| 173 | } |
| 174 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 175 | /// @returns a reference to the program's SymbolTable |
| 176 | SymbolTable& Symbols() { |
| 177 | AssertNotMoved(); |
| 178 | return symbols_; |
| 179 | } |
| 180 | |
Ben Clayton | 708dc2d | 2021-01-29 11:22:40 +0000 | [diff] [blame] | 181 | /// @returns a reference to the program's SymbolTable |
| 182 | const SymbolTable& Symbols() const { |
| 183 | AssertNotMoved(); |
| 184 | return symbols_; |
| 185 | } |
| 186 | |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 187 | /// @returns a reference to the program's diagnostics |
| 188 | diag::List& Diagnostics() { |
| 189 | AssertNotMoved(); |
| 190 | return diagnostics_; |
| 191 | } |
| 192 | |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 193 | /// @returns a reference to the program's diagnostics |
| 194 | const diag::List& Diagnostics() const { |
| 195 | AssertNotMoved(); |
| 196 | return diagnostics_; |
| 197 | } |
| 198 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 199 | /// Controls whether the Resolver will be run on the program when it is built. |
Ben Clayton | dd69ac3 | 2021-01-27 19:23:55 +0000 | [diff] [blame] | 200 | /// @param enable the new flag value (defaults to true) |
| 201 | void SetResolveOnBuild(bool enable) { resolve_on_build_ = enable; } |
| 202 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 203 | /// @return true if the Resolver will be run on the program when it is |
Ben Clayton | dd69ac3 | 2021-01-27 19:23:55 +0000 | [diff] [blame] | 204 | /// built. |
| 205 | bool ResolveOnBuild() const { return resolve_on_build_; } |
| 206 | |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 207 | /// @returns true if the program has no error diagnostics and is not missing |
| 208 | /// information |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 209 | bool IsValid() const; |
| 210 | |
Ben Clayton | 708dc2d | 2021-01-29 11:22:40 +0000 | [diff] [blame] | 211 | /// Writes a representation of the node to the output stream |
| 212 | /// @note unlike str(), to_str() does not automatically demangle the string. |
| 213 | /// @param node the AST node |
| 214 | /// @param out the stream to write to |
| 215 | /// @param indent number of spaces to indent the node when writing |
| 216 | void to_str(const ast::Node* node, std::ostream& out, size_t indent) const { |
| 217 | node->to_str(Sem(), out, indent); |
| 218 | } |
| 219 | |
| 220 | /// Returns a demangled, string representation of `node`. |
| 221 | /// @param node the AST node |
| 222 | /// @returns a string representation of the node |
| 223 | std::string str(const ast::Node* node) const; |
| 224 | |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 225 | /// Creates a new ast::Node owned by the ProgramBuilder. When the |
| 226 | /// ProgramBuilder is destructed, the ast::Node will also be destructed. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 227 | /// @param source the Source of the node |
| 228 | /// @param args the arguments to pass to the type constructor |
| 229 | /// @returns the node pointer |
| 230 | template <typename T, typename... ARGS> |
| 231 | traits::EnableIfIsType<T, ast::Node>* create(const Source& source, |
| 232 | ARGS&&... args) { |
| 233 | AssertNotMoved(); |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 234 | return ast_nodes_.Create<T>(source, std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 237 | /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current |
| 238 | /// Source as set by the last call to SetSource() as the only argument to the |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 239 | /// constructor. |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 240 | /// When the ProgramBuilder is destructed, the ast::Node will also be |
| 241 | /// destructed. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 242 | /// @returns the node pointer |
| 243 | template <typename T> |
| 244 | traits::EnableIfIsType<T, ast::Node>* create() { |
| 245 | AssertNotMoved(); |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 246 | return ast_nodes_.Create<T>(source_); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 247 | } |
| 248 | |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 249 | /// Creates a new ast::Node owned by the ProgramBuilder, injecting the current |
| 250 | /// Source as set by the last call to SetSource() as the first argument to the |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 251 | /// constructor. |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 252 | /// When the ProgramBuilder is destructed, the ast::Node will also be |
| 253 | /// destructed. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 254 | /// @param arg0 the first arguments to pass to the type constructor |
| 255 | /// @param args the remaining arguments to pass to the type constructor |
| 256 | /// @returns the node pointer |
| 257 | template <typename T, typename ARG0, typename... ARGS> |
| 258 | traits::EnableIf</* T is ast::Node and ARG0 is not Source */ |
| 259 | traits::IsTypeOrDerived<T, ast::Node>::value && |
| 260 | !traits::IsTypeOrDerived<ARG0, Source>::value, |
| 261 | T>* |
| 262 | create(ARG0&& arg0, ARGS&&... args) { |
| 263 | AssertNotMoved(); |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 264 | return ast_nodes_.Create<T>(source_, std::forward<ARG0>(arg0), |
| 265 | std::forward<ARGS>(args)...); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 268 | /// Creates a new semantic::Node owned by the ProgramBuilder. |
| 269 | /// When the ProgramBuilder is destructed, the semantic::Node will also be |
| 270 | /// destructed. |
| 271 | /// @param args the arguments to pass to the type constructor |
| 272 | /// @returns the node pointer |
| 273 | template <typename T, typename... ARGS> |
| 274 | traits::EnableIfIsType<T, semantic::Node>* create(ARGS&&... args) { |
| 275 | AssertNotMoved(); |
| 276 | return sem_nodes_.Create<T>(std::forward<ARGS>(args)...); |
| 277 | } |
| 278 | |
| 279 | /// Creates a new type::Type owned by the ProgramBuilder. |
| 280 | /// When the ProgramBuilder is destructed, owned ProgramBuilder and the |
| 281 | /// returned`Type` will also be destructed. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 282 | /// Types are unique (de-aliased), and so calling create() for the same `T` |
| 283 | /// and arguments will return the same pointer. |
| 284 | /// @warning Use this method to acquire a type only if all of its type |
| 285 | /// information is provided in the constructor arguments `args`.<br> |
| 286 | /// If the type requires additional configuration after construction that |
| 287 | /// affect its fundamental type, build the type with `std::make_unique`, make |
| 288 | /// any necessary alterations and then call unique_type() instead. |
| 289 | /// @param args the arguments to pass to the type constructor |
| 290 | /// @returns the de-aliased type pointer |
| 291 | template <typename T, typename... ARGS> |
| 292 | traits::EnableIfIsType<T, type::Type>* create(ARGS&&... args) { |
| 293 | static_assert(std::is_base_of<type::Type, T>::value, |
| 294 | "T does not derive from type::Type"); |
| 295 | AssertNotMoved(); |
| 296 | return types_.Get<T>(std::forward<ARGS>(args)...); |
| 297 | } |
| 298 | |
| 299 | /// Marks this builder as moved, preventing any further use of the builder. |
| 300 | void MarkAsMoved(); |
| 301 | |
| 302 | ////////////////////////////////////////////////////////////////////////////// |
| 303 | // TypesBuilder |
| 304 | ////////////////////////////////////////////////////////////////////////////// |
| 305 | |
| 306 | /// TypesBuilder holds basic `tint` types and methods for constructing |
| 307 | /// complex types. |
| 308 | class TypesBuilder { |
| 309 | public: |
| 310 | /// Constructor |
| 311 | /// @param builder the program builder |
| 312 | explicit TypesBuilder(ProgramBuilder* builder); |
| 313 | |
| 314 | /// @return the tint AST type for the C type `T`. |
| 315 | template <typename T> |
| 316 | type::Type* Of() const { |
| 317 | return CToAST<T>::get(this); |
| 318 | } |
| 319 | |
| 320 | /// @returns a boolean type |
| 321 | type::Bool* bool_() const { return builder->create<type::Bool>(); } |
| 322 | |
| 323 | /// @returns a f32 type |
| 324 | type::F32* f32() const { return builder->create<type::F32>(); } |
| 325 | |
| 326 | /// @returns a i32 type |
| 327 | type::I32* i32() const { return builder->create<type::I32>(); } |
| 328 | |
| 329 | /// @returns a u32 type |
| 330 | type::U32* u32() const { return builder->create<type::U32>(); } |
| 331 | |
| 332 | /// @returns a void type |
| 333 | type::Void* void_() const { return builder->create<type::Void>(); } |
| 334 | |
| 335 | /// @return the tint AST type for a 2-element vector of the C type `T`. |
| 336 | template <typename T> |
| 337 | type::Vector* vec2() const { |
| 338 | return builder->create<type::Vector>(Of<T>(), 2); |
| 339 | } |
| 340 | |
| 341 | /// @return the tint AST type for a 3-element vector of the C type `T`. |
| 342 | template <typename T> |
| 343 | type::Vector* vec3() const { |
| 344 | return builder->create<type::Vector>(Of<T>(), 3); |
| 345 | } |
| 346 | |
| 347 | /// @return the tint AST type for a 4-element vector of the C type `T`. |
| 348 | template <typename T> |
| 349 | type::Type* vec4() const { |
| 350 | return builder->create<type::Vector>(Of<T>(), 4); |
| 351 | } |
| 352 | |
| 353 | /// @return the tint AST type for a 2x3 matrix of the C type `T`. |
| 354 | template <typename T> |
| 355 | type::Matrix* mat2x2() const { |
| 356 | return builder->create<type::Matrix>(Of<T>(), 2, 2); |
| 357 | } |
| 358 | |
| 359 | /// @return the tint AST type for a 2x3 matrix of the C type `T`. |
| 360 | template <typename T> |
| 361 | type::Matrix* mat2x3() const { |
| 362 | return builder->create<type::Matrix>(Of<T>(), 3, 2); |
| 363 | } |
| 364 | |
| 365 | /// @return the tint AST type for a 2x4 matrix of the C type `T`. |
| 366 | template <typename T> |
| 367 | type::Matrix* mat2x4() const { |
| 368 | return builder->create<type::Matrix>(Of<T>(), 4, 2); |
| 369 | } |
| 370 | |
| 371 | /// @return the tint AST type for a 3x2 matrix of the C type `T`. |
| 372 | template <typename T> |
| 373 | type::Matrix* mat3x2() const { |
| 374 | return builder->create<type::Matrix>(Of<T>(), 2, 3); |
| 375 | } |
| 376 | |
| 377 | /// @return the tint AST type for a 3x3 matrix of the C type `T`. |
| 378 | template <typename T> |
| 379 | type::Matrix* mat3x3() const { |
| 380 | return builder->create<type::Matrix>(Of<T>(), 3, 3); |
| 381 | } |
| 382 | |
| 383 | /// @return the tint AST type for a 3x4 matrix of the C type `T`. |
| 384 | template <typename T> |
| 385 | type::Matrix* mat3x4() const { |
| 386 | return builder->create<type::Matrix>(Of<T>(), 4, 3); |
| 387 | } |
| 388 | |
| 389 | /// @return the tint AST type for a 4x2 matrix of the C type `T`. |
| 390 | template <typename T> |
| 391 | type::Matrix* mat4x2() const { |
| 392 | return builder->create<type::Matrix>(Of<T>(), 2, 4); |
| 393 | } |
| 394 | |
| 395 | /// @return the tint AST type for a 4x3 matrix of the C type `T`. |
| 396 | template <typename T> |
| 397 | type::Matrix* mat4x3() const { |
| 398 | return builder->create<type::Matrix>(Of<T>(), 3, 4); |
| 399 | } |
| 400 | |
| 401 | /// @return the tint AST type for a 4x4 matrix of the C type `T`. |
| 402 | template <typename T> |
| 403 | type::Matrix* mat4x4() const { |
| 404 | return builder->create<type::Matrix>(Of<T>(), 4, 4); |
| 405 | } |
| 406 | |
| 407 | /// @param subtype the array element type |
| 408 | /// @param n the array size. 0 represents a runtime-array. |
| 409 | /// @return the tint AST type for a array of size `n` of type `T` |
| 410 | type::Array* array(type::Type* subtype, uint32_t n) const { |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 411 | return builder->create<type::Array>(subtype, n, ast::DecorationList{}); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 412 | } |
| 413 | |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 414 | /// @param subtype the array element type |
| 415 | /// @param n the array size. 0 represents a runtime-array. |
| 416 | /// @param stride the array stride. |
| 417 | /// @return the tint AST type for a array of size `n` of type `T` |
| 418 | type::Array* array(type::Type* subtype, uint32_t n, uint32_t stride) const { |
| 419 | return builder->create<type::Array>( |
| 420 | subtype, n, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 421 | ast::DecorationList{ |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 422 | builder->create<ast::StrideDecoration>(stride), |
| 423 | }); |
| 424 | } |
| 425 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 426 | /// @return the tint AST type for an array of size `N` of type `T` |
| 427 | template <typename T, int N = 0> |
| 428 | type::Array* array() const { |
| 429 | return array(Of<T>(), N); |
| 430 | } |
| 431 | |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 432 | /// @param stride the array stride |
| 433 | /// @return the tint AST type for an array of size `N` of type `T` |
| 434 | template <typename T, int N = 0> |
| 435 | type::Array* array(uint32_t stride) const { |
| 436 | return array(Of<T>(), N, stride); |
| 437 | } |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 438 | /// Creates an alias type |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 439 | /// @param name the alias name |
| 440 | /// @param type the alias type |
| 441 | /// @returns the alias pointer |
| 442 | type::Alias* alias(const std::string& name, type::Type* type) const { |
| 443 | return builder->create<type::Alias>(builder->Symbols().Register(name), |
| 444 | type); |
| 445 | } |
| 446 | |
| 447 | /// @return the tint AST pointer to type `T` with the given |
| 448 | /// ast::StorageClass. |
| 449 | /// @param storage_class the storage class of the pointer |
| 450 | template <typename T> |
| 451 | type::Pointer* pointer(ast::StorageClass storage_class) const { |
| 452 | return builder->create<type::Pointer>(Of<T>(), storage_class); |
| 453 | } |
| 454 | |
| 455 | /// @param name the struct name |
| 456 | /// @param impl the struct implementation |
| 457 | /// @returns a struct pointer |
| 458 | type::Struct* struct_(const std::string& name, ast::Struct* impl) const { |
| 459 | return builder->create<type::Struct>(builder->Symbols().Register(name), |
| 460 | impl); |
| 461 | } |
| 462 | |
| 463 | private: |
| 464 | /// CToAST<T> is specialized for various `T` types and each specialization |
| 465 | /// contains a single static `get()` method for obtaining the corresponding |
| 466 | /// AST type for the C type `T`. |
| 467 | /// `get()` has the signature: |
| 468 | /// `static type::Type* get(Types* t)` |
| 469 | template <typename T> |
| 470 | struct CToAST {}; |
| 471 | |
Ben Clayton | c7ca766 | 2021-02-17 16:23:52 +0000 | [diff] [blame] | 472 | ProgramBuilder* const builder; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 473 | }; |
| 474 | |
| 475 | ////////////////////////////////////////////////////////////////////////////// |
| 476 | // AST helper methods |
| 477 | ////////////////////////////////////////////////////////////////////////////// |
| 478 | |
| 479 | /// @param expr the expression |
| 480 | /// @return expr |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 481 | template <typename T> |
| 482 | traits::EnableIfIsType<T, ast::Expression>* Expr(T* expr) { |
| 483 | return expr; |
| 484 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 485 | |
| 486 | /// @param name the identifier name |
| 487 | /// @return an ast::IdentifierExpression with the given name |
| 488 | ast::IdentifierExpression* Expr(const std::string& name) { |
| 489 | return create<ast::IdentifierExpression>(Symbols().Register(name)); |
| 490 | } |
| 491 | |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 492 | /// @param symbol the identifier symbol |
| 493 | /// @return an ast::IdentifierExpression with the given symbol |
| 494 | ast::IdentifierExpression* Expr(Symbol symbol) { |
| 495 | return create<ast::IdentifierExpression>(symbol); |
| 496 | } |
| 497 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 498 | /// @param source the source information |
| 499 | /// @param name the identifier name |
| 500 | /// @return an ast::IdentifierExpression with the given name |
| 501 | ast::IdentifierExpression* Expr(const Source& source, |
| 502 | const std::string& name) { |
| 503 | return create<ast::IdentifierExpression>(source, Symbols().Register(name)); |
| 504 | } |
| 505 | |
| 506 | /// @param name the identifier name |
| 507 | /// @return an ast::IdentifierExpression with the given name |
| 508 | ast::IdentifierExpression* Expr(const char* name) { |
| 509 | return create<ast::IdentifierExpression>(Symbols().Register(name)); |
| 510 | } |
| 511 | |
| 512 | /// @param value the boolean value |
| 513 | /// @return a Scalar constructor for the given value |
| 514 | ast::ScalarConstructorExpression* Expr(bool value) { |
| 515 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 516 | } |
| 517 | |
| 518 | /// @param value the float value |
| 519 | /// @return a Scalar constructor for the given value |
| 520 | ast::ScalarConstructorExpression* Expr(f32 value) { |
| 521 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 522 | } |
| 523 | |
| 524 | /// @param value the integer value |
| 525 | /// @return a Scalar constructor for the given value |
| 526 | ast::ScalarConstructorExpression* Expr(i32 value) { |
| 527 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 528 | } |
| 529 | |
| 530 | /// @param value the unsigned int value |
| 531 | /// @return a Scalar constructor for the given value |
| 532 | ast::ScalarConstructorExpression* Expr(u32 value) { |
| 533 | return create<ast::ScalarConstructorExpression>(Literal(value)); |
| 534 | } |
| 535 | |
| 536 | /// Converts `arg` to an `ast::Expression` using `Expr()`, then appends it to |
| 537 | /// `list`. |
| 538 | /// @param list the list to append too |
| 539 | /// @param arg the arg to create |
| 540 | template <typename ARG> |
| 541 | void Append(ast::ExpressionList& list, ARG&& arg) { |
| 542 | list.emplace_back(Expr(std::forward<ARG>(arg))); |
| 543 | } |
| 544 | |
| 545 | /// Converts `arg0` and `args` to `ast::Expression`s using `Expr()`, |
| 546 | /// then appends them to `list`. |
| 547 | /// @param list the list to append too |
| 548 | /// @param arg0 the first argument |
| 549 | /// @param args the rest of the arguments |
| 550 | template <typename ARG0, typename... ARGS> |
| 551 | void Append(ast::ExpressionList& list, ARG0&& arg0, ARGS&&... args) { |
| 552 | Append(list, std::forward<ARG0>(arg0)); |
| 553 | Append(list, std::forward<ARGS>(args)...); |
| 554 | } |
| 555 | |
| 556 | /// @return an empty list of expressions |
| 557 | ast::ExpressionList ExprList() { return {}; } |
| 558 | |
| 559 | /// @param args the list of expressions |
| 560 | /// @return the list of expressions converted to `ast::Expression`s using |
| 561 | /// `Expr()`, |
| 562 | template <typename... ARGS> |
| 563 | ast::ExpressionList ExprList(ARGS&&... args) { |
| 564 | ast::ExpressionList list; |
| 565 | list.reserve(sizeof...(args)); |
| 566 | Append(list, std::forward<ARGS>(args)...); |
| 567 | return list; |
| 568 | } |
| 569 | |
| 570 | /// @param list the list of expressions |
| 571 | /// @return `list` |
| 572 | ast::ExpressionList ExprList(ast::ExpressionList list) { return list; } |
| 573 | |
| 574 | /// @param val the boolan value |
| 575 | /// @return a boolean literal with the given value |
| 576 | ast::BoolLiteral* Literal(bool val) { |
| 577 | return create<ast::BoolLiteral>(ty.bool_(), val); |
| 578 | } |
| 579 | |
| 580 | /// @param val the float value |
| 581 | /// @return a float literal with the given value |
| 582 | ast::FloatLiteral* Literal(f32 val) { |
| 583 | return create<ast::FloatLiteral>(ty.f32(), val); |
| 584 | } |
| 585 | |
| 586 | /// @param val the unsigned int value |
| 587 | /// @return a ast::UintLiteral with the given value |
| 588 | ast::UintLiteral* Literal(u32 val) { |
| 589 | return create<ast::UintLiteral>(ty.u32(), val); |
| 590 | } |
| 591 | |
| 592 | /// @param val the integer value |
| 593 | /// @return the ast::SintLiteral with the given value |
| 594 | ast::SintLiteral* Literal(i32 val) { |
| 595 | return create<ast::SintLiteral>(ty.i32(), val); |
| 596 | } |
| 597 | |
| 598 | /// @param args the arguments for the type constructor |
| 599 | /// @return an `ast::TypeConstructorExpression` of type `ty`, with the values |
| 600 | /// of `args` converted to `ast::Expression`s using `Expr()` |
| 601 | template <typename T, typename... ARGS> |
| 602 | ast::TypeConstructorExpression* Construct(ARGS&&... args) { |
| 603 | return create<ast::TypeConstructorExpression>( |
| 604 | ty.Of<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 605 | } |
| 606 | |
| 607 | /// @param type the type to construct |
| 608 | /// @param args the arguments for the constructor |
| 609 | /// @return an `ast::TypeConstructorExpression` of `type` constructed with the |
| 610 | /// values `args`. |
| 611 | template <typename... ARGS> |
| 612 | ast::TypeConstructorExpression* Construct(type::Type* type, ARGS&&... args) { |
| 613 | return create<ast::TypeConstructorExpression>( |
| 614 | type, ExprList(std::forward<ARGS>(args)...)); |
| 615 | } |
| 616 | |
| 617 | /// @param args the arguments for the vector constructor |
| 618 | /// @return an `ast::TypeConstructorExpression` of a 2-element vector of type |
| 619 | /// `T`, constructed with the values `args`. |
| 620 | template <typename T, typename... ARGS> |
| 621 | ast::TypeConstructorExpression* vec2(ARGS&&... args) { |
| 622 | return create<ast::TypeConstructorExpression>( |
| 623 | ty.vec2<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 624 | } |
| 625 | |
| 626 | /// @param args the arguments for the vector constructor |
| 627 | /// @return an `ast::TypeConstructorExpression` of a 3-element vector of type |
| 628 | /// `T`, constructed with the values `args`. |
| 629 | template <typename T, typename... ARGS> |
| 630 | ast::TypeConstructorExpression* vec3(ARGS&&... args) { |
| 631 | return create<ast::TypeConstructorExpression>( |
| 632 | ty.vec3<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 633 | } |
| 634 | |
| 635 | /// @param args the arguments for the vector constructor |
| 636 | /// @return an `ast::TypeConstructorExpression` of a 4-element vector of type |
| 637 | /// `T`, constructed with the values `args`. |
| 638 | template <typename T, typename... ARGS> |
| 639 | ast::TypeConstructorExpression* vec4(ARGS&&... args) { |
| 640 | return create<ast::TypeConstructorExpression>( |
| 641 | ty.vec4<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 642 | } |
| 643 | |
| 644 | /// @param args the arguments for the matrix constructor |
| 645 | /// @return an `ast::TypeConstructorExpression` of a 2x2 matrix of type |
| 646 | /// `T`, constructed with the values `args`. |
| 647 | template <typename T, typename... ARGS> |
| 648 | ast::TypeConstructorExpression* mat2x2(ARGS&&... args) { |
| 649 | return create<ast::TypeConstructorExpression>( |
| 650 | ty.mat2x2<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 651 | } |
| 652 | |
| 653 | /// @param args the arguments for the matrix constructor |
| 654 | /// @return an `ast::TypeConstructorExpression` of a 2x3 matrix of type |
| 655 | /// `T`, constructed with the values `args`. |
| 656 | template <typename T, typename... ARGS> |
| 657 | ast::TypeConstructorExpression* mat2x3(ARGS&&... args) { |
| 658 | return create<ast::TypeConstructorExpression>( |
| 659 | ty.mat2x3<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 660 | } |
| 661 | |
| 662 | /// @param args the arguments for the matrix constructor |
| 663 | /// @return an `ast::TypeConstructorExpression` of a 2x4 matrix of type |
| 664 | /// `T`, constructed with the values `args`. |
| 665 | template <typename T, typename... ARGS> |
| 666 | ast::TypeConstructorExpression* mat2x4(ARGS&&... args) { |
| 667 | return create<ast::TypeConstructorExpression>( |
| 668 | ty.mat2x4<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 669 | } |
| 670 | |
| 671 | /// @param args the arguments for the matrix constructor |
| 672 | /// @return an `ast::TypeConstructorExpression` of a 3x2 matrix of type |
| 673 | /// `T`, constructed with the values `args`. |
| 674 | template <typename T, typename... ARGS> |
| 675 | ast::TypeConstructorExpression* mat3x2(ARGS&&... args) { |
| 676 | return create<ast::TypeConstructorExpression>( |
| 677 | ty.mat3x2<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 678 | } |
| 679 | |
| 680 | /// @param args the arguments for the matrix constructor |
| 681 | /// @return an `ast::TypeConstructorExpression` of a 3x3 matrix of type |
| 682 | /// `T`, constructed with the values `args`. |
| 683 | template <typename T, typename... ARGS> |
| 684 | ast::TypeConstructorExpression* mat3x3(ARGS&&... args) { |
| 685 | return create<ast::TypeConstructorExpression>( |
| 686 | ty.mat3x3<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 687 | } |
| 688 | |
| 689 | /// @param args the arguments for the matrix constructor |
| 690 | /// @return an `ast::TypeConstructorExpression` of a 3x4 matrix of type |
| 691 | /// `T`, constructed with the values `args`. |
| 692 | template <typename T, typename... ARGS> |
| 693 | ast::TypeConstructorExpression* mat3x4(ARGS&&... args) { |
| 694 | return create<ast::TypeConstructorExpression>( |
| 695 | ty.mat3x4<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 696 | } |
| 697 | |
| 698 | /// @param args the arguments for the matrix constructor |
| 699 | /// @return an `ast::TypeConstructorExpression` of a 4x2 matrix of type |
| 700 | /// `T`, constructed with the values `args`. |
| 701 | template <typename T, typename... ARGS> |
| 702 | ast::TypeConstructorExpression* mat4x2(ARGS&&... args) { |
| 703 | return create<ast::TypeConstructorExpression>( |
| 704 | ty.mat4x2<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 705 | } |
| 706 | |
| 707 | /// @param args the arguments for the matrix constructor |
| 708 | /// @return an `ast::TypeConstructorExpression` of a 4x3 matrix of type |
| 709 | /// `T`, constructed with the values `args`. |
| 710 | template <typename T, typename... ARGS> |
| 711 | ast::TypeConstructorExpression* mat4x3(ARGS&&... args) { |
| 712 | return create<ast::TypeConstructorExpression>( |
| 713 | ty.mat4x3<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 714 | } |
| 715 | |
| 716 | /// @param args the arguments for the matrix constructor |
| 717 | /// @return an `ast::TypeConstructorExpression` of a 4x4 matrix of type |
| 718 | /// `T`, constructed with the values `args`. |
| 719 | template <typename T, typename... ARGS> |
| 720 | ast::TypeConstructorExpression* mat4x4(ARGS&&... args) { |
| 721 | return create<ast::TypeConstructorExpression>( |
| 722 | ty.mat4x4<T>(), ExprList(std::forward<ARGS>(args)...)); |
| 723 | } |
| 724 | |
| 725 | /// @param args the arguments for the array constructor |
| 726 | /// @return an `ast::TypeConstructorExpression` of an array with element type |
| 727 | /// `T`, constructed with the values `args`. |
| 728 | template <typename T, int N = 0, typename... ARGS> |
| 729 | ast::TypeConstructorExpression* array(ARGS&&... args) { |
| 730 | return create<ast::TypeConstructorExpression>( |
| 731 | ty.array<T, N>(), ExprList(std::forward<ARGS>(args)...)); |
| 732 | } |
| 733 | |
| 734 | /// @param subtype the array element type |
| 735 | /// @param n the array size. 0 represents a runtime-array. |
| 736 | /// @param args the arguments for the array constructor |
| 737 | /// @return an `ast::TypeConstructorExpression` of an array with element type |
| 738 | /// `subtype`, constructed with the values `args`. |
| 739 | template <typename... ARGS> |
| 740 | ast::TypeConstructorExpression* array(type::Type* subtype, |
| 741 | uint32_t n, |
| 742 | ARGS&&... args) { |
| 743 | return create<ast::TypeConstructorExpression>( |
| 744 | ty.array(subtype, n), ExprList(std::forward<ARGS>(args)...)); |
| 745 | } |
| 746 | |
| 747 | /// @param name the variable name |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 748 | /// @param type the variable type |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 749 | /// @param storage the variable storage class |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 750 | /// @param constructor constructor expression |
| 751 | /// @param decorations variable decorations |
| 752 | /// @returns a `ast::Variable` with the given name, storage and type |
| 753 | ast::Variable* Var(const std::string& name, |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 754 | type::Type* type, |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 755 | ast::StorageClass storage, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 756 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 757 | ast::DecorationList decorations = {}) { |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 758 | return create<ast::Variable>(Symbols().Register(name), storage, type, false, |
| 759 | constructor, decorations); |
| 760 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 761 | |
| 762 | /// @param source the variable source |
| 763 | /// @param name the variable name |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 764 | /// @param type the variable type |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 765 | /// @param storage the variable storage class |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 766 | /// @param constructor constructor expression |
| 767 | /// @param decorations variable decorations |
| 768 | /// @returns a `ast::Variable` with the given name, storage and type |
| 769 | ast::Variable* Var(const Source& source, |
| 770 | const std::string& name, |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 771 | type::Type* type, |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 772 | ast::StorageClass storage, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 773 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 774 | ast::DecorationList decorations = {}) { |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 775 | return create<ast::Variable>(source, Symbols().Register(name), storage, |
| 776 | type, false, constructor, decorations); |
| 777 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 778 | |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 779 | /// @param symbol the variable symbol |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 780 | /// @param type the variable type |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 781 | /// @param storage the variable storage class |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 782 | /// @param constructor constructor expression |
| 783 | /// @param decorations variable decorations |
| 784 | /// @returns a `ast::Variable` with the given symbol, storage and type |
| 785 | ast::Variable* Var(Symbol symbol, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 786 | type::Type* type, |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 787 | ast::StorageClass storage, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 788 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 789 | ast::DecorationList decorations = {}) { |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 790 | return create<ast::Variable>(symbol, storage, type, false, constructor, |
| 791 | decorations); |
| 792 | } |
| 793 | |
| 794 | /// @param source the variable source |
| 795 | /// @param symbol the variable symbol |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 796 | /// @param type the variable type |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 797 | /// @param storage the variable storage class |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 798 | /// @param constructor constructor expression |
| 799 | /// @param decorations variable decorations |
| 800 | /// @returns a `ast::Variable` with the given symbol, storage and type |
| 801 | ast::Variable* Var(const Source& source, |
| 802 | Symbol symbol, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 803 | type::Type* type, |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 804 | ast::StorageClass storage, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 805 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 806 | ast::DecorationList decorations = {}) { |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 807 | return create<ast::Variable>(source, symbol, storage, type, false, |
| 808 | constructor, decorations); |
| 809 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 810 | |
| 811 | /// @param name the variable name |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 812 | /// @param type the variable type |
| 813 | /// @param constructor optional constructor expression |
| 814 | /// @param decorations optional variable decorations |
| 815 | /// @returns a constant `ast::Variable` with the given name, storage and type |
| 816 | ast::Variable* Const(const std::string& name, |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 817 | type::Type* type, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 818 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 819 | ast::DecorationList decorations = {}) { |
Ben Clayton | 81a29fe | 2021-02-17 00:26:52 +0000 | [diff] [blame] | 820 | return create<ast::Variable>(Symbols().Register(name), |
| 821 | ast::StorageClass::kNone, type, true, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 822 | constructor, decorations); |
| 823 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 824 | |
| 825 | /// @param source the variable source |
| 826 | /// @param name the variable name |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 827 | /// @param type the variable type |
| 828 | /// @param constructor optional constructor expression |
| 829 | /// @param decorations optional variable decorations |
| 830 | /// @returns a constant `ast::Variable` with the given name, storage and type |
| 831 | ast::Variable* Const(const Source& source, |
| 832 | const std::string& name, |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 833 | type::Type* type, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 834 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 835 | ast::DecorationList decorations = {}) { |
Ben Clayton | 81a29fe | 2021-02-17 00:26:52 +0000 | [diff] [blame] | 836 | return create<ast::Variable>(source, Symbols().Register(name), |
| 837 | ast::StorageClass::kNone, type, true, |
| 838 | constructor, decorations); |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 839 | } |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 840 | |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 841 | /// @param symbol the variable symbol |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 842 | /// @param type the variable type |
| 843 | /// @param constructor optional constructor expression |
| 844 | /// @param decorations optional variable decorations |
| 845 | /// @returns a constant `ast::Variable` with the given symbol, storage and |
| 846 | /// type |
| 847 | ast::Variable* Const(Symbol symbol, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 848 | type::Type* type, |
| 849 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 850 | ast::DecorationList decorations = {}) { |
Ben Clayton | 81a29fe | 2021-02-17 00:26:52 +0000 | [diff] [blame] | 851 | return create<ast::Variable>(symbol, ast::StorageClass::kNone, type, true, |
| 852 | constructor, decorations); |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 853 | } |
| 854 | |
| 855 | /// @param source the variable source |
| 856 | /// @param symbol the variable symbol |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 857 | /// @param type the variable type |
| 858 | /// @param constructor optional constructor expression |
| 859 | /// @param decorations optional variable decorations |
| 860 | /// @returns a constant `ast::Variable` with the given symbol, storage and |
| 861 | /// type |
| 862 | ast::Variable* Const(const Source& source, |
| 863 | Symbol symbol, |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 864 | type::Type* type, |
| 865 | ast::Expression* constructor = nullptr, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 866 | ast::DecorationList decorations = {}) { |
Ben Clayton | 81a29fe | 2021-02-17 00:26:52 +0000 | [diff] [blame] | 867 | return create<ast::Variable>(source, symbol, ast::StorageClass::kNone, type, |
| 868 | true, constructor, decorations); |
Ben Clayton | 46d78d7 | 2021-02-10 21:34:26 +0000 | [diff] [blame] | 869 | } |
Ben Clayton | 37571bc | 2021-02-16 23:57:01 +0000 | [diff] [blame] | 870 | |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 871 | /// @param args the arguments to pass to Var() |
| 872 | /// @returns a `ast::Variable` constructed by calling Var() with the arguments |
| 873 | /// of `args`, which is automatically registered as a global variable with the |
| 874 | /// ast::Module. |
| 875 | template <typename... ARGS> |
| 876 | ast::Variable* Global(ARGS&&... args) { |
| 877 | auto* var = Var(std::forward<ARGS>(args)...); |
| 878 | AST().AddGlobalVariable(var); |
| 879 | return var; |
| 880 | } |
| 881 | |
| 882 | /// @param args the arguments to pass to Const() |
| 883 | /// @returns a const `ast::Variable` constructed by calling Var() with the |
| 884 | /// arguments of `args`, which is automatically registered as a global |
| 885 | /// variable with the ast::Module. |
| 886 | template <typename... ARGS> |
| 887 | ast::Variable* GlobalConst(ARGS&&... args) { |
| 888 | auto* var = Const(std::forward<ARGS>(args)...); |
| 889 | AST().AddGlobalVariable(var); |
| 890 | return var; |
| 891 | } |
| 892 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 893 | /// @param func the function name |
| 894 | /// @param args the function call arguments |
| 895 | /// @returns a `ast::CallExpression` to the function `func`, with the |
| 896 | /// arguments of `args` converted to `ast::Expression`s using `Expr()`. |
| 897 | template <typename NAME, typename... ARGS> |
| 898 | ast::CallExpression* Call(NAME&& func, ARGS&&... args) { |
| 899 | return create<ast::CallExpression>(Expr(func), |
| 900 | ExprList(std::forward<ARGS>(args)...)); |
| 901 | } |
| 902 | |
| 903 | /// @param lhs the left hand argument to the addition operation |
| 904 | /// @param rhs the right hand argument to the addition operation |
| 905 | /// @returns a `ast::BinaryExpression` summing the arguments `lhs` and `rhs` |
| 906 | template <typename LHS, typename RHS> |
| 907 | ast::Expression* Add(LHS&& lhs, RHS&& rhs) { |
| 908 | return create<ast::BinaryExpression>(ast::BinaryOp::kAdd, |
| 909 | Expr(std::forward<LHS>(lhs)), |
| 910 | Expr(std::forward<RHS>(rhs))); |
| 911 | } |
| 912 | |
| 913 | /// @param lhs the left hand argument to the subtraction operation |
| 914 | /// @param rhs the right hand argument to the subtraction operation |
| 915 | /// @returns a `ast::BinaryExpression` subtracting `rhs` from `lhs` |
| 916 | template <typename LHS, typename RHS> |
| 917 | ast::Expression* Sub(LHS&& lhs, RHS&& rhs) { |
| 918 | return create<ast::BinaryExpression>(ast::BinaryOp::kSubtract, |
| 919 | Expr(std::forward<LHS>(lhs)), |
| 920 | Expr(std::forward<RHS>(rhs))); |
| 921 | } |
| 922 | |
| 923 | /// @param lhs the left hand argument to the multiplication operation |
| 924 | /// @param rhs the right hand argument to the multiplication operation |
| 925 | /// @returns a `ast::BinaryExpression` multiplying `rhs` from `lhs` |
| 926 | template <typename LHS, typename RHS> |
| 927 | ast::Expression* Mul(LHS&& lhs, RHS&& rhs) { |
| 928 | return create<ast::BinaryExpression>(ast::BinaryOp::kMultiply, |
| 929 | Expr(std::forward<LHS>(lhs)), |
| 930 | Expr(std::forward<RHS>(rhs))); |
| 931 | } |
| 932 | |
| 933 | /// @param arr the array argument for the array accessor expression |
| 934 | /// @param idx the index argument for the array accessor expression |
| 935 | /// @returns a `ast::ArrayAccessorExpression` that indexes `arr` with `idx` |
| 936 | template <typename ARR, typename IDX> |
| 937 | ast::Expression* IndexAccessor(ARR&& arr, IDX&& idx) { |
| 938 | return create<ast::ArrayAccessorExpression>(Expr(std::forward<ARR>(arr)), |
| 939 | Expr(std::forward<IDX>(idx))); |
| 940 | } |
| 941 | |
| 942 | /// @param obj the object for the member accessor expression |
| 943 | /// @param idx the index argument for the array accessor expression |
| 944 | /// @returns a `ast::MemberAccessorExpression` that indexes `obj` with `idx` |
| 945 | template <typename OBJ, typename IDX> |
Ben Clayton | 6d612ad | 2021-02-24 14:15:02 +0000 | [diff] [blame] | 946 | ast::MemberAccessorExpression* MemberAccessor(OBJ&& obj, IDX&& idx) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 947 | return create<ast::MemberAccessorExpression>(Expr(std::forward<OBJ>(obj)), |
| 948 | Expr(std::forward<IDX>(idx))); |
| 949 | } |
| 950 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 951 | /// Creates a ast::StructMemberOffsetDecoration |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 952 | /// @param val the offset value |
| 953 | /// @returns the offset decoration pointer |
| 954 | ast::StructMemberOffsetDecoration* MemberOffset(uint32_t val) { |
| 955 | return create<ast::StructMemberOffsetDecoration>(source_, val); |
| 956 | } |
| 957 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 958 | /// Creates an ast::Function and registers it with the ast::Module. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 959 | /// @param source the source information |
| 960 | /// @param name the function name |
| 961 | /// @param params the function parameters |
| 962 | /// @param type the function return type |
| 963 | /// @param body the function body |
| 964 | /// @param decorations the function decorations |
| 965 | /// @returns the function pointer |
| 966 | ast::Function* Func(Source source, |
| 967 | std::string name, |
| 968 | ast::VariableList params, |
| 969 | type::Type* type, |
| 970 | ast::StatementList body, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 971 | ast::DecorationList decorations) { |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 972 | auto* func = |
| 973 | create<ast::Function>(source, Symbols().Register(name), params, type, |
| 974 | create<ast::BlockStatement>(body), decorations); |
James Price | 3eaa450 | 2021-02-09 21:26:21 +0000 | [diff] [blame] | 975 | AST().AddFunction(func); |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 976 | return func; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 979 | /// Creates an ast::Function and registers it with the ast::Module. |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 980 | /// @param name the function name |
| 981 | /// @param params the function parameters |
| 982 | /// @param type the function return type |
| 983 | /// @param body the function body |
| 984 | /// @param decorations the function decorations |
| 985 | /// @returns the function pointer |
| 986 | ast::Function* Func(std::string name, |
| 987 | ast::VariableList params, |
| 988 | type::Type* type, |
| 989 | ast::StatementList body, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 990 | ast::DecorationList decorations) { |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 991 | auto* func = |
| 992 | create<ast::Function>(Symbols().Register(name), params, type, |
| 993 | create<ast::BlockStatement>(body), decorations); |
James Price | 3eaa450 | 2021-02-09 21:26:21 +0000 | [diff] [blame] | 994 | AST().AddFunction(func); |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 995 | return func; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 996 | } |
| 997 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 998 | /// Creates a ast::StructMember |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 999 | /// @param source the source information |
| 1000 | /// @param name the struct member name |
| 1001 | /// @param type the struct member type |
| 1002 | /// @returns the struct member pointer |
| 1003 | ast::StructMember* Member(const Source& source, |
| 1004 | const std::string& name, |
| 1005 | type::Type* type) { |
| 1006 | return create<ast::StructMember>(source, Symbols().Register(name), type, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 1007 | ast::DecorationList{}); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 1010 | /// Creates a ast::StructMember |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1011 | /// @param name the struct member name |
| 1012 | /// @param type the struct member type |
| 1013 | /// @returns the struct member pointer |
| 1014 | ast::StructMember* Member(const std::string& name, type::Type* type) { |
| 1015 | return create<ast::StructMember>(source_, Symbols().Register(name), type, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 1016 | ast::DecorationList{}); |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
Ben Clayton | 42d1e09 | 2021-02-02 14:29:15 +0000 | [diff] [blame] | 1019 | /// Creates a ast::StructMember |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1020 | /// @param name the struct member name |
| 1021 | /// @param type the struct member type |
| 1022 | /// @param decorations the struct member decorations |
| 1023 | /// @returns the struct member pointer |
| 1024 | ast::StructMember* Member(const std::string& name, |
| 1025 | type::Type* type, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 1026 | ast::DecorationList decorations) { |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1027 | return create<ast::StructMember>(source_, Symbols().Register(name), type, |
| 1028 | decorations); |
| 1029 | } |
| 1030 | |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 1031 | /// Creates a ast::StructMember with the given byte offset |
| 1032 | /// @param offset the offset to use in the StructMemberOffsetDecoration |
| 1033 | /// @param name the struct member name |
| 1034 | /// @param type the struct member type |
| 1035 | /// @returns the struct member pointer |
| 1036 | ast::StructMember* Member(uint32_t offset, |
| 1037 | const std::string& name, |
| 1038 | type::Type* type) { |
| 1039 | return create<ast::StructMember>( |
| 1040 | source_, Symbols().Register(name), type, |
James Price | 95d4077 | 2021-03-11 17:39:32 +0000 | [diff] [blame] | 1041 | ast::DecorationList{ |
Ben Clayton | bab3197 | 2021-02-08 21:16:21 +0000 | [diff] [blame] | 1042 | create<ast::StructMemberOffsetDecoration>(offset), |
| 1043 | }); |
| 1044 | } |
| 1045 | |
Antonio Maiorano | fd31bbd | 2021-03-09 10:26:57 +0000 | [diff] [blame] | 1046 | /// Creates a ast::BlockStatement with input statements |
| 1047 | /// @param statements statements of block |
| 1048 | /// @returns the block statement pointer |
| 1049 | template <typename... Statements> |
| 1050 | ast::BlockStatement* Block(Statements&&... statements) { |
| 1051 | return create<ast::BlockStatement>( |
| 1052 | ast::StatementList{std::forward<Statements>(statements)...}); |
| 1053 | } |
| 1054 | |
| 1055 | /// Creates a ast::ElseStatement with input condition and body |
| 1056 | /// @param condition the else condition expression |
| 1057 | /// @param body the else body |
| 1058 | /// @returns the else statement pointer |
| 1059 | ast::ElseStatement* Else(ast::Expression* condition, |
| 1060 | ast::BlockStatement* body) { |
| 1061 | return create<ast::ElseStatement>(condition, body); |
| 1062 | } |
| 1063 | |
| 1064 | /// Creates a ast::IfStatement with input condition, body, and optional |
| 1065 | /// variadic else statements |
| 1066 | /// @param condition the if statement condition expression |
| 1067 | /// @param body the if statement body |
| 1068 | /// @param elseStatements optional variadic else statements |
| 1069 | /// @returns the if statement pointer |
| 1070 | template <typename... ElseStatements> |
| 1071 | ast::IfStatement* If(ast::Expression* condition, |
| 1072 | ast::BlockStatement* body, |
| 1073 | ElseStatements&&... elseStatements) { |
| 1074 | return create<ast::IfStatement>( |
| 1075 | condition, body, |
| 1076 | ast::ElseStatementList{ |
| 1077 | std::forward<ElseStatements>(elseStatements)...}); |
| 1078 | } |
| 1079 | |
| 1080 | /// Creates a ast::AssignmentStatement with input lhs and rhs expressions |
| 1081 | /// @param lhs the left hand side expression |
| 1082 | /// @param rhs the right hand side expression |
| 1083 | /// @returns the assignment statement pointer |
| 1084 | ast::AssignmentStatement* Assign(ast::Expression* lhs, ast::Expression* rhs) { |
| 1085 | return create<ast::AssignmentStatement>(lhs, rhs); |
| 1086 | } |
| 1087 | |
| 1088 | /// Creates a ast::LoopStatement with input body and optional continuing |
| 1089 | /// @param body the loop body |
| 1090 | /// @param continuing the optional continuing block |
| 1091 | /// @returns the loop statement pointer |
| 1092 | ast::LoopStatement* Loop(ast::BlockStatement* body, |
| 1093 | ast::BlockStatement* continuing = nullptr) { |
| 1094 | return create<ast::LoopStatement>(body, continuing); |
| 1095 | } |
| 1096 | |
| 1097 | /// Creates a ast::VariableDeclStatement for the input variable |
| 1098 | /// @param var the variable to wrap in a decl statement |
| 1099 | /// @returns the variable decl statement pointer |
| 1100 | ast::VariableDeclStatement* Decl(ast::Variable* var) { |
| 1101 | return create<ast::VariableDeclStatement>(var); |
| 1102 | } |
| 1103 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1104 | /// Sets the current builder source to `src` |
| 1105 | /// @param src the Source used for future create() calls |
| 1106 | void SetSource(const Source& src) { |
| 1107 | AssertNotMoved(); |
| 1108 | source_ = src; |
| 1109 | } |
| 1110 | |
| 1111 | /// Sets the current builder source to `loc` |
| 1112 | /// @param loc the Source used for future create() calls |
| 1113 | void SetSource(const Source::Location& loc) { |
| 1114 | AssertNotMoved(); |
| 1115 | source_ = Source(loc); |
| 1116 | } |
| 1117 | |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 1118 | /// Helper for returning the resolved semantic type of the expression `expr`. |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1119 | /// @note As the Resolver is run when the Program is built, this will only be |
| 1120 | /// useful for the Resolver itself and tests that use their own Resolver. |
Ben Clayton | 3335254 | 2021-01-29 16:43:41 +0000 | [diff] [blame] | 1121 | /// @param expr the AST expression |
| 1122 | /// @return the resolved semantic type for the expression, or nullptr if the |
| 1123 | /// expression has no resolved type. |
| 1124 | type::Type* TypeOf(ast::Expression* expr) const; |
| 1125 | |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1126 | /// Wraps the ast::Expression in a statement. This is used by tests that |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1127 | /// construct a partial AST and require the Resolver to reach these |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1128 | /// nodes. |
| 1129 | /// @param expr the ast::Expression to be wrapped by an ast::Statement |
| 1130 | /// @return the ast::Statement that wraps the ast::Expression |
| 1131 | ast::Statement* WrapInStatement(ast::Expression* expr); |
| 1132 | /// Wraps the ast::Variable in a ast::VariableDeclStatement. This is used by |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1133 | /// tests that construct a partial AST and require the Resolver to reach |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1134 | /// these nodes. |
| 1135 | /// @param v the ast::Variable to be wrapped by an ast::VariableDeclStatement |
| 1136 | /// @return the ast::VariableDeclStatement that wraps the ast::Variable |
| 1137 | ast::VariableDeclStatement* WrapInStatement(ast::Variable* v); |
| 1138 | /// Returns the statement argument. Used as a passthrough-overload by |
| 1139 | /// WrapInFunction(). |
| 1140 | /// @param stmt the ast::Statement |
| 1141 | /// @return `stmt` |
| 1142 | ast::Statement* WrapInStatement(ast::Statement* stmt); |
| 1143 | /// Wraps the list of arguments in a simple function so that each is reachable |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1144 | /// by the Resolver. |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1145 | /// @param args a mix of ast::Expression, ast::Statement, ast::Variables. |
| 1146 | template <typename... ARGS> |
| 1147 | void WrapInFunction(ARGS&&... args) { |
| 1148 | ast::StatementList stmts{WrapInStatement(std::forward<ARGS>(args))...}; |
| 1149 | WrapInFunction(stmts); |
| 1150 | } |
| 1151 | /// @param stmts a list of ast::Statement that will be wrapped by a function, |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1152 | /// so that each statement is reachable by the Resolver. |
Ben Clayton | 401b96b | 2021-02-03 17:19:59 +0000 | [diff] [blame] | 1153 | void WrapInFunction(ast::StatementList stmts); |
| 1154 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1155 | /// The builder types |
Ben Clayton | c7ca766 | 2021-02-17 16:23:52 +0000 | [diff] [blame] | 1156 | TypesBuilder const ty{this}; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1157 | |
| 1158 | protected: |
| 1159 | /// Asserts that the builder has not been moved. |
| 1160 | void AssertNotMoved() const; |
| 1161 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1162 | private: |
| 1163 | type::Manager types_; |
Ben Clayton | 7fdfff1 | 2021-01-29 15:17:30 +0000 | [diff] [blame] | 1164 | ASTNodeAllocator ast_nodes_; |
| 1165 | SemNodeAllocator sem_nodes_; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1166 | ast::Module* ast_; |
Ben Clayton | dd1b6fc | 2021-01-29 10:55:40 +0000 | [diff] [blame] | 1167 | semantic::Info sem_; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1168 | SymbolTable symbols_; |
Ben Clayton | 844217f | 2021-01-27 18:49:05 +0000 | [diff] [blame] | 1169 | diag::List diagnostics_; |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1170 | |
| 1171 | /// The source to use when creating AST nodes without providing a Source as |
| 1172 | /// the first argument. |
| 1173 | Source source_; |
| 1174 | |
Ben Clayton | 5f0ea11 | 2021-03-09 10:54:37 +0000 | [diff] [blame] | 1175 | /// Set by SetResolveOnBuild(). If set, the Resolver will be run on the |
Ben Clayton | dd69ac3 | 2021-01-27 19:23:55 +0000 | [diff] [blame] | 1176 | /// program when built. |
| 1177 | bool resolve_on_build_ = true; |
| 1178 | |
Ben Clayton | a6b9a8e | 2021-01-26 16:57:10 +0000 | [diff] [blame] | 1179 | /// Set by MarkAsMoved(). Once set, no methods may be called on this builder. |
| 1180 | bool moved_ = false; |
| 1181 | }; |
| 1182 | |
| 1183 | //! @cond Doxygen_Suppress |
| 1184 | // Various template specializations for ProgramBuilder::TypesBuilder::CToAST. |
| 1185 | template <> |
| 1186 | struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::i32> { |
| 1187 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 1188 | return t->i32(); |
| 1189 | } |
| 1190 | }; |
| 1191 | template <> |
| 1192 | struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::u32> { |
| 1193 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 1194 | return t->u32(); |
| 1195 | } |
| 1196 | }; |
| 1197 | template <> |
| 1198 | struct ProgramBuilder::TypesBuilder::CToAST<ProgramBuilder::f32> { |
| 1199 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 1200 | return t->f32(); |
| 1201 | } |
| 1202 | }; |
| 1203 | template <> |
| 1204 | struct ProgramBuilder::TypesBuilder::CToAST<bool> { |
| 1205 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 1206 | return t->bool_(); |
| 1207 | } |
| 1208 | }; |
| 1209 | template <> |
| 1210 | struct ProgramBuilder::TypesBuilder::CToAST<void> { |
| 1211 | static type::Type* get(const ProgramBuilder::TypesBuilder* t) { |
| 1212 | return t->void_(); |
| 1213 | } |
| 1214 | }; |
| 1215 | //! @endcond |
| 1216 | |
| 1217 | } // namespace tint |
| 1218 | |
| 1219 | #endif // SRC_PROGRAM_BUILDER_H_ |