| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "TypeSpecifiers.h" | ||
| 4 | |||
| 5 | #include <exception/CompilerError.h> | ||
| 6 | #include <symboltablebuilder/TypeChain.h> | ||
| 7 | |||
| 8 | namespace spice::compiler { | ||
| 9 | |||
| 10 | /** | ||
| 11 | * Get default type specifiers for a given super type | ||
| 12 | * | ||
| 13 | * @param superType Super type | ||
| 14 | * @return Default type specifiers | ||
| 15 | */ | ||
| 16 | 4511821 | TypeSpecifiers TypeSpecifiers::of(uint16_t superType) { | |
| 17 | 5/6✓ Branch 0 (2→3) taken 109960 times. ✓ Branch 1 (2→6) taken 249516 times. ✓ Branch 2 (2→9) taken 59443 times. ✓ Branch 3 (2→12) taken 1453 times. ✓ Branch 4 (2→15) taken 4091449 times. ✗ Branch 5 (2→18) not taken. | 4511821 | switch (superType) { | 
| 18 | 109960 | case TY_DOUBLE: // fall-through | |
| 19 | case TY_INT: // fall-through | ||
| 20 | case TY_SHORT: // fall-through | ||
| 21 | case TY_LONG: | ||
| 22 | 109960 | return {/*const*/ false, /*signed*/ true, /*unsigned*/ false, /*heap*/ false}; | |
| 23 | 249516 | 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 | 249516 | return {/*const*/ false, /*signed*/ false, /*unsigned*/ true, /*heap*/ false}; | |
| 35 | 59443 | case TY_GENERIC: | |
| 36 | // Generics must be non-signed and non-unsigned at the same time to ensure a proper function matching | ||
| 37 | 59443 | return {/*const*/ false, /*signed*/ false, /*unsigned*/ false, /*heap*/ false}; | |
| 38 | 1453 | case TY_ENUM: // fall-through | |
| 39 | case TY_ALIAS: // fall-through | ||
| 40 | case TY_IMPORT: | ||
| 41 | 1453 | return {/*const*/ true, /*signed*/ false, /*unsigned*/ true, /*heap*/ false}; | |
| 42 | 4091449 | case TY_DYN: // fall-through | |
| 43 | case TY_INVALID: // fall-through | ||
| 44 | case TY_UNRESOLVED: | ||
| 45 | // Return all-false specifiers to not match anything | ||
| 46 | 4091449 | return {/*const*/ false, /*signed*/ false, /*unsigned*/ false, /*heap*/ false}; | |
| 47 | ✗ | default: | |
| 48 | − | throw CompilerError(UNHANDLED_BRANCH, "Symbol specifier fallthrough"); // GCOV_EXCL_LINE | |
| 49 | } | ||
| 50 | } | ||
| 51 | |||
| 52 | /** | ||
| 53 | * Merge two sets of type specifiers. If possible, prefer the opposite of the default of the super type | ||
| 54 | * | ||
| 55 | * @param other Other type specifiers object | ||
| 56 | * @return Merged specifiers object | ||
| 57 | */ | ||
| 58 | 12590 | TypeSpecifiers TypeSpecifiers::merge(const TypeSpecifiers &other) const { | |
| 59 | 12590 | TypeSpecifiers result; | |
| 60 | 6/8✓ Branch 0 (2→3) taken 12590 times. ✗ Branch 1 (2→23) not taken. ✓ Branch 2 (3→4) taken 12237 times. ✓ Branch 3 (3→7) taken 353 times. ✓ Branch 4 (4→5) taken 12237 times. ✗ Branch 5 (4→23) not taken. ✓ Branch 6 (5→6) taken 8050 times. ✓ Branch 7 (5→7) taken 4187 times. | 12590 | const bool isGeneric = !getBit(BIT_INDEX_SIGNED) && !getBit(BIT_INDEX_UNSIGNED); | 
| 61 | 2/2✓ Branch 0 (20→9) taken 88130 times. ✓ Branch 1 (20→21) taken 12590 times. | 100720 | for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) { | 
| 62 | 1/2✓ Branch 0 (9→10) taken 88130 times. ✗ Branch 1 (9→23) not taken. | 88130 | const bool x = getBit(i); | 
| 63 | 1/2✓ Branch 0 (10→11) taken 88130 times. ✗ Branch 1 (10→23) not taken. | 88130 | const bool y = other.getBit(i); | 
| 64 | |||
| 65 | 4/4✓ Branch 0 (11→12) taken 75540 times. ✓ Branch 1 (11→13) taken 12590 times. ✓ Branch 2 (12→13) taken 12590 times. ✓ Branch 3 (12→18) taken 62950 times. | 88130 | if (i == BIT_INDEX_SIGNED || i == BIT_INDEX_UNSIGNED) { | 
| 66 | 3/4✓ Branch 0 (13→14) taken 16100 times. ✓ Branch 1 (13→15) taken 9080 times. ✓ Branch 2 (16→17) taken 25180 times. ✗ Branch 3 (16→23) not taken. | 25180 | result.setBit(i, isGeneric ? y : x); | 
| 67 | } else { | ||
| 68 | 1/2✓ Branch 0 (18→19) taken 62950 times. ✗ Branch 1 (18→23) not taken. | 62950 | result.setBit(i, x | y); | 
| 69 | } | ||
| 70 | } | ||
| 71 | 12590 | return result; | |
| 72 | } | ||
| 73 | |||
| 74 | /** | ||
| 75 | * Check if two sets of type specifiers match | ||
| 76 | * | ||
| 77 | * @param other The rhs specifiers | ||
| 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 | 9994 | bool TypeSpecifiers::match(TypeSpecifiers other, bool allowConstify) const { | |
| 82 | 9994 | const TypeSpecifiers thisSpecifiers = *this; | |
| 83 | |||
| 84 | // If allowConstify is enabled, only allow to match lhs=const and rhs=non-const | ||
| 85 | 4/4✓ Branch 0 (2→3) taken 9974 times. ✓ Branch 1 (2→5) taken 20 times. ✓ Branch 2 (3→4) taken 1314 times. ✓ Branch 3 (3→5) taken 8660 times. | 9994 | if (allowConstify && thisSpecifiers.isConst) | 
| 86 | 1314 | other.isConst = true; | |
| 87 | |||
| 88 | // Check if specifiers are equal | ||
| 89 | 9994 | return thisSpecifiers == other; | |
| 90 | } | ||
| 91 | |||
| 92 | /** | ||
| 93 | * Erase all specifiers that are set in the mask. This is used in type matching. | ||
| 94 | * | ||
| 95 | * @param mask Bitmask to erase with | ||
| 96 | */ | ||
| 97 | 4129 | void TypeSpecifiers::eraseWithMask(const TypeSpecifiers &mask) { | |
| 98 | // Zero out all bits that are set in the mask | ||
| 99 | 2/2✓ Branch 0 (11→3) taken 28903 times. ✓ Branch 1 (11→12) taken 4129 times. | 33032 | for (uint8_t i = 0; i <= BIT_INDEX_MAX; i++) { | 
| 100 | 2/2✓ Branch 0 (4→5) taken 937 times. ✓ Branch 1 (4→10) taken 27966 times. | 28903 | if (mask.getBit(i)) { | 
| 101 | // Zero out the bit | ||
| 102 | 937 | setBit(i, false); | |
| 103 | |||
| 104 | // If we set the signed/unsigned bit to zero, we need to set the other to one | ||
| 105 | 1/2✗ Branch 0 (6→7) not taken. ✓ Branch 1 (6→8) taken 937 times. | 937 | if (i == BIT_INDEX_SIGNED) { | 
| 106 | ✗ | setBit(BIT_INDEX_UNSIGNED, true); | |
| 107 | 2/2✓ Branch 0 (8→9) taken 179 times. ✓ Branch 1 (8→10) taken 758 times. | 937 | } else if (i == BIT_INDEX_UNSIGNED) { | 
| 108 | 179 | setBit(BIT_INDEX_SIGNED, true); | |
| 109 | } | ||
| 110 | } | ||
| 111 | } | ||
| 112 | 4129 | } | |
| 113 | |||
| 114 | 10083 | bool operator==(const TypeSpecifiers &lhs, const TypeSpecifiers &rhs) { | |
| 115 | 10083 | const bool isConst = lhs.isConst == rhs.isConst; | |
| 116 | 10083 | const bool isSigned = lhs.isSigned == rhs.isSigned; | |
| 117 | 10083 | const bool isUnsigned = lhs.isUnsigned == rhs.isUnsigned; | |
| 118 | 10083 | const bool isHeap = lhs.isHeap == rhs.isHeap; | |
| 119 | 6/8✓ Branch 0 (2→3) taken 10068 times. ✓ Branch 1 (2→7) taken 15 times. ✓ Branch 2 (3→4) taken 10068 times. ✗ Branch 3 (3→7) not taken. ✓ Branch 4 (4→5) taken 10068 times. ✗ Branch 5 (4→7) not taken. ✓ Branch 6 (5→6) taken 9980 times. ✓ Branch 7 (5→7) taken 88 times. | 10083 | return isConst && isSigned && isUnsigned && isHeap; | 
| 120 | } | ||
| 121 | |||
| 122 | 229990 | bool TypeSpecifiers::getBit(uint8_t index) const { | |
| 123 | 7/8✓ Branch 0 (2→3) taken 29309 times. ✓ Branch 1 (2→4) taken 41899 times. ✓ Branch 2 (2→5) taken 41546 times. ✓ Branch 3 (2→6) taken 29309 times. ✓ Branch 4 (2→7) taken 29309 times. ✓ Branch 5 (2→8) taken 29309 times. ✓ Branch 6 (2→9) taken 29309 times. ✗ Branch 7 (2→10) not taken. | 229990 | switch (index) { | 
| 124 | 29309 | case BIT_INDEX_CONST: | |
| 125 | 29309 | return isConst; | |
| 126 | 41899 | case BIT_INDEX_SIGNED: | |
| 127 | 41899 | return isSigned; | |
| 128 | 41546 | case BIT_INDEX_UNSIGNED: | |
| 129 | 41546 | return isUnsigned; | |
| 130 | 29309 | case BIT_INDEX_HEAP: | |
| 131 | 29309 | return isHeap; | |
| 132 | 29309 | case BIT_INDEX_PUBLIC: | |
| 133 | 29309 | return isPublic; | |
| 134 | 29309 | case BIT_INDEX_INLINE: | |
| 135 | 29309 | return isInline; | |
| 136 | 29309 | case BIT_INDEX_COMPOSITION: | |
| 137 | 29309 | return isComposition; | |
| 138 | ✗ | default: | |
| 139 | − | throw CompilerError(UNHANDLED_BRANCH, "Bit index fallthrough"); // GCOV_EXCL_LINE | |
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | 89246 | bool TypeSpecifiers::setBit(uint8_t index, bool value) { | |
| 144 | 7/8✓ Branch 0 (2→3) taken 13348 times. ✓ Branch 1 (2→4) taken 12769 times. ✓ Branch 2 (2→5) taken 12769 times. ✓ Branch 3 (2→6) taken 12590 times. ✓ Branch 4 (2→7) taken 12590 times. ✓ Branch 5 (2→8) taken 12590 times. ✓ Branch 6 (2→9) taken 12590 times. ✗ Branch 7 (2→10) not taken. | 89246 | switch (index) { | 
| 145 | 13348 | case BIT_INDEX_CONST: | |
| 146 | 13348 | return isConst = value; | |
| 147 | 12769 | case BIT_INDEX_SIGNED: | |
| 148 | 12769 | return isSigned = value; | |
| 149 | 12769 | case BIT_INDEX_UNSIGNED: | |
| 150 | 12769 | return isUnsigned = value; | |
| 151 | 12590 | case BIT_INDEX_HEAP: | |
| 152 | 12590 | return isHeap = value; | |
| 153 | 12590 | case BIT_INDEX_PUBLIC: | |
| 154 | 12590 | return isPublic = value; | |
| 155 | 12590 | case BIT_INDEX_INLINE: | |
| 156 | 12590 | return isInline = value; | |
| 157 | 12590 | case BIT_INDEX_COMPOSITION: | |
| 158 | 12590 | return isComposition = value; | |
| 159 | ✗ | default: | |
| 160 | − | throw CompilerError(UNHANDLED_BRANCH, "Bit index fallthrough"); // GCOV_EXCL_LINE | |
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | } // namespace spice::compiler | ||
| 165 |