Skip to content

Commit b5d290b

Browse files
committed
chore: Add views::base_classes
Signed-off-by: Roberto Raggi <[email protected]>
1 parent 0913436 commit b5d290b

File tree

6 files changed

+128
-3
lines changed

6 files changed

+128
-3
lines changed

src/parser/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,17 @@
1818
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1919

2020
file(GLOB CXX_INCLUDE_HEADER_FILES cxx/*.h)
21+
file(GLOB CXX_VIEWS_INCLUDE_HEADER_FILES cxx/views/*.h)
2122

2223
aux_source_directory(cxx SOURCES)
2324

2425
add_library(cxx-parser ${SOURCES}
2526
# generated files
2627
keywords-priv.h
2728
pp_keywords-priv.h
29+
# headers
30+
${CXX_INCLUDE_HEADER_FILES}
31+
${CXX_VIEWS_INCLUDE_HEADER_FILES}
2832
)
2933

3034
target_compile_definitions(cxx-parser PUBLIC
@@ -85,6 +89,11 @@ install(
8589
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cxx
8690
)
8791

92+
install(
93+
FILES ${CXX_VIEWS_INCLUDE_HEADER_FILES}
94+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cxx/views
95+
)
96+
8897
install(
8998
TARGETS cxx-parser
9099
EXPORT cxxTargets

src/parser/cxx/parser.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
#include <cxx/name_printer.h>
3030
#include <cxx/names.h>
3131
#include <cxx/scope.h>
32-
#include <cxx/symbol_chain_view.h>
3332
#include <cxx/symbol_instantiation.h>
3433
#include <cxx/symbols.h>
3534
#include <cxx/token.h>
3635
#include <cxx/type_printer.h>
3736
#include <cxx/types.h>
37+
#include <cxx/views/symbol_chain.h>
3838

3939
#include <algorithm>
4040
#include <cstring>
@@ -9868,6 +9868,10 @@ void Parser::parse_base_specifier(BaseSpecifierAST*& yyast) {
98689868
baseClassSymbol->setVirtual(ast->isVirtual);
98699869
baseClassSymbol->setSymbol(symbol);
98709870

9871+
if (symbol) {
9872+
baseClassSymbol->setName(symbol->name());
9873+
}
9874+
98719875
switch (ast->accessSpecifier) {
98729876
case TokenKind::T_PRIVATE:
98739877
baseClassSymbol->setAccessSpecifier(AccessSpecifier::kPrivate);

src/parser/cxx/scope.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@
2121
#pragma once
2222

2323
#include <cxx/names_fwd.h>
24-
#include <cxx/symbol_chain_view.h>
2524
#include <cxx/symbols.h>
2625
#include <cxx/symbols_fwd.h>
2726
#include <cxx/types_fwd.h>
27+
#include <cxx/views/symbol_chain.h>
2828

2929
#include <vector>
3030

src/parser/cxx/symbol_chain_view.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
1919
// SOFTWARE.
2020

21-
#include <cxx/symbol_chain_view.h>
21+
#include <cxx/views/symbol_chain.h>
22+
23+
// cxx
2224
#include <cxx/symbols.h>
2325

2426
namespace cxx {
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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+
25+
#include <ranges>
26+
#include <stack>
27+
28+
namespace cxx {
29+
30+
class BaseClassesView {
31+
public:
32+
explicit BaseClassesView(ClassSymbol* classSymbol)
33+
: state_(std::make_shared<State>()) {
34+
if (classSymbol) {
35+
for (auto base : classSymbol->baseClasses() | std::views::reverse) {
36+
state_->stack.push(base);
37+
}
38+
}
39+
}
40+
41+
~BaseClassesView() = default;
42+
43+
auto begin() { return Generator{this}; }
44+
auto end() { return std::default_sentinel; }
45+
46+
private:
47+
class Generator {
48+
public:
49+
using value_type = BaseClassSymbol*;
50+
using difference_type = std::ptrdiff_t;
51+
52+
explicit Generator(BaseClassesView* view) : view_{view} {}
53+
54+
auto operator*() const -> BaseClassSymbol* {
55+
auto state = view_->state_;
56+
if (state->stack.empty()) return nullptr;
57+
return state->stack.top();
58+
}
59+
60+
auto operator++() -> Generator& {
61+
auto state = view_->state_;
62+
auto base = state->stack.top();
63+
state->stack.pop();
64+
65+
if (auto classSymbol = symbol_cast<ClassSymbol>(base->symbol())) {
66+
if (!state->visited.insert(classSymbol).second) {
67+
return *this;
68+
}
69+
70+
for (auto base : classSymbol->baseClasses() | std::views::reverse) {
71+
state->stack.push(base);
72+
}
73+
}
74+
75+
return *this;
76+
}
77+
78+
auto operator++(int) -> Generator {
79+
auto tmp = *this;
80+
++*this;
81+
return tmp;
82+
}
83+
84+
auto operator==(const std::default_sentinel_t&) const -> bool {
85+
auto state = view_->state_;
86+
return state->stack.empty();
87+
}
88+
89+
private:
90+
BaseClassesView* view_ = nullptr;
91+
};
92+
93+
private:
94+
struct State {
95+
std::unordered_set<ClassSymbol*> visited;
96+
std::stack<BaseClassSymbol*> stack;
97+
};
98+
99+
std::shared_ptr<State> state_ = std::make_shared<State>();
100+
};
101+
102+
namespace views {
103+
104+
inline auto base_classes(ClassSymbol* classSymbol) -> BaseClassesView {
105+
return BaseClassesView{classSymbol};
106+
}
107+
108+
} // namespace views
109+
110+
} // namespace cxx
File renamed without changes.

0 commit comments

Comments
 (0)