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 | 15340601 | TypeQualifiers TypeQualifiers::of(uint16_t superType) { | |
| 17 |
5/6✓ Branch 2 → 3 taken 1298500 times.
✓ Branch 2 → 5 taken 3737081 times.
✓ Branch 2 → 7 taken 935188 times.
✓ Branch 2 → 9 taken 31616 times.
✓ Branch 2 → 11 taken 9338216 times.
✗ Branch 2 → 13 not taken.
|
15340601 | switch (superType) { |
| 18 | 1298500 | case TY_DOUBLE: // fall-through | |
| 19 | case TY_INT: // fall-through | ||
| 20 | case TY_SHORT: // fall-through | ||
| 21 | case TY_LONG: | ||
| 22 | 1298500 | return {/*const*/ false, /*signed*/ true, /*unsigned*/ false}; | |
| 23 | 3737081 | 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 | 3737081 | return {/*const*/ false, /*signed*/ false, /*unsigned*/ true}; | |
| 36 | 935188 | case TY_GENERIC: | |
| 37 | // Generics must be non-signed and non-unsigned at the same time to ensure a proper function matching | ||
| 38 | 935188 | return {/*const*/ false, /*signed*/ false, /*unsigned*/ false}; | |
| 39 | 31616 | case TY_ENUM: // fall-through | |
| 40 | case TY_ALIAS: // fall-through | ||
| 41 | case TY_IMPORT: | ||
| 42 | 31616 | return {/*const*/ true, /*signed*/ false, /*unsigned*/ true}; | |
| 43 | 9338216 | 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 | 9338216 | 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 | 177432 | TypeQualifiers TypeQualifiers::merge(const TypeQualifiers &other) const { | |
| 60 | 177432 | TypeQualifiers result; | |
| 61 |
6/8✓ Branch 2 → 3 taken 177432 times.
✗ Branch 2 → 23 not taken.
✓ Branch 3 → 4 taken 171081 times.
✓ Branch 3 → 7 taken 6351 times.
✓ Branch 4 → 5 taken 171081 times.
✗ Branch 4 → 23 not taken.
✓ Branch 5 → 6 taken 153628 times.
✓ Branch 5 → 7 taken 17453 times.
|
177432 | const bool isGeneric = !getBit(BIT_INDEX_SIGNED) && !getBit(BIT_INDEX_UNSIGNED); |
| 62 |
2/2✓ Branch 20 → 9 taken 1242024 times.
✓ Branch 20 → 21 taken 177432 times.
|
1419456 | for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) { |
| 63 |
1/2✓ Branch 9 → 10 taken 1242024 times.
✗ Branch 9 → 23 not taken.
|
1242024 | const bool x = getBit(i); |
| 64 |
1/2✓ Branch 10 → 11 taken 1242024 times.
✗ Branch 10 → 23 not taken.
|
1242024 | const bool y = other.getBit(i); |
| 65 | |||
| 66 |
4/4✓ Branch 11 → 12 taken 1064592 times.
✓ Branch 11 → 13 taken 177432 times.
✓ Branch 12 → 13 taken 177432 times.
✓ Branch 12 → 18 taken 887160 times.
|
1242024 | if (i == BIT_INDEX_SIGNED || i == BIT_INDEX_UNSIGNED) { |
| 67 |
3/4✓ Branch 13 → 14 taken 307256 times.
✓ Branch 13 → 15 taken 47608 times.
✓ Branch 16 → 17 taken 354864 times.
✗ Branch 16 → 23 not taken.
|
354864 | result.setBit(i, isGeneric ? y : x); |
| 68 | } else { | ||
| 69 |
1/2✓ Branch 18 → 19 taken 887160 times.
✗ Branch 18 → 23 not taken.
|
887160 | result.setBit(i, x | y); |
| 70 | } | ||
| 71 | } | ||
| 72 | 177432 | 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 | 159563 | bool TypeQualifiers::match(TypeQualifiers other, bool allowConstify) const { | |
| 83 | 159563 | 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 159533 times.
✓ Branch 2 → 5 taken 30 times.
✓ Branch 3 → 4 taken 19980 times.
✓ Branch 3 → 5 taken 139553 times.
|
159563 | if (allowConstify && thisQualifiers.isConst) |
| 87 | 19980 | other.isConst = true; | |
| 88 | |||
| 89 | // Check if qualifiers are equal | ||
| 90 | 159563 | 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 | 51442 | void TypeQualifiers::eraseWithMask(const TypeQualifiers &mask) { | |
| 99 | // Zero out all bits that are set in the mask | ||
| 100 |
2/2✓ Branch 11 → 3 taken 360094 times.
✓ Branch 11 → 12 taken 51442 times.
|
411536 | for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) { |
| 101 |
2/2✓ Branch 4 → 5 taken 81469 times.
✓ Branch 4 → 10 taken 278625 times.
|
360094 | if (mask.getBit(i)) { |
| 102 | // Zero out the bit | ||
| 103 | 81469 | 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 13520 times.
✓ Branch 6 → 8 taken 67949 times.
|
81469 | if (i == BIT_INDEX_SIGNED) { |
| 107 | 13520 | setBit(BIT_INDEX_UNSIGNED, true); | |
| 108 |
2/2✓ Branch 8 → 9 taken 34865 times.
✓ Branch 8 → 10 taken 33084 times.
|
67949 | } else if (i == BIT_INDEX_UNSIGNED) { |
| 109 | 34865 | setBit(BIT_INDEX_SIGNED, true); | |
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | 51442 | } | |
| 114 | |||
| 115 | 160498 | bool operator==(const TypeQualifiers &lhs, const TypeQualifiers &rhs) { | |
| 116 | 160498 | const bool isConst = lhs.isConst == rhs.isConst; | |
| 117 | 160498 | const bool isSigned = lhs.isSigned == rhs.isSigned; | |
| 118 | 160498 | const bool isUnsigned = lhs.isUnsigned == rhs.isUnsigned; | |
| 119 | 160498 | const bool isHeap = lhs.isHeap == rhs.isHeap; | |
| 120 |
6/8✓ Branch 2 → 3 taken 160394 times.
✓ Branch 2 → 7 taken 104 times.
✓ Branch 3 → 4 taken 160394 times.
✗ Branch 3 → 7 not taken.
✓ Branch 4 → 5 taken 160394 times.
✗ Branch 4 → 7 not taken.
✓ Branch 5 → 6 taken 159461 times.
✓ Branch 5 → 7 taken 933 times.
|
160498 | return isConst && isSigned && isUnsigned && isHeap; |
| 121 | } | ||
| 122 | |||
| 123 | 3192655 | bool TypeQualifiers::getBit(uint8_t index) const { | |
| 124 |
7/8✓ Branch 2 → 3 taken 406306 times.
✓ Branch 2 → 4 taken 583738 times.
✓ Branch 2 → 5 taken 577387 times.
✓ Branch 2 → 6 taken 406306 times.
✓ Branch 2 → 7 taken 406306 times.
✓ Branch 2 → 8 taken 406306 times.
✓ Branch 2 → 9 taken 406306 times.
✗ Branch 2 → 10 not taken.
|
3192655 | switch (index) { |
| 125 | 406306 | case BIT_INDEX_CONST: | |
| 126 | 406306 | return isConst; | |
| 127 | 583738 | case BIT_INDEX_SIGNED: | |
| 128 | 583738 | return isSigned; | |
| 129 | 577387 | case BIT_INDEX_UNSIGNED: | |
| 130 | 577387 | return isUnsigned; | |
| 131 | 406306 | case BIT_INDEX_HEAP: | |
| 132 | 406306 | return isHeap; | |
| 133 | 406306 | case BIT_INDEX_PUBLIC: | |
| 134 | 406306 | return isPublic; | |
| 135 | 406306 | case BIT_INDEX_INLINE: | |
| 136 | 406306 | return isInline; | |
| 137 | 406306 | case BIT_INDEX_COMPOSITION: | |
| 138 | 406306 | return isComposition; | |
| 139 | − | default: // GCOV_EXCL_LINE | |
| 140 | − | throw CompilerError(UNHANDLED_BRANCH, "Bit index fallthrough"); // GCOV_EXCL_LINE | |
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | 1371878 | bool TypeQualifiers::setBit(uint8_t index, bool value) { | |
| 145 |
7/8✓ Branch 2 → 3 taken 185398 times.
✓ Branch 2 → 4 taken 225817 times.
✓ Branch 2 → 5 taken 225817 times.
✓ Branch 2 → 6 taken 178672 times.
✓ Branch 2 → 7 taken 201310 times.
✓ Branch 2 → 8 taken 177432 times.
✓ Branch 2 → 9 taken 177432 times.
✗ Branch 2 → 10 not taken.
|
1371878 | switch (index) { |
| 146 | 185398 | case BIT_INDEX_CONST: | |
| 147 | 185398 | return isConst = value; | |
| 148 | 225817 | case BIT_INDEX_SIGNED: | |
| 149 | 225817 | return isSigned = value; | |
| 150 | 225817 | case BIT_INDEX_UNSIGNED: | |
| 151 | 225817 | return isUnsigned = value; | |
| 152 | 178672 | case BIT_INDEX_HEAP: | |
| 153 | 178672 | return isHeap = value; | |
| 154 | 201310 | case BIT_INDEX_PUBLIC: | |
| 155 | 201310 | return isPublic = value; | |
| 156 | 177432 | case BIT_INDEX_INLINE: | |
| 157 | 177432 | return isInline = value; | |
| 158 | 177432 | case BIT_INDEX_COMPOSITION: | |
| 159 | 177432 | 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 |