Add support for multiple namespace entities with the same name

This commit is contained in:
Jonathan Müller 2017-03-22 12:09:13 +01:00
commit d21d017d8a
13 changed files with 207 additions and 203 deletions

View file

@ -107,7 +107,7 @@ namespace cppast
void on_insert(const cpp_entity& parent) noexcept
{
parent_ = parent;
parent_ = type_safe::ref(parent);
}
std::string name_;

View file

@ -5,19 +5,19 @@
#ifndef CPPAST_CPP_ENTITY_INDEX_HPP_INCLUDED
#define CPPAST_CPP_ENTITY_INDEX_HPP_INCLUDED
#include <unordered_map>
#include <mutex>
#include <string>
#include <unordered_map>
#include <vector>
#include <type_safe/optional_ref.hpp>
#include <type_safe/reference.hpp>
#include <type_safe/strong_typedef.hpp>
#include <cppast/detail/assert.hpp>
namespace cppast
{
class cpp_entity;
class cpp_namespace;
/// \exclude
namespace detail
@ -36,7 +36,7 @@ namespace cppast
///
/// It is comparable for equality.
struct cpp_entity_id : type_safe::strong_typedef<cpp_entity_id, std::size_t>,
type_safe::strong_typedef_op::equality_comparison<cpp_entity_id, bool>
type_safe::strong_typedef_op::equality_comparison<cpp_entity_id>
{
explicit cpp_entity_id(const std::string& str) : cpp_entity_id(str.c_str())
{
@ -66,58 +66,42 @@ namespace cppast
/// It will override any previously registered declarations of the same entity.
/// \requires If the entity has been registered before, it must be as declaration,
/// and the entity must live as long as the index lives.
/// \requires The entity must not be a namespace.
/// \notes This operation is thread safe.
void register_definition(cpp_entity_id id,
type_safe::object_ref<const cpp_entity> entity) const
{
std::lock_guard<std::mutex> lock(mutex_);
auto result = map_.emplace(std::move(id), value(entity, true));
if (!result.second)
{
// already in map, override declaration
auto& value = result.first->second;
DEBUG_ASSERT(!value.is_definition, detail::precondition_error_handler{},
"duplicate entity registration");
value.is_definition = true;
value.entity = entity;
}
}
type_safe::object_ref<const cpp_entity> entity) const;
/// \effects Registers a new [cppast::cpp_entity]() which is a declaration.
/// Only the first declaration will be registered.
/// \requires The entity must live as long as the index lives.
/// \notes This operaiton is thread safe.
void register_forward_declaration(cpp_entity_id id,
type_safe::object_ref<const cpp_entity> entity) const
{
std::lock_guard<std::mutex> lock(mutex_);
map_.emplace(std::move(id), value(entity, false));
}
/// \returns A [ts::optional_ref]() corresponding to the entity of the given [cppast::cpp_entity_id]().
/// If no definition has been registered, it return the first declaration that was registered.
/// \requires The entity must not be a namespace.
/// \notes This operation is thread safe.
type_safe::optional_ref<const cpp_entity> lookup(const cpp_entity_id& id) const noexcept
{
std::lock_guard<std::mutex> lock(mutex_);
auto iter = map_.find(id);
if (iter == map_.end())
return {};
return iter->second.entity.get();
}
void register_forward_declaration(cpp_entity_id id,
type_safe::object_ref<const cpp_entity> entity) const;
/// \effects Registers a new [cppast::cpp_namespace]().
/// \notes The namespace object must live as long as the index lives.
/// \notes This operation is thread safe.
void register_namespace(cpp_entity_id id,
type_safe::object_ref<const cpp_namespace> ns) const;
/// \returns A [ts::optional_ref]() corresponding to the entity(/ies) of the given [cppast::cpp_entity_id]().
/// If no definition has been registered, it return the first declaration that was registered.
/// If the id resolves to a namespaces, returns an empty optional.
/// \notes This operation is thread safe.
type_safe::optional_ref<const cpp_entity> lookup(const cpp_entity_id& id) const noexcept;
/// \returns A [ts::optional_ref]() corresponding to the entity of the given [cppast::cpp_entity_id]().
/// If no definition has been registered, it returns an empty optional.
/// \notes This operation is thread safe.
type_safe::optional_ref<const cpp_entity> lookup_definition(const cpp_entity_id& id) const
noexcept
{
std::lock_guard<std::mutex> lock(mutex_);
auto iter = map_.find(id);
if (iter == map_.end() || !iter->second.is_definition)
return {};
return iter->second.entity.get();
}
noexcept;
/// \returns A [ts::array_ref]() of references to all namespaces matching the given [cppast::cpp_entity_id]().
/// If no namespace is found, it returns an empty array reference.
/// \notes This operation is thread safe.
auto lookup_namespace(const cpp_entity_id& id) const noexcept
-> type_safe::array_ref<type_safe::object_ref<const cpp_namespace>>;
private:
struct hash
@ -141,6 +125,9 @@ namespace cppast
mutable std::mutex mutex_;
mutable std::unordered_map<cpp_entity_id, value, hash> map_;
mutable std::unordered_map<cpp_entity_id,
std::vector<type_safe::object_ref<const cpp_namespace>>, hash>
ns_;
};
} // namespace cppast

View file

@ -70,110 +70,43 @@ namespace cppast
}
}
private:
class ref_impl
{
public:
ref_impl(type_safe::object_ref<const cpp_entity_index> idx,
type_safe::array_ref<const cpp_entity_id> ids)
: ids_(ids), index_(idx)
{
}
class iterator
{
public:
using value_type = type_safe::optional_ref<const T>;
using reference = value_type;
using pointer = value_type*;
using difference_type = std::ptrdiff_t;
using iterator_category = std::forward_iterator_tag;
reference operator*() const noexcept
{
return (*ref_)[i_];
}
pointer operator->() const noexcept
{
return &(*ref_)[i_];
}
iterator& operator++() noexcept
{
++i_;
return *this;
}
iterator operator++(int)noexcept
{
auto tmp = *this;
++(*this);
return tmp;
}
friend bool operator==(const iterator& a, const iterator& b) noexcept
{
return a.ref_ == b.ref_ && a.i_ == b.i_;
}
friend bool operator!=(const iterator& a, const iterator& b) noexcept
{
return !(a == b);
}
private:
iterator(type_safe::object_ref<const ref_impl> ref, type_safe::index_t i)
: ref_(ref), i_(i)
{
}
type_safe::object_ref<const ref_impl> ref_;
type_safe::index_t i_;
friend ref_impl;
};
iterator begin() const noexcept
{
return iterator(type_safe::ref(*this), 0u);
}
iterator end() const noexcept
{
return iterator(type_safe::ref(*this), size());
}
type_safe::optional_ref<const T> operator[](type_safe::index_t i) const
{
return index_->lookup(ids_[i]).map([](const cpp_entity& e) -> const T& {
DEBUG_ASSERT(Predicate{}(e), detail::precondition_error_handler{},
"predicate not fulfilled");
return static_cast<const T&>(e);
});
}
type_safe::size_t size() const noexcept
{
return ids_.size();
}
private:
type_safe::array_ref<const cpp_entity_id> ids_;
type_safe::object_ref<const cpp_entity_index> index_;
};
public:
/// \returns An array reference to the entities it refers to.
/// The return type provides `operator[]` + `size()`,
/// as well as `begin()` and `end()` returning forward iterators.
/// \exclude return
ref_impl get(const cpp_entity_index& idx) const noexcept
std::vector<type_safe::object_ref<const T>> get(const cpp_entity_index& idx) const
{
return ref_impl(type_safe::ref(idx), id());
std::vector<type_safe::object_ref<const T>> result;
get_impl(std::is_convertible<cpp_namespace&, T&>{}, result, idx);
return result;
}
private:
void get_impl(std::true_type, std::vector<type_safe::object_ref<const T>>& result,
const cpp_entity_index& idx) const
{
for (auto& cur : id())
for (auto& ns : idx.lookup_namespace(cur))
result.push_back(ns);
if (!std::is_same<T, cpp_namespace>::value)
get_impl(std::false_type{}, result, idx);
}
void get_impl(std::false_type, std::vector<type_safe::object_ref<const T>>& result,
const cpp_entity_index& idx) const
{
for (auto& cur : id())
{
auto entity = idx.lookup(cur).map([](const cpp_entity& e) {
DEBUG_ASSERT(Predicate{}(e), detail::precondition_error_handler{},
"invalid entity type");
return type_safe::ref(static_cast<const T&>(e));
});
if (entity)
result.push_back(type_safe::ref(entity.value()));
}
}
type_safe::variant<cpp_entity_id, std::vector<cpp_entity_id>> target_;
std::string name_;
};

View file

@ -77,9 +77,9 @@ namespace cppast
return idx
.lookup_definition(entity.definition().value())
// downcast
.map([](const cpp_entity& e) -> const T& {
.map([](const cpp_entity& e) {
DEBUG_ASSERT(e.kind() == T::kind(), detail::assert_handler{});
return static_cast<const T&>(e);
return type_safe::ref(static_cast<const T&>(e));
});
}

View file

@ -46,7 +46,7 @@ namespace cppast
/// \returns The finished namespace.
std::unique_ptr<cpp_namespace> finish(const cpp_entity_index& idx, cpp_entity_id id)
{
idx.register_definition(std::move(id), type_safe::ref(*namespace_));
idx.register_namespace(std::move(id), type_safe::ref(*namespace_));
return std::move(namespace_);
}

View file

@ -26,7 +26,7 @@ namespace cppast
/// \returns A [ts::optional_ref]() to the [cppast::cpp_expression]() that is the default value.
type_safe::optional_ref<const cpp_expression> default_value() const noexcept
{
return *default_;
return type_safe::ref(*default_);
}
protected:

View file

@ -130,7 +130,7 @@ namespace cppast
if (last_)
{
auto ptr = intrusive_list_access<T>::set_next(last_.value(), std::move(obj));
last_ = *ptr;
last_ = type_safe::ref(*ptr);
}
else
{