dan sinclair | c9949ac | 2022-12-08 12:03:53 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Tint Authors. |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 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 | |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 15 | #ifndef SRC_TINT_TYPE_MANAGER_H_ |
| 16 | #define SRC_TINT_TYPE_MANAGER_H_ |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 17 | |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 18 | #include <utility> |
| 19 | |
dan sinclair | 5f764d8 | 2022-12-08 00:32:27 +0000 | [diff] [blame] | 20 | #include "src/tint/type/type.h" |
Ben Clayton | 7c3e9a6 | 2022-12-16 15:31:19 +0000 | [diff] [blame] | 21 | #include "src/tint/utils/hash.h" |
Ben Clayton | 4391975 | 2022-03-07 17:05:28 +0000 | [diff] [blame] | 22 | #include "src/tint/utils/unique_allocator.h" |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 23 | |
dan sinclair | c9949ac | 2022-12-08 12:03:53 +0000 | [diff] [blame] | 24 | namespace tint::type { |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 25 | |
| 26 | /// The type manager holds all the pointers to the known types. |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 27 | class Manager final { |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 28 | public: |
| 29 | /// Iterator is the type returned by begin() and end() |
dan sinclair | 98705d4 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 30 | using TypeIterator = utils::BlockAllocator<Type>::ConstIterator; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 31 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 32 | /// Constructor |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 33 | Manager(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 34 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 35 | /// Move constructor |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 36 | Manager(Manager&&); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 37 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 38 | /// Move assignment operator |
| 39 | /// @param rhs the Manager to move |
| 40 | /// @return this Manager |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 41 | Manager& operator=(Manager&& rhs); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 42 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 43 | /// Destructor |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 44 | ~Manager(); |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 45 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 46 | /// Wrap returns a new Manager created with the types of `inner`. |
| 47 | /// The Manager returned by Wrap is intended to temporarily extend the types |
| 48 | /// of an existing immutable Manager. |
| 49 | /// As the copied types are owned by `inner`, `inner` must not be destructed |
| 50 | /// or assigned while using the returned Manager. |
| 51 | /// TODO(bclayton) - Evaluate whether there are safer alternatives to this |
| 52 | /// function. See crbug.com/tint/460. |
| 53 | /// @param inner the immutable Manager to extend |
| 54 | /// @return the Manager that wraps `inner` |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 55 | static Manager Wrap(const Manager& inner) { |
| 56 | Manager out; |
dan sinclair | 847cfa0 | 2022-12-01 21:42:47 +0000 | [diff] [blame] | 57 | out.types_.Wrap(inner.types_); |
Ben Clayton | 7c3e9a6 | 2022-12-16 15:31:19 +0000 | [diff] [blame] | 58 | out.unique_nodes_.Wrap(inner.unique_nodes_); |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 59 | return out; |
| 60 | } |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 61 | |
Ben Clayton | 7c3e9a6 | 2022-12-16 15:31:19 +0000 | [diff] [blame] | 62 | /// @param args the arguments used to construct the type, unique node or node. |
dan sinclair | 847cfa0 | 2022-12-01 21:42:47 +0000 | [diff] [blame] | 63 | /// @return a pointer to an instance of `T` with the provided arguments. |
Ben Clayton | 7c3e9a6 | 2022-12-16 15:31:19 +0000 | [diff] [blame] | 64 | /// If NODE derives from UniqueNode and an existing instance of `T` has been |
| 65 | /// constructed, then the same pointer is returned. |
| 66 | template <typename NODE, typename... ARGS> |
| 67 | NODE* Get(ARGS&&... args) { |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 68 | if constexpr (utils::traits::IsTypeOrDerived<NODE, Type>) { |
Ben Clayton | 7c3e9a6 | 2022-12-16 15:31:19 +0000 | [diff] [blame] | 69 | return types_.Get<NODE>(std::forward<ARGS>(args)...); |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 70 | } else if constexpr (utils::traits::IsTypeOrDerived<NODE, UniqueNode>) { |
Ben Clayton | 7c3e9a6 | 2022-12-16 15:31:19 +0000 | [diff] [blame] | 71 | return unique_nodes_.Get<NODE>(std::forward<ARGS>(args)...); |
| 72 | } else { |
| 73 | return nodes_.Create<NODE>(std::forward<ARGS>(args)...); |
| 74 | } |
dan sinclair | 847cfa0 | 2022-12-01 21:42:47 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /// @param args the arguments used to create the temporary used for the search. |
| 78 | /// @return a pointer to an instance of `T` with the provided arguments, or nullptr if the item |
Ben Clayton | 2b47c21 | 2022-09-01 09:15:20 +0000 | [diff] [blame] | 79 | /// was not found. |
dan sinclair | 847cfa0 | 2022-12-01 21:42:47 +0000 | [diff] [blame] | 80 | template <typename TYPE, |
dan sinclair | 0559005 | 2023-04-19 16:52:46 +0000 | [diff] [blame] | 81 | typename _ = std::enable_if<utils::traits::IsTypeOrDerived<TYPE, Type>>, |
dan sinclair | 847cfa0 | 2022-12-01 21:42:47 +0000 | [diff] [blame] | 82 | typename... ARGS> |
Ben Clayton | 2b47c21 | 2022-09-01 09:15:20 +0000 | [diff] [blame] | 83 | TYPE* Find(ARGS&&... args) const { |
dan sinclair | 847cfa0 | 2022-12-01 21:42:47 +0000 | [diff] [blame] | 84 | return types_.Find<TYPE>(std::forward<ARGS>(args)...); |
Ben Clayton | 2b47c21 | 2022-09-01 09:15:20 +0000 | [diff] [blame] | 85 | } |
| 86 | |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 87 | /// @returns an iterator to the beginning of the types |
dan sinclair | 847cfa0 | 2022-12-01 21:42:47 +0000 | [diff] [blame] | 88 | TypeIterator begin() const { return types_.begin(); } |
dan sinclair | 41e4d9a | 2022-05-01 14:40:55 +0000 | [diff] [blame] | 89 | /// @returns an iterator to the end of the types |
dan sinclair | 847cfa0 | 2022-12-01 21:42:47 +0000 | [diff] [blame] | 90 | TypeIterator end() const { return types_.end(); } |
| 91 | |
| 92 | private: |
Ben Clayton | 7c3e9a6 | 2022-12-16 15:31:19 +0000 | [diff] [blame] | 93 | /// Unique types owned by the manager |
dan sinclair | 98705d4 | 2022-12-08 22:21:24 +0000 | [diff] [blame] | 94 | utils::UniqueAllocator<Type> types_; |
Ben Clayton | 7c3e9a6 | 2022-12-16 15:31:19 +0000 | [diff] [blame] | 95 | /// Unique nodes (excluding types) owned by the manager |
| 96 | utils::UniqueAllocator<UniqueNode> unique_nodes_; |
| 97 | /// Non-unique nodes owned by the manager |
| 98 | utils::BlockAllocator<Node> nodes_; |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 99 | }; |
| 100 | |
dan sinclair | c9949ac | 2022-12-08 12:03:53 +0000 | [diff] [blame] | 101 | } // namespace tint::type |
Ryan Harrison | dbc13af | 2022-02-21 15:19:07 +0000 | [diff] [blame] | 102 | |
dan sinclair | 837b804 | 2022-12-09 05:00:07 +0000 | [diff] [blame] | 103 | #endif // SRC_TINT_TYPE_MANAGER_H_ |