src/model/StructBase.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "StructBase.h" | ||
| 4 | |||
| 5 | #include <ast/ASTBuilder.h> | ||
| 6 | #include <ast/ASTNodes.h> | ||
| 7 | #include <typechecker/TypeMatcher.h> | ||
| 8 | #include <util/CommonUtil.h> | ||
| 9 | |||
| 10 | namespace spice::compiler { | ||
| 11 | |||
| 12 | 836 | StructBase::StructBase(std::string name, SymbolTableEntry *entry, Scope *scope, std::vector<GenericType> templateTypes, | |
| 13 | 836 | ASTNode *declNode) | |
| 14 | 836 | : name(std::move(name)), templateTypes(std::move(templateTypes)), entry(entry), scope(scope), declNode(declNode) {} | |
| 15 | |||
| 16 | /** | ||
| 17 | * Get a string representation of the current struct | ||
| 18 | * | ||
| 19 | * @return String representation as struct signature | ||
| 20 | */ | ||
| 21 | 4594 | std::string StructBase::getSignature() const { | |
| 22 | 4594 | QualTypeList templateSymbolTypes; | |
| 23 |
1/2✓ Branch 3 → 4 taken 4594 times.
✗ Branch 3 → 31 not taken.
|
4594 | templateSymbolTypes.reserve(templateTypes.size()); |
| 24 |
2/2✓ Branch 24 → 6 taken 5374 times.
✓ Branch 24 → 25 taken 4594 times.
|
9968 | for (const GenericType &genericType : templateTypes) { |
| 25 |
7/8✓ Branch 7 → 8 taken 5374 times.
✗ Branch 7 → 30 not taken.
✓ Branch 8 → 9 taken 1903 times.
✓ Branch 8 → 12 taken 3471 times.
✓ Branch 10 → 11 taken 1386 times.
✓ Branch 10 → 12 taken 517 times.
✓ Branch 13 → 14 taken 1386 times.
✓ Branch 13 → 21 taken 3988 times.
|
5374 | if (genericType.is(TY_GENERIC) && !typeMapping.empty()) { |
| 26 |
3/6✓ Branch 14 → 15 taken 1386 times.
✗ Branch 14 → 30 not taken.
✓ Branch 15 → 16 taken 1386 times.
✗ Branch 15 → 30 not taken.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 1386 times.
|
1386 | assert(typeMapping.contains(genericType.getSubType())); |
| 27 |
3/6✓ Branch 18 → 19 taken 1386 times.
✗ Branch 18 → 30 not taken.
✓ Branch 19 → 20 taken 1386 times.
✗ Branch 19 → 30 not taken.
✓ Branch 20 → 22 taken 1386 times.
✗ Branch 20 → 30 not taken.
|
1386 | templateSymbolTypes.push_back(typeMapping.at(genericType.getSubType())); |
| 28 | } else { | ||
| 29 |
1/2✓ Branch 21 → 22 taken 3988 times.
✗ Branch 21 → 30 not taken.
|
3988 | templateSymbolTypes.push_back(genericType); |
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 |
1/2✓ Branch 25 → 26 taken 4594 times.
✗ Branch 25 → 31 not taken.
|
9188 | return getSignature(name, templateSymbolTypes); |
| 34 | 4594 | } | |
| 35 | |||
| 36 | /** | ||
| 37 | * Get the signature from the struct name and the concrete template types | ||
| 38 | * | ||
| 39 | * Example: | ||
| 40 | * Pair<int,double> | ||
| 41 | * | ||
| 42 | * @param name Struct name | ||
| 43 | * @param concreteTemplateTypes Concrete template types | ||
| 44 | * @return Signature | ||
| 45 | */ | ||
| 46 | 27101 | std::string StructBase::getSignature(const std::string &name, const QualTypeList &concreteTemplateTypes) { | |
| 47 | // Build template type string | ||
| 48 |
1/2✓ Branch 2 → 3 taken 27101 times.
✗ Branch 2 → 48 not taken.
|
27101 | std::stringstream templateTyStr; |
| 49 |
2/2✓ Branch 4 → 5 taken 8289 times.
✓ Branch 4 → 17 taken 18812 times.
|
27101 | if (!concreteTemplateTypes.empty()) { |
| 50 |
1/2✓ Branch 5 → 6 taken 8289 times.
✗ Branch 5 → 46 not taken.
|
8289 | templateTyStr << "<"; |
| 51 |
2/2✓ Branch 15 → 7 taken 10720 times.
✓ Branch 15 → 16 taken 8289 times.
|
19009 | for (size_t i = 0; i < concreteTemplateTypes.size(); i++) { |
| 52 |
2/2✓ Branch 7 → 8 taken 2431 times.
✓ Branch 7 → 9 taken 8289 times.
|
10720 | if (i > 0) |
| 53 |
1/2✓ Branch 8 → 9 taken 2431 times.
✗ Branch 8 → 46 not taken.
|
2431 | templateTyStr << ","; |
| 54 |
3/6✓ Branch 9 → 10 taken 10720 times.
✗ Branch 9 → 33 not taken.
✓ Branch 10 → 11 taken 10720 times.
✗ Branch 10 → 33 not taken.
✓ Branch 11 → 12 taken 10720 times.
✗ Branch 11 → 31 not taken.
|
10720 | templateTyStr << concreteTemplateTypes.at(i).getName(false, true); |
| 55 | } | ||
| 56 |
1/2✓ Branch 16 → 17 taken 8289 times.
✗ Branch 16 → 46 not taken.
|
8289 | templateTyStr << ">"; |
| 57 | } | ||
| 58 | |||
| 59 |
4/8✓ Branch 17 → 18 taken 27101 times.
✗ Branch 17 → 45 not taken.
✓ Branch 20 → 21 taken 27101 times.
✗ Branch 20 → 38 not taken.
✓ Branch 21 → 22 taken 27101 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 27101 times.
✗ Branch 22 → 34 not taken.
|
108404 | return CommonUtil::getLastFragment(name, SCOPE_ACCESS_TOKEN) + templateTyStr.str(); |
| 60 | 27101 | } | |
| 61 | |||
| 62 | /** | ||
| 63 | * Checks if a struct contains generic template types. | ||
| 64 | * This would imply that the struct is not substantiated by its generic types yet. | ||
| 65 | * | ||
| 66 | * @return Substantiated generics or not | ||
| 67 | */ | ||
| 68 | 3397 | bool StructBase::hasSubstantiatedGenerics() const { | |
| 69 | 2741 | const auto pred = [](const GenericType &genericType) { return genericType.hasAnyGenericParts(); }; | |
| 70 |
1/2✓ Branch 2 → 3 taken 3397 times.
✗ Branch 2 → 6 not taken.
|
6794 | return std::ranges::none_of(templateTypes, pred); |
| 71 | } | ||
| 72 | |||
| 73 | /** | ||
| 74 | * Checks if a struct has generic types present. | ||
| 75 | * This would imply that the struct is not fully substantiated yet. | ||
| 76 | * | ||
| 77 | * @return Fully substantiated or not | ||
| 78 | */ | ||
| 79 | 3397 | bool StructBase::isFullySubstantiated() const { return hasSubstantiatedGenerics(); } | |
| 80 | |||
| 81 | /** | ||
| 82 | * Retrieve the template types as vector of symbol types | ||
| 83 | * | ||
| 84 | * @return Template types as vector of symbol types | ||
| 85 | */ | ||
| 86 | 934 | QualTypeList StructBase::getTemplateTypes() const { | |
| 87 | 934 | QualTypeList templateSymbolTypes; | |
| 88 |
2/2✓ Branch 8 → 4 taken 896 times.
✓ Branch 8 → 9 taken 934 times.
|
1830 | for (const GenericType &genericTemplateType : templateTypes) |
| 89 |
1/2✓ Branch 5 → 6 taken 896 times.
✗ Branch 5 → 11 not taken.
|
896 | templateSymbolTypes.push_back(genericTemplateType); |
| 90 | 934 | return templateSymbolTypes; | |
| 91 | ✗ | } | |
| 92 | |||
| 93 | /** | ||
| 94 | * Retrieve the declaration code location of this struct | ||
| 95 | * | ||
| 96 | * @return Declaration code location | ||
| 97 | */ | ||
| 98 | 809 | const CodeLoc &StructBase::getDeclCodeLoc() const { return declNode->codeLoc; } | |
| 99 | |||
| 100 | /** | ||
| 101 | * Returns, if this struct is a substantiation of a generic one. | ||
| 102 | * | ||
| 103 | * @return Generic substantiation or not | ||
| 104 | */ | ||
| 105 | 4659 | bool StructBase::isGenericSubstantiation() const { return genericPreset != nullptr; } | |
| 106 | |||
| 107 | } // namespace spice::compiler | ||
| 108 |