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