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