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