Register declarations as well
Will be overriden by definitions.
This commit is contained in:
parent
db0e649bf6
commit
cb9ad57e87
21 changed files with 98 additions and 43 deletions
|
|
@ -174,17 +174,13 @@ namespace cppast
|
||||||
/// \effects Registers the class in the [cppast::cpp_entity_index](),
|
/// \effects Registers the class in the [cppast::cpp_entity_index](),
|
||||||
/// using the given [cppast::cpp_entity_id]().
|
/// using the given [cppast::cpp_entity_id]().
|
||||||
/// \returns The finished class.
|
/// \returns The finished class.
|
||||||
std::unique_ptr<cpp_class> finish(const cpp_entity_index& idx,
|
std::unique_ptr<cpp_class> finish(const cpp_entity_index& idx, cpp_entity_id id);
|
||||||
cpp_entity_id id) noexcept;
|
|
||||||
|
|
||||||
/// \effects Marks the class as forward declaration.
|
/// \effects Marks the class as forward declaration.
|
||||||
/// \returns The finished class.
|
/// \returns The finished class.
|
||||||
/// \notes It will not be registered, as it is not the main definition.
|
/// \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
|
std::unique_ptr<cpp_class> finish_declaration(const cpp_entity_index& idx,
|
||||||
{
|
cpp_entity_id definition_id);
|
||||||
class_->set_definition(definition_id);
|
|
||||||
return std::move(class_);
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::unique_ptr<cpp_class> class_;
|
std::unique_ptr<cpp_class> class_;
|
||||||
|
|
|
||||||
|
|
@ -62,19 +62,40 @@ namespace cppast
|
||||||
class cpp_entity_index
|
class cpp_entity_index
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// \effects Registers a new [cppast::cpp_entity]() also giving its [cppast::cpp_entity_id]().
|
/// \effects Registers a new [cppast::cpp_entity]() which is a definition.
|
||||||
/// \requires The entity must not have been registered before,
|
/// It will override any previously registered declarations of the same entity.
|
||||||
/// and it must live as long as the index lives.
|
/// \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.
|
/// \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_);
|
std::lock_guard<std::mutex> lock(mutex_);
|
||||||
auto result = map_.emplace(std::move(id), std::move(entity));
|
auto result = map_.emplace(std::move(id), value(entity, true));
|
||||||
DEBUG_ASSERT(result.second, detail::precondition_error_handler{},
|
if (!result.second)
|
||||||
"duplicate index registration");
|
{
|
||||||
|
// 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]().
|
/// \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.
|
/// \notes This operation is thread safe.
|
||||||
type_safe::optional_ref<const cpp_entity> lookup(const cpp_entity_id& id) const noexcept
|
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);
|
auto iter = map_.find(id);
|
||||||
if (iter == map_.end())
|
if (iter == map_.end())
|
||||||
return {};
|
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:
|
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::mutex mutex_;
|
||||||
mutable std::unordered_map<cpp_entity_id, type_safe::object_ref<const cpp_entity>, hash>
|
mutable std::unordered_map<cpp_entity_id, value, hash> map_;
|
||||||
map_;
|
|
||||||
};
|
};
|
||||||
} // namespace cppast
|
} // namespace cppast
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -87,16 +87,17 @@ namespace cppast
|
||||||
/// \returns The finished enum.
|
/// \returns The finished enum.
|
||||||
std::unique_ptr<cpp_enum> finish(const cpp_entity_index& idx, cpp_entity_id id) noexcept
|
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_);
|
return std::move(enum_);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// \effects Marks the enum as forward declaration.
|
/// \effects Marks the enum as forward declaration.
|
||||||
/// \returns The finished enum.
|
/// \returns The finished enum.
|
||||||
/// \notes It will not be registered, as it is not the main definition.
|
std::unique_ptr<cpp_enum> finish_declaration(const cpp_entity_index& idx,
|
||||||
std::unique_ptr<cpp_enum> finish_declaration(cpp_entity_id definition_id) noexcept
|
cpp_entity_id definition_id) noexcept
|
||||||
{
|
{
|
||||||
enum_->set_definition(definition_id);
|
enum_->set_definition(definition_id);
|
||||||
|
idx.register_forward_declaration(std::move(definition_id), type_safe::ref(*enum_));
|
||||||
return std::move(enum_);
|
return std::move(enum_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -53,7 +53,7 @@ namespace cppast
|
||||||
/// \returns The finished file.
|
/// \returns The finished file.
|
||||||
std::unique_ptr<cpp_file> finish(const cpp_entity_index& idx) noexcept
|
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_);
|
return std::move(file_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -69,10 +69,13 @@ namespace cppast
|
||||||
type_safe::optional_ref<const T>>::type
|
type_safe::optional_ref<const T>>::type
|
||||||
{
|
{
|
||||||
if (!entity.definition())
|
if (!entity.definition())
|
||||||
|
// entity is definition itself
|
||||||
return type_safe::opt_cref(&entity);
|
return type_safe::opt_cref(&entity);
|
||||||
else
|
else
|
||||||
|
// entity is not a definition
|
||||||
|
// lookup the definition
|
||||||
return idx
|
return idx
|
||||||
.lookup(entity.definition().value())
|
.lookup_definition(entity.definition().value())
|
||||||
// downcast
|
// downcast
|
||||||
.map([](const cpp_entity& e) -> const T& {
|
.map([](const cpp_entity& e) -> const T& {
|
||||||
DEBUG_ASSERT(e.kind() == T::kind(), detail::assert_handler{});
|
DEBUG_ASSERT(e.kind() == T::kind(), detail::assert_handler{});
|
||||||
|
|
|
||||||
|
|
@ -136,9 +136,12 @@ namespace cppast
|
||||||
{
|
{
|
||||||
function->body_ = body_kind;
|
function->body_ = body_kind;
|
||||||
if (cppast::is_definition(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
|
else
|
||||||
|
{
|
||||||
function->set_definition(id);
|
function->set_definition(id);
|
||||||
|
idx.register_forward_declaration(std::move(id), type_safe::ref(*function));
|
||||||
|
}
|
||||||
return std::move(function);
|
return std::move(function);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -46,7 +46,7 @@ namespace cppast
|
||||||
/// \returns The finished namespace.
|
/// \returns The finished namespace.
|
||||||
std::unique_ptr<cpp_namespace> finish(const cpp_entity_index& idx, cpp_entity_id id)
|
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_);
|
return std::move(namespace_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,7 @@ namespace cppast
|
||||||
/// \returns The finished template.
|
/// \returns The finished template.
|
||||||
std::unique_ptr<T> finish(const cpp_entity_index& idx, cpp_entity_id id)
|
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);
|
return std::move(template_entity);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -206,7 +206,7 @@ namespace cppast
|
||||||
std::unique_ptr<cpp_template_template_parameter> finish(const cpp_entity_index& idx,
|
std::unique_ptr<cpp_template_template_parameter> finish(const cpp_entity_index& idx,
|
||||||
cpp_entity_id id)
|
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_);
|
return std::move(parameter_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,10 +9,17 @@
|
||||||
|
|
||||||
using namespace cppast;
|
using namespace cppast;
|
||||||
|
|
||||||
std::unique_ptr<cpp_class> cpp_class::builder::finish(const cpp_entity_index& idx,
|
std::unique_ptr<cpp_class> cpp_class::builder::finish(const cpp_entity_index& idx, cpp_entity_id id)
|
||||||
cpp_entity_id id) noexcept
|
|
||||||
{
|
{
|
||||||
idx.register_entity(std::move(id), type_safe::ref(*class_));
|
idx.register_definition(std::move(id), type_safe::ref(*class_));
|
||||||
|
return std::move(class_);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<cpp_class> cpp_class::builder::finish_declaration(const cpp_entity_index& idx,
|
||||||
|
cpp_entity_id definition_id)
|
||||||
|
{
|
||||||
|
class_->set_definition(definition_id);
|
||||||
|
idx.register_forward_declaration(std::move(definition_id), type_safe::ref(*class_));
|
||||||
return std::move(class_);
|
return std::move(class_);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ std::unique_ptr<cpp_enum_value> cpp_enum_value::build(const cpp_entity_index& id
|
||||||
{
|
{
|
||||||
auto result =
|
auto result =
|
||||||
std::unique_ptr<cpp_enum_value>(new cpp_enum_value(std::move(name), std::move(value)));
|
std::unique_ptr<cpp_enum_value>(new cpp_enum_value(std::move(name), std::move(value)));
|
||||||
idx.register_entity(std::move(id), type_safe::ref(*result));
|
idx.register_definition(std::move(id), type_safe::ref(*result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -19,7 +19,7 @@ std::unique_ptr<cpp_function_parameter> cpp_function_parameter::build(
|
||||||
{
|
{
|
||||||
auto result = std::unique_ptr<cpp_function_parameter>(
|
auto result = std::unique_ptr<cpp_function_parameter>(
|
||||||
new cpp_function_parameter(std::move(name), std::move(type), std::move(def)));
|
new cpp_function_parameter(std::move(name), std::move(type), std::move(def)));
|
||||||
idx.register_entity(std::move(id), type_safe::cref(*result));
|
idx.register_definition(std::move(id), type_safe::cref(*result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ std::unique_ptr<cpp_member_variable> cpp_member_variable::build(const cpp_entity
|
||||||
{
|
{
|
||||||
auto result = std::unique_ptr<cpp_member_variable>(
|
auto result = std::unique_ptr<cpp_member_variable>(
|
||||||
new cpp_member_variable(std::move(name), std::move(type), std::move(def), is_mutable));
|
new cpp_member_variable(std::move(name), std::move(type), std::move(def), is_mutable));
|
||||||
idx.register_entity(std::move(id), type_safe::cref(*result));
|
idx.register_definition(std::move(id), type_safe::cref(*result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -41,7 +41,7 @@ std::unique_ptr<cpp_bitfield> cpp_bitfield::build(const cpp_entity_index& idx, c
|
||||||
{
|
{
|
||||||
auto result = std::unique_ptr<cpp_bitfield>(
|
auto result = std::unique_ptr<cpp_bitfield>(
|
||||||
new cpp_bitfield(std::move(name), std::move(type), no_bits, is_mutable));
|
new cpp_bitfield(std::move(name), std::move(type), no_bits, is_mutable));
|
||||||
idx.register_entity(std::move(id), type_safe::cref(*result));
|
idx.register_definition(std::move(id), type_safe::cref(*result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ std::unique_ptr<cpp_namespace_alias> cpp_namespace_alias::build(const cpp_entity
|
||||||
{
|
{
|
||||||
auto ptr = std::unique_ptr<cpp_namespace_alias>(
|
auto ptr = std::unique_ptr<cpp_namespace_alias>(
|
||||||
new cpp_namespace_alias(std::move(name), std::move(target)));
|
new cpp_namespace_alias(std::move(name), std::move(target)));
|
||||||
idx.register_entity(std::move(id), type_safe::ref(*ptr));
|
idx.register_definition(std::move(id), type_safe::ref(*ptr));
|
||||||
return ptr;
|
return ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,7 @@ std::unique_ptr<cpp_template_type_parameter> cpp_template_type_parameter::build(
|
||||||
{
|
{
|
||||||
std::unique_ptr<cpp_template_type_parameter> result(
|
std::unique_ptr<cpp_template_type_parameter> result(
|
||||||
new cpp_template_type_parameter(std::move(name), kw, variadic, std::move(default_type)));
|
new cpp_template_type_parameter(std::move(name), kw, variadic, std::move(default_type)));
|
||||||
idx.register_entity(std::move(id), type_safe::cref(*result));
|
idx.register_definition(std::move(id), type_safe::cref(*result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -48,7 +48,7 @@ std::unique_ptr<cpp_non_type_template_parameter> cpp_non_type_template_parameter
|
||||||
std::unique_ptr<cpp_non_type_template_parameter> result(
|
std::unique_ptr<cpp_non_type_template_parameter> result(
|
||||||
new cpp_non_type_template_parameter(std::move(name), std::move(type), is_variadic,
|
new cpp_non_type_template_parameter(std::move(name), std::move(type), is_variadic,
|
||||||
std::move(default_value)));
|
std::move(default_value)));
|
||||||
idx.register_entity(std::move(id), type_safe::cref(*result));
|
idx.register_definition(std::move(id), type_safe::cref(*result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -18,7 +18,7 @@ std::unique_ptr<cpp_type_alias> cpp_type_alias::build(const cpp_entity_index& id
|
||||||
std::unique_ptr<cpp_type> type)
|
std::unique_ptr<cpp_type> type)
|
||||||
{
|
{
|
||||||
auto result = build(std::move(name), std::move(type));
|
auto result = build(std::move(name), std::move(type));
|
||||||
idx.register_entity(std::move(id), type_safe::cref(*result));
|
idx.register_definition(std::move(id), type_safe::cref(*result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,7 @@ std::unique_ptr<cpp_variable> cpp_variable::build(const cpp_entity_index& idx, c
|
||||||
{
|
{
|
||||||
auto result = std::unique_ptr<cpp_variable>(
|
auto result = std::unique_ptr<cpp_variable>(
|
||||||
new cpp_variable(std::move(name), std::move(type), std::move(def), spec, is_constexpr));
|
new cpp_variable(std::move(name), std::move(type), std::move(def), spec, is_constexpr));
|
||||||
idx.register_entity(std::move(id), type_safe::cref(*result));
|
idx.register_definition(std::move(id), type_safe::cref(*result));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -107,5 +107,5 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_class(const detail::parse_context&
|
||||||
if (clang_isCursorDefinition(cur))
|
if (clang_isCursorDefinition(cur))
|
||||||
return builder.finish(*context.idx, get_entity_id(cur));
|
return builder.finish(*context.idx, get_entity_id(cur));
|
||||||
else
|
else
|
||||||
return builder.finish_declaration(get_entity_id(cur));
|
return builder.finish_declaration(*context.idx, get_entity_id(cur));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -34,8 +34,9 @@ namespace
|
||||||
void detail::print_cursor_info(const CXCursor& cur) noexcept
|
void detail::print_cursor_info(const CXCursor& cur) noexcept
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx);
|
std::lock_guard<std::mutex> lock(mtx);
|
||||||
std::printf("[debug] cursor '%s' (%s)\n", get_display_name(cur).c_str(),
|
std::printf("[debug] cursor '%s' (%s): %s\n", get_display_name(cur).c_str(),
|
||||||
cxstring(clang_getCursorKindSpelling(cur.kind)).c_str());
|
cxstring(clang_getCursorKindSpelling(cur.kind)).c_str(),
|
||||||
|
cxstring(clang_getCursorUSR(cur)).c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
void detail::print_tokens(const CXTranslationUnit& tu, const CXFile& file,
|
void detail::print_tokens(const CXTranslationUnit& tu, const CXFile& file,
|
||||||
|
|
|
||||||
|
|
@ -85,5 +85,5 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_enum(const detail::parse_context&
|
||||||
if (clang_isCursorDefinition(cur))
|
if (clang_isCursorDefinition(cur))
|
||||||
return builder.finish(*context.idx, get_entity_id(cur));
|
return builder.finish(*context.idx, get_entity_id(cur));
|
||||||
else
|
else
|
||||||
return builder.finish_declaration(get_entity_id(cur));
|
return builder.finish_declaration(*context.idx, get_entity_id(cur));
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -176,7 +176,7 @@ namespace
|
||||||
DEBUG_ASSERT(allow_virtual, detail::parse_error_handler{}, stream.cursor(),
|
DEBUG_ASSERT(allow_virtual, detail::parse_error_handler{}, stream.cursor(),
|
||||||
"unexpected token");
|
"unexpected token");
|
||||||
if (result.virtual_keywords)
|
if (result.virtual_keywords)
|
||||||
result.virtual_keywords.value() &= cpp_virtual_flags::pure;
|
result.virtual_keywords.value() |= cpp_virtual_flags::pure;
|
||||||
else
|
else
|
||||||
result.virtual_keywords = cpp_virtual_flags::pure;
|
result.virtual_keywords = cpp_virtual_flags::pure;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue