|
| 1 | +// Copyright (c) 2024 Roberto Raggi <[email protected]> |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +// of this software and associated documentation files (the "Software"), to deal |
| 5 | +// in the Software without restriction, including without limitation the rights |
| 6 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +// copies of the Software, and to permit persons to whom the Software is |
| 8 | +// furnished to do so, subject to the following conditions: |
| 9 | +// |
| 10 | +// The above copyright notice and this permission notice shall be included in |
| 11 | +// all copies or substantial portions of the Software. |
| 12 | +// |
| 13 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +// SOFTWARE. |
| 20 | + |
| 21 | +#pragma once |
| 22 | + |
| 23 | +#include <cxx/symbols.h> |
| 24 | +#include <memory.h> |
| 25 | + |
| 26 | +#include <ranges> |
| 27 | +#include <stack> |
| 28 | + |
| 29 | +namespace cxx { |
| 30 | + |
| 31 | +class BaseClassesView { |
| 32 | + public: |
| 33 | + explicit BaseClassesView(ClassSymbol* classSymbol) : root_(classSymbol) {} |
| 34 | + ~BaseClassesView() = default; |
| 35 | + |
| 36 | + auto begin() const { return Generator{root_}; } |
| 37 | + auto end() const { return std::default_sentinel; } |
| 38 | + |
| 39 | + private: |
| 40 | + class Generator { |
| 41 | + public: |
| 42 | + using value_type = BaseClassSymbol*; |
| 43 | + using difference_type = std::ptrdiff_t; |
| 44 | + |
| 45 | + explicit Generator(ClassSymbol* classSymbol) { |
| 46 | + if (classSymbol) { |
| 47 | + for (auto base : classSymbol->baseClasses() | std::views::reverse) { |
| 48 | + state_->stack.push(base); |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + auto operator*() const -> BaseClassSymbol* { |
| 54 | + if (state_->stack.empty()) return nullptr; |
| 55 | + return state_->stack.top(); |
| 56 | + } |
| 57 | + |
| 58 | + auto operator++() -> Generator& { |
| 59 | + auto base = state_->stack.top(); |
| 60 | + state_->stack.pop(); |
| 61 | + |
| 62 | + if (auto classSymbol = symbol_cast<ClassSymbol>(base->symbol())) { |
| 63 | + if (!state_->visited.insert(classSymbol).second) { |
| 64 | + return *this; |
| 65 | + } |
| 66 | + |
| 67 | + for (auto base : classSymbol->baseClasses() | std::views::reverse) { |
| 68 | + state_->stack.push(base); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return *this; |
| 73 | + } |
| 74 | + |
| 75 | + auto operator++(int) -> Generator { |
| 76 | + auto tmp = *this; |
| 77 | + ++*this; |
| 78 | + return tmp; |
| 79 | + } |
| 80 | + |
| 81 | + auto operator==(const std::default_sentinel_t&) const -> bool { |
| 82 | + return state_->stack.empty(); |
| 83 | + } |
| 84 | + |
| 85 | + private: |
| 86 | + struct State { |
| 87 | + std::unordered_set<ClassSymbol*> visited; |
| 88 | + std::stack<BaseClassSymbol*> stack; |
| 89 | + }; |
| 90 | + |
| 91 | + std::shared_ptr<State> state_ = std::make_shared<State>(); |
| 92 | + }; |
| 93 | + |
| 94 | + private: |
| 95 | + ClassSymbol* root_; |
| 96 | +}; |
| 97 | + |
| 98 | +namespace views { |
| 99 | + |
| 100 | +inline auto base_classes(ClassSymbol* classSymbol) -> BaseClassesView { |
| 101 | + return BaseClassesView{classSymbol}; |
| 102 | +} |
| 103 | + |
| 104 | +} // namespace views |
| 105 | + |
| 106 | +} // namespace cxx |
0 commit comments