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