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