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 | 3656 | StructBase::StructBase(std::string name, SymbolTableEntry *entry, Scope *scope, std::vector<GenericType> templateTypes, | |
| 13 | ASTNode *declNode) | ||
| 14 | 10968 | : 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 |
2/4✓ Branch 2 → 3 taken 19723 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 19723 times.
✗ Branch 3 → 8 not taken.
|
39446 | std::string StructBase::getSignature() const { return getSignature(name, getConcreteTemplateTypes()); } |
| 22 | |||
| 23 | /** | ||
| 24 | * Get the signature from the struct name and the concrete template types | ||
| 25 | * | ||
| 26 | * Example: | ||
| 27 | * Pair<int,double> | ||
| 28 | * | ||
| 29 | * @param name Struct name | ||
| 30 | * @param concreteTemplateTypes Concrete template types | ||
| 31 | * @return Signature | ||
| 32 | */ | ||
| 33 | 109876 | std::string StructBase::getSignature(const std::string &name, const QualTypeList &concreteTemplateTypes) { | |
| 34 | // Build template type string | ||
| 35 |
1/2✓ Branch 2 → 3 taken 109876 times.
✗ Branch 2 → 48 not taken.
|
109876 | std::stringstream templateTyStr; |
| 36 |
2/2✓ Branch 4 → 5 taken 40691 times.
✓ Branch 4 → 17 taken 69185 times.
|
109876 | if (!concreteTemplateTypes.empty()) { |
| 37 |
1/2✓ Branch 5 → 6 taken 40691 times.
✗ Branch 5 → 46 not taken.
|
40691 | templateTyStr << "<"; |
| 38 |
2/2✓ Branch 15 → 7 taken 52362 times.
✓ Branch 15 → 16 taken 40691 times.
|
93053 | for (size_t i = 0; i < concreteTemplateTypes.size(); i++) { |
| 39 |
2/2✓ Branch 7 → 8 taken 11671 times.
✓ Branch 7 → 9 taken 40691 times.
|
52362 | if (i > 0) |
| 40 |
1/2✓ Branch 8 → 9 taken 11671 times.
✗ Branch 8 → 46 not taken.
|
11671 | templateTyStr << ","; |
| 41 |
3/6✓ Branch 9 → 10 taken 52362 times.
✗ Branch 9 → 33 not taken.
✓ Branch 10 → 11 taken 52362 times.
✗ Branch 10 → 33 not taken.
✓ Branch 11 → 12 taken 52362 times.
✗ Branch 11 → 31 not taken.
|
52362 | templateTyStr << concreteTemplateTypes.at(i).getName(false, true); |
| 42 | } | ||
| 43 |
1/2✓ Branch 16 → 17 taken 40691 times.
✗ Branch 16 → 46 not taken.
|
40691 | templateTyStr << ">"; |
| 44 | } | ||
| 45 | |||
| 46 |
4/8✓ Branch 17 → 18 taken 109876 times.
✗ Branch 17 → 45 not taken.
✓ Branch 20 → 21 taken 109876 times.
✗ Branch 20 → 38 not taken.
✓ Branch 21 → 22 taken 109876 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 109876 times.
✗ Branch 22 → 34 not taken.
|
549380 | return CommonUtil::getLastFragment(name, SCOPE_ACCESS_TOKEN) + templateTyStr.str(); |
| 47 | 109876 | } | |
| 48 | |||
| 49 | /** | ||
| 50 | * Checks if a struct contains generic template types. | ||
| 51 | * This would imply that the struct is not substantiated by its generic types yet. | ||
| 52 | * | ||
| 53 | * @return Substantiated generics or not | ||
| 54 | */ | ||
| 55 | 21257 | bool StructBase::hasSubstantiatedGenerics() const { | |
| 56 | 14240 | const auto pred = [](const GenericType &genericType) { return genericType.hasAnyGenericParts(); }; | |
| 57 |
1/2✓ Branch 2 → 3 taken 21257 times.
✗ Branch 2 → 6 not taken.
|
42514 | return std::ranges::none_of(templateTypes, pred); |
| 58 | } | ||
| 59 | |||
| 60 | /** | ||
| 61 | * Checks if a struct has generic types present. | ||
| 62 | * This would imply that the struct is not fully substantiated yet. | ||
| 63 | * | ||
| 64 | * @return Fully substantiated or not | ||
| 65 | */ | ||
| 66 | 21257 | bool StructBase::isFullySubstantiated() const { return hasSubstantiatedGenerics(); } | |
| 67 | |||
| 68 | /** | ||
| 69 | * Retrieve the concrete template types of this struct. For generic substantiations, the generic types are replaced | ||
| 70 | * by the concrete types from the type mapping. | ||
| 71 | * | ||
| 72 | * @return Concrete template types as vector of symbol types | ||
| 73 | */ | ||
| 74 | 20033 | QualTypeList StructBase::getConcreteTemplateTypes() const { | |
| 75 | 20033 | QualTypeList concreteTemplateTypes; | |
| 76 |
1/2✓ Branch 3 → 4 taken 20033 times.
✗ Branch 3 → 36 not taken.
|
20033 | concreteTemplateTypes.reserve(templateTypes.size()); |
| 77 |
2/2✓ Branch 32 → 6 taken 22310 times.
✓ Branch 32 → 33 taken 20033 times.
|
62376 | for (const GenericType &genericType : templateTypes) { |
| 78 |
7/8✓ Branch 8 → 9 taken 22310 times.
✗ Branch 8 → 35 not taken.
✓ Branch 9 → 10 taken 3580 times.
✓ Branch 9 → 13 taken 18730 times.
✓ Branch 11 → 12 taken 2181 times.
✓ Branch 11 → 13 taken 1399 times.
✓ Branch 14 → 15 taken 2181 times.
✓ Branch 14 → 22 taken 20129 times.
|
22310 | if (genericType.is(TY_GENERIC) && !typeMapping.empty()) { |
| 79 |
3/6✓ Branch 15 → 16 taken 2181 times.
✗ Branch 15 → 35 not taken.
✓ Branch 16 → 17 taken 2181 times.
✗ Branch 16 → 35 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 2181 times.
|
2181 | assert(typeMapping.contains(genericType.getSubType())); |
| 80 |
3/6✓ Branch 19 → 20 taken 2181 times.
✗ Branch 19 → 35 not taken.
✓ Branch 20 → 21 taken 2181 times.
✗ Branch 20 → 35 not taken.
✓ Branch 21 → 23 taken 2181 times.
✗ Branch 21 → 35 not taken.
|
2181 | concreteTemplateTypes.push_back(typeMapping.at(genericType.getSubType())); |
| 81 | } else { | ||
| 82 |
1/2✓ Branch 22 → 23 taken 20129 times.
✗ Branch 22 → 35 not taken.
|
20129 | concreteTemplateTypes.push_back(genericType); |
| 83 | } | ||
| 84 | } | ||
| 85 | 20033 | return concreteTemplateTypes; | |
| 86 | ✗ | } | |
| 87 | |||
| 88 | /** | ||
| 89 | * Retrieve the template types as vector of symbol types | ||
| 90 | * | ||
| 91 | * @return Template types as vector of symbol types | ||
| 92 | */ | ||
| 93 | 4590 | QualTypeList StructBase::getTemplateTypes() const { | |
| 94 | 4590 | QualTypeList templateSymbolTypes; | |
| 95 |
2/2✓ Branch 16 → 4 taken 4362 times.
✓ Branch 16 → 17 taken 4590 times.
|
13542 | for (const GenericType &genericTemplateType : templateTypes) |
| 96 |
1/2✓ Branch 6 → 7 taken 4362 times.
✗ Branch 6 → 19 not taken.
|
4362 | templateSymbolTypes.push_back(genericTemplateType); |
| 97 | 4590 | return templateSymbolTypes; | |
| 98 | ✗ | } | |
| 99 | |||
| 100 | /** | ||
| 101 | * Retrieve the declaration code location of this struct | ||
| 102 | * | ||
| 103 | * @return Declaration code location | ||
| 104 | */ | ||
| 105 | 23250 | const CodeLoc &StructBase::getDeclCodeLoc() const { return declNode->codeLoc; } | |
| 106 | |||
| 107 | /** | ||
| 108 | * Returns, if this struct is a substantiation of a generic one. | ||
| 109 | * | ||
| 110 | * @return Generic substantiation or not | ||
| 111 | */ | ||
| 112 | 143079 | bool StructBase::isGenericSubstantiation() const { return genericPreset != nullptr; } | |
| 113 | |||
| 114 | } // namespace spice::compiler | ||
| 115 |