src/model/Struct.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "Struct.h" | ||
| 4 | |||
| 5 | #include <ast/ASTNodes.h> | ||
| 6 | #include <model/GenericType.h> | ||
| 7 | #include <symboltablebuilder/Scope.h> | ||
| 8 | |||
| 9 | namespace spice::compiler { | ||
| 10 | |||
| 11 | static constexpr auto STRUCT_SCOPE_PREFIX = "struct:"; | ||
| 12 | |||
| 13 | 742 | Struct::Struct(std::string name, SymbolTableEntry *entry, Scope *scope, QualTypeList fieldTypes, | |
| 14 | 742 | std::vector<GenericType> templateTypes, QualTypeList interfaceTypes, ASTNode *declNode) | |
| 15 | 1484 | : StructBase(std::move(name), entry, scope, std::move(templateTypes), declNode), fieldTypes(std::move(fieldTypes)), | |
| 16 |
1/2✓ Branch 6 → 7 taken 742 times.
✗ Branch 6 → 14 not taken.
|
2226 | interfaceTypes(std::move(interfaceTypes)) {} |
| 17 | |||
| 18 | /** | ||
| 19 | * Retrieve the name of the scope, where members and methods are placed. This is used to navigate to the scope of the struct | ||
| 20 | * from the parent scope. | ||
| 21 | * | ||
| 22 | * @return Name of the struct scope | ||
| 23 | */ | ||
| 24 | 936 | std::string Struct::getScopeName() const { | |
| 25 |
5/8✓ Branch 2 → 3 taken 936 times.
✗ Branch 2 → 13 not taken.
✓ Branch 3 → 4 taken 468 times.
✓ Branch 3 → 5 taken 468 times.
✓ Branch 4 → 6 taken 468 times.
✗ Branch 4 → 13 not taken.
✓ Branch 5 → 6 taken 468 times.
✗ Branch 5 → 13 not taken.
|
936 | const std::string &appendix = isGenericSubstantiation() ? getSignature() : name; |
| 26 |
1/2✓ Branch 6 → 7 taken 936 times.
✗ Branch 6 → 11 not taken.
|
1872 | return STRUCT_SCOPE_PREFIX + appendix; |
| 27 | 936 | } | |
| 28 | |||
| 29 | /** | ||
| 30 | * Retrieve the name of the scope, where members and methods are placed. This is used to navigate to the scope of the struct | ||
| 31 | * from the parent scope. | ||
| 32 | * | ||
| 33 | * @return Name of the struct scope | ||
| 34 | */ | ||
| 35 | 20502 | std::string Struct::getScopeName(const std::string &name, const QualTypeList &concreteTemplateTypes) { | |
| 36 |
2/4✓ Branch 2 → 3 taken 20502 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 20502 times.
✗ Branch 3 → 8 not taken.
|
20502 | return STRUCT_SCOPE_PREFIX + getSignature(name, concreteTemplateTypes); |
| 37 | } | ||
| 38 | |||
| 39 | /** | ||
| 40 | * Checks at least one field is a reference. | ||
| 41 | * This is used to prohibit constant instantiations. | ||
| 42 | * | ||
| 43 | * @return Has reference as field type or not | ||
| 44 | */ | ||
| 45 | 290 | bool Struct::hasReferenceFields() const { | |
| 46 | 734 | return std::ranges::any_of(fieldTypes, [](const QualType &fieldType) { return fieldType.isRef(); }); | |
| 47 | } | ||
| 48 | |||
| 49 | /** | ||
| 50 | * Check that all fields are in a certain lifecycle state. | ||
| 51 | * | ||
| 52 | * @param state Lifecycle state to check for | ||
| 53 | * @return nullptr if all fields are in this state, otherwise the first mismatched field symbol | ||
| 54 | */ | ||
| 55 | 256 | const SymbolTableEntry *Struct::areAllFieldsInState(LifecycleState state) const { | |
| 56 |
2/2✓ Branch 20 → 3 taken 475 times.
✓ Branch 20 → 21 taken 256 times.
|
731 | for (size_t i = 0; i < fieldTypes.size(); i++) { |
| 57 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 475 times.
|
475 | const SymbolTableEntry *fieldSymbol = scope->lookupField(i); |
| 58 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 475 times.
|
475 | assert(fieldSymbol != nullptr); |
| 59 |
4/6✓ Branch 10 → 11 taken 392 times.
✓ Branch 10 → 15 taken 83 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 392 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 475 times.
|
475 | if (!fieldSymbol->isImplicitField && fieldSymbol->getLifecycle().getCurrentState() != state) |
| 60 | ✗ | return fieldSymbol; | |
| 61 | } | ||
| 62 | 256 | return nullptr; | |
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * Check that all fields are initialized. | ||
| 67 | * | ||
| 68 | * @return nullptr if all fields are initialized, otherwise the first uninitialized field symbol | ||
| 69 | */ | ||
| 70 | 256 | const SymbolTableEntry *Struct::areAllFieldsInitialized() const { return areAllFieldsInState(INITIALIZED); } | |
| 71 | |||
| 72 | /** | ||
| 73 | * Reset the initialization state of all fields to DECLARED. | ||
| 74 | * | ||
| 75 | * @param node AST node to associate with the update | ||
| 76 | */ | ||
| 77 | 1454 | void Struct::resetFieldSymbolsToDeclared(const ASTNode *node) const { | |
| 78 |
2/2✓ Branch 11 → 3 taken 3388 times.
✓ Branch 11 → 12 taken 1454 times.
|
4842 | for (size_t i = 0; i < fieldTypes.size(); i++) |
| 79 |
2/4✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3388 times.
✓ Branch 8 → 9 taken 3388 times.
✗ Branch 8 → 13 not taken.
|
6776 | scope->lookupField(i)->updateState(DECLARED, node); |
| 80 | 1454 | } | |
| 81 | |||
| 82 | } // namespace spice::compiler | ||
| 83 |