Register declarations as well

Will be overriden by definitions.
This commit is contained in:
Jonathan Müller 2017-03-18 11:01:27 +01:00
commit cb9ad57e87
21 changed files with 98 additions and 43 deletions

View file

@ -174,17 +174,13 @@ namespace cppast
/// \effects Registers the class in the [cppast::cpp_entity_index](),
/// using the given [cppast::cpp_entity_id]().
/// \returns The finished class.
std::unique_ptr<cpp_class> finish(const cpp_entity_index& idx,
cpp_entity_id id) noexcept;
std::unique_ptr<cpp_class> finish(const cpp_entity_index& idx, cpp_entity_id id);
/// \effects Marks the class as forward declaration.
/// \returns The finished class.
/// \notes It will not be registered, as it is not the main definition.
std::unique_ptr<cpp_class> finish_declaration(cpp_entity_id definition_id) noexcept
{
class_->set_definition(definition_id);
return std::move(class_);
}
std::unique_ptr<cpp_class> finish_declaration(const cpp_entity_index& idx,
cpp_entity_id definition_id);
private:
std::unique_ptr<cpp_class> class_;

View file

@ -62,19 +62,40 @@ namespace cppast
class cpp_entity_index
{
public:
/// \effects Registers a new [cppast::cpp_entity]() also giving its [cppast::cpp_entity_id]().
/// \requires The entity must not have been registered before,
/// and it must live as long as the index lives.
/// \effects Registers a new [cppast::cpp_entity]() which is a definition.
/// 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.
/// \notes This operation is thread safe.
void register_entity(cpp_entity_id id, type_safe::object_ref<const cpp_entity> entity) const
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), std::move(entity));
DEBUG_ASSERT(result.second, detail::precondition_error_handler{},
"duplicate index registration");
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;
}
}
/// \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.
/// \notes This operation is thread safe.
type_safe::optional_ref<const cpp_entity> lookup(const cpp_entity_id& id) const noexcept
{
@ -82,7 +103,20 @@ namespace cppast
auto iter = map_.find(id);
if (iter == map_.end())
return {};
return iter->second.get();
return iter->second.entity.get();
}
/// \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();
}
private:
@ -94,9 +128,19 @@ namespace cppast
}
};
struct value
{
type_safe::object_ref<const cpp_entity> entity;
bool is_definition;
value(type_safe::object_ref<const cpp_entity> e, bool def)
: entity(std::move(e)), is_definition(def)
{
}
};
mutable std::mutex mutex_;
mutable std::unordered_map<cpp_entity_id, type_safe::object_ref<const cpp_entity>, hash>
map_;
mutable std::unordered_map<cpp_entity_id, value, hash> map_;
};
} // namespace cppast

View file

@ -87,16 +87,17 @@ namespace cppast
/// \returns The finished enum.
std::unique_ptr<cpp_enum> finish(const cpp_entity_index& idx, cpp_entity_id id) noexcept
{
idx.register_entity(std::move(id), type_safe::ref(*enum_));
idx.register_definition(std::move(id), type_safe::ref(*enum_));
return std::move(enum_);
}
/// \effects Marks the enum as forward declaration.
/// \returns The finished enum.
/// \notes It will not be registered, as it is not the main definition.
std::unique_ptr<cpp_enum> finish_declaration(cpp_entity_id definition_id) noexcept
std::unique_ptr<cpp_enum> finish_declaration(const cpp_entity_index& idx,
cpp_entity_id definition_id) noexcept
{
enum_->set_definition(definition_id);
idx.register_forward_declaration(std::move(definition_id), type_safe::ref(*enum_));
return std::move(enum_);
}

View file

@ -53,7 +53,7 @@ namespace cppast
/// \returns The finished file.
std::unique_ptr<cpp_file> finish(const cpp_entity_index& idx) noexcept
{
idx.register_entity(cpp_entity_id(file_->name()), type_safe::ref(*file_));
idx.register_definition(cpp_entity_id(file_->name()), type_safe::ref(*file_));
return std::move(file_);
}

View file

@ -69,10 +69,13 @@ namespace cppast
type_safe::optional_ref<const T>>::type
{
if (!entity.definition())
// entity is definition itself
return type_safe::opt_cref(&entity);
else
// entity is not a definition
// lookup the definition
return idx
.lookup(entity.definition().value())
.lookup_definition(entity.definition().value())
// downcast
.map([](const cpp_entity& e) -> const T& {
DEBUG_ASSERT(e.kind() == T::kind(), detail::assert_handler{});

View file

@ -136,9 +136,12 @@ namespace cppast
{
function->body_ = body_kind;
if (cppast::is_definition(body_kind))
idx.register_entity(std::move(id), type_safe::cref(*function));
idx.register_definition(std::move(id), type_safe::ref(*function));
else
{
function->set_definition(id);
idx.register_forward_declaration(std::move(id), type_safe::ref(*function));
}
return std::move(function);
}

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_entity(std::move(id), type_safe::ref(*namespace_));
idx.register_definition(std::move(id), type_safe::ref(*namespace_));
return std::move(namespace_);
}

View file

@ -50,7 +50,7 @@ namespace cppast
/// \returns The finished template.
std::unique_ptr<T> finish(const cpp_entity_index& idx, cpp_entity_id id)
{
idx.register_entity(std::move(id), type_safe::cref(*template_entity));
idx.register_definition(std::move(id), type_safe::cref(*template_entity));
return std::move(template_entity);
}

View file

@ -206,7 +206,7 @@ namespace cppast
std::unique_ptr<cpp_template_template_parameter> finish(const cpp_entity_index& idx,
cpp_entity_id id)
{
idx.register_entity(std::move(id), type_safe::ref(*parameter_));
idx.register_definition(std::move(id), type_safe::ref(*parameter_));
return std::move(parameter_);
}