Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2024 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "TypeChain.h" | ||
4 | |||
5 | #include <exception/CompilerError.h> | ||
6 | |||
7 | namespace spice::compiler { | ||
8 | |||
9 | 79460 | bool operator==(const TypeChainElement &lhs, const TypeChainElement &rhs) { | |
10 | // Check super type | ||
11 |
2/2✓ Branch 0 taken 21039 times.
✓ Branch 1 taken 58421 times.
|
79460 | if (lhs.superType != rhs.superType) |
12 | 21039 | return false; | |
13 | |||
14 | // Check data | ||
15 |
6/7✓ Branch 0 taken 92 times.
✓ Branch 1 taken 28978 times.
✓ Branch 2 taken 416 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 122 times.
✓ Branch 5 taken 3563 times.
✓ Branch 6 taken 25250 times.
|
58421 | switch (lhs.superType) { |
16 | 92 | case TY_ARRAY: | |
17 | 92 | return lhs.data.arraySize == rhs.data.arraySize; | |
18 | 28978 | case TY_STRUCT: | |
19 |
2/4✓ Branch 0 taken 28978 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28978 times.
✗ Branch 3 not taken.
|
28978 | assert(lhs.data.bodyScope != nullptr && rhs.data.bodyScope != nullptr); |
20 |
4/4✓ Branch 0 taken 27553 times.
✓ Branch 1 taken 1425 times.
✓ Branch 3 taken 27541 times.
✓ Branch 4 taken 12 times.
|
28978 | return lhs.typeId == rhs.typeId && lhs.templateTypes == rhs.templateTypes; |
21 | 416 | case TY_INTERFACE: | |
22 | 416 | return lhs.typeId == rhs.typeId; | |
23 | ✗ | case TY_ENUM: | |
24 | ✗ | assert(lhs.data.bodyScope != nullptr && rhs.data.bodyScope != nullptr); | |
25 | ✗ | return lhs.typeId == rhs.typeId && lhs.data.bodyScope == rhs.data.bodyScope; | |
26 | 122 | case TY_FUNCTION: // fall-through | |
27 | case TY_PROCEDURE: | ||
28 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 122 times.
|
122 | if (lhs.paramTypes.size() != rhs.paramTypes.size()) |
29 | ✗ | return false; | |
30 |
2/2✓ Branch 1 taken 197 times.
✓ Branch 2 taken 120 times.
|
317 | for (size_t i = 0; i < lhs.paramTypes.size(); i++) |
31 |
2/2✓ Branch 3 taken 2 times.
✓ Branch 4 taken 195 times.
|
197 | if (lhs.paramTypes.at(i) != rhs.paramTypes.at(i)) |
32 | 2 | return false; | |
33 | 120 | return true; | |
34 | 3563 | case TY_GENERIC: | |
35 | 3563 | return lhs.subType == rhs.subType; | |
36 | 25250 | default: | |
37 | 25250 | return true; | |
38 | } | ||
39 | } | ||
40 | |||
41 | 79460 | bool operator!=(const TypeChainElement &lhs, const TypeChainElement &rhs) { return !(lhs == rhs); } | |
42 | |||
43 | 788878 | void TypeChainElement::getName(std::stringstream &name, bool withSize) const { | |
44 |
19/20✓ Branch 0 taken 81594 times.
✓ Branch 1 taken 87106 times.
✓ Branch 2 taken 484 times.
✓ Branch 3 taken 1166 times.
✓ Branch 4 taken 32771 times.
✓ Branch 5 taken 1643 times.
✓ Branch 6 taken 78669 times.
✓ Branch 7 taken 10634 times.
✓ Branch 8 taken 22101 times.
✓ Branch 9 taken 58011 times.
✓ Branch 10 taken 30236 times.
✓ Branch 11 taken 157627 times.
✓ Branch 12 taken 82 times.
✓ Branch 13 taken 115875 times.
✓ Branch 14 taken 154 times.
✓ Branch 15 taken 45080 times.
✓ Branch 16 taken 35591 times.
✓ Branch 17 taken 1053 times.
✓ Branch 18 taken 29001 times.
✗ Branch 19 not taken.
|
788878 | switch (superType) { |
45 | 81594 | case TY_PTR: | |
46 | 81594 | name << "*"; | |
47 | 81594 | break; | |
48 | 87106 | case TY_REF: | |
49 | 87106 | name << "&"; | |
50 | 87106 | break; | |
51 | 484 | case TY_ARRAY: | |
52 |
9/14✓ Branch 1 taken 228 times.
✓ Branch 2 taken 256 times.
✓ Branch 3 taken 97 times.
✓ Branch 4 taken 131 times.
✓ Branch 7 taken 387 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 484 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 484 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 387 times.
✓ Branch 17 taken 97 times.
✗ Branch 19 not taken.
✗ Branch 20 not taken.
|
871 | name << "[" << (withSize && data.arraySize != ARRAY_SIZE_UNKNOWN ? std::to_string(data.arraySize) : "") << "]"; |
53 | 484 | break; | |
54 | 1166 | case TY_DOUBLE: | |
55 | 1166 | name << "double"; | |
56 | 1166 | break; | |
57 | 32771 | case TY_INT: | |
58 | 32771 | name << "int"; | |
59 | 32771 | break; | |
60 | 1643 | case TY_SHORT: | |
61 | 1643 | name << "short"; | |
62 | 1643 | break; | |
63 | 78669 | case TY_LONG: | |
64 | 78669 | name << "long"; | |
65 | 78669 | break; | |
66 | 10634 | case TY_BYTE: | |
67 | 10634 | name << "byte"; | |
68 | 10634 | break; | |
69 | 22101 | case TY_CHAR: | |
70 | 22101 | name << "char"; | |
71 | 22101 | break; | |
72 | 58011 | case TY_STRING: | |
73 | 58011 | name << "string"; | |
74 | 58011 | break; | |
75 | 30236 | case TY_BOOL: | |
76 | 30236 | name << "bool"; | |
77 | 30236 | break; | |
78 | 157627 | case TY_STRUCT: // fall-through | |
79 | case TY_INTERFACE: | ||
80 | 157627 | name << subType; | |
81 |
2/2✓ Branch 1 taken 66690 times.
✓ Branch 2 taken 90937 times.
|
157627 | if (!templateTypes.empty()) { |
82 | 66690 | name << "<"; | |
83 |
2/2✓ Branch 1 taken 91642 times.
✓ Branch 2 taken 66690 times.
|
158332 | for (size_t i = 0; i < templateTypes.size(); i++) { |
84 |
2/2✓ Branch 0 taken 24952 times.
✓ Branch 1 taken 66690 times.
|
91642 | if (i > 0) |
85 | 24952 | name << ","; | |
86 | 91642 | templateTypes.at(i).getName(name, withSize); | |
87 | } | ||
88 | 66690 | name << ">"; | |
89 | } | ||
90 | 157627 | break; | |
91 | 82 | case TY_ENUM: | |
92 | 82 | name << "enum"; | |
93 | 82 | break; | |
94 | 115875 | case TY_GENERIC: // fall-through | |
95 | case TY_ALIAS: | ||
96 | 115875 | name << subType; | |
97 | 115875 | break; | |
98 | 154 | case TY_DYN: | |
99 | 154 | name << "dyn"; | |
100 | 154 | break; | |
101 | 45080 | case TY_FUNCTION: { | |
102 | 45080 | name << "f"; | |
103 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 45072 times.
|
45080 | if (data.hasCaptures) |
104 | 8 | name << "[]"; | |
105 |
2/2✓ Branch 1 taken 44895 times.
✓ Branch 2 taken 185 times.
|
45080 | if (!paramTypes.empty()) |
106 |
3/6✓ Branch 3 taken 44895 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 44895 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 44895 times.
✗ Branch 10 not taken.
|
44895 | name << "<" << paramTypes.front().getName(true) << ">"; |
107 | 45080 | name << "("; | |
108 |
2/2✓ Branch 1 taken 44235 times.
✓ Branch 2 taken 45080 times.
|
89315 | for (size_t i = 1; i < paramTypes.size(); i++) { |
109 |
2/2✓ Branch 0 taken 19114 times.
✓ Branch 1 taken 25121 times.
|
44235 | if (i > 1) |
110 | 19114 | name << ","; | |
111 | 44235 | paramTypes.at(i).getName(name, true); | |
112 | } | ||
113 | 45080 | name << ")"; | |
114 | 45080 | break; | |
115 | } | ||
116 | 35591 | case TY_PROCEDURE: { | |
117 | 35591 | name << "p"; | |
118 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 35565 times.
|
35591 | if (data.hasCaptures) |
119 | 26 | name << "[]"; | |
120 | 35591 | name << "("; | |
121 |
2/2✓ Branch 1 taken 35010 times.
✓ Branch 2 taken 35591 times.
|
70601 | for (size_t i = 1; i < paramTypes.size(); i++) { |
122 |
2/2✓ Branch 0 taken 8511 times.
✓ Branch 1 taken 26499 times.
|
35010 | if (i > 1) |
123 | 8511 | name << ","; | |
124 | 35010 | paramTypes.at(i).getName(name, true); | |
125 | } | ||
126 | 35591 | name << ")"; | |
127 | 35591 | break; | |
128 | } | ||
129 | 1053 | case TY_IMPORT: | |
130 | 1053 | name << "import"; | |
131 | 1053 | break; | |
132 | − | case TY_INVALID: // GCOV_EXCL_LINE | |
133 | − | name << "invalid"; // GCOV_EXCL_LINE | |
134 | 29001 | break; | |
135 | ✗ | default: | |
136 | ✗ | throw CompilerError(INTERNAL_ERROR, "Could not get name of this type chain element"); | |
137 | } | ||
138 | 788878 | } | |
139 | |||
140 | /** | ||
141 | * Return the type name as string | ||
142 | * | ||
143 | * @param withSize Also encode array sizes | ||
144 | * @return Name as string | ||
145 | */ | ||
146 | 788878 | std::string TypeChainElement::getName(bool withSize) const { | |
147 |
1/2✓ Branch 1 taken 788878 times.
✗ Branch 2 not taken.
|
788878 | std::stringstream name; |
148 |
1/2✓ Branch 1 taken 788878 times.
✗ Branch 2 not taken.
|
788878 | getName(name, withSize); |
149 |
1/2✓ Branch 1 taken 788878 times.
✗ Branch 2 not taken.
|
1577756 | return name.str(); |
150 | 788878 | } | |
151 | |||
152 | } // namespace spice::compiler | ||
153 |