-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinitial_pass_compos.cpp
138 lines (114 loc) · 4.81 KB
/
initial_pass_compos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include "initial_pass.h"
#include <iostream>
#include <vector>
#include <string>
std::string FindPrefixName(const kslicer::ClassInfo& mainClassInfo, const kslicer::ClassInfo& apiClassInfo)
{
std::string prefixName = "";
std::vector<std::string> allowedNames = {
apiClassInfo.name,
apiClassInfo.name + "*",
std::string("std::shared_ptr<") + apiClassInfo.name + ">",
std::string("std::unique_ptr<") + apiClassInfo.name + ">"
};
for(const auto& member : mainClassInfo.dataMembers) {
for(const auto& possName : allowedNames)
if(member.second.type == possName)
prefixName = member.second.name;
}
return prefixName;
}
std::string kslicer::PerformClassComposition(kslicer::ClassInfo& mainClassInfo, const kslicer::ClassInfo& apiClassInfo, const kslicer::ClassInfo& implClassInfo)
{
// (1) find member with type of apiClassInfo.name
//
std::string prefixName = FindPrefixName(mainClassInfo, apiClassInfo);
if(prefixName == "")
return "";
// (2) merge data and functions (dataMembers, allMemberFunctions)
//
for(auto member : implClassInfo.dataMembers) {
member.second.name = prefixName + "_" + member.second.name;
member.second.hasPrefix = true;
member.second.prefixName = prefixName;
mainClassInfo.dataMembers[member.second.name] = member.second;
}
for(auto member : implClassInfo.allMemberFunctions) {
std::string name = prefixName + "_" + member.first;
mainClassInfo.allMemberFunctions[name] = member.second;
}
return prefixName;
}
void kslicer::PerformInheritanceMerge(kslicer::ClassInfo& mainClassInfo, const kslicer::ClassInfo& baseClassInfo)
{
// (2) merge data and functions (dataMembers, allMemberFunctions)
//
for(auto member : baseClassInfo.dataMembers)
{
member.second.name = member.second.name;
member.second.hasPrefix = false;
member.second.prefixName = "";
mainClassInfo.dataMembers[member.second.name] = member.second;
}
for(auto member : baseClassInfo.allMemberFunctions)
{
std::string name = member.first;
auto p = mainClassInfo.allMemberFunctions.find(name);
if(p == mainClassInfo.allMemberFunctions.end()) // because implementation in main (derived) class
mainClassInfo.allMemberFunctions[name] = member.second; // always overrides any implementations in base class
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
std::vector<std::string> kslicer::GetBaseClassesNames(const clang::CXXRecordDecl* mainClassASTNode)
{
std::vector<std::string> baseClassNames;
return baseClassNames;
/*
if(mainClassASTNode == nullptr)
return baseClassNames;
std::cout << "[GetBaseClassesNames]: (0): ok, pointer = " << mainClassASTNode << std::endl;
auto name = mainClassASTNode->getName().str();
std::cout << "[GetBaseClassesNames]: (1): ok, pointer = " << mainClassASTNode << std::endl;
std::cout << "[MainFuncSeeker]: find main class " << name.c_str() << ", name = " << name.c_str() << std::endl;
// Итерируемся по базовым классам
for (const auto& base : mainClassASTNode->bases()) {
std::cout << "[GetBaseClassesNames]: (1): ok " << std::endl;
const auto baseT = base.getType();
std::cout << "[GetBaseClassesNames]: (2): ok " << std::endl;
const clang::CXXRecordDecl* baseDecl = baseT->getAsCXXRecordDecl();
std::cout << "[GetBaseClassesNames]: (3): ok " << std::endl;
if (baseDecl)
baseClassNames.push_back(baseDecl->getNameAsString());
}
return baseClassNames;
*/
}
namespace kslicer
{
// Helper function to get the depth of a class in the inheritance hierarchy
int GetClassDepth(const clang::CXXRecordDecl* derived)
{
int depth = 0;
while (!derived->bases().empty()) {
derived = derived->bases_begin()->getType()->getAsCXXRecordDecl();
depth++;
}
return depth;
}
}
std::vector<const clang::CXXRecordDecl*> kslicer::ExtractAndSortBaseClasses(const std::vector<const clang::CXXRecordDecl*>& classes, const clang::CXXRecordDecl* derived)
{
std::vector<const clang::CXXRecordDecl*> result;
result.reserve(classes.size());
for(size_t i=0;i<classes.size();i++)
if(derived->isDerivedFrom(classes[i]))
result.push_back(classes[i]);
std::unordered_map<const clang::CXXRecordDecl*, int> depth;
for(auto node : result)
depth[node] = GetClassDepth(node);
std::sort(result.begin(), result.end(), [&](const clang::CXXRecordDecl* a, const clang::CXXRecordDecl* b) { return depth[a] > depth[b]; });
return result;
}