| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 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 | /** | ||
| 13 | * Get a string representation of the current struct | ||
| 14 | * | ||
| 15 | * @return String representation as struct signature | ||
| 16 | */ | ||
| 17 | 3691 | std::string StructBase::getSignature() const { | |
| 18 | 3691 | QualTypeList templateSymbolTypes; | |
| 19 |
1/2✓ Branch 3 → 4 taken 3691 times.
✗ Branch 3 → 31 not taken.
|
3691 | templateSymbolTypes.reserve(templateTypes.size()); |
| 20 |
2/2✓ Branch 24 → 6 taken 4230 times.
✓ Branch 24 → 25 taken 3691 times.
|
7921 | for (const GenericType &genericType : templateTypes) { |
| 21 |
7/8✓ Branch 7 → 8 taken 4230 times.
✗ Branch 7 → 30 not taken.
✓ Branch 8 → 9 taken 1702 times.
✓ Branch 8 → 12 taken 2528 times.
✓ Branch 10 → 11 taken 1235 times.
✓ Branch 10 → 12 taken 467 times.
✓ Branch 13 → 14 taken 1235 times.
✓ Branch 13 → 21 taken 2995 times.
|
4230 | if (genericType.is(TY_GENERIC) && !typeMapping.empty()) { |
| 22 |
3/6✓ Branch 14 → 15 taken 1235 times.
✗ Branch 14 → 30 not taken.
✓ Branch 15 → 16 taken 1235 times.
✗ Branch 15 → 30 not taken.
✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 1235 times.
|
1235 | assert(typeMapping.contains(genericType.getSubType())); |
| 23 |
3/6✓ Branch 18 → 19 taken 1235 times.
✗ Branch 18 → 30 not taken.
✓ Branch 19 → 20 taken 1235 times.
✗ Branch 19 → 30 not taken.
✓ Branch 20 → 22 taken 1235 times.
✗ Branch 20 → 30 not taken.
|
1235 | templateSymbolTypes.push_back(typeMapping.at(genericType.getSubType())); |
| 24 | } else { | ||
| 25 |
1/2✓ Branch 21 → 22 taken 2995 times.
✗ Branch 21 → 30 not taken.
|
2995 | templateSymbolTypes.push_back(genericType); |
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 |
1/2✓ Branch 25 → 26 taken 3691 times.
✗ Branch 25 → 31 not taken.
|
7382 | return getSignature(name, templateSymbolTypes); |
| 30 | 3691 | } | |
| 31 | |||
| 32 | /** | ||
| 33 | * Get the signature from the struct name and the concrete template types | ||
| 34 | * | ||
| 35 | * Example: | ||
| 36 | * Pair<int,double> | ||
| 37 | * | ||
| 38 | * @param name Struct name | ||
| 39 | * @param concreteTemplateTypes Concrete template types | ||
| 40 | * @return Signature | ||
| 41 | */ | ||
| 42 | 17435 | std::string StructBase::getSignature(const std::string &name, const QualTypeList &concreteTemplateTypes) { | |
| 43 | // Build template type string | ||
| 44 |
1/2✓ Branch 2 → 3 taken 17435 times.
✗ Branch 2 → 48 not taken.
|
17435 | std::stringstream templateTyStr; |
| 45 |
2/2✓ Branch 4 → 5 taken 7214 times.
✓ Branch 4 → 17 taken 10221 times.
|
17435 | if (!concreteTemplateTypes.empty()) { |
| 46 |
1/2✓ Branch 5 → 6 taken 7214 times.
✗ Branch 5 → 46 not taken.
|
7214 | templateTyStr << "<"; |
| 47 |
2/2✓ Branch 15 → 7 taken 9342 times.
✓ Branch 15 → 16 taken 7214 times.
|
16556 | for (size_t i = 0; i < concreteTemplateTypes.size(); i++) { |
| 48 |
2/2✓ Branch 7 → 8 taken 2128 times.
✓ Branch 7 → 9 taken 7214 times.
|
9342 | if (i > 0) |
| 49 |
1/2✓ Branch 8 → 9 taken 2128 times.
✗ Branch 8 → 46 not taken.
|
2128 | templateTyStr << ","; |
| 50 |
3/6✓ Branch 9 → 10 taken 9342 times.
✗ Branch 9 → 33 not taken.
✓ Branch 10 → 11 taken 9342 times.
✗ Branch 10 → 33 not taken.
✓ Branch 11 → 12 taken 9342 times.
✗ Branch 11 → 31 not taken.
|
9342 | templateTyStr << concreteTemplateTypes.at(i).getName(false, true); |
| 51 | } | ||
| 52 |
1/2✓ Branch 16 → 17 taken 7214 times.
✗ Branch 16 → 46 not taken.
|
7214 | templateTyStr << ">"; |
| 53 | } | ||
| 54 | |||
| 55 |
4/8✓ Branch 17 → 18 taken 17435 times.
✗ Branch 17 → 45 not taken.
✓ Branch 20 → 21 taken 17435 times.
✗ Branch 20 → 38 not taken.
✓ Branch 21 → 22 taken 17435 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 17435 times.
✗ Branch 22 → 34 not taken.
|
69740 | return CommonUtil::getLastFragment(name, SCOPE_ACCESS_TOKEN) + templateTyStr.str(); |
| 56 | 17435 | } | |
| 57 | |||
| 58 | /** | ||
| 59 | * Checks if a struct contains template types. | ||
| 60 | * This would imply that the struct is not substantiated by its generic types yet. | ||
| 61 | * | ||
| 62 | * @return Substantiated generics or not | ||
| 63 | */ | ||
| 64 | 3164 | bool StructBase::hasSubstantiatedGenerics() const { | |
| 65 | 2573 | const auto lambda = [](const GenericType &genericType) { return genericType.hasAnyGenericParts(); }; | |
| 66 |
1/2✓ Branch 2 → 3 taken 3164 times.
✗ Branch 2 → 6 not taken.
|
6328 | return std::ranges::none_of(templateTypes, lambda); |
| 67 | } | ||
| 68 | |||
| 69 | /** | ||
| 70 | * Checks if a struct has generic types present. | ||
| 71 | * This would imply that the struct is not fully substantiated yet. | ||
| 72 | * | ||
| 73 | * @return Fully substantiated or not | ||
| 74 | */ | ||
| 75 | 3164 | bool StructBase::isFullySubstantiated() const { return hasSubstantiatedGenerics(); } | |
| 76 | |||
| 77 | /** | ||
| 78 | * Retrieve the template types as vector of symbol types | ||
| 79 | * | ||
| 80 | * @return Template types as vector of symbol types | ||
| 81 | */ | ||
| 82 | 895 | QualTypeList StructBase::getTemplateTypes() const { | |
| 83 | 895 | QualTypeList templateSymbolTypes; | |
| 84 |
2/2✓ Branch 8 → 4 taken 846 times.
✓ Branch 8 → 9 taken 895 times.
|
1741 | for (const GenericType &genericTemplateType : templateTypes) |
| 85 |
1/2✓ Branch 5 → 6 taken 846 times.
✗ Branch 5 → 11 not taken.
|
846 | templateSymbolTypes.push_back(genericTemplateType); |
| 86 | 895 | return templateSymbolTypes; | |
| 87 | ✗ | } | |
| 88 | |||
| 89 | /** | ||
| 90 | * Retrieve the declaration code location of this struct | ||
| 91 | * | ||
| 92 | * @return Declaration code location | ||
| 93 | */ | ||
| 94 | 762 | const CodeLoc &StructBase::getDeclCodeLoc() const { return declNode->codeLoc; } | |
| 95 | |||
| 96 | /** | ||
| 97 | * Returns, if this struct is a substantiation of a generic one. | ||
| 98 | * | ||
| 99 | * @return Generic substantiation or not | ||
| 100 | */ | ||
| 101 | 3243 | bool StructBase::isGenericSubstantiation() const { return genericPreset != nullptr; } | |
| 102 | |||
| 103 | } // namespace spice::compiler | ||
| 104 |