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