Parse cpp_namespace_alias
This commit is contained in:
parent
7e1ef01105
commit
45e9e5305b
9 changed files with 161 additions and 11 deletions
15
src/cpp_entity_ref.cpp
Normal file
15
src/cpp_entity_ref.cpp
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
// Copyright (C) 2017 Jonathan Müller <jonathanmueller.dev@gmail.com>
|
||||
// This file is subject to the license terms in the LICENSE file
|
||||
// found in the top-level directory of this distribution.
|
||||
|
||||
#include <cppast/cpp_entity_ref.hpp>
|
||||
|
||||
#include <cppast/cpp_entity.hpp>
|
||||
|
||||
using namespace cppast;
|
||||
|
||||
void detail::check_entity_cast(const cpp_entity& e, cpp_entity_kind expected_kind)
|
||||
{
|
||||
DEBUG_ASSERT(e.kind() == expected_kind, detail::precondition_error_handler{},
|
||||
"mismatched entity kind");
|
||||
}
|
||||
|
|
@ -33,11 +33,16 @@ std::unique_ptr<cpp_namespace_alias> cpp_namespace_alias::build(const cpp_entity
|
|||
return ptr;
|
||||
}
|
||||
|
||||
cpp_entity_kind cpp_namespace_alias::do_get_entity_kind() const noexcept
|
||||
cpp_entity_kind cpp_namespace_alias::kind() noexcept
|
||||
{
|
||||
return cpp_entity_kind::namespace_alias_t;
|
||||
}
|
||||
|
||||
cpp_entity_kind cpp_namespace_alias::do_get_entity_kind() const noexcept
|
||||
{
|
||||
return kind();
|
||||
}
|
||||
|
||||
cpp_entity_kind cpp_using_directive::do_get_entity_kind() const noexcept
|
||||
{
|
||||
return cpp_entity_kind::using_directive_t;
|
||||
|
|
|
|||
|
|
@ -15,15 +15,20 @@ namespace cppast
|
|||
{
|
||||
// visits direct children of an entity
|
||||
template <typename Func>
|
||||
void visit_children(CXCursor parent, Func f)
|
||||
void visit_children(CXCursor parent, Func f, bool recurse = false)
|
||||
{
|
||||
clang_visitChildren(parent,
|
||||
[](CXCursor cur, CXCursor, CXClientData data) {
|
||||
auto& actual_cb = *static_cast<Func*>(data);
|
||||
actual_cb(cur);
|
||||
return CXChildVisit_Continue;
|
||||
},
|
||||
&f);
|
||||
auto continue_lambda = [](CXCursor cur, CXCursor, CXClientData data) {
|
||||
auto& actual_cb = *static_cast<Func*>(data);
|
||||
actual_cb(cur);
|
||||
return CXChildVisit_Continue;
|
||||
};
|
||||
auto recurse_lambda = [](CXCursor cur, CXCursor, CXClientData data) {
|
||||
auto& actual_cb = *static_cast<Func*>(data);
|
||||
actual_cb(cur);
|
||||
return CXChildVisit_Recurse;
|
||||
};
|
||||
|
||||
clang_visitChildren(parent, recurse ? recurse_lambda : continue_lambda, &f);
|
||||
}
|
||||
|
||||
// visits a translation unit
|
||||
|
|
|
|||
|
|
@ -12,7 +12,8 @@ using namespace cppast;
|
|||
|
||||
namespace
|
||||
{
|
||||
cpp_namespace::builder make_builder(const detail::parse_context& context, const CXCursor& cur)
|
||||
cpp_namespace::builder make_ns_builder(const detail::parse_context& context,
|
||||
const CXCursor& cur)
|
||||
{
|
||||
detail::tokenizer tokenizer(context.tu, context.file, cur);
|
||||
detail::token_stream stream(tokenizer, cur);
|
||||
|
|
@ -38,7 +39,7 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_namespace(const detail::parse_cont
|
|||
{
|
||||
DEBUG_ASSERT(cur.kind == CXCursor_Namespace, detail::assert_handler{});
|
||||
|
||||
auto builder = make_builder(context, cur);
|
||||
auto builder = make_ns_builder(context, cur);
|
||||
detail::visit_children(cur, [&](const CXCursor& cur) {
|
||||
auto entity = parse_entity(context, cur);
|
||||
if (entity)
|
||||
|
|
@ -46,3 +47,49 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_namespace(const detail::parse_cont
|
|||
});
|
||||
return builder.finish(*context.idx, get_entity_id(cur));
|
||||
}
|
||||
|
||||
namespace
|
||||
{
|
||||
cpp_entity_id ns_alias_target_id(const CXCursor& cur)
|
||||
{
|
||||
cpp_entity_id result("");
|
||||
detail::visit_children(cur,
|
||||
[&](const CXCursor& child) {
|
||||
auto referenced = clang_getCursorReferenced(child);
|
||||
auto kind = clang_getCursorKind(referenced);
|
||||
if (kind == CXCursor_Namespace)
|
||||
result = detail::get_entity_id(referenced);
|
||||
else if (kind == CXCursor_NamespaceAlias)
|
||||
// get target of namespace alias instead
|
||||
result = ns_alias_target_id(referenced);
|
||||
else
|
||||
DEBUG_UNREACHABLE(detail::parse_error_handler{}, cur,
|
||||
"unexpected child of namespace alias");
|
||||
},
|
||||
true);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<cpp_entity> detail::parse_cpp_namespace_alias(const detail::parse_context& context,
|
||||
const CXCursor& cur)
|
||||
{
|
||||
DEBUG_ASSERT(cur.kind == CXCursor_NamespaceAlias, detail::assert_handler{});
|
||||
|
||||
detail::tokenizer tokenizer(context.tu, context.file, cur);
|
||||
detail::token_stream stream(tokenizer, cur);
|
||||
|
||||
// namespace <identifier> = <nested identifier>;
|
||||
detail::skip(stream, "namespace");
|
||||
auto name = stream.get().c_str();
|
||||
detail::skip(stream, "=");
|
||||
|
||||
// <nested identifier>;
|
||||
std::string target_name;
|
||||
while (!detail::skip_if(stream, ";"))
|
||||
target_name += stream.get().c_str();
|
||||
|
||||
auto target = cpp_namespace_ref(ns_alias_target_id(cur), std::move(target_name));
|
||||
return cpp_namespace_alias::build(*context.idx, get_entity_id(cur), std::move(name),
|
||||
std::move(target));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,8 @@ std::unique_ptr<cpp_entity> detail::parse_entity(const detail::parse_context& co
|
|||
{
|
||||
case CXCursor_Namespace:
|
||||
return parse_cpp_namespace(context, cur);
|
||||
case CXCursor_NamespaceAlias:
|
||||
return parse_cpp_namespace_alias(context, cur);
|
||||
|
||||
default:
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -29,6 +29,9 @@ namespace cppast
|
|||
std::unique_ptr<cpp_entity> parse_cpp_namespace(const parse_context& context,
|
||||
const CXCursor& cur);
|
||||
|
||||
std::unique_ptr<cpp_entity> parse_cpp_namespace_alias(const parse_context& context,
|
||||
const CXCursor& cur);
|
||||
|
||||
std::unique_ptr<cpp_entity> parse_entity(const parse_context& context, const CXCursor& cur);
|
||||
}
|
||||
} // namespace cppast::detail
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue