Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lsp/cxx/lsp/cxx_document.cc
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ void CxxDocument::parse(std::string source) {

if (auto classType = type_cast<ClassType>(objectType)) {
auto classSymbol = classType->symbol();
for (auto member : classSymbol->scope()->symbols()) {
for (auto member : views::members(classSymbol)) {
if (!member->name()) continue;
auto item = d->completionItems.emplace_back();
item.label(to_string(member->name()));
Expand Down
9 changes: 9 additions & 0 deletions src/parser/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

file(GLOB CXX_INCLUDE_HEADER_FILES cxx/*.h)
file(GLOB CXX_VIEWS_INCLUDE_HEADER_FILES cxx/views/*.h)

aux_source_directory(cxx SOURCES)

add_library(cxx-parser ${SOURCES}
# generated files
keywords-priv.h
pp_keywords-priv.h
# headers
${CXX_INCLUDE_HEADER_FILES}
${CXX_VIEWS_INCLUDE_HEADER_FILES}
)

target_compile_definitions(cxx-parser PUBLIC
Expand Down Expand Up @@ -85,6 +89,11 @@ install(
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cxx
)

install(
FILES ${CXX_VIEWS_INCLUDE_HEADER_FILES}
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/cxx/views
)

install(
TARGETS cxx-parser
EXPORT cxxTargets
Expand Down
21 changes: 21 additions & 0 deletions src/parser/cxx/base_classes.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) 2024 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <cxx/views/base_classes.h>
6 changes: 5 additions & 1 deletion src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
#include <cxx/name_printer.h>
#include <cxx/names.h>
#include <cxx/scope.h>
#include <cxx/symbol_chain_view.h>
#include <cxx/symbol_instantiation.h>
#include <cxx/symbols.h>
#include <cxx/token.h>
#include <cxx/type_printer.h>
#include <cxx/types.h>
#include <cxx/views/symbol_chain.h>

#include <algorithm>
#include <cstring>
Expand Down Expand Up @@ -9868,6 +9868,10 @@ void Parser::parse_base_specifier(BaseSpecifierAST*& yyast) {
baseClassSymbol->setVirtual(ast->isVirtual);
baseClassSymbol->setSymbol(symbol);

if (symbol) {
baseClassSymbol->setName(symbol->name());
}

switch (ast->accessSpecifier) {
case TokenKind::T_PRIVATE:
baseClassSymbol->setAccessSpecifier(AccessSpecifier::kPrivate);
Expand Down
14 changes: 13 additions & 1 deletion src/parser/cxx/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
#pragma once

#include <cxx/names_fwd.h>
#include <cxx/symbol_chain_view.h>
#include <cxx/symbols.h>
#include <cxx/symbols_fwd.h>
#include <cxx/types_fwd.h>
#include <cxx/views/symbol_chain.h>

#include <vector>

Expand Down Expand Up @@ -106,6 +106,18 @@ constexpr auto functions = std::views::filter(&Symbol::isFunction) |
constexpr auto variables = std::views::filter(&Symbol::isVariable) |
std::views::transform(symbol_cast<VariableSymbol>);

inline auto members(Scope* scope) { return std::views::all(scope->symbols()); }

inline auto members(ScopedSymbol* symbol) {
return std::views::all(symbol->scope()->symbols());
}

inline auto find(Scope* scope, const Name* name) {
return scope ? scope->find(name) : SymbolChainView{nullptr};
}

constexpr auto named_symbol = std::views::filter(&Symbol::name);

} // namespace views

} // namespace cxx
4 changes: 3 additions & 1 deletion src/parser/cxx/symbol_chain_view.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#include <cxx/symbol_chain_view.h>
#include <cxx/views/symbol_chain.h>

// cxx
#include <cxx/symbols.h>

namespace cxx {
Expand Down
12 changes: 6 additions & 6 deletions src/parser/cxx/symbol_instantiation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ auto SymbolInstantiation::VisitSymbol::operator()(ClassSymbol* symbol)
auto newCtor = self.instantiate(ctor);
newSymbol->addConstructor(newCtor);
}
for (auto member : symbol->scope()->symbols()) {
for (auto member : views::members(symbol)) {
auto newMember = self.instantiate(member);
newSymbol->addMember(newMember);
}
Expand All @@ -239,7 +239,7 @@ auto SymbolInstantiation::VisitSymbol::operator()(ClassSymbol* symbol)
auto SymbolInstantiation::VisitSymbol::operator()(EnumSymbol* symbol)
-> Symbol* {
auto newSymbol = self.replacement(symbol);
for (auto member : symbol->scope()->symbols()) {
for (auto member : views::members(symbol)) {
auto newMember = self.instantiate(member);
newSymbol->addMember(newMember);
}
Expand All @@ -249,7 +249,7 @@ auto SymbolInstantiation::VisitSymbol::operator()(EnumSymbol* symbol)
auto SymbolInstantiation::VisitSymbol::operator()(ScopedEnumSymbol* symbol)
-> Symbol* {
auto newSymbol = self.replacement(symbol);
for (auto member : symbol->scope()->symbols()) {
for (auto member : views::members(symbol)) {
auto newMember = self.instantiate(member);
newSymbol->addMember(newMember);
}
Expand All @@ -259,7 +259,7 @@ auto SymbolInstantiation::VisitSymbol::operator()(ScopedEnumSymbol* symbol)
auto SymbolInstantiation::VisitSymbol::operator()(FunctionSymbol* symbol)
-> Symbol* {
auto newSymbol = self.replacement(symbol);
for (auto member : symbol->scope()->symbols()) {
for (auto member : views::members(symbol)) {
if (member->isBlock()) continue;
auto newMember = self.instantiate(member);
newSymbol->addMember(newMember);
Expand Down Expand Up @@ -300,7 +300,7 @@ auto SymbolInstantiation::VisitSymbol::operator()(EnumeratorSymbol* symbol)
auto SymbolInstantiation::VisitSymbol::operator()(
FunctionParametersSymbol* symbol) -> Symbol* {
auto newSymbol = self.replacement(symbol);
for (auto member : symbol->scope()->symbols()) {
for (auto member : views::members(symbol)) {
if (member->isBlock()) continue;
auto newMember = self.instantiate(member);
newSymbol->addMember(newMember);
Expand All @@ -311,7 +311,7 @@ auto SymbolInstantiation::VisitSymbol::operator()(
auto SymbolInstantiation::VisitSymbol::operator()(
TemplateParametersSymbol* symbol) -> Symbol* {
auto newSymbol = self.replacement(symbol);
for (auto member : symbol->scope()->symbols()) {
for (auto member : views::members(symbol)) {
auto newMember = self.instantiate(member);
newSymbol->addMember(newMember);
}
Expand Down
106 changes: 106 additions & 0 deletions src/parser/cxx/views/base_classes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// Copyright (c) 2024 Roberto Raggi <roberto.raggi@gmail.com>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

#pragma once

#include <cxx/symbols.h>
#include <memory.h>

#include <ranges>
#include <stack>

namespace cxx {

class BaseClassesView {
public:
explicit BaseClassesView(ClassSymbol* classSymbol) : root_(classSymbol) {}
~BaseClassesView() = default;

auto begin() const { return Generator{root_}; }
auto end() const { return std::default_sentinel; }

private:
class Generator {
public:
using value_type = BaseClassSymbol*;
using difference_type = std::ptrdiff_t;

explicit Generator(ClassSymbol* classSymbol) {
if (classSymbol) {
for (auto base : classSymbol->baseClasses() | std::views::reverse) {
state_->stack.push(base);
}
}
}

auto operator*() const -> BaseClassSymbol* {
if (state_->stack.empty()) return nullptr;
return state_->stack.top();
}

auto operator++() -> Generator& {
auto base = state_->stack.top();
state_->stack.pop();

if (auto classSymbol = symbol_cast<ClassSymbol>(base->symbol())) {
if (!state_->visited.insert(classSymbol).second) {
return *this;
}

for (auto base : classSymbol->baseClasses() | std::views::reverse) {
state_->stack.push(base);
}
}

return *this;
}

auto operator++(int) -> Generator {
auto tmp = *this;
++*this;
return tmp;
}

auto operator==(const std::default_sentinel_t&) const -> bool {
return state_->stack.empty();
}

private:
struct State {
std::unordered_set<ClassSymbol*> visited;
std::stack<BaseClassSymbol*> stack;
};

std::shared_ptr<State> state_ = std::make_shared<State>();
};

private:
ClassSymbol* root_;
};

namespace views {

inline auto base_classes(ClassSymbol* classSymbol) -> BaseClassesView {
return BaseClassesView{classSymbol};
}

} // namespace views

} // namespace cxx
File renamed without changes.