Add full_name() function

This commit is contained in:
Jonathan Müller 2017-01-21 22:59:38 +01:00
commit 0b88656cc2
5 changed files with 51 additions and 0 deletions

View file

@ -35,6 +35,13 @@ namespace cppast
return name_;
}
/// \returns The name of the new scope created by the entity,
/// if there is any.
type_safe::optional<std::string> scope_name() const
{
return do_get_scope_name();
}
/// \returns A [ts::optional_ref]() to the parent entity in the AST.
type_safe::optional_ref<const cpp_entity> parent() const noexcept
{
@ -51,6 +58,13 @@ namespace cppast
/// \returns The type of the entity.
virtual cpp_entity_type do_get_entity_type() const noexcept = 0;
/// \returns The name of the new scope created by the entity, if any.
/// By default, there is no scope created.
virtual type_safe::optional<std::string> do_get_scope_name() const
{
return type_safe::nullopt;
}
void on_insert(const cpp_entity& parent) noexcept
{
parent_ = parent;
@ -63,6 +77,9 @@ namespace cppast
friend struct detail::intrusive_list_access;
friend detail::intrusive_list_node<cpp_entity>;
};
/// \returns The full name of the [cppast::cpp_entity](), with all scopes.
std::string full_name(const cpp_entity& e);
} // namespace cppast
#endif // CPPAST_CPP_ENTITY_HPP_INCLUDED

View file

@ -106,6 +106,10 @@ namespace cppast
cpp_entity_type do_get_entity_type() const noexcept override;
/// \returns If the enum is scoped, the name of the enum,
/// otherwise [ts::nullopt]().
type_safe::optional<std::string> do_get_scope_name() const override;
std::unique_ptr<cpp_type> type_;
bool scoped_;
};

View file

@ -61,6 +61,12 @@ namespace cppast
cpp_entity_type do_get_entity_type() const noexcept override;
/// \returns The name of the namespace.
type_safe::optional<std::string> do_get_scope_name() const override
{
return name();
}
bool inline_;
};

19
src/cpp_entity.cpp Normal file
View file

@ -0,0 +1,19 @@
// 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.hpp>
using namespace cppast;
std::string cppast::full_name(const cpp_entity& e)
{
std::string scopes;
for (auto cur = e.parent(); cur; cur = cur.value().parent())
// prepend each scope, if there is any
type_safe::with(cur.value().scope_name(),
[&](const std::string& cur_scope) { scopes = cur_scope + "::" + scopes; });
return scopes + e.name();
}

View file

@ -17,3 +17,8 @@ cpp_entity_type cpp_enum::do_get_entity_type() const noexcept
{
return cpp_entity_type::enum_t;
}
type_safe::optional<std::string> cpp_enum::do_get_scope_name() const
{
return scoped_ ? type_safe::make_optional(name()) : type_safe::nullopt;
}