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