|
1 | 1 | #include "Struct.h"
|
2 | 2 | #include "../Utils.h"
|
| 3 | +#include "Union.h" |
3 | 4 | #include "types/ArrayType.h"
|
4 | 5 | #include "types/FunctionPointerType.h"
|
5 | 6 | #include "types/PointerType.h"
|
6 | 7 | #include "types/PrimitiveType.h"
|
7 | 8 | #include <sstream>
|
8 | 9 |
|
9 |
| -Field::Field(std::string name, std::shared_ptr<const Type> type) |
10 |
| - : TypeAndName(std::move(name), std::move(type)) {} |
11 |
| - |
12 |
| -Field::Field(std::string name, std::shared_ptr<const Type> type, |
13 |
| - uint64_t offsetInBits) |
14 |
| - : TypeAndName(std::move(name), std::move(type)), |
15 |
| - offsetInBits(offsetInBits) {} |
16 |
| - |
17 |
| -uint64_t Field::getOffsetInBits() const { return offsetInBits; } |
18 |
| - |
19 |
| -StructOrUnion::StructOrUnion(std::string name, |
20 |
| - std::vector<std::shared_ptr<Field>> fields, |
21 |
| - std::shared_ptr<Location> location) |
22 |
| - : LocatableType(std::move(location)), name(std::move(name)), |
23 |
| - fields(std::move(fields)) {} |
24 |
| - |
25 |
| -std::string StructOrUnion::getName() const { return name; } |
26 |
| - |
27 |
| -bool StructOrUnion::hasHelperMethods() const { return !fields.empty(); } |
28 |
| - |
29 | 10 | Struct::Struct(std::string name, std::vector<std::shared_ptr<Field>> fields,
|
30 | 11 | uint64_t typeSize, std::shared_ptr<Location> location,
|
31 | 12 | bool isPacked, bool isBitField)
|
32 |
| - : StructOrUnion(std::move(name), std::move(fields), std::move(location)), |
| 13 | + : Record(std::move(name), std::move(fields), std::move(location)), |
33 | 14 | typeSize(typeSize), isPacked(isPacked), hasBitField(isBitField) {}
|
34 | 15 |
|
35 |
| -bool StructOrUnion::usesType( |
36 |
| - const std::shared_ptr<const Type> &type, bool stopOnTypeDefs, |
37 |
| - std::vector<std::shared_ptr<const Type>> &visitedTypes) const { |
38 |
| - |
39 |
| - if (contains(this, visitedTypes)) { |
40 |
| - return false; |
41 |
| - } |
42 |
| - visitedTypes.push_back(shared_from_this()); |
43 |
| - |
44 |
| - for (const auto &field : fields) { |
45 |
| - if (*field->getType() == *type || |
46 |
| - field->getType()->usesType(type, stopOnTypeDefs, visitedTypes)) { |
47 |
| - visitedTypes.pop_back(); |
48 |
| - return true; |
49 |
| - } |
50 |
| - } |
51 |
| - visitedTypes.pop_back(); |
52 |
| - return false; |
53 |
| -} |
54 |
| - |
55 | 16 | std::shared_ptr<TypeDef> Struct::generateTypeDef() {
|
56 | 17 | if (isRepresentedAsStruct()) {
|
57 | 18 | return std::make_shared<TypeDef>(getTypeAlias(), shared_from_this(),
|
@@ -382,80 +343,3 @@ bool Struct::hasBiggestName(const CycleNode &node,
|
382 | 343 | }
|
383 | 344 | return false;
|
384 | 345 | }
|
385 |
| - |
386 |
| -Union::Union(std::string name, std::vector<std::shared_ptr<Field>> fields, |
387 |
| - uint64_t maxSize, std::shared_ptr<Location> location) |
388 |
| - : StructOrUnion(std::move(name), std::move(fields), std::move(location)), |
389 |
| - ArrayType(std::make_shared<PrimitiveType>("Byte"), maxSize) {} |
390 |
| - |
391 |
| -std::shared_ptr<TypeDef> Union::generateTypeDef() { |
392 |
| - return std::make_shared<TypeDef>(getTypeAlias(), shared_from_this(), |
393 |
| - nullptr); |
394 |
| -} |
395 |
| - |
396 |
| -std::string Union::generateHelperClass() const { |
397 |
| - assert(hasHelperMethods()); |
398 |
| - std::stringstream s; |
399 |
| - std::string type = getTypeAlias(); |
400 |
| - s << " implicit class " << type << "_pos" |
401 |
| - << "(val p: native.Ptr[" << type << "]) extends AnyVal {\n"; |
402 |
| - for (const auto &field : fields) { |
403 |
| - if (!field->getName().empty()) { |
404 |
| - s << generateGetter(field); |
405 |
| - s << generateSetter(field); |
406 |
| - } |
407 |
| - } |
408 |
| - s << " }\n"; |
409 |
| - return s.str(); |
410 |
| -} |
411 |
| - |
412 |
| -std::string Union::getTypeAlias() const { return "union_" + name; } |
413 |
| - |
414 |
| -bool Union::operator==(const Type &other) const { |
415 |
| - if (this == &other) { |
416 |
| - return true; |
417 |
| - } |
418 |
| - auto *u = dynamic_cast<const Union *>(&other); |
419 |
| - if (u) { |
420 |
| - /* unions have unique names */ |
421 |
| - return name == u->name; |
422 |
| - } |
423 |
| - return false; |
424 |
| -} |
425 |
| - |
426 |
| -bool Union::usesType( |
427 |
| - const std::shared_ptr<const Type> &type, bool stopOnTypeDefs, |
428 |
| - std::vector<std::shared_ptr<const Type>> &visitedTypes) const { |
429 |
| - |
430 |
| - if (contains(this, visitedTypes)) { |
431 |
| - return false; |
432 |
| - } |
433 |
| - visitedTypes.push_back(shared_from_this()); |
434 |
| - |
435 |
| - if (ArrayType::usesType(type, stopOnTypeDefs, visitedTypes)) { |
436 |
| - visitedTypes.pop_back(); |
437 |
| - return true; |
438 |
| - } |
439 |
| - visitedTypes.pop_back(); |
440 |
| - |
441 |
| - return StructOrUnion::usesType(type, stopOnTypeDefs, visitedTypes); |
442 |
| -} |
443 |
| - |
444 |
| -std::string Union::generateGetter(const std::shared_ptr<Field> &field) const { |
445 |
| - std::string getter = handleReservedWords(field->getName()); |
446 |
| - std::string ftype = field->getType()->str(); |
447 |
| - return " def " + getter + ": native.Ptr[" + ftype + |
448 |
| - "] = p.cast[native.Ptr[" + ftype + "]]\n"; |
449 |
| -} |
450 |
| - |
451 |
| -std::string Union::generateSetter(const std::shared_ptr<Field> &field) const { |
452 |
| - std::string setter = handleReservedWords(field->getName(), "_="); |
453 |
| - std::string ftype = field->getType()->str(); |
454 |
| - if (isAliasForType<ArrayType>(field->getType().get()) || |
455 |
| - isAliasForType<Struct>(field->getType().get())) { |
456 |
| - return " def " + setter + "(value: native.Ptr[" + ftype + |
457 |
| - "]): Unit = !p.cast[native.Ptr[" + ftype + "]] = !value\n"; |
458 |
| - } |
459 |
| - return " def " + setter + "(value: " + ftype + |
460 |
| - "): Unit = !p.cast[native.Ptr[" + ftype + "]] = value\n"; |
461 |
| -} |
0 commit comments