GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 45 / 0 / 45
Functions: 100.0% 6 / 0 / 6
Branches: 100.0% 6 / 0 / 6

src/util/CustomHashFunctions.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "CustomHashFunctions.h"
4
5 #include <numeric>
6 #include <string>
7
8 #include <symboltablebuilder/Type.h>
9 #include <symboltablebuilder/TypeChain.h>
10
11 namespace spice::compiler {
12
13 249945839 uint64_t hashMix(uint64_t hash) noexcept {
14 249945839 hash += 0x9e3779b97f4a7c15ull;
15 249945839 hash = (hash ^ (hash >> 30)) * 0xbf58476d1ce4e5b9ull;
16 249945839 hash = (hash ^ (hash >> 27)) * 0x94d049bb133111ebull;
17 249945839 hash ^= (hash >> 31);
18 249945839 return hash;
19 }
20
21 191631768 void hashCombine64(uint64_t &hash, uint64_t value) noexcept {
22 191631768 hash ^= hashMix(value + 0x9e3779b97f4a7c15ull + (hash << 6) + (hash >> 2));
23 191631768 }
24
25 } // namespace spice::compiler
26
27 namespace std {
28
29 22560912 size_t hash<spice::compiler::TypeChainElement>::operator()(const spice::compiler::TypeChainElement &tce) const noexcept {
30 using namespace spice::compiler;
31 22560912 uint64_t hash = 0;
32
33 22560912 hashCombine64(hash, tce.superType);
34 22560912 hashCombine64(hash, std::hash<std::string>{}(tce.subType));
35 22560912 hashCombine64(hash, tce.typeId);
36
37
4/4
✓ Branch 6 → 7 taken 2767 times.
✓ Branch 6 → 9 taken 495743 times.
✓ Branch 6 → 14 taken 9888837 times.
✓ Branch 6 → 17 taken 12173565 times.
22560912 switch (tce.superType) {
38 2767 case TY_ARRAY:
39 2767 hashCombine64(hash, tce.data.arraySize);
40 2767 break;
41 495743 case TY_FUNCTION:
42 case TY_PROCEDURE:
43
2/2
✓ Branch 9 → 10 taken 715 times.
✓ Branch 9 → 11 taken 495028 times.
495743 hashCombine64(hash, tce.data.hasCaptures ? 0xF00D1234ULL : 0xBAD0C0DEULL);
44 495743 break;
45 9888837 case TY_STRUCT:
46 case TY_INTERFACE:
47 case TY_ENUM:
48 // Stable hash based on pointer identity, but randomized for safety
49 9888837 hashCombine64(hash, hashPointer(tce.data.bodyScope));
50 9888837 break;
51 12173565 default:
52 12173565 break;
53 }
54
55 22560912 hashCombine64(hash, hashVector(tce.templateTypes));
56 22560912 hashCombine64(hash, hashVector(tce.paramTypes));
57
58 22560912 return hashMix(hash);
59 }
60
61 21112779 size_t hash<spice::compiler::Type>::operator()(const spice::compiler::Type &t) const noexcept {
62 using namespace spice::compiler;
63 21112779 uint64_t hash = 0;
64 21112779 hashCombine64(hash, hashVector(t.typeChain));
65 21112779 return hashMix(hash);
66 }
67
68 7320190 size_t hash<spice::compiler::TypeQualifiers>::operator()(const spice::compiler::TypeQualifiers &qualifiers) const noexcept {
69 using namespace spice::compiler;
70 7320190 const uint8_t bits = (qualifiers.isConst << 0) | (qualifiers.isSigned << 1) | (qualifiers.isUnsigned << 2) |
71 7320190 (qualifiers.isHeap << 3) | (qualifiers.isPublic << 4) | (qualifiers.isInline << 5) |
72 7320190 (qualifiers.isComposition << 6);
73 7320190 return hashMix(bits);
74 }
75
76 7320190 size_t hash<spice::compiler::QualType>::operator()(const spice::compiler::QualType &qualType) const noexcept {
77 using namespace spice::compiler;
78 7320190 uint64_t seed = 0;
79
80 // Hash type pointer content if possible
81 7320190 hashCombine64(seed, std::hash<Type>{}(*qualType.getType()));
82
83 7320190 TypeQualifiers qualifiers = qualType.getQualifiers();
84 7320190 qualifiers.isPublic = false; // Ignore the public qualifier for hashing
85 7320190 hashCombine64(seed, std::hash<TypeQualifiers>{}(qualifiers));
86 7320190 return hashMix(seed);
87 }
88
89 } // namespace std
90