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 | 2804 | Struct::Struct(std::string name, SymbolTableEntry *entry, Scope *scope, QualTypeList fieldTypes, | |
| 14 | std::vector<GenericType> templateTypes, QualTypeList interfaceTypes, ASTNode *declNode) | ||
| 15 | 2804 | : StructBase(std::move(name), entry, scope, std::move(templateTypes), declNode), fieldTypes(std::move(fieldTypes)), | |
| 16 |
1/2✓ Branch 8 → 9 taken 2804 times.
✗ Branch 8 → 18 not taken.
|
14020 | 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 | 4066 | std::string Struct::getScopeName() const { | |
| 25 |
5/8✓ Branch 2 → 3 taken 4066 times.
✗ Branch 2 → 13 not taken.
✓ Branch 3 → 4 taken 2033 times.
✓ Branch 3 → 5 taken 2033 times.
✓ Branch 4 → 6 taken 2033 times.
✗ Branch 4 → 13 not taken.
✓ Branch 5 → 6 taken 2033 times.
✗ Branch 5 → 13 not taken.
|
4066 | const std::string &appendix = isGenericSubstantiation() ? getSignature() : name; |
| 26 |
1/2✓ Branch 6 → 7 taken 4066 times.
✗ Branch 6 → 11 not taken.
|
8132 | return STRUCT_SCOPE_PREFIX + appendix; |
| 27 | 4066 | } | |
| 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 | ||
| 31 | * struct from the parent scope. | ||
| 32 | * | ||
| 33 | * @param name Struct name | ||
| 34 | * @param concreteTemplateTypes Concrete template types | ||
| 35 | * @return Name of the struct scope | ||
| 36 | */ | ||
| 37 | 66417 | std::string Struct::getScopeName(const std::string &name, const QualTypeList &concreteTemplateTypes) { | |
| 38 |
2/4✓ Branch 2 → 3 taken 66417 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 66417 times.
✗ Branch 3 → 8 not taken.
|
132834 | return STRUCT_SCOPE_PREFIX + getSignature(name, concreteTemplateTypes); |
| 39 | } | ||
| 40 | |||
| 41 | /** | ||
| 42 | * Checks at least one field is a reference. | ||
| 43 | * This is used to prohibit constant instantiations. | ||
| 44 | * | ||
| 45 | * @return Has reference as field type or not | ||
| 46 | */ | ||
| 47 | 1296 | bool Struct::hasReferenceFields() const { | |
| 48 | 3080 | return std::ranges::any_of(fieldTypes, [](const QualType &fieldType) { return fieldType.isRef(); }); | |
| 49 | } | ||
| 50 | |||
| 51 | /** | ||
| 52 | * Check that all fields are in a certain lifecycle state. | ||
| 53 | * | ||
| 54 | * @param state Lifecycle state to check for | ||
| 55 | * @return nullptr if all fields are in this state, otherwise the first mismatched field symbol | ||
| 56 | */ | ||
| 57 | 3132 | const SymbolTableEntry *Struct::areAllFieldsInState(LifecycleState state) const { | |
| 58 |
2/2✓ Branch 20 → 3 taken 4977 times.
✓ Branch 20 → 21 taken 3132 times.
|
8109 | for (size_t i = 0; i < fieldTypes.size(); i++) { |
| 59 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4977 times.
|
4977 | const SymbolTableEntry *fieldSymbol = scope->lookupField(i); |
| 60 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 4977 times.
|
4977 | assert(fieldSymbol != nullptr); |
| 61 |
4/6✓ Branch 10 → 11 taken 4624 times.
✓ Branch 10 → 15 taken 353 times.
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 4624 times.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 4977 times.
|
4977 | if (!fieldSymbol->isImplicitField && fieldSymbol->getLifecycle().getCurrentState() != state) |
| 62 | ✗ | return fieldSymbol; | |
| 63 | } | ||
| 64 | 3132 | return nullptr; | |
| 65 | } | ||
| 66 | |||
| 67 | /** | ||
| 68 | * Check that all fields are initialized. | ||
| 69 | * | ||
| 70 | * @return nullptr if all fields are initialized, otherwise the first uninitialized field symbol | ||
| 71 | */ | ||
| 72 | 3132 | const SymbolTableEntry *Struct::areAllFieldsInitialized() const { return areAllFieldsInState(INITIALIZED); } | |
| 73 | |||
| 74 | /** | ||
| 75 | * Reset the initialization state of all fields to DECLARED. | ||
| 76 | * | ||
| 77 | * @param node AST node to associate with the update | ||
| 78 | */ | ||
| 79 | 7700 | void Struct::resetFieldSymbolsToDeclared(const ASTNode *node) const { | |
| 80 | // Reset every explicit field | ||
| 81 | 7700 | const size_t fieldCount = scope->getFieldCount(); | |
| 82 |
2/2✓ Branch 13 → 4 taken 18546 times.
✓ Branch 13 → 14 taken 7700 times.
|
26246 | for (size_t i = 0; i < fieldCount; i++) { |
| 83 |
1/2✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 18546 times.
|
18546 | SymbolTableEntry *fieldSymbol = scope->lookupField(i); |
| 84 |
2/2✓ Branch 9 → 10 taken 16082 times.
✓ Branch 9 → 12 taken 2464 times.
|
18546 | if (!fieldSymbol->isImplicitField) |
| 85 |
1/2✓ Branch 10 → 11 taken 16082 times.
✗ Branch 10 → 15 not taken.
|
16082 | fieldSymbol->updateState(DECLARED, node); |
| 86 | } | ||
| 87 | 7700 | } | |
| 88 | |||
| 89 | } // namespace spice::compiler | ||
| 90 |