Refactored template instantiation builder methods to a separate class

This commit is contained in:
Bartek Kryza
2023-04-15 22:11:45 +02:00
parent f034b554de
commit 758c1418a6
6 changed files with 1084 additions and 918 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,139 @@
/**
* src/class_diagram/visitor/template_builder.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "class_diagram/model/class.h"
#include "class_diagram/model/concept.h"
#include "class_diagram/model/diagram.h"
#include "common/visitor/ast_id_mapper.h"
#include "config/config.h"
namespace clanguml::class_diagram::visitor {
using class_diagram::model::class_;
using common::model::namespace_;
using common::model::relationship_t;
using common::model::template_parameter;
class template_builder {
public:
template_builder(class_diagram::model::diagram &d,
const config::class_diagram &config,
common::visitor::ast_id_mapper &id_mapper,
clang::SourceManager &source_manager);
class_diagram::model::diagram &diagram();
const config::class_diagram &config() const;
const namespace_ &using_namespace() const;
bool simplify_system_template(
template_parameter &ct, const std::string &full_name) const;
std::unique_ptr<clanguml::class_diagram::model::class_> build(
const clang::Decl *cls,
const clang::TemplateSpecializationType &template_type_decl,
std::optional<clanguml::class_diagram::model::class_ *> parent = {});
std::unique_ptr<clanguml::class_diagram::model::class_>
build_from_class_template_specialization(
const clang::ClassTemplateSpecializationDecl &template_specialization,
const clang::RecordType &record_type,
std::optional<clanguml::class_diagram::model::class_ *> parent = {});
bool add_base_classes(clanguml::class_diagram::model::class_ &tinst,
std::deque<std::tuple<std::string, int, bool>> &template_base_params,
int arg_index, bool variadic_params,
const clanguml::common::model::template_parameter &ct);
void process_template_arguments(
std::optional<clanguml::class_diagram::model::class_ *> &parent,
const clang::Decl *cls,
std::deque<std::tuple<std::string, int, bool>> &template_base_params,
const clang::ArrayRef<clang::TemplateArgument> &template_args,
model::class_ &template_instantiation,
const std::string &full_template_specialization_name,
const clang::TemplateDecl *template_decl);
void argument_process_dispatch(
std::optional<clanguml::class_diagram::model::class_ *> &parent,
const clang::Decl *cls, class_ &template_instantiation,
const std::string &full_template_specialization_name,
const clang::TemplateDecl *template_decl,
const clang::TemplateArgument &arg,
std::vector<template_parameter> &argument);
void process_tag_argument(model::class_ &template_instantiation,
const std::string &full_template_specialization_name,
const clang::TemplateDecl *template_decl,
const clang::TemplateArgument &arg,
common::model::template_parameter &argument);
template_parameter process_expression_argument(
const clang::TemplateArgument &arg);
template_parameter process_integral_argument(
const clang::TemplateArgument &arg);
template_parameter process_nullptr_argument(
const clang::TemplateArgument &arg);
template_parameter process_null_argument(
const clang::TemplateArgument &arg);
std::vector<template_parameter> process_pack_argument(
std::optional<clanguml::class_diagram::model::class_ *> &parent,
const clang::Decl *cls, class_ &template_instantiation,
const std::string &full_template_specialization_name,
const clang::TemplateDecl *template_decl,
const clang::TemplateArgument &arg,
std::vector<template_parameter> &argument);
template_parameter process_type_argument(
std::optional<clanguml::class_diagram::model::class_ *> &parent,
const clang::Decl *cls,
const std::string &full_template_specialization_name,
const clang::TemplateDecl *template_decl,
const clang::TemplateArgument &arg,
model::class_ &template_instantiation);
common::model::template_parameter process_template_argument(
const clang::TemplateArgument &arg);
std::optional<template_parameter>
get_template_argument_from_type_parameter_string(
const clang::Decl *decl, const std::string &return_type_name) const;
common::visitor::ast_id_mapper &id_mapper();
clang::SourceManager &source_manager() const;
private:
// Reference to the output diagram model
clanguml::class_diagram::model::diagram &diagram_;
// Reference to class diagram config
const clanguml::config::class_diagram &config_;
common::visitor::ast_id_mapper &id_mapper_;
clang::SourceManager &source_manager_;
};
}

File diff suppressed because it is too large Load Diff

View File

@@ -22,8 +22,10 @@
#include "class_diagram/model/diagram.h"
#include "common/model/enums.h"
#include "common/model/template_trait.h"
#include "common/visitor/ast_id_mapper.h"
#include "common/visitor/translation_unit_visitor.h"
#include "config/config.h"
#include "template_builder.h"
#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/Basic/SourceManager.h>
@@ -93,6 +95,11 @@ public:
*/
clanguml::class_diagram::model::diagram &diagram() { return diagram_; }
const clanguml::class_diagram::model::diagram &diagram() const
{
return diagram_;
}
/**
* @brief Get diagram config instance
*
@@ -151,12 +158,6 @@ private:
const clang::TemplateArgument &arg, size_t argument_index,
bool in_parameter_pack = false);
void process_template_record_containment(const clang::TagDecl &record,
clanguml::common::model::element &c) const;
void process_record_containment(const clang::TagDecl &record,
clanguml::common::model::element &c) const;
void process_method(const clang::CXXMethodDecl &mf,
clanguml::class_diagram::model::class_ &c);
@@ -186,66 +187,6 @@ private:
const found_relationships_t &relationships,
bool break_on_first_aggregation = false);
std::unique_ptr<clanguml::class_diagram::model::class_>
build_template_instantiation(const clang::Decl *cls,
const clang::TemplateSpecializationType &template_type,
std::optional<clanguml::class_diagram::model::class_ *> parent = {});
std::unique_ptr<clanguml::class_diagram::model::class_>
build_template_instantiation_from_class_template_specialization(
const clang::ClassTemplateSpecializationDecl &template_specialization,
const clang::RecordType &record_type,
std::optional<clanguml::class_diagram::model::class_ *> parent = {});
bool build_template_instantiation_add_base_classes(
clanguml::class_diagram::model::class_ &tinst,
std::deque<std::tuple<std::string, int, bool>> &template_base_params,
int arg_index, bool variadic_params,
const clanguml::common::model::template_parameter &ct) const;
void build_template_instantiation_process_template_arguments(
std::optional<clanguml::class_diagram::model::class_ *> &parent,
const clang::Decl *cls,
std::deque<std::tuple<std::string, int, bool>> &template_base_params,
const clang::ArrayRef<clang::TemplateArgument> &template_args,
model::class_ &template_instantiation,
const std::string &full_template_specialization_name,
const clang::TemplateDecl *template_decl);
void build_template_instantiation_process_tag_argument(
model::class_ &template_instantiation,
const std::string &full_template_specialization_name,
const clang::TemplateDecl *template_decl,
const clang::TemplateArgument &arg,
common::model::template_parameter &argument);
template_parameter build_template_instantiation_process_expression_argument(
const clang::TemplateArgument &arg) const;
template_parameter build_template_instantiation_process_integral_argument(
const clang::TemplateArgument &arg) const;
template_parameter build_template_instantiation_process_nullptr_argument(
const clang::TemplateArgument &arg) const;
template_parameter build_template_instantiation_process_null_argument(
const clang::TemplateArgument &arg) const;
template_parameter build_template_instantiation_process_pack_argument(
const clang::TemplateArgument &arg) const;
template_parameter build_template_instantiation_process_type_argument(
std::optional<clanguml::class_diagram::model::class_ *> &parent,
const clang::Decl *cls,
const std::string &full_template_specialization_name,
const clang::TemplateDecl *template_decl,
const clang::TemplateArgument &arg,
model::class_ &template_instantiation);
common::model::template_parameter
build_template_instantiation_process_template_argument(
const clang::TemplateArgument &arg) const;
void ensure_lambda_type_is_relative(std::string &parameter_type) const;
void process_record_parent(
@@ -290,10 +231,6 @@ private:
std::vector<std::string> &constrained_template_params,
size_t argument_index, std::string &type_name) const;
std::optional<template_parameter>
get_template_argument_from_type_parameter_string(
const clang::Decl *decl, const std::string &return_type_name) const;
/// Store the mapping from local clang entity id (obtained using
/// getID()) method to clang-uml global id
void set_ast_local_id(
@@ -303,9 +240,9 @@ private:
bool has_processed_template_class(const std::string &qualified_name) const;
/// Retrieve the global clang-uml entity id based on the clang local id
std::optional<common::model::diagram_element::id_t> get_ast_local_id(
int64_t local_id) const;
common::visitor::ast_id_mapper &id_mapper() const { return id_mapper_; }
template_builder &tbuilder() { return template_builder_; }
// Reference to the output diagram model
clanguml::class_diagram::model::diagram &diagram_;
@@ -313,12 +250,14 @@ private:
// Reference to class diagram config
const clanguml::config::class_diagram &config_;
mutable common::visitor::ast_id_mapper id_mapper_;
template_builder template_builder_;
std::map<common::model::diagram_element::id_t,
std::unique_ptr<clanguml::class_diagram::model::class_>>
forward_declarations_;
std::map<int64_t, common::model::diagram_element::id_t> local_ast_id_map_;
std::map<int64_t /* local anonymous struct id */,
std::tuple<std::string /* field name */, common::model::relationship_t,
common::model::access_t>>

View File

@@ -0,0 +1,36 @@
/**
* src/class_diagram/visitor/ast_id_mapper.cc
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ast_id_mapper.h"
namespace clanguml::common::visitor {
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)
{
if (id_map_.count(ast_id) == 0)
return {};
return id_map_.at(ast_id);
}
} // namespace clanguml::common::visitor

View File

@@ -0,0 +1,43 @@
/**
* src/class_diagram/visitor/ast_id_mapper.h
*
* Copyright (c) 2021-2023 Bartek Kryza <bkryza@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include "common/model/diagram_element.h"
#include <cstdint>
#include <map>
namespace clanguml::common::visitor {
class ast_id_mapper {
public:
using id_t = common::model::diagram_element::id_t;
ast_id_mapper() = default;
void add(int64_t ast_id, id_t global_id);
std::optional<id_t> get_global_id(int64_t ast_id);
private:
std::map</* Clang AST translation unit local id */ int64_t,
/* clang-uml global id */ id_t>
id_map_;
};
} // namespace clanguml::class_diagram::visitor