GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 74 / 6 / 80
Functions: 100.0% 7 / 0 / 7
Branches: 83.3% 55 / 12 / 78

src/symboltablebuilder/TypeQualifiers.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "TypeQualifiers.h"
4
5 #include <exception/CompilerError.h>
6 #include <symboltablebuilder/TypeChain.h>
7
8 namespace spice::compiler {
9
10 /**
11 * Get default type qualifiers for a given super type
12 *
13 * @param superType Super type
14 * @return Default type qualifiers
15 */
16 10924349 TypeQualifiers TypeQualifiers::of(uint16_t superType) {
17
5/6
✓ Branch 2 → 3 taken 946336 times.
✓ Branch 2 → 5 taken 2216980 times.
✓ Branch 2 → 7 taken 585814 times.
✓ Branch 2 → 9 taken 15143 times.
✓ Branch 2 → 11 taken 7160076 times.
✗ Branch 2 → 13 not taken.
10924349 switch (superType) {
18 946336 case TY_DOUBLE: // fall-through
19 case TY_INT: // fall-through
20 case TY_SHORT: // fall-through
21 case TY_LONG:
22 946336 return {/*const*/ false, /*signed*/ true, /*unsigned*/ false};
23 2216980 case TY_BYTE: // fall-through
24 case TY_CHAR: // fall-through
25 case TY_STRING: // fall-through
26 case TY_BOOL: // fall-through
27 case TY_PTR: // fall-through
28 case TY_REF: // fall-through
29 case TY_ARRAY: // fall-through
30 case TY_STRUCT: // fall-through
31 case TY_INTERFACE: // fall-through
32 case TY_UNION: // fall-through
33 case TY_FUNCTION: // fall-through
34 case TY_PROCEDURE:
35 2216980 return {/*const*/ false, /*signed*/ false, /*unsigned*/ true};
36 585814 case TY_GENERIC:
37 // Generics must be non-signed and non-unsigned at the same time to ensure a proper function matching
38 585814 return {/*const*/ false, /*signed*/ false, /*unsigned*/ false};
39 15143 case TY_ENUM: // fall-through
40 case TY_ALIAS: // fall-through
41 case TY_IMPORT:
42 15143 return {/*const*/ true, /*signed*/ false, /*unsigned*/ true};
43 7160076 case TY_DYN: // fall-through
44 case TY_INVALID: // fall-through
45 case TY_UNRESOLVED:
46 // Return all-false qualifiers to not match anything
47 7160076 return {/*const*/ false, /*signed*/ false, /*unsigned*/ false};
48 − default: // GCOV_EXCL_LINE
49 − throw CompilerError(UNHANDLED_BRANCH, "Symbol qualifier fallthrough"); // GCOV_EXCL_LINE
50 }
51 }
52
53 /**
54 * Merge two sets of type qualifiers. If possible, prefer the opposite of the default of the super type
55 *
56 * @param other Other type qualifiers object
57 * @return Merged qualifiers object
58 */
59 122915 TypeQualifiers TypeQualifiers::merge(const TypeQualifiers &other) const {
60 122915 TypeQualifiers result;
61
6/8
✓ Branch 2 → 3 taken 122915 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 116382 times.
✓ Branch 3 → 7 taken 6533 times.
✓ Branch 4 → 5 taken 116382 times.
✗ Branch 4 → 23 not taken.
✓ Branch 5 → 6 taken 104563 times.
✓ Branch 5 → 7 taken 11819 times.
122915 const bool isGeneric = !getBit(BIT_INDEX_SIGNED) && !getBit(BIT_INDEX_UNSIGNED);
62
2/2
✓ Branch 20 → 9 taken 860405 times.
✓ Branch 20 → 21 taken 122915 times.
983320 for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) {
63
1/2
✓ Branch 9 → 10 taken 860405 times.
✗ Branch 9 → 23 not taken.
860405 const bool x = getBit(i);
64
1/2
✓ Branch 10 → 11 taken 860405 times.
✗ Branch 10 → 23 not taken.
860405 const bool y = other.getBit(i);
65
66
4/4
✓ Branch 11 → 12 taken 737490 times.
✓ Branch 11 → 13 taken 122915 times.
✓ Branch 12 → 13 taken 122915 times.
✓ Branch 12 → 18 taken 614575 times.
860405 if (i == BIT_INDEX_SIGNED || i == BIT_INDEX_UNSIGNED) {
67
3/4
✓ Branch 13 → 14 taken 209126 times.
✓ Branch 13 → 15 taken 36704 times.
✓ Branch 16 → 17 taken 245830 times.
✗ Branch 16 → 23 not taken.
245830 result.setBit(i, isGeneric ? y : x);
68 } else {
69
1/2
✓ Branch 18 → 19 taken 614575 times.
✗ Branch 18 → 23 not taken.
614575 result.setBit(i, x | y);
70 }
71 }
72 122915 return result;
73 }
74
75 /**
76 * Check if two sets of type qualifiers match
77 *
78 * @param other The rhs qualifiers
79 * @param allowConstify Match when the types are the same, but the lhs type is more const restrictive than the rhs type
80 * @return Matching or not
81 */
82 78362 bool TypeQualifiers::match(TypeQualifiers other, bool allowConstify) const {
83 78362 const TypeQualifiers thisQualifiers = *this;
84
85 // If allowConstify is enabled, only allow to match lhs=const and rhs=non-const
86
4/4
✓ Branch 2 → 3 taken 78322 times.
✓ Branch 2 → 5 taken 40 times.
✓ Branch 3 → 4 taken 8235 times.
✓ Branch 3 → 5 taken 70087 times.
78362 if (allowConstify && thisQualifiers.isConst)
87 8235 other.isConst = true;
88
89 // Check if qualifiers are equal
90 78362 return thisQualifiers == other;
91 }
92
93 /**
94 * Erase all qualifiers that are set in the mask. This is used in type matching.
95 *
96 * @param mask Bitmask to erase with
97 */
98 35194 void TypeQualifiers::eraseWithMask(const TypeQualifiers &mask) {
99 // Zero out all bits that are set in the mask
100
2/2
✓ Branch 11 → 3 taken 246358 times.
✓ Branch 11 → 12 taken 35194 times.
281552 for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) {
101
2/2
✓ Branch 4 → 5 taken 50580 times.
✓ Branch 4 → 10 taken 195778 times.
246358 if (mask.getBit(i)) {
102 // Zero out the bit
103 50580 setBit(i, false);
104
105 // If we set the signed/unsigned bit to zero, we need to set the other to one
106
2/2
✓ Branch 6 → 7 taken 12903 times.
✓ Branch 6 → 8 taken 37677 times.
50580 if (i == BIT_INDEX_SIGNED) {
107 12903 setBit(BIT_INDEX_UNSIGNED, true);
108
2/2
✓ Branch 8 → 9 taken 19692 times.
✓ Branch 8 → 10 taken 17985 times.
37677 } else if (i == BIT_INDEX_UNSIGNED) {
109 19692 setBit(BIT_INDEX_SIGNED, true);
110 }
111 }
112 }
113 35194 }
114
115 79435 bool operator==(const TypeQualifiers &lhs, const TypeQualifiers &rhs) {
116 79435 const bool isConst = lhs.isConst == rhs.isConst;
117 79435 const bool isSigned = lhs.isSigned == rhs.isSigned;
118 79435 const bool isUnsigned = lhs.isUnsigned == rhs.isUnsigned;
119 79435 const bool isHeap = lhs.isHeap == rhs.isHeap;
120
6/8
✓ Branch 2 → 3 taken 79349 times.
✓ Branch 2 → 7 taken 86 times.
✓ Branch 3 → 4 taken 79349 times.
✗ Branch 3 → 7 not taken.
✓ Branch 4 → 5 taken 79349 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 78278 times.
✓ Branch 5 → 7 taken 1071 times.
79435 return isConst && isSigned && isUnsigned && isHeap;
121 }
122
123 2206465 bool TypeQualifiers::getBit(uint8_t index) const {
124
7/8
✓ Branch 2 → 3 taken 281024 times.
✓ Branch 2 → 4 taken 403939 times.
✓ Branch 2 → 5 taken 397406 times.
✓ Branch 2 → 6 taken 281024 times.
✓ Branch 2 → 7 taken 281024 times.
✓ Branch 2 → 8 taken 281024 times.
✓ Branch 2 → 9 taken 281024 times.
✗ Branch 2 → 10 not taken.
2206465 switch (index) {
125 281024 case BIT_INDEX_CONST:
126 281024 return isConst;
127 403939 case BIT_INDEX_SIGNED:
128 403939 return isSigned;
129 397406 case BIT_INDEX_UNSIGNED:
130 397406 return isUnsigned;
131 281024 case BIT_INDEX_HEAP:
132 281024 return isHeap;
133 281024 case BIT_INDEX_PUBLIC:
134 281024 return isPublic;
135 281024 case BIT_INDEX_INLINE:
136 281024 return isInline;
137 281024 case BIT_INDEX_COMPOSITION:
138 281024 return isComposition;
139 − default: // GCOV_EXCL_LINE
140 − throw CompilerError(UNHANDLED_BRANCH, "Bit index fallthrough"); // GCOV_EXCL_LINE
141 }
142 }
143
144 943580 bool TypeQualifiers::setBit(uint8_t index, bool value) {
145
7/8
✓ Branch 2 → 3 taken 128265 times.
✓ Branch 2 → 4 taken 155510 times.
✓ Branch 2 → 5 taken 155510 times.
✓ Branch 2 → 6 taken 124066 times.
✓ Branch 2 → 7 taken 134399 times.
✓ Branch 2 → 8 taken 122915 times.
✓ Branch 2 → 9 taken 122915 times.
✗ Branch 2 → 10 not taken.
943580 switch (index) {
146 128265 case BIT_INDEX_CONST:
147 128265 return isConst = value;
148 155510 case BIT_INDEX_SIGNED:
149 155510 return isSigned = value;
150 155510 case BIT_INDEX_UNSIGNED:
151 155510 return isUnsigned = value;
152 124066 case BIT_INDEX_HEAP:
153 124066 return isHeap = value;
154 134399 case BIT_INDEX_PUBLIC:
155 134399 return isPublic = value;
156 122915 case BIT_INDEX_INLINE:
157 122915 return isInline = value;
158 122915 case BIT_INDEX_COMPOSITION:
159 122915 return isComposition = value;
160 − default: // GCOV_EXCL_LINE
161 − throw CompilerError(UNHANDLED_BRANCH, "Bit index fallthrough"); // GCOV_EXCL_LINE
162 }
163 }
164
165 } // namespace spice::compiler
166