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