-
-
Notifications
You must be signed in to change notification settings - Fork 42
Expand file tree
/
Copy pathUtils.cpp
More file actions
186 lines (172 loc) · 7.33 KB
/
Copy pathUtils.cpp
File metadata and controls
186 lines (172 loc) · 7.33 KB
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#include "Utils.h"
#include "TypeEntities.h"
namespace Meta {
bool areRecordFieldListsEqual(const std::vector<RecordField>& vector1, const std::vector<RecordField>& vector2)
{
if (vector1.size() != vector2.size()) {
return false;
}
for (std::vector<RecordField>::size_type i = 0; i < vector1.size(); i++) {
if ((vector1[i].name != vector2[i].name) || !Utils::areTypesEqual(*vector1[i].encoding, *vector2[i].encoding)) {
return false;
}
}
return true;
}
// TODO: This logic should be moved in types (and meta entities) entites
bool Utils::areTypesEqual(const Type& type1, const Type& type2)
{
if (type1.getType() != type2.getType())
return false;
switch (type1.getType()) {
case TypeType::TypeClass: {
const ClassType& classType1 = type1.as<ClassType>();
const ClassType& classType2 = type2.as<ClassType>();
return classType1.protocols == classType2.protocols;
}
case TypeType::TypeId: {
const IdType& idType1 = type1.as<IdType>();
const IdType& idType2 = type2.as<IdType>();
return idType1.protocols == idType2.protocols;
};
case TypeType::TypeConstantArray: {
const ConstantArrayType& arrayType1 = type1.as<ConstantArrayType>();
const ConstantArrayType& arrayType2 = type2.as<ConstantArrayType>();
return arrayType1.size == arrayType2.size && areTypesEqual(*arrayType1.innerType, *arrayType2.innerType);
};
case TypeType::TypeExtVector: {
const ExtVectorType& arrayType1 = type1.as<ExtVectorType>();
const ExtVectorType& arrayType2 = type2.as<ExtVectorType>();
return arrayType1.size == arrayType2.size && areTypesEqual(*arrayType1.innerType, *arrayType2.innerType);
};
case TypeType::TypeIncompleteArray: {
const IncompleteArrayType& arrayType1 = type1.as<IncompleteArrayType>();
const IncompleteArrayType& arrayType2 = type2.as<IncompleteArrayType>();
return areTypesEqual(*arrayType1.innerType, *arrayType2.innerType);
};
case TypeType::TypePointer: {
const PointerType& pointerType1 = type1.as<PointerType>();
const PointerType& pointerType2 = type2.as<PointerType>();
return areTypesEqual(*pointerType1.innerType, *pointerType2.innerType);
};
case TypeType::TypeBlock: {
const BlockType& blockType1 = type1.as<BlockType>();
const BlockType& blockType2 = type2.as<BlockType>();
return Utils::areTypesEqual(blockType1.signature, blockType2.signature);
};
case TypeType::TypeFunctionPointer: {
const FunctionPointerType& functionType1 = type1.as<FunctionPointerType>();
const FunctionPointerType& functionType2 = type2.as<FunctionPointerType>();
return Utils::areTypesEqual(functionType1.signature, functionType2.signature);
};
case TypeType::TypeInterface: {
const InterfaceType& interfaceType1 = type1.as<InterfaceType>();
const InterfaceType& interfaceType2 = type2.as<InterfaceType>();
return interfaceType1.interface == interfaceType2.interface && interfaceType1.protocols == interfaceType2.protocols;
};
case TypeType::TypeBridgedInterface: {
const BridgedInterfaceType& interfaceType1 = type1.as<BridgedInterfaceType>();
const BridgedInterfaceType& interfaceType2 = type2.as<BridgedInterfaceType>();
return interfaceType1.name == interfaceType2.name;
};
case TypeType::TypeStruct: {
const StructType& structType1 = type1.as<StructType>();
const StructType& structType2 = type2.as<StructType>();
return structType1.structMeta == structType2.structMeta;
};
case TypeType::TypeUnion: {
const UnionType& unionType1 = type1.as<UnionType>();
const UnionType& unionType2 = type2.as<UnionType>();
return unionType1.unionMeta == unionType2.unionMeta;
};
case TypeType::TypeAnonymousStruct: {
const AnonymousStructType& structType1 = type1.as<AnonymousStructType>();
const AnonymousStructType& structType2 = type2.as<AnonymousStructType>();
return areRecordFieldListsEqual(structType1.fields, structType2.fields);
};
case TypeType::TypeAnonymousUnion: {
const AnonymousUnionType& unionType1 = type1.as<AnonymousUnionType>();
const AnonymousUnionType& unionType2 = type2.as<AnonymousUnionType>();
return areRecordFieldListsEqual(unionType1.fields, unionType2.fields);
};
case TypeType::TypeTypeArgument: {
const TypeArgumentType& argType1 = type1.as<TypeArgumentType>();
const TypeArgumentType& argType2 = type2.as<TypeArgumentType>();
return areTypesEqual(*argType1.underlyingType, *argType2.underlyingType);
};
default: {
return true;
}
}
}
bool Utils::areTypesEqual(const std::vector<Type*>& vector1, const std::vector<Type*>& vector2)
{
if (vector1.size() != vector2.size()) {
return false;
}
for (std::vector<Type*>::size_type i = 0; i < vector1.size(); i++) {
if (!Utils::areTypesEqual(*vector1[i], *vector2[i])) {
return false;
}
}
return true;
}
static bool isalpha(const std::vector<std::string>& strings, size_t index)
{
for (auto& str : strings) {
if (!std::isalpha(str[index])) {
return false;
}
}
return true;
}
static std::string createValidPrefix(const std::vector<std::string>& fieldNames, const std::string& prefix)
{
if (!prefix.empty()) {
for (const std::string& field : fieldNames) {
if (std::isdigit(field[prefix.size()])) {
int newPrefixLength = prefix.size();
while (newPrefixLength > 0 && !std::isupper(field[newPrefixLength])) {
newPrefixLength--;
}
std::string newPrefix = prefix.substr(0, newPrefixLength);
return createValidPrefix(fieldNames, newPrefix);
}
}
bool allMembersStartWithUnderscore = true;
for (const std::string& field : fieldNames) {
if (field[prefix.size()] != '_') {
allMembersStartWithUnderscore = false;
break;
}
}
if (allMembersStartWithUnderscore) {
return createValidPrefix(fieldNames, prefix + '_');
}
}
return prefix;
}
std::string Utils::calculateEnumFieldsPrefix(const std::string& enumName, const std::vector<std::string>& fields)
{
for (size_t prefixLength = 0; prefixLength < enumName.size(); prefixLength++) {
char c = enumName[prefixLength];
for (size_t i = 0; i < fields.size(); i++) {
if (prefixLength >= fields[i].size() || fields[i][prefixLength] != c) {
while (prefixLength > 0 && (!std::isupper(fields[i][prefixLength]) || !isalpha(fields, prefixLength))) {
prefixLength--;
}
return createValidPrefix(fields, fields[i].substr(0, prefixLength));
}
}
}
return createValidPrefix(fields, enumName);
}
void Utils::getAllLinkLibraries(clang::Module* module, std::vector<clang::Module::LinkLibrary>& result)
{
for (clang::Module::LinkLibrary lib : module->LinkLibraries)
result.push_back(lib);
for (clang::Module::submodule_const_iterator it = module->submodule_begin();
it != module->submodule_end(); ++it)
getAllLinkLibraries(*it, result);
}
}