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