Refactoring template_parameter model
This commit is contained in:
@@ -44,15 +44,22 @@ void to_json(nlohmann::json &j, const element &c)
|
||||
|
||||
void to_json(nlohmann::json &j, const template_parameter &c)
|
||||
{
|
||||
j["type"] = c.type();
|
||||
j["name"] = c.name();
|
||||
if (!c.default_value().empty())
|
||||
j["default_value"] = c.default_value();
|
||||
j["is_template_parameter"] = c.is_template_parameter();
|
||||
j["is_template_template_parameter"] = c.is_template_template_parameter();
|
||||
if (const auto &constraint = c.concept_constraint(); constraint)
|
||||
j["concept_constraint"] = constraint.value();
|
||||
j["is_variadic"] = c.is_variadic();
|
||||
j["kind"] = to_string(c.kind());
|
||||
|
||||
if(c.kind() == template_parameter_kind_t::template_type) {
|
||||
j["name"] = c.name().value();
|
||||
}
|
||||
|
||||
|
||||
// j["type"] = c.type();
|
||||
// j["name"] = c.name();
|
||||
// if (!c.default_value().empty())
|
||||
// j["default_value"] = c.default_value();
|
||||
// j["is_template_parameter"] = c.is_template_parameter();
|
||||
// j["is_template_template_parameter"] = c.is_template_template_parameter();
|
||||
// if (const auto &constraint = c.concept_constraint(); constraint)
|
||||
// j["concept_constraint"] = constraint.value();
|
||||
// j["is_variadic"] = c.is_variadic();
|
||||
}
|
||||
|
||||
void to_json(nlohmann::json &j, const relationship &c)
|
||||
|
||||
@@ -949,12 +949,15 @@ bool translation_unit_visitor::process_template_parameters(
|
||||
nullptr) {
|
||||
const auto *template_type_parameter =
|
||||
clang::dyn_cast_or_null<clang::TemplateTypeParmDecl>(parameter);
|
||||
template_parameter ct;
|
||||
ct.set_type("");
|
||||
ct.is_template_parameter(true);
|
||||
ct.set_name(template_type_parameter->getNameAsString());
|
||||
ct.set_default_value("");
|
||||
ct.is_variadic(template_type_parameter->isParameterPack());
|
||||
|
||||
std::optional<std::string> default_arg;
|
||||
if (template_type_parameter->hasDefaultArgument()) {
|
||||
default_arg =
|
||||
template_type_parameter->getDefaultArgument().getAsString();
|
||||
}
|
||||
auto ct = template_parameter::make_template_type(
|
||||
template_type_parameter->getNameAsString(), default_arg,
|
||||
template_type_parameter->isParameterPack());
|
||||
|
||||
if (template_type_parameter->getTypeConstraint() != nullptr) {
|
||||
util::apply_if_not_null(
|
||||
@@ -970,7 +973,7 @@ bool translation_unit_visitor::process_template_parameters(
|
||||
{relationship_t::kConstraint,
|
||||
get_ast_local_id(named_concept->getID())
|
||||
.value(),
|
||||
access_t::kNone, ct.name()});
|
||||
access_t::kNone, ct.name().value()});
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -982,12 +985,14 @@ bool translation_unit_visitor::process_template_parameters(
|
||||
const auto *template_nontype_parameter =
|
||||
clang::dyn_cast_or_null<clang::NonTypeTemplateParmDecl>(
|
||||
parameter);
|
||||
template_parameter ct;
|
||||
ct.set_type(template_nontype_parameter->getType().getAsString());
|
||||
ct.set_name(template_nontype_parameter->getNameAsString());
|
||||
ct.is_template_parameter(false);
|
||||
ct.set_default_value("");
|
||||
ct.is_variadic(template_nontype_parameter->isParameterPack());
|
||||
std::optional<std::string> default_arg;
|
||||
if (template_nontype_parameter->hasDefaultArgument())
|
||||
default_arg = common::to_string(
|
||||
template_nontype_parameter->getDefaultArgument());
|
||||
auto ct = template_parameter::make_non_type_template(
|
||||
template_nontype_parameter->getType().getAsString(),
|
||||
template_nontype_parameter->getNameAsString(), default_arg,
|
||||
template_nontype_parameter->isParameterPack());
|
||||
|
||||
c.add_template(std::move(ct));
|
||||
}
|
||||
@@ -996,12 +1001,15 @@ bool translation_unit_visitor::process_template_parameters(
|
||||
const auto *template_template_parameter =
|
||||
clang::dyn_cast_or_null<clang::TemplateTemplateParmDecl>(
|
||||
parameter);
|
||||
template_parameter ct;
|
||||
ct.set_type("");
|
||||
ct.set_name(template_template_parameter->getNameAsString() + "<>");
|
||||
ct.is_template_parameter(true);
|
||||
ct.set_default_value("");
|
||||
ct.is_variadic(template_template_parameter->isParameterPack());
|
||||
std::optional<std::string> default_arg;
|
||||
if (template_template_parameter->hasDefaultArgument())
|
||||
default_arg = common::to_string(
|
||||
template_template_parameter->getDefaultArgument()
|
||||
.getArgument()
|
||||
.getAsExpr());
|
||||
auto ct = template_parameter::make_template_template_type(
|
||||
template_template_parameter->getNameAsString(), default_arg,
|
||||
template_template_parameter->isParameterPack());
|
||||
|
||||
c.add_template(std::move(ct));
|
||||
}
|
||||
@@ -1798,8 +1806,7 @@ void translation_unit_visitor::process_template_specialization_argument(
|
||||
const auto argument_kind = arg.getKind();
|
||||
|
||||
if (argument_kind == clang::TemplateArgument::Type) {
|
||||
template_parameter argument;
|
||||
argument.is_template_parameter(false);
|
||||
auto argument = template_parameter::make_argument({});
|
||||
|
||||
// If this is a nested template type - add nested templates as
|
||||
// template arguments
|
||||
@@ -1812,7 +1819,7 @@ void translation_unit_visitor::process_template_specialization_argument(
|
||||
.getAsTemplateDecl()
|
||||
->getQualifiedNameAsString();
|
||||
|
||||
argument.set_name(nested_template_name);
|
||||
argument.set_type(nested_template_name);
|
||||
|
||||
auto nested_template_instantiation = build_template_instantiation(
|
||||
*nested_template_type, {&template_instantiation});
|
||||
@@ -1821,12 +1828,6 @@ void translation_unit_visitor::process_template_specialization_argument(
|
||||
|
||||
for (const auto &t : nested_template_instantiation->templates())
|
||||
argument.add_template_param(t);
|
||||
|
||||
// Check if this template should be simplified (e.g. system
|
||||
// template aliases such as 'std:basic_string<char>' should be
|
||||
// simply 'std::string')
|
||||
simplify_system_template(argument,
|
||||
argument.to_string(config().using_namespace(), false));
|
||||
}
|
||||
else if (arg.getAsType()->getAs<clang::TemplateTypeParmType>() !=
|
||||
nullptr) {
|
||||
@@ -1857,7 +1858,7 @@ void translation_unit_visitor::process_template_specialization_argument(
|
||||
}
|
||||
}
|
||||
|
||||
argument.set_name(type_name);
|
||||
argument.set_type(type_name);
|
||||
}
|
||||
else {
|
||||
auto type_name =
|
||||
@@ -1879,7 +1880,7 @@ void translation_unit_visitor::process_template_specialization_argument(
|
||||
type_name.substr(0, type_name.find('<'));
|
||||
ensure_lambda_type_is_relative(unexposed_type_name);
|
||||
|
||||
argument.set_name(unexposed_type_name);
|
||||
argument.set_type(unexposed_type_name);
|
||||
}
|
||||
else if (type_name.find("type-parameter-") == 0) {
|
||||
auto declaration_text = common::get_source_text_raw(
|
||||
@@ -1903,10 +1904,10 @@ void translation_unit_visitor::process_template_specialization_argument(
|
||||
|
||||
// Otherwise just set the name for the template argument to
|
||||
// whatever clang says
|
||||
argument.set_name(type_name);
|
||||
argument.set_type(type_name);
|
||||
}
|
||||
else {
|
||||
argument.set_name(type_name);
|
||||
argument.set_type(type_name);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1919,23 +1920,18 @@ void translation_unit_visitor::process_template_specialization_argument(
|
||||
template_instantiation.add_template(std::move(argument));
|
||||
}
|
||||
else if (argument_kind == clang::TemplateArgument::Integral) {
|
||||
template_parameter argument;
|
||||
argument.is_template_parameter(false);
|
||||
argument.set_type(std::to_string(arg.getAsIntegral().getExtValue()));
|
||||
auto argument = template_parameter::make_argument(
|
||||
std::to_string(arg.getAsIntegral().getExtValue()));
|
||||
template_instantiation.add_template(std::move(argument));
|
||||
}
|
||||
else if (argument_kind == clang::TemplateArgument::Expression) {
|
||||
template_parameter argument;
|
||||
argument.is_template_parameter(false);
|
||||
argument.set_type(common::get_source_text(
|
||||
arg.getAsExpr()->getSourceRange(), source_manager()));
|
||||
auto argument =
|
||||
template_parameter::make_argument(common::get_source_text(
|
||||
arg.getAsExpr()->getSourceRange(), source_manager()));
|
||||
template_instantiation.add_template(std::move(argument));
|
||||
}
|
||||
else if (argument_kind == clang::TemplateArgument::TemplateExpansion) {
|
||||
template_parameter argument;
|
||||
argument.is_template_parameter(true);
|
||||
|
||||
cls->getLocation().dump(source_manager());
|
||||
// TODO
|
||||
}
|
||||
else if (argument_kind == clang::TemplateArgument::Pack) {
|
||||
// This will only work for now if pack is at the end
|
||||
@@ -1973,17 +1969,18 @@ void translation_unit_visitor::
|
||||
bool translation_unit_visitor::find_relationships_in_unexposed_template_params(
|
||||
const template_parameter &ct, found_relationships_t &relationships)
|
||||
{
|
||||
assert(ct.type());
|
||||
|
||||
bool found{false};
|
||||
LOG_DBG("Finding relationships in user defined type: {}",
|
||||
ct.to_string(config().using_namespace(), false));
|
||||
|
||||
// auto type_with_namespace = ctx.get_name_with_namespace(ct.type());
|
||||
auto type_with_namespace =
|
||||
std::make_optional<common::model::namespace_>(ct.type());
|
||||
std::make_optional<common::model::namespace_>(ct.type().value());
|
||||
|
||||
if (!type_with_namespace.has_value()) {
|
||||
// Couldn't find declaration of this type
|
||||
type_with_namespace = common::model::namespace_{ct.type()};
|
||||
type_with_namespace = common::model::namespace_{ct.type().value()};
|
||||
}
|
||||
|
||||
auto element_opt = diagram().get(type_with_namespace.value().to_string());
|
||||
@@ -2293,26 +2290,27 @@ void translation_unit_visitor::
|
||||
auto arg_index = 0;
|
||||
for (const auto &arg : template_args) {
|
||||
const auto argument_kind = arg.getKind();
|
||||
template_parameter argument;
|
||||
std::optional<template_parameter> argument;
|
||||
if (argument_kind == clang::TemplateArgument::Template) {
|
||||
build_template_instantiation_process_template_argument(
|
||||
arg, argument);
|
||||
argument =
|
||||
build_template_instantiation_process_template_argument(arg);
|
||||
}
|
||||
else if (argument_kind == clang::TemplateArgument::Type) {
|
||||
build_template_instantiation_process_type_argument(parent,
|
||||
full_template_specialization_name, template_decl, arg,
|
||||
template_instantiation, argument);
|
||||
argument = build_template_instantiation_process_type_argument(
|
||||
parent, full_template_specialization_name, template_decl, arg,
|
||||
template_instantiation);
|
||||
}
|
||||
else if (argument_kind == clang::TemplateArgument::Integral) {
|
||||
build_template_instantiation_process_integral_argument(
|
||||
arg, argument);
|
||||
argument =
|
||||
build_template_instantiation_process_integral_argument(arg);
|
||||
}
|
||||
else if (argument_kind == clang::TemplateArgument::Expression) {
|
||||
build_template_instantiation_process_expression_argument(
|
||||
arg, argument);
|
||||
argument =
|
||||
build_template_instantiation_process_expression_argument(arg);
|
||||
}
|
||||
else {
|
||||
LOG_ERROR("Unsupported argument type {}", arg.getKind());
|
||||
continue;
|
||||
}
|
||||
|
||||
// We can figure this only when we encounter variadic param in
|
||||
@@ -2326,43 +2324,42 @@ void translation_unit_visitor::
|
||||
if (!template_base_params.empty()) {
|
||||
variadic_params = build_template_instantiation_add_base_classes(
|
||||
template_instantiation, template_base_params, arg_index,
|
||||
variadic_params, argument);
|
||||
variadic_params, argument.value());
|
||||
}
|
||||
|
||||
LOG_DBG("Adding template argument {} to template "
|
||||
"specialization/instantiation {}",
|
||||
argument.name(), template_instantiation.name());
|
||||
argument.value().to_string(config().using_namespace(), false),
|
||||
template_instantiation.name());
|
||||
|
||||
simplify_system_template(
|
||||
argument, argument.to_string(config().using_namespace(), false));
|
||||
simplify_system_template(argument.value(),
|
||||
argument.value().to_string(config().using_namespace(), false));
|
||||
|
||||
template_instantiation.add_template(std::move(argument));
|
||||
template_instantiation.add_template(std::move(argument.value()));
|
||||
|
||||
arg_index++;
|
||||
}
|
||||
}
|
||||
|
||||
void translation_unit_visitor::
|
||||
template_parameter translation_unit_visitor::
|
||||
build_template_instantiation_process_template_argument(
|
||||
const clang::TemplateArgument &arg, template_parameter &argument) const
|
||||
const clang::TemplateArgument &arg) const
|
||||
{
|
||||
argument.is_template_parameter(true);
|
||||
auto arg_name =
|
||||
arg.getAsTemplate().getAsTemplateDecl()->getQualifiedNameAsString();
|
||||
argument.set_type(arg_name);
|
||||
return template_parameter::make_template_type(arg_name);
|
||||
}
|
||||
|
||||
void translation_unit_visitor::
|
||||
build_template_instantiation_process_type_argument(
|
||||
std::optional<clanguml::class_diagram::model::class_ *> &parent,
|
||||
const std::string &full_template_specialization_name,
|
||||
const clang::TemplateDecl *template_decl,
|
||||
const clang::TemplateArgument &arg, class_ &template_instantiation,
|
||||
template_parameter &argument)
|
||||
template_parameter
|
||||
translation_unit_visitor::build_template_instantiation_process_type_argument(
|
||||
std::optional<clanguml::class_diagram::model::class_ *> &parent,
|
||||
const std::string &full_template_specialization_name,
|
||||
const clang::TemplateDecl *template_decl,
|
||||
const clang::TemplateArgument &arg, class_ &template_instantiation)
|
||||
{
|
||||
assert(arg.getKind() == clang::TemplateArgument::Type);
|
||||
|
||||
argument.is_template_parameter(false);
|
||||
auto argument = template_parameter::make_argument({});
|
||||
|
||||
// If this is a nested template type - add nested templates as
|
||||
// template arguments
|
||||
@@ -2414,12 +2411,11 @@ void translation_unit_visitor::
|
||||
arg.getAsType()->getAs<clang::TemplateSpecializationType>();
|
||||
nested_template_type != nullptr) {
|
||||
|
||||
const auto nested_template_name =
|
||||
nested_template_type->getTemplateName()
|
||||
.getAsTemplateDecl()
|
||||
->getQualifiedNameAsString();
|
||||
const auto nested_type_name = nested_template_type->getTemplateName()
|
||||
.getAsTemplateDecl()
|
||||
->getQualifiedNameAsString();
|
||||
|
||||
argument.set_name(nested_template_name);
|
||||
argument.set_type(nested_type_name);
|
||||
|
||||
auto nested_template_instantiation =
|
||||
build_template_instantiation(*nested_template_type,
|
||||
@@ -2462,7 +2458,7 @@ void translation_unit_visitor::
|
||||
}
|
||||
else if (arg.getAsType()->getAs<clang::TemplateTypeParmType>() != nullptr) {
|
||||
argument.is_template_parameter(true);
|
||||
argument.set_name(
|
||||
argument.set_type(
|
||||
common::to_string(arg.getAsType(), template_decl->getASTContext()));
|
||||
}
|
||||
else {
|
||||
@@ -2471,26 +2467,26 @@ void translation_unit_visitor::
|
||||
template_instantiation, full_template_specialization_name,
|
||||
template_decl, arg, argument);
|
||||
}
|
||||
|
||||
return argument;
|
||||
}
|
||||
|
||||
void translation_unit_visitor::
|
||||
template_parameter translation_unit_visitor::
|
||||
build_template_instantiation_process_integral_argument(
|
||||
const clang::TemplateArgument &arg, template_parameter &argument) const
|
||||
const clang::TemplateArgument &arg) const
|
||||
{
|
||||
assert(arg.getKind() == clang::TemplateArgument::Integral);
|
||||
|
||||
argument.is_template_parameter(false);
|
||||
argument.set_type(std::to_string(arg.getAsIntegral().getExtValue()));
|
||||
return template_parameter::make_argument(
|
||||
std::to_string(arg.getAsIntegral().getExtValue()));
|
||||
}
|
||||
|
||||
void translation_unit_visitor::
|
||||
template_parameter translation_unit_visitor::
|
||||
build_template_instantiation_process_expression_argument(
|
||||
const clang::TemplateArgument &arg, template_parameter &argument) const
|
||||
const clang::TemplateArgument &arg) const
|
||||
{
|
||||
assert(arg.getKind() == clang::TemplateArgument::Expression);
|
||||
|
||||
argument.is_template_parameter(false);
|
||||
argument.set_type(common::get_source_text(
|
||||
return template_parameter::make_argument(common::get_source_text(
|
||||
arg.getAsExpr()->getSourceRange(), source_manager()));
|
||||
}
|
||||
|
||||
@@ -2505,7 +2501,7 @@ void translation_unit_visitor::
|
||||
|
||||
argument.is_template_parameter(false);
|
||||
|
||||
argument.set_name(
|
||||
argument.set_type(
|
||||
common::to_string(arg.getAsType(), template_decl->getASTContext()));
|
||||
|
||||
if (const auto *record_type = arg.getAsType()->getAs<clang::RecordType>();
|
||||
@@ -2688,7 +2684,7 @@ void translation_unit_visitor::process_field(
|
||||
for (const auto &template_argument :
|
||||
template_specialization.templates()) {
|
||||
|
||||
LOG_DBG("Looking for nested relationships from {}:{} in "
|
||||
LOG_DBG("Looking for nested relationships from {}::{} in "
|
||||
"template {}",
|
||||
c.full_name(false), field_name,
|
||||
template_argument.to_string(
|
||||
@@ -2792,7 +2788,7 @@ bool translation_unit_visitor::simplify_system_template(
|
||||
template_parameter &ct, const std::string &full_name) const
|
||||
{
|
||||
if (config().type_aliases().count(full_name) > 0) {
|
||||
ct.set_name(config().type_aliases().at(full_name));
|
||||
ct.set_type(config().type_aliases().at(full_name));
|
||||
ct.clear_params();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -218,25 +218,22 @@ private:
|
||||
const clang::TemplateArgument &arg,
|
||||
common::model::template_parameter &argument);
|
||||
|
||||
void build_template_instantiation_process_expression_argument(
|
||||
const clang::TemplateArgument &arg,
|
||||
common::model::template_parameter &argument) const;
|
||||
template_parameter build_template_instantiation_process_expression_argument(
|
||||
const clang::TemplateArgument &arg) const;
|
||||
|
||||
void build_template_instantiation_process_integral_argument(
|
||||
const clang::TemplateArgument &arg,
|
||||
common::model::template_parameter &argument) const;
|
||||
template_parameter build_template_instantiation_process_integral_argument(
|
||||
const clang::TemplateArgument &arg) const;
|
||||
|
||||
void build_template_instantiation_process_type_argument(
|
||||
template_parameter build_template_instantiation_process_type_argument(
|
||||
std::optional<clanguml::class_diagram::model::class_ *> &parent,
|
||||
const std::string &full_template_specialization_name,
|
||||
const clang::TemplateDecl *template_decl,
|
||||
const clang::TemplateArgument &arg,
|
||||
model::class_ &template_instantiation,
|
||||
common::model::template_parameter &argument);
|
||||
model::class_ &template_instantiation);
|
||||
|
||||
void build_template_instantiation_process_template_argument(
|
||||
const clang::TemplateArgument &arg,
|
||||
common::model::template_parameter &argument) const;
|
||||
common::model::template_parameter
|
||||
build_template_instantiation_process_template_argument(
|
||||
const clang::TemplateArgument &arg) const;
|
||||
|
||||
void ensure_lambda_type_is_relative(std::string ¶meter_type) const;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user