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