GCC Code Coverage Report


Directory: ../
File: src/symboltablebuilder/TypeChain.cpp
Date: 2025-08-26 18:26:32
Exec Total Coverage
Lines: 112 118 94.9%
Functions: 4 4 100.0%
Branches: 70 95 73.7%

Line Branch Exec Source
1 // Copyright (c) 2021-2025 ChilliBits. All rights reserved.
2
3 #include "TypeChain.h"
4
5 #include <exception/CompilerError.h>
6
7 namespace spice::compiler {
8
9 92146 bool operator==(const TypeChainElement &lhs, const TypeChainElement &rhs) {
10 // Check super type
11
2/2
✓ Branch 0 (2→3) taken 29389 times.
✓ Branch 1 (2→4) taken 62757 times.
92146 if (lhs.superType != rhs.superType)
12 29389 return false;
13
14 // Check data
15
6/7
✓ Branch 0 (4→5) taken 108 times.
✓ Branch 1 (4→6) taken 21517 times.
✓ Branch 2 (4→16) taken 362 times.
✗ Branch 3 (4→17) not taken.
✓ Branch 4 (4→26) taken 105 times.
✓ Branch 5 (4→40) taken 4526 times.
✓ Branch 6 (4→41) taken 36139 times.
62757 switch (lhs.superType) {
16 108 case TY_ARRAY:
17 108 return lhs.data.arraySize == rhs.data.arraySize;
18 21517 case TY_STRUCT:
19
2/4
✓ Branch 0 (6→7) taken 21517 times.
✗ Branch 1 (6→9) not taken.
✓ Branch 2 (7→8) taken 21517 times.
✗ Branch 3 (7→9) not taken.
21517 assert(lhs.data.bodyScope != nullptr && rhs.data.bodyScope != nullptr);
20
4/4
✓ Branch 0 (10→11) taken 20633 times.
✓ Branch 1 (10→14) taken 884 times.
✓ Branch 2 (12→13) taken 20620 times.
✓ Branch 3 (12→14) taken 13 times.
21517 return lhs.typeId == rhs.typeId && lhs.templateTypes == rhs.templateTypes;
21 362 case TY_INTERFACE:
22 362 return lhs.typeId == rhs.typeId;
23 case TY_ENUM:
24 assert(lhs.data.bodyScope != nullptr && rhs.data.bodyScope != nullptr);
25 return lhs.typeId == rhs.typeId && lhs.data.bodyScope == rhs.data.bodyScope;
26 105 case TY_FUNCTION: // fall-through
27 case TY_PROCEDURE:
28
1/2
✗ Branch 0 (28→29) not taken.
✓ Branch 1 (28→30) taken 105 times.
105 if (lhs.paramTypes.size() != rhs.paramTypes.size())
29 return false;
30
2/2
✓ Branch 0 (38→31) taken 160 times.
✓ Branch 1 (38→39) taken 103 times.
263 for (size_t i = 0; i < lhs.paramTypes.size(); i++)
31
2/2
✓ Branch 0 (34→35) taken 2 times.
✓ Branch 1 (34→36) taken 158 times.
160 if (lhs.paramTypes.at(i) != rhs.paramTypes.at(i))
32 2 return false;
33 103 return true;
34 4526 case TY_GENERIC:
35 4526 return lhs.subType == rhs.subType;
36 36139 default:
37 36139 return true;
38 }
39 }
40
41 92146 bool operator!=(const TypeChainElement &lhs, const TypeChainElement &rhs) { return !(lhs == rhs); }
42
43 464989 void TypeChainElement::getName(std::stringstream &name, bool withSize, bool ignorePublic) const {
44
19/20
✓ Branch 0 (2→3) taken 42226 times.
✓ Branch 1 (2→4) taken 48951 times.
✓ Branch 2 (2→6) taken 493 times.
✓ Branch 3 (2→20) taken 2280 times.
✓ Branch 4 (2→22) taken 28040 times.
✓ Branch 5 (2→24) taken 2775 times.
✓ Branch 6 (2→26) taken 38867 times.
✓ Branch 7 (2→28) taken 11901 times.
✓ Branch 8 (2→30) taken 24013 times.
✓ Branch 9 (2→32) taken 29691 times.
✓ Branch 10 (2→34) taken 21979 times.
✓ Branch 11 (2→36) taken 87543 times.
✓ Branch 12 (2→50) taken 127 times.
✓ Branch 13 (2→54) taken 68697 times.
✓ Branch 14 (2→56) taken 164 times.
✓ Branch 15 (2→58) taken 26321 times.
✓ Branch 16 (2→78) taken 16638 times.
✓ Branch 17 (2→92) taken 1191 times.
✓ Branch 18 (2→94) taken 13092 times.
✗ Branch 19 (2→96) not taken.
464989 switch (superType) {
45 42226 case TY_PTR:
46 42226 name << "*";
47 42226 break;
48 48951 case TY_REF:
49 48951 name << "&";
50 48951 break;
51 493 case TY_ARRAY:
52
9/14
✓ Branch 0 (7→8) taken 235 times.
✓ Branch 1 (7→10) taken 258 times.
✓ Branch 2 (8→9) taken 113 times.
✓ Branch 3 (8→10) taken 122 times.
✓ Branch 4 (12→13) taken 380 times.
✗ Branch 5 (12→108) not taken.
✓ Branch 6 (13→14) taken 493 times.
✗ Branch 7 (13→106) not taken.
✓ Branch 8 (14→15) taken 493 times.
✗ Branch 9 (14→106) not taken.
✓ Branch 10 (16→17) taken 380 times.
✓ Branch 11 (16→19) taken 113 times.
✗ Branch 12 (108→109) not taken.
✗ Branch 13 (108→111) not taken.
873 name << "[" << (withSize && data.arraySize != ARRAY_SIZE_UNKNOWN ? std::to_string(data.arraySize) : "") << "]";
53 493 break;
54 2280 case TY_DOUBLE:
55 2280 name << "double";
56 2280 break;
57 28040 case TY_INT:
58 28040 name << "int";
59 28040 break;
60 2775 case TY_SHORT:
61 2775 name << "short";
62 2775 break;
63 38867 case TY_LONG:
64 38867 name << "long";
65 38867 break;
66 11901 case TY_BYTE:
67 11901 name << "byte";
68 11901 break;
69 24013 case TY_CHAR:
70 24013 name << "char";
71 24013 break;
72 29691 case TY_STRING:
73 29691 name << "string";
74 29691 break;
75 21979 case TY_BOOL:
76 21979 name << "bool";
77 21979 break;
78 87543 case TY_STRUCT: // fall-through
79 case TY_INTERFACE:
80 87543 name << subType;
81
2/2
✓ Branch 0 (38→39) taken 38907 times.
✓ Branch 1 (38→49) taken 48636 times.
87543 if (!templateTypes.empty()) {
82 38907 name << "<";
83
2/2
✓ Branch 0 (47→41) taken 52528 times.
✓ Branch 1 (47→48) taken 38907 times.
91435 for (size_t i = 0; i < templateTypes.size(); i++) {
84
2/2
✓ Branch 0 (41→42) taken 13621 times.
✓ Branch 1 (41→43) taken 38907 times.
52528 if (i > 0)
85 13621 name << ",";
86 52528 templateTypes.at(i).getName(name, withSize, ignorePublic);
87 }
88 38907 name << ">";
89 }
90 87543 break;
91 127 case TY_ENUM:
92 127 name << "enum(" << subType << ")";
93 127 break;
94 68697 case TY_GENERIC: // fall-through
95 case TY_ALIAS:
96 68697 name << subType;
97 68697 break;
98 164 case TY_DYN:
99 164 name << "dyn";
100 164 break;
101 26321 case TY_FUNCTION: {
102 26321 name << "f";
103
2/2
✓ Branch 0 (59→60) taken 12 times.
✓ Branch 1 (59→61) taken 26309 times.
26321 if (data.hasCaptures)
104 12 name << "[]";
105
2/2
✓ Branch 0 (62→63) taken 26122 times.
✓ Branch 1 (62→67) taken 199 times.
26321 if (!paramTypes.empty()) {
106 26122 name << "<";
107 26122 paramTypes.front().getName(name, true, ignorePublic);
108 26122 name << ">";
109 }
110 26321 name << "(";
111
2/2
✓ Branch 0 (75→69) taken 26888 times.
✓ Branch 1 (75→76) taken 26321 times.
53209 for (size_t i = 1; i < paramTypes.size(); i++) {
112
2/2
✓ Branch 0 (69→70) taken 10308 times.
✓ Branch 1 (69→71) taken 16580 times.
26888 if (i > 1)
113 10308 name << ",";
114 26888 paramTypes.at(i).getName(name, true, ignorePublic);
115 }
116 26321 name << ")";
117 26321 break;
118 }
119 16638 case TY_PROCEDURE: {
120 16638 name << "p";
121
2/2
✓ Branch 0 (79→80) taken 26 times.
✓ Branch 1 (79→81) taken 16612 times.
16638 if (data.hasCaptures)
122 26 name << "[]";
123 16638 name << "(";
124
2/2
✓ Branch 0 (89→83) taken 16382 times.
✓ Branch 1 (89→90) taken 16638 times.
33020 for (size_t i = 1; i < paramTypes.size(); i++) {
125
2/2
✓ Branch 0 (83→84) taken 4322 times.
✓ Branch 1 (83→85) taken 12060 times.
16382 if (i > 1)
126 4322 name << ",";
127 16382 paramTypes.at(i).getName(name, true, ignorePublic);
128 }
129 16638 name << ")";
130 16638 break;
131 }
132 1191 case TY_IMPORT:
133 1191 name << "import";
134 1191 break;
135 case TY_INVALID: // GCOV_EXCL_LINE
136 name << "invalid"; // GCOV_EXCL_LINE
137 13092 break;
138 default:
139 throw CompilerError(INTERNAL_ERROR, "Could not get name of this type chain element");
140 }
141 464989 }
142
143 /**
144 * Return the type name as string
145 *
146 * @param withSize Also encode array sizes
147 * @param ignorePublic Ignore public qualifier
148 * @return Name as string
149 */
150 464989 std::string TypeChainElement::getName(bool withSize, bool ignorePublic) const {
151
1/2
✓ Branch 0 (2→3) taken 464989 times.
✗ Branch 1 (2→11) not taken.
464989 std::stringstream name;
152
1/2
✓ Branch 0 (3→4) taken 464989 times.
✗ Branch 1 (3→9) not taken.
464989 getName(name, withSize, ignorePublic);
153
1/2
✓ Branch 0 (4→5) taken 464989 times.
✗ Branch 1 (4→9) not taken.
929978 return name.str();
154 464989 }
155
156 } // namespace spice::compiler
157