Skip to content

Commit cb6b43d

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

File tree

8 files changed

+146
-3
lines changed

8 files changed

+146
-3
lines changed

src/lsp/cxx/lsp/cxx_document.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ void CxxDocument::parse(std::string source) {
260260
if (auto classType = type_cast<ClassType>(objectType)) {
261261
auto classSymbol = classType->symbol();
262262
for (auto member : views::members(classSymbol)) {
263+
if (!member->name()) continue;
263264
auto item = d->completionItems.emplace_back();
264265
item.label(to_string(member->name()));
265266
}

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/base_classes.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
#include <cxx/views/base_classes.h>

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: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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
File renamed without changes.

0 commit comments

Comments
 (0)