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