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