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 3159 StructBase::StructBase(std::string name, SymbolTableEntry *entry, Scope *scope, std::vector<GenericType> templateTypes,
13 ASTNode *declNode)
14 9477 : 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 17474 std::string StructBase::getSignature() const {
22 17474 QualTypeList templateSymbolTypes;
23
1/2
✓ Branch 3 → 4 taken 17474 times.
✗ Branch 3 → 39 not taken.
17474 templateSymbolTypes.reserve(templateTypes.size());
24
2/2
✓ Branch 32 → 6 taken 19118 times.
✓ Branch 32 → 33 taken 17474 times.
54066 for (const GenericType &genericType : templateTypes) {
25
7/8
✓ Branch 8 → 9 taken 19118 times.
✗ Branch 8 → 38 not taken.
✓ Branch 9 → 10 taken 3011 times.
✓ Branch 9 → 13 taken 16107 times.
✓ Branch 11 → 12 taken 1843 times.
✓ Branch 11 → 13 taken 1168 times.
✓ Branch 14 → 15 taken 1843 times.
✓ Branch 14 → 22 taken 17275 times.
19118 if (genericType.is(TY_GENERIC) && !typeMapping.empty()) {
26
3/6
✓ Branch 15 → 16 taken 1843 times.
✗ Branch 15 → 38 not taken.
✓ Branch 16 → 17 taken 1843 times.
✗ Branch 16 → 38 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 1843 times.
1843 assert(typeMapping.contains(genericType.getSubType()));
27
3/6
✓ Branch 19 → 20 taken 1843 times.
✗ Branch 19 → 38 not taken.
✓ Branch 20 → 21 taken 1843 times.
✗ Branch 20 → 38 not taken.
✓ Branch 21 → 23 taken 1843 times.
✗ Branch 21 → 38 not taken.
1843 templateSymbolTypes.push_back(typeMapping.at(genericType.getSubType()));
28 } else {
29
1/2
✓ Branch 22 → 23 taken 17275 times.
✗ Branch 22 → 38 not taken.
17275 templateSymbolTypes.push_back(genericType);
30 }
31 }
32
33
1/2
✓ Branch 33 → 34 taken 17474 times.
✗ Branch 33 → 39 not taken.
34948 return getSignature(name, templateSymbolTypes);
34 17474 }
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 94153 std::string StructBase::getSignature(const std::string &name, const QualTypeList &concreteTemplateTypes) {
47 // Build template type string
48
1/2
✓ Branch 2 → 3 taken 94153 times.
✗ Branch 2 → 48 not taken.
94153 std::stringstream templateTyStr;
49
2/2
✓ Branch 4 → 5 taken 35922 times.
✓ Branch 4 → 17 taken 58231 times.
94153 if (!concreteTemplateTypes.empty()) {
50
1/2
✓ Branch 5 → 6 taken 35922 times.
✗ Branch 5 → 46 not taken.
35922 templateTyStr << "<";
51
2/2
✓ Branch 15 → 7 taken 45426 times.
✓ Branch 15 → 16 taken 35922 times.
81348 for (size_t i = 0; i < concreteTemplateTypes.size(); i++) {
52
2/2
✓ Branch 7 → 8 taken 9504 times.
✓ Branch 7 → 9 taken 35922 times.
45426 if (i > 0)
53
1/2
✓ Branch 8 → 9 taken 9504 times.
✗ Branch 8 → 46 not taken.
9504 templateTyStr << ",";
54
3/6
✓ Branch 9 → 10 taken 45426 times.
✗ Branch 9 → 33 not taken.
✓ Branch 10 → 11 taken 45426 times.
✗ Branch 10 → 33 not taken.
✓ Branch 11 → 12 taken 45426 times.
✗ Branch 11 → 31 not taken.
45426 templateTyStr << concreteTemplateTypes.at(i).getName(false, true);
55 }
56
1/2
✓ Branch 16 → 17 taken 35922 times.
✗ Branch 16 → 46 not taken.
35922 templateTyStr << ">";
57 }
58
59
4/8
✓ Branch 17 → 18 taken 94153 times.
✗ Branch 17 → 45 not taken.
✓ Branch 20 → 21 taken 94153 times.
✗ Branch 20 → 38 not taken.
✓ Branch 21 → 22 taken 94153 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 94153 times.
✗ Branch 22 → 34 not taken.
470765 return CommonUtil::getLastFragment(name, SCOPE_ACCESS_TOKEN) + templateTyStr.str();
60 94153 }
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 18041 bool StructBase::hasSubstantiatedGenerics() const {
69 11806 const auto pred = [](const GenericType &genericType) { return genericType.hasAnyGenericParts(); };
70
1/2
✓ Branch 2 → 3 taken 18041 times.
✗ Branch 2 → 6 not taken.
36082 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 18041 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 4280 QualTypeList StructBase::getTemplateTypes() const {
87 4280 QualTypeList templateSymbolTypes;
88
2/2
✓ Branch 16 → 4 taken 3937 times.
✓ Branch 16 → 17 taken 4280 times.
12497 for (const GenericType &genericTemplateType : templateTypes)
89
1/2
✓ Branch 6 → 7 taken 3937 times.
✗ Branch 6 → 19 not taken.
3937 templateSymbolTypes.push_back(genericTemplateType);
90 4280 return templateSymbolTypes;
91 }
92
93 /**
94 * Retrieve the declaration code location of this struct
95 *
96 * @return Declaration code location
97 */
98 18375 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 128030 bool StructBase::isGenericSubstantiation() const { return genericPreset != nullptr; }
106
107 } // namespace spice::compiler
108