GCC Code Coverage Report


Directory: ../
File: src/symboltablebuilder/TypeQualifiers.cpp
Date: 2025-11-30 14:27:30
Coverage Exec Excl Total
Lines: 96.1% 74 3 80
Functions: 100.0% 7 0 7
Branches: 83.3% 55 12 78

Line Branch Exec Source
1 // Copyright (c) 2021-2025 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 5765155 TypeQualifiers TypeQualifiers::of(uint16_t superType) {
17
5/6
✓ Branch 2 → 3 taken 155861 times.
✓ Branch 2 → 6 taken 346928 times.
✓ Branch 2 → 9 taken 91727 times.
✓ Branch 2 → 12 taken 2056 times.
✓ Branch 2 → 15 taken 5168583 times.
✗ Branch 2 → 18 not taken.
5765155 switch (superType) {
18 155861 case TY_DOUBLE: // fall-through
19 case TY_INT: // fall-through
20 case TY_SHORT: // fall-through
21 case TY_LONG:
22 155861 return {/*const*/ false, /*signed*/ true, /*unsigned*/ false};
23 346928 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 346928 return {/*const*/ false, /*signed*/ false, /*unsigned*/ true};
35 91727 case TY_GENERIC:
36 // Generics must be non-signed and non-unsigned at the same time to ensure a proper function matching
37 91727 return {/*const*/ false, /*signed*/ false, /*unsigned*/ false};
38 2056 case TY_ENUM: // fall-through
39 case TY_ALIAS: // fall-through
40 case TY_IMPORT:
41 2056 return {/*const*/ true, /*signed*/ false, /*unsigned*/ true};
42 5168583 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 5168583 return {/*const*/ false, /*signed*/ false, /*unsigned*/ false};
47 default:
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 24051 TypeQualifiers TypeQualifiers::merge(const TypeQualifiers &other) const {
59 24051 TypeQualifiers result;
60
6/8
✓ Branch 2 → 3 taken 24051 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 22230 times.
✓ Branch 3 → 7 taken 1821 times.
✓ Branch 4 → 5 taken 22230 times.
✗ Branch 4 → 23 not taken.
✓ Branch 5 → 6 taken 20325 times.
✓ Branch 5 → 7 taken 1905 times.
24051 const bool isGeneric = !getBit(BIT_INDEX_SIGNED) && !getBit(BIT_INDEX_UNSIGNED);
61
2/2
✓ Branch 20 → 9 taken 168357 times.
✓ Branch 20 → 21 taken 24051 times.
192408 for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) {
62
1/2
✓ Branch 9 → 10 taken 168357 times.
✗ Branch 9 → 23 not taken.
168357 const bool x = getBit(i);
63
1/2
✓ Branch 10 → 11 taken 168357 times.
✗ Branch 10 → 23 not taken.
168357 const bool y = other.getBit(i);
64
65
4/4
✓ Branch 11 → 12 taken 144306 times.
✓ Branch 11 → 13 taken 24051 times.
✓ Branch 12 → 13 taken 24051 times.
✓ Branch 12 → 18 taken 120255 times.
168357 if (i == BIT_INDEX_SIGNED || i == BIT_INDEX_UNSIGNED) {
66
3/4
✓ Branch 13 → 14 taken 40650 times.
✓ Branch 13 → 15 taken 7452 times.
✓ Branch 16 → 17 taken 48102 times.
✗ Branch 16 → 23 not taken.
48102 result.setBit(i, isGeneric ? y : x);
67 } else {
68
1/2
✓ Branch 18 → 19 taken 120255 times.
✗ Branch 18 → 23 not taken.
120255 result.setBit(i, x | y);
69 }
70 }
71 24051 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 14082 bool TypeQualifiers::match(TypeQualifiers other, bool allowConstify) const {
82 14082 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 14077 times.
✓ Branch 2 → 5 taken 5 times.
✓ Branch 3 → 4 taken 1584 times.
✓ Branch 3 → 5 taken 12493 times.
14082 if (allowConstify && thisQualifiers.isConst)
86 1584 other.isConst = true;
87
88 // Check if qualifiers are equal
89 14082 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 6771 void TypeQualifiers::eraseWithMask(const TypeQualifiers &mask) {
98 // Zero out all bits that are set in the mask
99
2/2
✓ Branch 11 → 3 taken 47397 times.
✓ Branch 11 → 12 taken 6771 times.
54168 for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) {
100
2/2
✓ Branch 4 → 5 taken 8757 times.
✓ Branch 4 → 10 taken 38640 times.
47397 if (mask.getBit(i)) {
101 // Zero out the bit
102 8757 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 2870 times.
✓ Branch 6 → 8 taken 5887 times.
8757 if (i == BIT_INDEX_SIGNED) {
106 2870 setBit(BIT_INDEX_UNSIGNED, true);
107
2/2
✓ Branch 8 → 9 taken 2714 times.
✓ Branch 8 → 10 taken 3173 times.
5887 } else if (i == BIT_INDEX_UNSIGNED) {
108 2714 setBit(BIT_INDEX_SIGNED, true);
109 }
110 }
111 }
112 6771 }
113
114 14235 bool operator==(const TypeQualifiers &lhs, const TypeQualifiers &rhs) {
115 14235 const bool isConst = lhs.isConst == rhs.isConst;
116 14235 const bool isSigned = lhs.isSigned == rhs.isSigned;
117 14235 const bool isUnsigned = lhs.isUnsigned == rhs.isUnsigned;
118 14235 const bool isHeap = lhs.isHeap == rhs.isHeap;
119
6/8
✓ Branch 2 → 3 taken 14210 times.
✓ Branch 2 → 7 taken 25 times.
✓ Branch 3 → 4 taken 14210 times.
✗ Branch 3 → 7 not taken.
✓ Branch 4 → 5 taken 14210 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 14058 times.
✓ Branch 5 → 7 taken 152 times.
14235 return isConst && isSigned && isUnsigned && isHeap;
120 }
121
122 430392 bool TypeQualifiers::getBit(uint8_t index) const {
123
7/8
✓ Branch 2 → 3 taken 54873 times.
✓ Branch 2 → 4 taken 78924 times.
✓ Branch 2 → 5 taken 77103 times.
✓ Branch 2 → 6 taken 54873 times.
✓ Branch 2 → 7 taken 54873 times.
✓ Branch 2 → 8 taken 54873 times.
✓ Branch 2 → 9 taken 54873 times.
✗ Branch 2 → 10 not taken.
430392 switch (index) {
124 54873 case BIT_INDEX_CONST:
125 54873 return isConst;
126 78924 case BIT_INDEX_SIGNED:
127 78924 return isSigned;
128 77103 case BIT_INDEX_UNSIGNED:
129 77103 return isUnsigned;
130 54873 case BIT_INDEX_HEAP:
131 54873 return isHeap;
132 54873 case BIT_INDEX_PUBLIC:
133 54873 return isPublic;
134 54873 case BIT_INDEX_INLINE:
135 54873 return isInline;
136 54873 case BIT_INDEX_COMPOSITION:
137 54873 return isComposition;
138 default:
139 throw CompilerError(UNHANDLED_BRANCH, "Bit index fallthrough"); // GCOV_EXCL_LINE
140 }
141 }
142
143 182698 bool TypeQualifiers::setBit(uint8_t index, bool value) {
144
7/8
✓ Branch 2 → 3 taken 25733 times.
✓ Branch 2 → 4 taken 29635 times.
✓ Branch 2 → 5 taken 29635 times.
✓ Branch 2 → 6 taken 24188 times.
✓ Branch 2 → 7 taken 25405 times.
✓ Branch 2 → 8 taken 24051 times.
✓ Branch 2 → 9 taken 24051 times.
✗ Branch 2 → 10 not taken.
182698 switch (index) {
145 25733 case BIT_INDEX_CONST:
146 25733 return isConst = value;
147 29635 case BIT_INDEX_SIGNED:
148 29635 return isSigned = value;
149 29635 case BIT_INDEX_UNSIGNED:
150 29635 return isUnsigned = value;
151 24188 case BIT_INDEX_HEAP:
152 24188 return isHeap = value;
153 25405 case BIT_INDEX_PUBLIC:
154 25405 return isPublic = value;
155 24051 case BIT_INDEX_INLINE:
156 24051 return isInline = value;
157 24051 case BIT_INDEX_COMPOSITION:
158 24051 return isComposition = value;
159 default:
160 throw CompilerError(UNHANDLED_BRANCH, "Bit index fallthrough"); // GCOV_EXCL_LINE
161 }
162 }
163
164 } // namespace spice::compiler
165