GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 94.4% 34 / 0 / 36
Functions: 100.0% 10 / 0 / 10
Branches: 62.5% 40 / 0 / 64

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 8270 StructBase::StructBase(std::string name, SymbolTableEntry *entry, Scope *scope, std::vector<GenericType> templateTypes,
13 ASTNode *declNode)
14 24810 : 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 55483 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 55483 times.
✗ Branch 3 → 8 not taken.
110966 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 251074 std::string StructBase::getSignature(const std::string &name, const QualTypeList &concreteTemplateTypes) {
34 // Build template type string
35
1/2
✓ Branch 2 → 3 taken 251074 times.
✗ Branch 2 → 48 not taken.
251074 std::stringstream templateTyStr;
36
2/2
✓ Branch 4 → 5 taken 89128 times.
✓ Branch 4 → 17 taken 161946 times.
251074 if (!concreteTemplateTypes.empty()) {
37
1/2
✓ Branch 5 → 6 taken 89128 times.
✗ Branch 5 → 46 not taken.
89128 templateTyStr << "<";
38
2/2
✓ Branch 15 → 7 taken 115140 times.
✓ Branch 15 → 16 taken 89128 times.
204268 for (size_t i = 0; i < concreteTemplateTypes.size(); i++) {
39
2/2
✓ Branch 7 → 8 taken 26012 times.
✓ Branch 7 → 9 taken 89128 times.
115140 if (i > 0)
40
1/2
✓ Branch 8 → 9 taken 26012 times.
✗ Branch 8 → 46 not taken.
26012 templateTyStr << ",";
41
3/6
✓ Branch 9 → 10 taken 115140 times.
✗ Branch 9 → 33 not taken.
✓ Branch 10 → 11 taken 115140 times.
✗ Branch 10 → 33 not taken.
✓ Branch 11 → 12 taken 115140 times.
✗ Branch 11 → 31 not taken.
115140 templateTyStr << concreteTemplateTypes.at(i).getName(false, true);
42 }
43
1/2
✓ Branch 16 → 17 taken 89128 times.
✗ Branch 16 → 46 not taken.
89128 templateTyStr << ">";
44 }
45
46
4/8
✓ Branch 17 → 18 taken 251074 times.
✗ Branch 17 → 45 not taken.
✓ Branch 20 → 21 taken 251074 times.
✗ Branch 20 → 38 not taken.
✓ Branch 21 → 22 taken 251074 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 251074 times.
✗ Branch 22 → 34 not taken.
1255370 return CommonUtil::getLastFragment(name, SCOPE_ACCESS_TOKEN) + templateTyStr.str();
47 251074 }
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 53179 bool StructBase::hasSubstantiatedGenerics() const {
56 37427 const auto pred = [](const GenericType &genericType) { return genericType.hasAnyGenericParts(); };
57
1/2
✓ Branch 2 → 3 taken 53179 times.
✗ Branch 2 → 6 not taken.
106358 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 53179 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 62510 QualTypeList StructBase::getConcreteTemplateTypes() const {
75 62510 QualTypeList concreteTemplateTypes;
76
1/2
✓ Branch 3 → 4 taken 62510 times.
✗ Branch 3 → 36 not taken.
62510 concreteTemplateTypes.reserve(templateTypes.size());
77
2/2
✓ Branch 32 → 6 taken 62044 times.
✓ Branch 32 → 33 taken 62510 times.
187064 for (const GenericType &genericType : templateTypes) {
78
7/8
✓ Branch 8 → 9 taken 62044 times.
✗ Branch 8 → 35 not taken.
✓ Branch 9 → 10 taken 7109 times.
✓ Branch 9 → 13 taken 54935 times.
✓ Branch 11 → 12 taken 4312 times.
✓ Branch 11 → 13 taken 2797 times.
✓ Branch 14 → 15 taken 4312 times.
✓ Branch 14 → 22 taken 57732 times.
62044 if (genericType.is(TY_GENERIC) && !typeMapping.empty()) {
79
3/6
✓ Branch 15 → 16 taken 4312 times.
✗ Branch 15 → 35 not taken.
✓ Branch 16 → 17 taken 4312 times.
✗ Branch 16 → 35 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 4312 times.
4312 assert(typeMapping.contains(genericType.getSubType()));
80
3/6
✓ Branch 19 → 20 taken 4312 times.
✗ Branch 19 → 35 not taken.
✓ Branch 20 → 21 taken 4312 times.
✗ Branch 20 → 35 not taken.
✓ Branch 21 → 23 taken 4312 times.
✗ Branch 21 → 35 not taken.
4312 concreteTemplateTypes.push_back(typeMapping.at(genericType.getSubType()));
81 } else {
82
1/2
✓ Branch 22 → 23 taken 57732 times.
✗ Branch 22 → 35 not taken.
57732 concreteTemplateTypes.push_back(genericType);
83 }
84 }
85 62510 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 10066 QualTypeList StructBase::getTemplateTypes() const {
94 10066 QualTypeList templateSymbolTypes;
95
2/2
✓ Branch 16 → 4 taken 8664 times.
✓ Branch 16 → 17 taken 10066 times.
28796 for (const GenericType &genericTemplateType : templateTypes)
96
1/2
✓ Branch 6 → 7 taken 8664 times.
✗ Branch 6 → 19 not taken.
8664 templateSymbolTypes.push_back(genericTemplateType);
97 10066 return templateSymbolTypes;
98 }
99
100 /**
101 * Retrieve the declaration code location of this struct
102 *
103 * @return Declaration code location
104 */
105 63412 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 293702 bool StructBase::isGenericSubstantiation() const { return genericPreset != nullptr; }
113
114 } // namespace spice::compiler
115