symboltablebuilder/type-qualifiers.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Own imports | ||
| 4 | import "@/symboltablebuilder/super-type"; | ||
| 5 | |||
| 6 | // Constants | ||
| 7 | const unsigned short BIT_INDEX_COMPOSITION = 0s; | ||
| 8 | const unsigned short BIT_INDEX_INLINE = 1s; | ||
| 9 | const unsigned short BIT_INDEX_PUBLIC = 2s; | ||
| 10 | const unsigned short BIT_INDEX_HEAP = 3s; | ||
| 11 | const unsigned short BIT_INDEX_UNSIGNED = 4s; | ||
| 12 | const unsigned short BIT_INDEX_SIGNED = 5s; | ||
| 13 | const unsigned short BIT_INDEX_CONST = 6s; | ||
| 14 | const unsigned short BIT_INDEX_MAX = 6s; // Please adjust if something changes above | ||
| 15 | |||
| 16 | 29062226 | public type TypeQualifiers struct { | |
| 17 | // Note: Please adjust bit indices above, if something changes here | ||
| 18 | 29062226 | public bool isConst = false | |
| 19 | 29062226 | public bool isSigned = false | |
| 20 | 29062226 | public bool isUnsigned = false | |
| 21 | 29062226 | public bool isHeap = false | |
| 22 | 29062226 | public bool isPublic = false | |
| 23 | 29062226 | public bool isInline = false | |
| 24 | 29062226 | public bool isComposition = false | |
| 25 | } | ||
| 26 | |||
| 27 | /** | ||
| 28 | * Get default type qualifiers for a given super type | ||
| 29 | * | ||
| 30 | * @param superType Super type | ||
| 31 | * @return Default type qualifiers | ||
| 32 | */ | ||
| 33 | 5672514 | public f<TypeQualifiers> getTypeQualifiersOf(SuperType superType) { | |
| 34 | 5672514 | switch superType { | |
| 35 | case SuperType::TY_DOUBLE, SuperType::TY_INT, SuperType::TY_SHORT, SuperType::TY_LONG: { | ||
| 36 | 568048 | return TypeQualifiers{ | |
| 37 | /*isConst=*/false, | ||
| 38 | /*isSigned*/true, | ||
| 39 | /*isUnsigned*/false, | ||
| 40 | /*isHeap*/false, | ||
| 41 | /*isPublic*/false, | ||
| 42 | /*isInline*/false, | ||
| 43 | /*isComposition*/false | ||
| 44 | }; | ||
| 45 | } | ||
| 46 | case SuperType::TY_BYTE, SuperType::TY_CHAR, SuperType::TY_STRING, SuperType::TY_BOOL, SuperType::TY_PTR, SuperType::TY_REF, SuperType::TY_ARRAY, SuperType::TY_STRUCT, SuperType::TY_INTERFACE, SuperType::TY_UNION, SuperType::TY_FUNCTION, SuperType::TY_PROCEDURE: { | ||
| 47 | 1154832 | return TypeQualifiers{ | |
| 48 | /*isConst=*/false, | ||
| 49 | /*isSigned*/false, | ||
| 50 | /*isUnsigned*/true, | ||
| 51 | /*isHeap*/false, | ||
| 52 | /*isPublic*/false, | ||
| 53 | /*isInline*/false, | ||
| 54 | /*isComposition*/false | ||
| 55 | }; | ||
| 56 | } | ||
| 57 | case SuperType::TY_GENERIC: { | ||
| 58 | // Generics must be non-signed and non-unsigned at the same time to ensure a proper function matching | ||
| 59 | 255004 | return TypeQualifiers{ | |
| 60 | /*isConst=*/false, | ||
| 61 | /*isSigned*/false, | ||
| 62 | /*isUnsigned*/false, | ||
| 63 | /*isHeap*/false, | ||
| 64 | /*isPublic*/false, | ||
| 65 | /*isInline*/false, | ||
| 66 | /*isComposition*/false | ||
| 67 | }; | ||
| 68 | } | ||
| 69 | case SuperType::TY_ENUM, SuperType::TY_ALIAS, SuperType::TY_IMPORT: { | ||
| 70 | 6201 | return TypeQualifiers{ | |
| 71 | /*isConst=*/true, | ||
| 72 | /*isSigned*/false, | ||
| 73 | /*isUnsigned*/true, | ||
| 74 | /*isHeap*/false, | ||
| 75 | /*isPublic*/false, | ||
| 76 | /*isInline*/false, | ||
| 77 | /*isComposition*/false | ||
| 78 | }; | ||
| 79 | } | ||
| 80 | case SuperType::TY_DYN, SuperType::TY_INVALID, SuperType::TY_UNRESOLVED: { | ||
| 81 | // Return all-false qualifiers to not match anything | ||
| 82 | 3688429 | return TypeQualifiers{ | |
| 83 | /*isConst=*/false, | ||
| 84 | /*isSigned*/false, | ||
| 85 | /*isUnsigned*/false, | ||
| 86 | /*isHeap*/false, | ||
| 87 | /*isPublic*/false, | ||
| 88 | /*isInline*/false, | ||
| 89 | /*isComposition*/false | ||
| 90 | }; | ||
| 91 | } | ||
| 92 | default: { | ||
| 93 | ✗ | panic(Error("Symbol qualifier fallthrough")); | |
| 94 | } | ||
| 95 | } | ||
| 96 | ✗ | } | |
| 97 | |||
| 98 | /** | ||
| 99 | * Merge two sets of type qualifiers. If possible, prefer the opposite of the default of the super type | ||
| 100 | * | ||
| 101 | * @param other Other type qualifiers object | ||
| 102 | * @return Merged qualifiers object | ||
| 103 | */ | ||
| 104 | 81569 | public f<TypeQualifiers> TypeQualifiers.merge(const TypeQualifiers& other) { | |
| 105 | 81569 | result = TypeQualifiers(); | |
| 106 | 81569 | const bool isGeneric = !this.getBit(BIT_INDEX_SIGNED) && !this.getBit(BIT_INDEX_UNSIGNED); | |
| 107 | 652552 | for unsigned short i = 0s; i <= BIT_INDEX_MAX; i++ { | |
| 108 | 570983 | const bool x = this.getBit(i); | |
| 109 | 570983 | const bool y = other.getBit(i); | |
| 110 | |||
| 111 | 570983 | if i == BIT_INDEX_SIGNED || i == BIT_INDEX_UNSIGNED { | |
| 112 | 163138 | result.setBit(i, isGeneric ? y : x); | |
| 113 | 163138 | } else { | |
| 114 | 407845 | result.setBit(i, x | y); | |
| 115 | 407845 | } | |
| 116 | 570983 | } | |
| 117 | 81569 | } | |
| 118 | |||
| 119 | /** | ||
| 120 | * Check if two sets of type qualifiers match | ||
| 121 | * | ||
| 122 | * @param other The rhs qualifiers | ||
| 123 | * @param allowConstify Match when the types are the same, but the lhs type is more const restrictive than the rhs type | ||
| 124 | * @return Matching or not | ||
| 125 | */ | ||
| 126 | 55691 | public f<bool> TypeQualifiers.match(TypeQualifiers otherQualifiers, bool allowConstify) { | |
| 127 | 55691 | const TypeQualifiers thisQualifiers = *this; | |
| 128 | |||
| 129 | // If allowConstify is enabled, only allow to match lhs=const and rhs=non-const | ||
| 130 | 55691 | if allowConstify && thisQualifiers.isConst { | |
| 131 | 3973 | otherQualifiers.isConst = true; | |
| 132 | 3973 | } | |
| 133 | |||
| 134 | // Check if qualifiers are equal | ||
| 135 | 55691 | return thisQualifiers == otherQualifiers; | |
| 136 | } | ||
| 137 | |||
| 138 | /** | ||
| 139 | * Erase all qualifiers that are set in the mask. This is used in type matching. | ||
| 140 | * | ||
| 141 | * @param mask Bitmask to erase with | ||
| 142 | */ | ||
| 143 | 22929 | public p TypeQualifiers.eraseWithMask(const TypeQualifiers& mask) { | |
| 144 | // Zero out all bits that are set in the mask | ||
| 145 | 183432 | for unsigned short i = 0s; i <= BIT_INDEX_MAX; i++ { | |
| 146 | 160503 | if mask.getBit(i) { | |
| 147 | // Zero out the bit | ||
| 148 | 32280 | this.setBit(i, false); | |
| 149 | |||
| 150 | // If we set the signed/unsigned bit to zero, we need to set the other to one | ||
| 151 | 32280 | if i == BIT_INDEX_SIGNED { | |
| 152 | 7479 | this.setBit(BIT_INDEX_UNSIGNED, true); | |
| 153 | 32280 | } else if i == BIT_INDEX_UNSIGNED { | |
| 154 | 13631 | this.setBit(BIT_INDEX_SIGNED, true); | |
| 155 | 24801 | } | |
| 156 | 32280 | } | |
| 157 | 160503 | } | |
| 158 | 22929 | } | |
| 159 | |||
| 160 | 57748 | public f<bool> operator==(const TypeQualifiers& lhs, const TypeQualifiers& rhs) { | |
| 161 | 57748 | const bool isConst = lhs.isConst == rhs.isConst; | |
| 162 | 57748 | const bool isSigned = lhs.isSigned == rhs.isSigned; | |
| 163 | 57748 | const bool isUnsigned = lhs.isUnsigned == rhs.isUnsigned; | |
| 164 | 57748 | const bool isHeap = lhs.isHeap == rhs.isHeap; | |
| 165 | 57748 | return isConst && isSigned && isUnsigned && isHeap; | |
| 166 | } | ||
| 167 | |||
| 168 | 1461670 | f<bool> TypeQualifiers.getBit(unsigned short index) { | |
| 169 | 1461670 | switch index { | |
| 170 | 186067 | case BIT_INDEX_CONST: { return this.isConst; } | |
| 171 | 267636 | case BIT_INDEX_SIGNED: { return this.isSigned; } | |
| 172 | 263699 | case BIT_INDEX_UNSIGNED: { return this.isUnsigned; } | |
| 173 | 186067 | case BIT_INDEX_HEAP: { return this.isHeap; } | |
| 174 | 186067 | case BIT_INDEX_PUBLIC: { return this.isPublic; } | |
| 175 | 186067 | case BIT_INDEX_INLINE: { return this.isInline; } | |
| 176 | 186067 | case BIT_INDEX_COMPOSITION: { return this.isComposition; } | |
| 177 | ✗ | default: { panic(Error("Bit index fallthrough")); } | |
| 178 | } | ||
| 179 | ✗ | } | |
| 180 | |||
| 181 | 624373 | p TypeQualifiers.setBit(unsigned short index, bool value) { | |
| 182 | 624373 | switch index { | |
| 183 | 84017 | case BIT_INDEX_CONST: { this.isConst = value; } | |
| 184 | 102679 | case BIT_INDEX_SIGNED: { this.isSigned = value; } | |
| 185 | 102679 | case BIT_INDEX_UNSIGNED: { this.isUnsigned = value; } | |
| 186 | 82455 | case BIT_INDEX_HEAP: { this.isHeap = value; } | |
| 187 | 89405 | case BIT_INDEX_PUBLIC: { this.isPublic = value; } | |
| 188 | 81569 | case BIT_INDEX_INLINE: { this.isInline = value; } | |
| 189 | 81569 | case BIT_INDEX_COMPOSITION: { this.isComposition = value; } | |
| 190 | ✗ | default: { panic(Error("Bit index fallthrough")); } | |
| 191 | } | ||
| 192 | 624373 | } | |
| 193 |