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