-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathStruct.cpp
354 lines (329 loc) · 14.3 KB
/
Struct.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#include "Struct.h"
#include "../Utils.h"
#include "Union.h"
#include "types/ArrayType.h"
#include "types/FunctionPointerType.h"
#include "types/PointerType.h"
#include "types/PrimitiveType.h"
#include <sstream>
Struct::Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
uint64_t typeSize, std::shared_ptr<Location> location,
bool isPacked, bool isBitField)
: Record(std::move(name), std::move(fields), std::move(location)),
typeSize(typeSize), isPacked(isPacked), hasBitField(isBitField) {}
std::shared_ptr<TypeDef> Struct::generateTypeDef() {
if (isRepresentedAsStruct()) {
return std::make_shared<TypeDef>(getTypeName(), shared_from_this(),
nullptr);
} else {
// There is no easy way to represent it as a struct in scala native,
// have to represent it as an array and then Add helpers to help with
// its manipulation
return std::make_shared<TypeDef>(
getTypeName(),
std::make_shared<ArrayType>(std::make_shared<PrimitiveType>("Byte"),
typeSize),
location);
}
}
std::string
Struct::generateHelperClass(const LocationManager &locationManager) const {
assert(hasHelperMethods());
std::stringstream s;
std::string type = replaceChar(getTypeName(), " ", "_");
s << " implicit class " << type << "_ops(val p: native.Ptr[" << type
<< "])"
<< " extends AnyVal {\n";
if (isRepresentedAsStruct()) {
s << generateHelperClassMethodsForStructRepresentation(locationManager);
} else {
s << generateHelperClassMethodsForArrayRepresentation(locationManager);
}
s << " }\n";
/* makes struct instantiation easier */
s << " def "
<< type + "()(implicit z: native.Zone): native.Ptr[" + type + "]"
<< " = native.alloc[" + type + "]\n";
return s.str();
}
bool Struct::hasHelperMethods() const {
if (hasBitField) {
return false;
}
if (!isRepresentedAsStruct()) {
return !fields.empty();
}
return !isPacked && !fields.empty();
}
std::string Struct::generateHelperClassMethodsForStructRepresentation(
const LocationManager &locationManager) const {
std::stringstream s;
for (unsigned fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) {
if (!fields[fieldIndex]->getName().empty()) {
s << generateGetterForStructRepresentation(fieldIndex,
locationManager);
s << generateSetterForStructRepresentation(fieldIndex,
locationManager);
}
}
return s.str();
}
std::string Struct::generateHelperClassMethodsForArrayRepresentation(
const LocationManager &locationManager) const {
std::stringstream s;
for (unsigned fieldIndex = 0; fieldIndex < fields.size(); fieldIndex++) {
if (!fields[fieldIndex]->getName().empty()) {
s << generateGetterForArrayRepresentation(fieldIndex,
locationManager);
s << generateSetterForArrayRepresentation(fieldIndex,
locationManager);
}
}
return s.str();
}
std::string Struct::getTypeName() const { return "struct " + name; }
std::string Struct::str(const LocationManager &locationManager) const {
std::stringstream ss;
ss << "native.CStruct" << std::to_string(fields.size()) << "[";
std::string sep = "";
for (const auto &field : fields) {
ss << sep;
std::vector<std::shared_ptr<const Struct>>
structTypesThatShouldBeReplaced = shouldFieldBreakCycle(field);
if (structTypesThatShouldBeReplaced.empty()) {
ss << field->getType()->str(locationManager);
} else {
/* field type is changed to avoid cyclic types in generated code */
std::shared_ptr<const Type> typeReplacement = getTypeReplacement(
field->getType(), structTypesThatShouldBeReplaced);
ss << typeReplacement->str(locationManager);
}
sep = ", ";
}
ss << "]";
return ss.str();
}
bool Struct::operator==(const Type &other) const {
if (this == &other) {
return true;
}
auto *s = dynamic_cast<const Struct *>(&other);
if (s) {
/* structs have unique names */
return name == s->name;
}
return false;
}
std::string Struct::generateSetterForStructRepresentation(
unsigned fieldIndex, const LocationManager &locationManager) const {
std::shared_ptr<Field> field = fields[fieldIndex];
std::string setter = handleReservedWords(field->getName(), "_=");
std::string parameterType = field->getType()->str(locationManager);
std::string value = "value";
std::vector<std::shared_ptr<const Struct>> structTypesThatShouldBeReplaced =
shouldFieldBreakCycle(field);
if (!structTypesThatShouldBeReplaced.empty()) {
/* field type is changed to avoid cyclic types in generated code */
std::shared_ptr<const Type> typeReplacement = getTypeReplacement(
field->getType(), structTypesThatShouldBeReplaced);
value = value + ".cast[" + typeReplacement->str(locationManager) + "]";
} else if (isAliasForType<ArrayType>(field->getType().get()) ||
isAliasForType<Struct>(field->getType().get())) {
parameterType = "native.Ptr[" + parameterType + "]";
value = "!" + value;
}
std::stringstream s;
s << " def " << setter << "(value: " + parameterType + "): Unit = !p._"
<< std::to_string(fieldIndex + 1) << " = " << value << "\n";
return s.str();
}
std::string Struct::generateGetterForStructRepresentation(
unsigned fieldIndex, const LocationManager &locationManager) const {
std::shared_ptr<Field> field = fields[fieldIndex];
std::string getter = handleReservedWords(field->getName());
std::string returnType = field->getType()->str(locationManager);
std::string methodBody = "p._" + std::to_string(fieldIndex + 1);
if (isAliasForType<ArrayType>(field->getType().get()) ||
isAliasForType<Struct>(field->getType().get())) {
returnType = "native.Ptr[" + returnType + "]";
} else if (!shouldFieldBreakCycle(field).empty()) {
/* field type is changed to avoid cyclic types in generated code */
methodBody = "(!" + methodBody + ").cast[" +
field->getType()->str(locationManager) + "]";
} else {
methodBody = "!" + methodBody;
}
std::stringstream s;
s << " def " << getter << ": " << returnType << " = " << methodBody
<< "\n";
return s.str();
}
bool Struct::isRepresentedAsStruct() const {
return fields.size() <= SCALA_NATIVE_MAX_STRUCT_FIELDS && !hasBitField;
}
std::string Struct::generateSetterForArrayRepresentation(
unsigned int fieldIndex, const LocationManager &locationManager) const {
std::shared_ptr<Field> field = fields[fieldIndex];
std::string setter = handleReservedWords(field->getName(), "_=");
std::string parameterType = field->getType()->str(locationManager);
std::string value = "value";
std::string castedField = "p._1";
PointerType pointerToFieldType = PointerType(field->getType());
uint64_t offsetInBytes = field->getOffsetInBits() / 8;
if (offsetInBytes > 0) {
castedField =
"(" + castedField + " + " + std::to_string(offsetInBytes) + ")";
}
castedField = "!" + castedField + ".cast[" +
pointerToFieldType.str(locationManager) + "]";
std::vector<std::shared_ptr<const Struct>> structTypesThatShouldBeReplaced =
shouldFieldBreakCycle(field);
if (!structTypesThatShouldBeReplaced.empty()) {
/* field type is changed to avoid cyclic types in generated code */
std::shared_ptr<const Type> typeReplacement = getTypeReplacement(
field->getType(), structTypesThatShouldBeReplaced);
value = value + ".cast[" + typeReplacement->str(locationManager) + "]";
} else if (isAliasForType<ArrayType>(field->getType().get()) ||
isAliasForType<Struct>(field->getType().get())) {
parameterType = pointerToFieldType.str(locationManager);
value = "!" + value;
}
std::stringstream s;
s << " def " << setter
<< "(value: " + parameterType + "): Unit = " << castedField << " = "
<< value << "\n";
return s.str();
}
std::string Struct::generateGetterForArrayRepresentation(
unsigned fieldIndex, const LocationManager &locationManager) const {
std::shared_ptr<Field> field = fields[fieldIndex];
std::string getter = handleReservedWords(field->getName());
std::string returnType;
std::string methodBody;
PointerType pointerToFieldType = PointerType(field->getType());
uint64_t offsetInBytes = field->getOffsetInBits() / 8;
if (offsetInBytes > 0) {
methodBody = "(p._1 + " + std::to_string(offsetInBytes) + ")";
} else {
methodBody = "p._1";
}
methodBody =
methodBody + ".cast[" + pointerToFieldType.str(locationManager) + "]";
if (isAliasForType<ArrayType>(field->getType().get()) ||
isAliasForType<Struct>(field->getType().get())) {
returnType = pointerToFieldType.str(locationManager);
} else if (!shouldFieldBreakCycle(field).empty()) {
/* field type is changed to avoid cyclic types in generated code */
methodBody = "(!" + methodBody + ").cast[" +
field->getType()->str(locationManager) + "]";
returnType = field->getType()->str(locationManager);
} else {
methodBody = "!" + methodBody;
returnType = field->getType()->str(locationManager);
}
std::stringstream s;
s << " def " << getter << ": " << returnType << " = " << methodBody
<< "\n";
return s.str();
}
std::shared_ptr<const Type>
Struct::getTypeReplacement(std::shared_ptr<const Type> type,
std::vector<std::shared_ptr<const Struct>>
structTypesThatShouldBeReplaced) const {
std::shared_ptr<const Type> replacementType = type->unrollTypedefs();
std::shared_ptr<PointerType> pointerToByte =
std::make_shared<PointerType>(std::make_shared<PrimitiveType>("Byte"));
for (const auto &recordType : structTypesThatShouldBeReplaced) {
std::shared_ptr<TypeDef> recordTypeDef = std::make_shared<TypeDef>(
recordType->getTypeName(), recordType, nullptr);
std::shared_ptr<Type> pointerToRecord =
std::make_shared<PointerType>(recordTypeDef);
if (*replacementType == *pointerToRecord) {
replacementType = pointerToByte;
} else {
replacementType =
replacementType->replaceType(pointerToRecord, pointerToByte);
}
std::vector<std::shared_ptr<const Type>> visitedTypes;
if (replacementType->usesType(recordType, false, visitedTypes)) {
assert(isInstanceOf<FunctionPointerType>(replacementType.get()));
/* function pointer types may have return value or a parameter of
* value type */
replacementType = replacementType->replaceType(
recordTypeDef,
std::make_shared<PrimitiveType>("native.CStruct0"));
}
}
return replacementType;
}
std::vector<std::shared_ptr<const Struct>>
Struct::shouldFieldBreakCycle(const std::shared_ptr<Field> &field) const {
std::vector<std::shared_ptr<const Struct>> structTypesThatShouldBeReplaced;
if (isAliasForType<Struct>(field->getType().get()) ||
isAliasForType<Union>(field->getType().get())) {
/* cycle should be broken on pointer type */
return structTypesThatShouldBeReplaced;
}
CycleNode baseNode(shared_from_base<Struct>(), field.get());
std::vector<std::shared_ptr<const Type>> visitedTypes;
if (field->getType()->findAllCycles(shared_from_base<Struct>(), baseNode,
visitedTypes)) {
/* one or more cycles were found but type of the filed should be
* changed if this struct has the biggest name compared to other structs
* in cycle that have fields of non-value type.
*/
if (baseNode.cycleNodes.empty()) {
/* field references containing struct */
structTypesThatShouldBeReplaced.push_back(
shared_from_base<Struct>());
}
for (const auto &nextCycleNode : baseNode.cycleNodes) {
std::vector<std::string> namesInCycle;
if (hasBiggestName(nextCycleNode, namesInCycle)) {
structTypesThatShouldBeReplaced.push_back(nextCycleNode.s);
}
}
}
return structTypesThatShouldBeReplaced;
}
bool Struct::findAllCycles(
const std::shared_ptr<const Struct> &startStruct, CycleNode &cycleNode,
std::vector<std::shared_ptr<const Type>> &visitedTypes) const {
if (this == startStruct.get()) {
return true;
}
/* visitedTypes check is ignored because it is not necessary to save Struct
* and Union types in visitedTypes if it is done for TypeDefs (Struct and
* Union types can be references only through TypeDefs) */
bool belongsToCycle = false;
for (const auto &field : fields) {
CycleNode newCycleNode(shared_from_base<Struct>(), field.get());
if (field->getType()->findAllCycles(startStruct, newCycleNode,
visitedTypes)) {
if (isAliasForType<Struct>(field->getType().get()) ||
isAliasForType<Union>(field->getType().get())) {
/* cycles cannot be broken on value type fields */
newCycleNode.isValueType = true;
}
belongsToCycle = true;
cycleNode.cycleNodes.push_back(newCycleNode);
}
}
return belongsToCycle;
}
bool Struct::hasBiggestName(const CycleNode &node,
std::vector<std::string> namesInCycle) const {
if (!node.isValueType) {
namesInCycle.push_back(node.s->getTypeName());
}
if (node.cycleNodes.empty()) {
std::sort(namesInCycle.begin(), namesInCycle.end());
return getTypeName() >= namesInCycle.back();
}
for (const auto &cycleNode : node.cycleNodes) {
if (hasBiggestName(cycleNode, namesInCycle)) {
return true;
}
}
return false;
}