GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 97.1% 34 / 0 / 35
Functions: 100.0% 9 / 0 / 9
Branches: 62.9% 39 / 0 / 62

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 2837 StructBase::StructBase(std::string name, SymbolTableEntry *entry, Scope *scope, std::vector<GenericType> templateTypes,
13 ASTNode *declNode)
14 8511 : 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 16395 std::string StructBase::getSignature() const {
22 16395 QualTypeList templateSymbolTypes;
23
1/2
✓ Branch 3 → 4 taken 16395 times.
✗ Branch 3 → 39 not taken.
16395 templateSymbolTypes.reserve(templateTypes.size());
24
2/2
✓ Branch 32 → 6 taken 18233 times.
✓ Branch 32 → 33 taken 16395 times.
51023 for (const GenericType &genericType : templateTypes) {
25
7/8
✓ Branch 8 → 9 taken 18233 times.
✗ Branch 8 → 38 not taken.
✓ Branch 9 → 10 taken 2876 times.
✓ Branch 9 → 13 taken 15357 times.
✓ Branch 11 → 12 taken 1777 times.
✓ Branch 11 → 13 taken 1099 times.
✓ Branch 14 → 15 taken 1777 times.
✓ Branch 14 → 22 taken 16456 times.
18233 if (genericType.is(TY_GENERIC) && !typeMapping.empty()) {
26
3/6
✓ Branch 15 → 16 taken 1777 times.
✗ Branch 15 → 38 not taken.
✓ Branch 16 → 17 taken 1777 times.
✗ Branch 16 → 38 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 1777 times.
1777 assert(typeMapping.contains(genericType.getSubType()));
27
3/6
✓ Branch 19 → 20 taken 1777 times.
✗ Branch 19 → 38 not taken.
✓ Branch 20 → 21 taken 1777 times.
✗ Branch 20 → 38 not taken.
✓ Branch 21 → 23 taken 1777 times.
✗ Branch 21 → 38 not taken.
1777 templateSymbolTypes.push_back(typeMapping.at(genericType.getSubType()));
28 } else {
29
1/2
✓ Branch 22 → 23 taken 16456 times.
✗ Branch 22 → 38 not taken.
16456 templateSymbolTypes.push_back(genericType);
30 }
31 }
32
33
1/2
✓ Branch 33 → 34 taken 16395 times.
✗ Branch 33 → 39 not taken.
32790 return getSignature(name, templateSymbolTypes);
34 16395 }
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 84712 std::string StructBase::getSignature(const std::string &name, const QualTypeList &concreteTemplateTypes) {
47 // Build template type string
48
1/2
✓ Branch 2 → 3 taken 84712 times.
✗ Branch 2 → 48 not taken.
84712 std::stringstream templateTyStr;
49
2/2
✓ Branch 4 → 5 taken 33828 times.
✓ Branch 4 → 17 taken 50884 times.
84712 if (!concreteTemplateTypes.empty()) {
50
1/2
✓ Branch 5 → 6 taken 33828 times.
✗ Branch 5 → 46 not taken.
33828 templateTyStr << "<";
51
2/2
✓ Branch 15 → 7 taken 43128 times.
✓ Branch 15 → 16 taken 33828 times.
76956 for (size_t i = 0; i < concreteTemplateTypes.size(); i++) {
52
2/2
✓ Branch 7 → 8 taken 9300 times.
✓ Branch 7 → 9 taken 33828 times.
43128 if (i > 0)
53
1/2
✓ Branch 8 → 9 taken 9300 times.
✗ Branch 8 → 46 not taken.
9300 templateTyStr << ",";
54
3/6
✓ Branch 9 → 10 taken 43128 times.
✗ Branch 9 → 33 not taken.
✓ Branch 10 → 11 taken 43128 times.
✗ Branch 10 → 33 not taken.
✓ Branch 11 → 12 taken 43128 times.
✗ Branch 11 → 31 not taken.
43128 templateTyStr << concreteTemplateTypes.at(i).getName(false, true);
55 }
56
1/2
✓ Branch 16 → 17 taken 33828 times.
✗ Branch 16 → 46 not taken.
33828 templateTyStr << ">";
57 }
58
59
4/8
✓ Branch 17 → 18 taken 84712 times.
✗ Branch 17 → 45 not taken.
✓ Branch 20 → 21 taken 84712 times.
✗ Branch 20 → 38 not taken.
✓ Branch 21 → 22 taken 84712 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 84712 times.
✗ Branch 22 → 34 not taken.
423560 return CommonUtil::getLastFragment(name, SCOPE_ACCESS_TOKEN) + templateTyStr.str();
60 84712 }
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 16538 bool StructBase::hasSubstantiatedGenerics() const {
69 11177 const auto pred = [](const GenericType &genericType) { return genericType.hasAnyGenericParts(); };
70
1/2
✓ Branch 2 → 3 taken 16538 times.
✗ Branch 2 → 6 not taken.
33076 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 16538 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 4120 QualTypeList StructBase::getTemplateTypes() const {
87 4120 QualTypeList templateSymbolTypes;
88
2/2
✓ Branch 16 → 4 taken 3754 times.
✓ Branch 16 → 17 taken 4120 times.
11994 for (const GenericType &genericTemplateType : templateTypes)
89
1/2
✓ Branch 6 → 7 taken 3754 times.
✗ Branch 6 → 19 not taken.
3754 templateSymbolTypes.push_back(genericTemplateType);
90 4120 return templateSymbolTypes;
91 }
92
93 /**
94 * Retrieve the declaration code location of this struct
95 *
96 * @return Declaration code location
97 */
98 15967 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 113801 bool StructBase::isGenericSubstantiation() const { return genericPreset != nullptr; }
106
107 } // namespace spice::compiler
108