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 6085643 TypeQualifiers TypeQualifiers::of(uint16_t superType) {
17
5/6
✓ Branch 2 → 3 taken 518300 times.
✓ Branch 2 → 5 taken 1546617 times.
✓ Branch 2 → 7 taken 399845 times.
✓ Branch 2 → 9 taken 11785 times.
✓ Branch 2 → 11 taken 3609096 times.
✗ Branch 2 → 13 not taken.
6085643 switch (superType) {
18 518300 case TY_DOUBLE: // fall-through
19 case TY_INT: // fall-through
20 case TY_SHORT: // fall-through
21 case TY_LONG:
22 518300 return {/*const*/ false, /*signed*/ true, /*unsigned*/ false};
23 1546617 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_FUNCTION: // fall-through
33 case TY_PROCEDURE:
34 1546617 return {/*const*/ false, /*signed*/ false, /*unsigned*/ true};
35 399845 case TY_GENERIC:
36 // Generics must be non-signed and non-unsigned at the same time to ensure a proper function matching
37 399845 return {/*const*/ false, /*signed*/ false, /*unsigned*/ false};
38 11785 case TY_ENUM: // fall-through
39 case TY_ALIAS: // fall-through
40 case TY_IMPORT:
41 11785 return {/*const*/ true, /*signed*/ false, /*unsigned*/ true};
42 3609096 case TY_DYN: // fall-through
43 case TY_INVALID: // fall-through
44 case TY_UNRESOLVED:
45 // Return all-false qualifiers to not match anything
46 3609096 return {/*const*/ false, /*signed*/ false, /*unsigned*/ false};
47 default: // GCOV_EXCL_LINE
48 throw CompilerError(UNHANDLED_BRANCH, "Symbol qualifier fallthrough"); // GCOV_EXCL_LINE
49 }
50 }
51
52 /**
53 * Merge two sets of type qualifiers. If possible, prefer the opposite of the default of the super type
54 *
55 * @param other Other type qualifiers object
56 * @return Merged qualifiers object
57 */
58 81659 TypeQualifiers TypeQualifiers::merge(const TypeQualifiers &other) const {
59 81659 TypeQualifiers result;
60
6/8
✓ Branch 2 → 3 taken 81659 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 79063 times.
✓ Branch 3 → 7 taken 2596 times.
✓ Branch 4 → 5 taken 79063 times.
✗ Branch 4 → 23 not taken.
✓ Branch 5 → 6 taken 71991 times.
✓ Branch 5 → 7 taken 7072 times.
81659 const bool isGeneric = !getBit(BIT_INDEX_SIGNED) && !getBit(BIT_INDEX_UNSIGNED);
61
2/2
✓ Branch 20 → 9 taken 571613 times.
✓ Branch 20 → 21 taken 81659 times.
653272 for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) {
62
1/2
✓ Branch 9 → 10 taken 571613 times.
✗ Branch 9 → 23 not taken.
571613 const bool x = getBit(i);
63
1/2
✓ Branch 10 → 11 taken 571613 times.
✗ Branch 10 → 23 not taken.
571613 const bool y = other.getBit(i);
64
65
4/4
✓ Branch 11 → 12 taken 489954 times.
✓ Branch 11 → 13 taken 81659 times.
✓ Branch 12 → 13 taken 81659 times.
✓ Branch 12 → 18 taken 408295 times.
571613 if (i == BIT_INDEX_SIGNED || i == BIT_INDEX_UNSIGNED) {
66
3/4
✓ Branch 13 → 14 taken 143982 times.
✓ Branch 13 → 15 taken 19336 times.
✓ Branch 16 → 17 taken 163318 times.
✗ Branch 16 → 23 not taken.
163318 result.setBit(i, isGeneric ? y : x);
67 } else {
68
1/2
✓ Branch 18 → 19 taken 408295 times.
✗ Branch 18 → 23 not taken.
408295 result.setBit(i, x | y);
69 }
70 }
71 81659 return result;
72 }
73
74 /**
75 * Check if two sets of type qualifiers match
76 *
77 * @param other The rhs qualifiers
78 * @param allowConstify Match when the types are the same, but the lhs type is more const restrictive than the rhs type
79 * @return Matching or not
80 */
81 65988 bool TypeQualifiers::match(TypeQualifiers other, bool allowConstify) const {
82 65988 const TypeQualifiers thisQualifiers = *this;
83
84 // If allowConstify is enabled, only allow to match lhs=const and rhs=non-const
85
4/4
✓ Branch 2 → 3 taken 65973 times.
✓ Branch 2 → 5 taken 15 times.
✓ Branch 3 → 4 taken 8564 times.
✓ Branch 3 → 5 taken 57409 times.
65988 if (allowConstify && thisQualifiers.isConst)
86 8564 other.isConst = true;
87
88 // Check if qualifiers are equal
89 65988 return thisQualifiers == other;
90 }
91
92 /**
93 * Erase all qualifiers that are set in the mask. This is used in type matching.
94 *
95 * @param mask Bitmask to erase with
96 */
97 23357 void TypeQualifiers::eraseWithMask(const TypeQualifiers &mask) {
98 // Zero out all bits that are set in the mask
99
2/2
✓ Branch 11 → 3 taken 163499 times.
✓ Branch 11 → 12 taken 23357 times.
186856 for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) {
100
2/2
✓ Branch 4 → 5 taken 36699 times.
✓ Branch 4 → 10 taken 126800 times.
163499 if (mask.getBit(i)) {
101 // Zero out the bit
102 36699 setBit(i, false);
103
104 // If we set the signed/unsigned bit to zero, we need to set the other to one
105
2/2
✓ Branch 6 → 7 taken 5765 times.
✓ Branch 6 → 8 taken 30934 times.
36699 if (i == BIT_INDEX_SIGNED) {
106 5765 setBit(BIT_INDEX_UNSIGNED, true);
107
2/2
✓ Branch 8 → 9 taken 15892 times.
✓ Branch 8 → 10 taken 15042 times.
30934 } else if (i == BIT_INDEX_UNSIGNED) {
108 15892 setBit(BIT_INDEX_SIGNED, true);
109 }
110 }
111 }
112 23357 }
113
114 66400 bool operator==(const TypeQualifiers &lhs, const TypeQualifiers &rhs) {
115 66400 const bool isConst = lhs.isConst == rhs.isConst;
116 66400 const bool isSigned = lhs.isSigned == rhs.isSigned;
117 66400 const bool isUnsigned = lhs.isUnsigned == rhs.isUnsigned;
118 66400 const bool isHeap = lhs.isHeap == rhs.isHeap;
119
6/8
✓ Branch 2 → 3 taken 66305 times.
✓ Branch 2 → 7 taken 95 times.
✓ Branch 3 → 4 taken 66305 times.
✗ Branch 3 → 7 not taken.
✓ Branch 4 → 5 taken 66305 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 65894 times.
✓ Branch 5 → 7 taken 411 times.
66400 return isConst && isSigned && isUnsigned && isHeap;
120 }
121
122 1467447 bool TypeQualifiers::getBit(uint8_t index) const {
123
7/8
✓ Branch 2 → 3 taken 186675 times.
✓ Branch 2 → 4 taken 268334 times.
✓ Branch 2 → 5 taken 265738 times.
✓ Branch 2 → 6 taken 186675 times.
✓ Branch 2 → 7 taken 186675 times.
✓ Branch 2 → 8 taken 186675 times.
✓ Branch 2 → 9 taken 186675 times.
✗ Branch 2 → 10 not taken.
1467447 switch (index) {
124 186675 case BIT_INDEX_CONST:
125 186675 return isConst;
126 268334 case BIT_INDEX_SIGNED:
127 268334 return isSigned;
128 265738 case BIT_INDEX_UNSIGNED:
129 265738 return isUnsigned;
130 186675 case BIT_INDEX_HEAP:
131 186675 return isHeap;
132 186675 case BIT_INDEX_PUBLIC:
133 186675 return isPublic;
134 186675 case BIT_INDEX_INLINE:
135 186675 return isInline;
136 186675 case BIT_INDEX_COMPOSITION:
137 186675 return isComposition;
138 default: // GCOV_EXCL_LINE
139 throw CompilerError(UNHANDLED_BRANCH, "Bit index fallthrough"); // GCOV_EXCL_LINE
140 }
141 }
142
143 629969 bool TypeQualifiers::setBit(uint8_t index, bool value) {
144
7/8
✓ Branch 2 → 3 taken 85654 times.
✓ Branch 2 → 4 taken 103316 times.
✓ Branch 2 → 5 taken 103316 times.
✓ Branch 2 → 6 taken 82010 times.
✓ Branch 2 → 7 taken 92355 times.
✓ Branch 2 → 8 taken 81659 times.
✓ Branch 2 → 9 taken 81659 times.
✗ Branch 2 → 10 not taken.
629969 switch (index) {
145 85654 case BIT_INDEX_CONST:
146 85654 return isConst = value;
147 103316 case BIT_INDEX_SIGNED:
148 103316 return isSigned = value;
149 103316 case BIT_INDEX_UNSIGNED:
150 103316 return isUnsigned = value;
151 82010 case BIT_INDEX_HEAP:
152 82010 return isHeap = value;
153 92355 case BIT_INDEX_PUBLIC:
154 92355 return isPublic = value;
155 81659 case BIT_INDEX_INLINE:
156 81659 return isInline = value;
157 81659 case BIT_INDEX_COMPOSITION:
158 81659 return isComposition = value;
159 default: // GCOV_EXCL_LINE
160 throw CompilerError(UNHANDLED_BRANCH, "Bit index fallthrough"); // GCOV_EXCL_LINE
161 }
162 }
163
164 } // namespace spice::compiler
165