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