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