Initial refactor of id_t to a separate class

This commit is contained in:
Bartek Kryza
2024-06-04 00:03:26 +02:00
parent e21c2d2b14
commit cf79b3184c
37 changed files with 374 additions and 248 deletions

View File

@@ -415,49 +415,49 @@ bool is_subexpr_of(const clang::Stmt *parent_stmt, const clang::Stmt *sub_stmt)
[sub_stmt](const auto *e) { return is_subexpr_of(e, sub_stmt); });
}
template <> id_t to_id(const std::string &full_name)
template <> common::id_t to_id(const std::string &full_name)
{
return static_cast<id_t>(std::hash<std::string>{}(full_name) >> 3U);
return static_cast<common::id_t>(std::hash<std::string>{}(full_name));
}
id_t to_id(const clang::QualType &type, const clang::ASTContext &ctx)
common::id_t to_id(const clang::QualType &type, const clang::ASTContext &ctx)
{
return to_id(common::to_string(type, ctx));
}
template <> id_t to_id(const clang::NamespaceDecl &declaration)
template <> common::id_t to_id(const clang::NamespaceDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> id_t to_id(const clang::RecordDecl &declaration)
template <> common::id_t to_id(const clang::RecordDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> id_t to_id(const clang::EnumDecl &declaration)
template <> common::id_t to_id(const clang::EnumDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> id_t to_id(const clang::TagDecl &declaration)
template <> common::id_t to_id(const clang::TagDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> id_t to_id(const clang::CXXRecordDecl &declaration)
template <> common::id_t to_id(const clang::CXXRecordDecl &declaration)
{
return to_id(get_qualified_name(declaration));
}
template <> id_t to_id(const clang::EnumType &t) { return to_id(*t.getDecl()); }
template <> common::id_t to_id(const clang::EnumType &t) { return to_id(*t.getDecl()); }
template <> id_t to_id(const std::filesystem::path &file)
template <> common::id_t to_id(const std::filesystem::path &file)
{
return to_id(file.lexically_normal().string());
}
template <> id_t to_id(const clang::TemplateArgument &template_argument)
template <> common::id_t to_id(const clang::TemplateArgument &template_argument)
{
if (template_argument.getKind() == clang::TemplateArgument::Type) {
if (const auto *enum_type =

View File

@@ -178,27 +178,27 @@ bool is_subexpr_of(const clang::Stmt *parent_stmt, const clang::Stmt *sub_stmt);
*
* @{
*/
template <typename T> id_t to_id(const T &declaration);
template <typename T> common::id_t to_id(const T &declaration);
template <> id_t to_id(const std::string &full_name);
template <> common::id_t to_id(const std::string &full_name);
id_t to_id(const clang::QualType &type, const clang::ASTContext &ctx);
common::id_t to_id(const clang::QualType &type, const clang::ASTContext &ctx);
template <> id_t to_id(const clang::NamespaceDecl &declaration);
template <> common::id_t to_id(const clang::NamespaceDecl &declaration);
template <> id_t to_id(const clang::CXXRecordDecl &declaration);
template <> common::id_t to_id(const clang::CXXRecordDecl &declaration);
template <> id_t to_id(const clang::RecordDecl &declaration);
template <> common::id_t to_id(const clang::RecordDecl &declaration);
template <> id_t to_id(const clang::EnumDecl &declaration);
template <> common::id_t to_id(const clang::EnumDecl &declaration);
template <> id_t to_id(const clang::TagDecl &declaration);
template <> common::id_t to_id(const clang::TagDecl &declaration);
template <> id_t to_id(const clang::EnumType &type);
template <> common::id_t to_id(const clang::EnumType &type);
template <> id_t to_id(const clang::TemplateSpecializationType &type);
template <> common::id_t to_id(const clang::TemplateSpecializationType &type);
template <> id_t to_id(const std::filesystem::path &type);
template <> common::id_t to_id(const std::filesystem::path &type);
/** @} */ // end of to_id
/**

View File

@@ -31,7 +31,7 @@ void to_json(nlohmann::json &j, const source_location &sl)
void to_json(nlohmann::json &j, const element &c)
{
j = json{{"id", std::to_string(c.id())},
j = json{{"id", std::to_string(c.id().value())},
{"name", common::generators::json::render_name(c.name())},
{"namespace", c.get_namespace().to_string()}, {"type", c.type_name()},
{"display_name",
@@ -62,7 +62,7 @@ void to_json(nlohmann::json &j, const template_parameter &c)
void to_json(nlohmann::json &j, const relationship &c)
{
j["type"] = to_string(c.type());
j["destination"] = std::to_string(c.destination());
j["destination"] = std::to_string(c.destination().value());
if (!c.multiplicity_source().empty())
j["multiplicity_source"] = c.multiplicity_source();
if (!c.multiplicity_destination().empty())

View File

@@ -26,7 +26,7 @@ namespace clanguml::common::model {
diagram_element::diagram_element() = default;
common::id_t diagram_element::id() const { return id_; }
const common::id_t & diagram_element::id() const { return id_; }
void diagram_element::set_id(common::id_t id) { id_ = id; }
@@ -42,9 +42,9 @@ void diagram_element::set_parent_element_id(common::id_t id)
std::string diagram_element::alias() const
{
assert(id_ >= 0);
assert(id_.value() >= 0);
return fmt::format("C_{:022}", id_);
return fmt::format("C_{:022}", id_.value());
}
void diagram_element::add_relationship(relationship &&cr)

View File

@@ -53,7 +53,7 @@ public:
*
* @return Elements id.
*/
common::id_t id() const;
const common::id_t& id() const;
/**
* Set elements id.
@@ -185,8 +185,8 @@ public:
void complete(bool completed);
private:
id_t id_{0};
std::optional<id_t> parent_element_id_{0};
id_t id_{};
std::optional<common::id_t> parent_element_id_{};
std::string name_;
std::vector<relationship> relationships_;
bool nested_{false};

View File

@@ -55,7 +55,8 @@ template <typename ElementT, typename DiagramT>
const clanguml::common::optional_ref<ElementT> get(
const DiagramT &d, const std::string &full_name);
template <typename ElementT> int64_t destination_comparator(const ElementT &e)
template <typename ElementT>
common::id_t destination_comparator(const ElementT &e)
{
return e.id();
}

View File

@@ -91,7 +91,7 @@ struct hash<std::reference_wrapper<clanguml::common::model::package>> {
{
using clanguml::common::id_t;
return std::hash<id_t>{}(key.get().id());
return std::hash<id_t::type>{}(key.get().id().value());
}
};
} // namespace std

View File

@@ -22,7 +22,7 @@
namespace clanguml::common::model {
relationship::relationship(relationship_t type, int64_t destination,
relationship::relationship(relationship_t type, common::id_t destination,
access_t access, std::string label, std::string multiplicity_source,
std::string multiplicity_destination)
: type_{type}
@@ -38,7 +38,7 @@ void relationship::set_type(relationship_t type) noexcept { type_ = type; }
relationship_t relationship::type() const noexcept { return type_; }
void relationship::set_destination(int64_t destination)
void relationship::set_destination(common::id_t destination)
{
destination_ = destination;
}

View File

@@ -74,14 +74,14 @@ public:
*
* @param destination Target element id.
*/
void set_destination(int64_t destination);
void set_destination(common::id_t destination);
/**
* Get the id of the target element of this relationship.
*
* @return Target element id.
*/
clanguml::common::id_t destination() const;
common::id_t destination() const;
/**
* Set the relationship multiplicity at the source.

View File

@@ -212,7 +212,7 @@ struct hash<std::reference_wrapper<clanguml::common::model::source_file>> {
{
using clanguml::common::id_t;
return std::hash<id_t>{}(key.get().id());
return std::hash<id_t::type>{}(key.get().id().value());
}
};
} // namespace std

View File

@@ -505,7 +505,7 @@ std::string template_parameter::to_string(
}
bool template_parameter::find_nested_relationships(
std::vector<std::pair<int64_t, common::model::relationship_t>>
std::vector<std::pair<common::id_t, common::model::relationship_t>>
&nested_relationships,
common::model::relationship_t hint,
const std::function<bool(const std::string &full_name)> &should_include)

View File

@@ -19,6 +19,7 @@
#include "common/model/enums.h"
#include "common/model/namespace.h"
#include "common/types.h"
#include <deque>
#include <optional>
@@ -165,14 +166,14 @@ public:
*
* @param id Id of parameter
*/
void set_id(const int64_t id) { id_ = id; }
void set_id(const common::id_t &id) { id_ = id; }
/**
* Get id of the template parameter
*
* @return Id of the template parameter
*/
const std::optional<int64_t> &id() const { return id_; }
const std::optional<common::id_t> &id() const { return id_; }
/**
* Set the name of the template parameter
@@ -388,7 +389,7 @@ public:
* @return
*/
bool find_nested_relationships(
std::vector<std::pair<int64_t, common::model::relationship_t>>
std::vector<std::pair<common::id_t, common::model::relationship_t>>
&nested_relationships,
common::model::relationship_t hint,
const std::function<bool(const std::string &full_name)> &should_include)
@@ -532,7 +533,7 @@ private:
*/
std::vector<template_parameter> template_params_;
std::optional<int64_t> id_;
std::optional<common::id_t> id_;
bool is_unexposed_{false};
};

View File

@@ -29,7 +29,87 @@
namespace clanguml::common {
using id_t = int64_t;
class id_t {
public:
using type = uint64_t;
id_t()
: value_{0ULL}
, is_global_{true}
{
}
explicit id_t(int64_t id)
: value_{static_cast<type>(id)}
, is_global_{false}
{
}
explicit id_t(type id)
: value_{id}
, is_global_{true}
{
}
id_t(const id_t &) = default;
id_t(id_t &&) noexcept = default;
id_t &operator=(const id_t &) = default;
id_t &operator=(id_t &&) noexcept = default;
id_t &operator=(int64_t ast_id)
{
assert(!is_global_);
value_ = static_cast<uint64_t>(ast_id);
return *this;
}
~id_t() = default;
bool is_global() const { return is_global_; }
friend bool operator==(const id_t &lhs, const id_t &rhs)
{
return (lhs.is_global_ == rhs.is_global_) && (lhs.value_ == rhs.value_);
}
friend bool operator==(const id_t &lhs, const uint64_t &v)
{
return lhs.value_ == v;
}
friend bool operator!=(const id_t &lhs, const uint64_t &v)
{
return lhs.value_ != v;
}
friend bool operator!=(const id_t &lhs, const id_t &rhs)
{
return !(lhs == rhs);
}
friend bool operator<(const id_t &lhs, const id_t &rhs)
{
if (lhs.is_global_ != rhs.is_global_) {
return lhs.value_ < rhs.value_ + 1;
}
return lhs.value_ <
rhs.value_; // Compare values if is_global_ are the same
}
type value() const { return value_; }
int64_t ast_local_value() const
{
assert(!is_global_);
return static_cast<int64_t>(value_);
}
private:
type value_;
bool is_global_;
};
/**
* Type of output diagram format generator.
@@ -292,4 +372,14 @@ using namespace_or_regex = common::or_regex<common::model::namespace_>;
struct path_or_regex : public or_regex<std::filesystem::path> { };
} // namespace clanguml::common
} // namespace clanguml::common
template <> class fmt::formatter<clanguml::common::id_t> {
public:
constexpr auto parse(format_parse_context &ctx) { return ctx.begin(); }
template <typename Context>
constexpr auto format(clanguml::common::id_t const &id, Context &ctx) const
{
return format_to(ctx.out(), "{}", id.value());
}
};

View File

@@ -25,12 +25,17 @@ void ast_id_mapper::add(int64_t ast_id, id_t global_id)
id_map_.emplace(ast_id, global_id);
}
std::optional<id_t> ast_id_mapper::get_global_id(int64_t ast_id)
std::optional<id_t> ast_id_mapper::get_global_id(common::id_t ast_id)
{
if (id_map_.count(ast_id) == 0)
assert(!ast_id.is_global());
if (ast_id.is_global())
return {};
return id_map_.at(ast_id);
if (id_map_.count(ast_id.ast_local_value()) == 0)
return {};
return id_map_.at(ast_id.ast_local_value());
}
} // namespace clanguml::common::visitor

View File

@@ -58,7 +58,7 @@ public:
* @param ast_id Clang's local AST id.
* @return Global id, if exists.
*/
std::optional<id_t> get_global_id(int64_t ast_id);
std::optional<id_t> get_global_id(common::id_t ast_id);
private:
std::map</* Clang AST translation unit local id */ int64_t,

View File

@@ -661,8 +661,8 @@ void template_builder<VisitorT>::build_from_template_declaration(
templated_element.value().add_relationship(
{relationship_t::kConstraint,
id_mapper()
.get_global_id(
named_concept->getID())
.get_global_id(common::id_t{
named_concept->getID()})
.value(),
model::access_t::kNone,
ct.name().value()});
@@ -865,7 +865,8 @@ void template_builder<VisitorT>::build(
if constexpr (std::is_same_v<typename VisitorT::diagram_t,
class_diagram::model::diagram>) {
find_instantiation_relationships(template_instantiation,
template_decl->getID(), full_template_specialization_name);
common::id_t{template_decl->getID()},
full_template_specialization_name);
}
template_instantiation.set_id(
@@ -910,7 +911,7 @@ void template_builder<VisitorT>::build_from_class_template_specialization(
if constexpr (std::is_same_v<typename VisitorT::diagram_t,
class_diagram::model::diagram>) {
find_instantiation_relationships(template_instantiation,
template_specialization.getID(), qualified_name);
common::id_t{template_specialization.getID()}, qualified_name);
}
visitor_.set_source_location(*template_decl, template_instantiation);