Line | Branch | Exec | Source |
---|---|---|---|
1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
2 | |||
3 | #include "TypeChecker.h" | ||
4 | |||
5 | #include <SourceFile.h> | ||
6 | #include <exception/SemanticError.h> | ||
7 | #include <symboltablebuilder/SymbolTableBuilder.h> | ||
8 | #include <typechecker/TypeMatcher.h> | ||
9 | |||
10 | namespace spice::compiler { | ||
11 | |||
12 | 401 | std::any TypeChecker::visitMainFctDefCheck(MainFctDefNode *node) { | |
13 | // Skip if already type-checked | ||
14 |
2/2✓ Branch 0 (2→3) taken 43 times.
✓ Branch 1 (2→5) taken 358 times.
|
401 | if (typeCheckedMainFct) |
15 |
1/2✓ Branch 0 (3→4) taken 43 times.
✗ Branch 1 (3→11) not taken.
|
43 | return nullptr; |
16 | |||
17 | 358 | node->resizeToNumberOfManifestations(1); | |
18 | |||
19 | // Change to function body scope | ||
20 | 358 | currentScope = node->bodyScope; | |
21 | // Visit statements in new scope | ||
22 |
2/2✓ Branch 0 (6→7) taken 330 times.
✓ Branch 1 (6→12) taken 28 times.
|
358 | visit(node->body); |
23 | // Leave main function body scope | ||
24 | 330 | currentScope = rootScope; | |
25 | |||
26 | // Set to type-checked | ||
27 | 330 | typeCheckedMainFct = true; | |
28 |
1/2✓ Branch 0 (8→9) taken 330 times.
✗ Branch 1 (8→13) not taken.
|
330 | return nullptr; |
29 | } | ||
30 | |||
31 | 13055 | std::any TypeChecker::visitFctDefCheck(FctDefNode *node) { | |
32 | 13055 | node->resizeToNumberOfManifestations(node->manifestations.size()); | |
33 | 13055 | manIdx = 0; // Reset the manifestation index | |
34 | |||
35 | // Get all manifestations for this function definition | ||
36 |
2/2✓ Branch 0 (50→6) taken 16347 times.
✓ Branch 1 (50→51) taken 13053 times.
|
29400 | for (Function *manifestation : node->manifestations) { |
37 | // Skip non-substantiated or already checked functions | ||
38 |
7/8✓ Branch 0 (7→8) taken 16347 times.
✗ Branch 1 (7→68) not taken.
✓ Branch 2 (8→9) taken 12898 times.
✓ Branch 3 (8→10) taken 3449 times.
✓ Branch 4 (9→10) taken 6940 times.
✓ Branch 5 (9→11) taken 5958 times.
✓ Branch 6 (12→13) taken 10389 times.
✓ Branch 7 (12→14) taken 5958 times.
|
16347 | if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) { |
39 | 10389 | manIdx++; // Increase the manifestation index | |
40 | 10389 | continue; | |
41 | } | ||
42 | |||
43 | // Change scope to concrete struct specialization scope | ||
44 |
2/2✓ Branch 0 (14→15) taken 3053 times.
✓ Branch 1 (14→22) taken 2905 times.
|
5958 | if (node->isMethod) { |
45 |
2/4✓ Branch 0 (15→16) taken 3053 times.
✗ Branch 1 (15→59) not taken.
✓ Branch 2 (16→17) taken 3053 times.
✗ Branch 3 (16→59) not taken.
|
3053 | const auto structSignature = Struct::getSignature(node->name->structName, manifestation->thisType.getTemplateTypes()); |
46 |
2/4✓ Branch 0 (17→18) taken 3053 times.
✗ Branch 1 (17→56) not taken.
✓ Branch 2 (18→19) taken 3053 times.
✗ Branch 3 (18→54) not taken.
|
3053 | changeToScope(STRUCT_SCOPE_PREFIX + structSignature, ScopeType::STRUCT); |
47 | 3053 | } | |
48 | |||
49 | // Change to function scope | ||
50 |
1/2✓ Branch 0 (22→23) taken 5958 times.
✗ Branch 1 (22→68) not taken.
|
5958 | changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY); |
51 | |||
52 | // Mount type mapping for this manifestation | ||
53 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 5958 times.
|
5958 | assert(typeMapping.empty()); |
54 |
1/2✓ Branch 0 (26→27) taken 5958 times.
✗ Branch 1 (26→68) not taken.
|
5958 | typeMapping = manifestation->typeMapping; |
55 | |||
56 | // Set return type to the result variable | ||
57 |
1/2✓ Branch 0 (29→30) taken 5958 times.
✗ Branch 1 (29→62) not taken.
|
17874 | SymbolTableEntry *resultVarEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME); |
58 |
1/2✗ Branch 0 (35→36) not taken.
✓ Branch 1 (35→37) taken 5958 times.
|
5958 | assert(resultVarEntry != nullptr); |
59 |
1/2✓ Branch 0 (37→38) taken 5958 times.
✗ Branch 1 (37→68) not taken.
|
5958 | resultVarEntry->updateType(manifestation->returnType, false); |
60 | 5958 | resultVarEntry->used = true; | |
61 | |||
62 | // Visit parameters | ||
63 | // This happens once in the type checker prepare stage. This second time is only required if we have a generic function | ||
64 |
2/2✓ Branch 0 (38→39) taken 4637 times.
✓ Branch 1 (38→42) taken 1321 times.
|
5958 | if (node->hasParams) |
65 |
1/2✓ Branch 0 (39→40) taken 4637 times.
✗ Branch 1 (39→66) not taken.
|
4637 | visit(node->paramLst); |
66 | |||
67 | // Visit statements in new scope | ||
68 |
2/2✓ Branch 0 (42→43) taken 5956 times.
✓ Branch 1 (42→67) taken 2 times.
|
5958 | visit(node->body); |
69 | |||
70 | // Clear type mapping | ||
71 | 5956 | typeMapping.clear(); | |
72 | |||
73 | // Change to root scope | ||
74 | 5956 | currentScope = rootScope; | |
75 |
1/2✗ Branch 0 (45→46) not taken.
✓ Branch 1 (45→47) taken 5956 times.
|
5956 | assert(currentScope->type == ScopeType::GLOBAL); |
76 | |||
77 | // Do not type-check this manifestation again | ||
78 | 5956 | manifestation->alreadyTypeChecked = true; | |
79 | |||
80 | 5956 | manIdx++; // Increase the manifestation index | |
81 | } | ||
82 | 13053 | manIdx = 0; // Reset the manifestation index | |
83 | |||
84 |
1/2✓ Branch 0 (51→52) taken 13053 times.
✗ Branch 1 (51→69) not taken.
|
13053 | return nullptr; |
85 | } | ||
86 | |||
87 | 7176 | std::any TypeChecker::visitProcDefCheck(ProcDefNode *node) { | |
88 | 7176 | node->resizeToNumberOfManifestations(node->manifestations.size()); | |
89 | 7176 | manIdx = 0; // Reset the manifestation index | |
90 | |||
91 | // Get all manifestations for this procedure definition | ||
92 |
2/2✓ Branch 0 (43→6) taken 8785 times.
✓ Branch 1 (43→44) taken 7176 times.
|
15961 | for (Function *manifestation : node->manifestations) { |
93 | // Skip non-substantiated or already checked procedures | ||
94 |
7/8✓ Branch 0 (7→8) taken 8785 times.
✗ Branch 1 (7→55) not taken.
✓ Branch 2 (8→9) taken 5164 times.
✓ Branch 3 (8→10) taken 3621 times.
✓ Branch 4 (9→10) taken 2464 times.
✓ Branch 5 (9→11) taken 2700 times.
✓ Branch 6 (12→13) taken 6085 times.
✓ Branch 7 (12→14) taken 2700 times.
|
8785 | if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) { |
95 | 6085 | manIdx++; // Increase the manifestation index | |
96 | 6085 | continue; | |
97 | } | ||
98 | |||
99 | // Change scope to concrete struct specialization scope | ||
100 |
2/2✓ Branch 0 (14→15) taken 2482 times.
✓ Branch 1 (14→22) taken 218 times.
|
2700 | if (node->isMethod) { |
101 |
2/4✓ Branch 0 (15→16) taken 2482 times.
✗ Branch 1 (15→52) not taken.
✓ Branch 2 (16→17) taken 2482 times.
✗ Branch 3 (16→52) not taken.
|
2482 | const auto structSignature = Struct::getSignature(node->name->structName, manifestation->thisType.getTemplateTypes()); |
102 |
2/4✓ Branch 0 (17→18) taken 2482 times.
✗ Branch 1 (17→49) not taken.
✓ Branch 2 (18→19) taken 2482 times.
✗ Branch 3 (18→47) not taken.
|
2482 | changeToScope(STRUCT_SCOPE_PREFIX + structSignature, ScopeType::STRUCT); |
103 | 2482 | } | |
104 | |||
105 | // Change to procedure scope | ||
106 |
1/2✓ Branch 0 (22→23) taken 2700 times.
✗ Branch 1 (22→55) not taken.
|
2700 | changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY); |
107 | |||
108 | // Mount type mapping for this manifestation | ||
109 |
1/2✗ Branch 0 (24→25) not taken.
✓ Branch 1 (24→26) taken 2700 times.
|
2700 | assert(typeMapping.empty()); |
110 |
1/2✓ Branch 0 (26→27) taken 2700 times.
✗ Branch 1 (26→55) not taken.
|
2700 | typeMapping = manifestation->typeMapping; |
111 | |||
112 | // Visit parameters | ||
113 | // This happens once in the type checker prepare stage. This second time is only required if we have a generic procedure | ||
114 |
2/2✓ Branch 0 (27→28) taken 1848 times.
✓ Branch 1 (27→31) taken 852 times.
|
2700 | if (node->hasParams) |
115 |
1/2✓ Branch 0 (28→29) taken 1848 times.
✗ Branch 1 (28→53) not taken.
|
1848 | visit(node->paramLst); |
116 | |||
117 | // Prepare generation of special ctor preamble to store VTable, default field values, etc. if required | ||
118 |
2/2✓ Branch 0 (31→32) taken 1157 times.
✓ Branch 1 (31→33) taken 1543 times.
|
2700 | if (node->isCtor) |
119 |
1/2✓ Branch 0 (32→33) taken 1157 times.
✗ Branch 1 (32→55) not taken.
|
1157 | createCtorBodyPreamble(node->scope); |
120 | |||
121 | // Visit statements in new scope | ||
122 |
1/2✓ Branch 0 (33→34) taken 2700 times.
✗ Branch 1 (33→54) not taken.
|
2700 | visit(node->body); |
123 | |||
124 | // Clear type mapping | ||
125 | 2700 | typeMapping.clear(); | |
126 | |||
127 | // Change to root scope | ||
128 | 2700 | currentScope = rootScope; | |
129 |
2/4✓ Branch 0 (36→37) taken 2700 times.
✗ Branch 1 (36→39) not taken.
✓ Branch 2 (37→38) taken 2700 times.
✗ Branch 3 (37→39) not taken.
|
2700 | assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL); |
130 | |||
131 | // Do not type-check this manifestation again | ||
132 | 2700 | manifestation->alreadyTypeChecked = true; | |
133 | |||
134 | 2700 | manIdx++; // Increase the manifestation index | |
135 | } | ||
136 | 7176 | manIdx = 0; // Reset the manifestation index | |
137 | |||
138 |
1/2✓ Branch 0 (44→45) taken 7176 times.
✗ Branch 1 (44→56) not taken.
|
7176 | return nullptr; |
139 | } | ||
140 | |||
141 | 982 | std::any TypeChecker::visitStructDefCheck(StructDefNode *node) { | |
142 | 982 | node->resizeToNumberOfManifestations(node->structManifestations.size()); | |
143 | 982 | manIdx = 0; // Reset the manifestation index | |
144 | |||
145 | // Get all manifestations for this procedure definition | ||
146 |
2/2✓ Branch 0 (146→6) taken 1569 times.
✓ Branch 1 (146→147) taken 982 times.
|
2551 | for (Struct *manifestation : node->structManifestations) { |
147 | // Skip non-substantiated or already checked procedures | ||
148 |
3/4✓ Branch 0 (7→8) taken 1569 times.
✗ Branch 1 (7→228) not taken.
✓ Branch 2 (8→9) taken 400 times.
✓ Branch 3 (8→10) taken 1169 times.
|
1569 | if (!manifestation->isFullySubstantiated()) { |
149 | 400 | manIdx++; // Increase the manifestation index | |
150 | 400 | continue; | |
151 | } | ||
152 | |||
153 | // Change to struct scope | ||
154 |
1/2✓ Branch 0 (10→11) taken 1169 times.
✗ Branch 1 (10→228) not taken.
|
1169 | changeToScope(manifestation->scope, ScopeType::STRUCT); |
155 | |||
156 | // Re-visit all default values. This is required, since the type of the default value might vary for different manifestations | ||
157 |
2/2✓ Branch 0 (20→13) taken 2790 times.
✓ Branch 1 (20→21) taken 1169 times.
|
3959 | for (const FieldNode *field : node->fields) |
158 |
2/2✓ Branch 0 (14→15) taken 452 times.
✓ Branch 1 (14→18) taken 2338 times.
|
2790 | if (field->defaultValue != nullptr) |
159 |
1/2✓ Branch 0 (15→16) taken 452 times.
✗ Branch 1 (15→150) not taken.
|
452 | visit(field->defaultValue); |
160 | |||
161 | // Build struct type | ||
162 |
1/2✓ Branch 0 (21→22) taken 1169 times.
✗ Branch 1 (21→228) not taken.
|
1169 | const QualType structType = manifestation->entry->getQualType(); |
163 | |||
164 | // Check if the struct implements all methods of all attached interfaces | ||
165 | 1169 | size_t vtableIndex = 0; | |
166 |
2/2✓ Branch 0 (100→24) taken 286 times.
✓ Branch 1 (100→101) taken 1169 times.
|
1455 | for (const QualType &interfaceType : manifestation->interfaceTypes) { |
167 |
1/2✓ Branch 0 (25→26) taken 286 times.
✗ Branch 1 (25→195) not taken.
|
286 | const Interface *interface = interfaceType.getInterface(node); |
168 |
1/2✗ Branch 0 (26→27) not taken.
✓ Branch 1 (26→28) taken 286 times.
|
286 | assert(interface != nullptr); |
169 | |||
170 | // Check for all methods, that it is implemented by the struct | ||
171 |
2/2✓ Branch 0 (97→30) taken 720 times.
✓ Branch 1 (97→98) taken 286 times.
|
1006 | for (const Function *expMethod : interface->methods) { |
172 |
1/2✓ Branch 0 (31→32) taken 720 times.
✗ Branch 1 (31→193) not taken.
|
720 | const std::string methodName = expMethod->name; |
173 |
1/2✓ Branch 0 (32→33) taken 720 times.
✗ Branch 1 (32→191) not taken.
|
720 | QualTypeList params = expMethod->getParamTypes(); |
174 | 720 | QualType returnType = expMethod->returnType; | |
175 | |||
176 | // Substantiate param and return types | ||
177 |
1/2✓ Branch 0 (33→34) taken 720 times.
✗ Branch 1 (33→189) not taken.
|
720 | TypeMatcher::substantiateTypesWithTypeMapping(params, interface->typeMapping, node); |
178 |
3/4✓ Branch 0 (34→35) taken 720 times.
✗ Branch 1 (34→189) not taken.
✓ Branch 2 (35→36) taken 402 times.
✓ Branch 3 (35→37) taken 318 times.
|
720 | if (returnType.hasAnyGenericParts()) |
179 |
1/2✓ Branch 0 (36→37) taken 402 times.
✗ Branch 1 (36→189) not taken.
|
402 | TypeMatcher::substantiateTypeWithTypeMapping(returnType, interface->typeMapping, node); |
180 | |||
181 | // Build args list | ||
182 | 720 | ArgList args; | |
183 |
1/2✓ Branch 0 (38→39) taken 720 times.
✗ Branch 1 (38→187) not taken.
|
720 | args.reserve(params.size()); |
184 |
2/2✓ Branch 0 (45→41) taken 14 times.
✓ Branch 1 (45→46) taken 720 times.
|
734 | for (const QualType ¶m : params) |
185 |
1/2✓ Branch 0 (42→43) taken 14 times.
✗ Branch 1 (42→152) not taken.
|
14 | args.emplace_back(param, nullptr); |
186 | |||
187 | // Search for method that has the required signature | ||
188 |
1/2✓ Branch 0 (47→48) taken 720 times.
✗ Branch 1 (47→154) not taken.
|
720 | Function *spiceFunction = FunctionManager::match(this, currentScope, methodName, structType, args, {}, true, node); |
189 |
2/2✓ Branch 0 (49→50) taken 4 times.
✓ Branch 1 (49→62) taken 716 times.
|
720 | if (spiceFunction == nullptr) { |
190 |
1/2✓ Branch 0 (55→56) taken 4 times.
✗ Branch 1 (55→157) not taken.
|
4 | softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED, |
191 |
4/8✓ Branch 0 (50→51) taken 4 times.
✗ Branch 1 (50→169) not taken.
✓ Branch 2 (51→52) taken 4 times.
✗ Branch 3 (51→165) not taken.
✓ Branch 4 (52→53) taken 4 times.
✗ Branch 5 (52→163) not taken.
✓ Branch 6 (53→54) taken 4 times.
✗ Branch 7 (53→161) not taken.
|
8 | "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() + |
192 |
1/2✓ Branch 0 (54→55) taken 4 times.
✗ Branch 1 (54→159) not taken.
|
4 | "'. The signature does not match."); |
193 | 4 | continue; | |
194 | } | ||
195 | |||
196 | // Check return type | ||
197 |
5/6✓ Branch 0 (62→63) taken 716 times.
✗ Branch 1 (62→187) not taken.
✓ Branch 2 (63→64) taken 120 times.
✓ Branch 3 (63→67) taken 596 times.
✓ Branch 4 (68→69) taken 2 times.
✓ Branch 5 (68→81) taken 714 times.
|
836 | if (spiceFunction->returnType != returnType && |
198 |
3/4✓ Branch 0 (64→65) taken 120 times.
✗ Branch 1 (64→187) not taken.
✓ Branch 2 (65→66) taken 2 times.
✓ Branch 3 (65→67) taken 118 times.
|
120 | !returnType.matchesInterfaceImplementedByStruct(spiceFunction->returnType)) { |
199 |
1/2✓ Branch 0 (74→75) taken 2 times.
✗ Branch 1 (74→172) not taken.
|
2 | softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED, |
200 |
4/8✓ Branch 0 (69→70) taken 2 times.
✗ Branch 1 (69→184) not taken.
✓ Branch 2 (70→71) taken 2 times.
✗ Branch 3 (70→180) not taken.
✓ Branch 4 (71→72) taken 2 times.
✗ Branch 5 (71→178) not taken.
✓ Branch 6 (72→73) taken 2 times.
✗ Branch 7 (72→176) not taken.
|
4 | "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() + |
201 |
1/2✓ Branch 0 (73→74) taken 2 times.
✗ Branch 1 (73→174) not taken.
|
2 | "'. The return type does not match."); |
202 | 2 | continue; | |
203 | } | ||
204 | // Set to virtual, since it overrides the interface method | ||
205 | 714 | spiceFunction->isVirtual = true; | |
206 | 714 | spiceFunction->vtableIndex = vtableIndex++; | |
207 |
6/6✓ Branch 0 (83→84) taken 714 times.
✓ Branch 1 (83→85) taken 6 times.
✓ Branch 2 (87→88) taken 714 times.
✓ Branch 3 (87→89) taken 6 times.
✓ Branch 4 (91→92) taken 714 times.
✓ Branch 5 (91→94) taken 6 times.
|
732 | } |
208 | } | ||
209 | |||
210 | // Generate default ctor body if required | ||
211 |
2/4✓ Branch 0 (104→105) taken 1169 times.
✗ Branch 1 (104→198) not taken.
✓ Branch 2 (105→106) taken 1169 times.
✗ Branch 3 (105→196) not taken.
|
3507 | const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, {}, true); |
212 |
4/4✓ Branch 0 (109→110) taken 539 times.
✓ Branch 1 (109→112) taken 630 times.
✓ Branch 2 (110→111) taken 72 times.
✓ Branch 3 (110→112) taken 467 times.
|
1169 | if (ctorFunc != nullptr && ctorFunc->implicitDefault) |
213 |
1/2✓ Branch 0 (111→112) taken 72 times.
✗ Branch 1 (111→228) not taken.
|
72 | createCtorBodyPreamble(ctorFunc->bodyScope); |
214 | |||
215 | // Generate default copy ctor body if required | ||
216 |
2/4✓ Branch 0 (112→113) taken 1169 times.
✗ Branch 1 (112→209) not taken.
✓ Branch 2 (116→117) taken 1169 times.
✗ Branch 3 (116→205) not taken.
|
3507 | const ArgList args = {{structType.toConstRef(node), false /* always non-temporary */}}; |
217 |
2/4✓ Branch 0 (120→121) taken 1169 times.
✗ Branch 1 (120→213) not taken.
✓ Branch 2 (121→122) taken 1169 times.
✗ Branch 3 (121→211) not taken.
|
1169 | const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, args, true); |
218 |
4/4✓ Branch 0 (124→125) taken 402 times.
✓ Branch 1 (124→127) taken 767 times.
✓ Branch 2 (125→126) taken 131 times.
✓ Branch 3 (125→127) taken 271 times.
|
1169 | if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault) |
219 |
1/2✓ Branch 0 (126→127) taken 131 times.
✗ Branch 1 (126→226) not taken.
|
131 | createCopyCtorBodyPreamble(copyCtorFunc->bodyScope); |
220 | |||
221 | // Generate default dtor body if required | ||
222 |
2/4✓ Branch 0 (130→131) taken 1169 times.
✗ Branch 1 (130→219) not taken.
✓ Branch 2 (131→132) taken 1169 times.
✗ Branch 3 (131→217) not taken.
|
3507 | const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, structType, {}, true); |
223 |
4/4✓ Branch 0 (135→136) taken 440 times.
✓ Branch 1 (135→138) taken 729 times.
✓ Branch 2 (136→137) taken 181 times.
✓ Branch 3 (136→138) taken 259 times.
|
1169 | if (dtorFunc != nullptr && dtorFunc->implicitDefault) |
224 |
1/2✓ Branch 0 (137→138) taken 181 times.
✗ Branch 1 (137→226) not taken.
|
181 | createDtorBodyPreamble(dtorFunc->bodyScope); |
225 | |||
226 | // Return to the root scope | ||
227 | 1169 | currentScope = rootScope; | |
228 |
2/4✓ Branch 0 (138→139) taken 1169 times.
✗ Branch 1 (138→141) not taken.
✓ Branch 2 (139→140) taken 1169 times.
✗ Branch 3 (139→141) not taken.
|
1169 | assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL); |
229 | |||
230 | 1169 | manIdx++; // Increase the manifestation index | |
231 | 1169 | } | |
232 | 982 | manIdx = 0; // Reset the manifestation index | |
233 | |||
234 |
1/2✓ Branch 0 (147→148) taken 982 times.
✗ Branch 1 (147→230) not taken.
|
982 | return nullptr; |
235 | } | ||
236 | |||
237 | } // namespace spice::compiler | ||
238 |