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