GCC Code Coverage Report


Directory: ../
File: src/typechecker/TypeCheckerPrepare.cpp
Date: 2024-12-24 01:17:15
Exec Total Coverage
Lines: 387 411 94.2%
Functions: 13 13 100.0%
Branches: 539 970 55.6%

Line Branch Exec Source
1 // Copyright (c) 2021-2024 ChilliBits. All rights reserved.
2
3 #include "TypeChecker.h"
4
5 #include <SourceFile.h>
6 #include <ast/Attributes.h>
7 #include <global/GlobalResourceManager.h>
8 #include <global/TypeRegistry.h>
9 #include <symboltablebuilder/SymbolTableBuilder.h>
10 #include <typechecker/MacroDefs.h>
11
12 namespace spice::compiler {
13
14 343 std::any TypeChecker::visitMainFctDefPrepare(MainFctDefNode *node) {
15 // Mark unreachable statements
16 343 bool returnsOnAllControlPaths = true;
17
1/2
✓ Branch 1 taken 343 times.
✗ Branch 2 not taken.
343 node->returnsOnAllControlPaths(&returnsOnAllControlPaths);
18
19 // Retrieve return type
20
1/2
✓ Branch 1 taken 343 times.
✗ Branch 2 not taken.
343 const QualType returnType(TY_INT);
21
22 // Change to function body scope
23 343 currentScope = node->bodyScope;
24
25 // Set type of 'result' variable to int
26
1/2
✓ Branch 1 taken 343 times.
✗ Branch 2 not taken.
1029 SymbolTableEntry *resultEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME);
27
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 343 times.
343 assert(resultEntry != nullptr);
28
1/2
✓ Branch 1 taken 343 times.
✗ Branch 2 not taken.
343 resultEntry->updateType(returnType, false);
29 343 resultEntry->used = true;
30
31 // Retrieve param types
32 343 QualTypeList paramTypes;
33
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 339 times.
343 if (node->takesArgs) {
34
2/4
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
4 auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst));
35
2/2
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 4 times.
12 for (const auto &[name, qualType, isOptional] : namedParamList)
36
1/2
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
8 paramTypes.push_back(qualType);
37 4 }
38
39 // Prepare type of function
40
2/4
✓ Branch 1 taken 343 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 343 times.
✗ Branch 5 not taken.
343 const QualType functionType = QualType(TY_FUNCTION).getWithFunctionParamAndReturnTypes(returnType, paramTypes);
41
42 // Update main function symbol type
43
1/2
✓ Branch 1 taken 343 times.
✗ Branch 2 not taken.
1029 SymbolTableEntry *functionEntry = rootScope->lookupStrict(MAIN_FUNCTION_NAME);
44
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 343 times.
343 assert(functionEntry != nullptr);
45
1/2
✓ Branch 1 taken 343 times.
✗ Branch 2 not taken.
343 functionEntry->updateType(functionType, false);
46 343 functionEntry->used = true;
47
48 // Leave main function body scope
49 343 currentScope = rootScope;
50
51
1/2
✓ Branch 1 taken 343 times.
✗ Branch 2 not taken.
686 return nullptr;
52 343 }
53
54 4247 std::any TypeChecker::visitFctDefPrepare(FctDefNode *node) {
55 // Check if name is dtor
56
3/4
✓ Branch 1 taken 4247 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4246 times.
4247 if (node->name->name == DTOR_FUNCTION_NAME)
57
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
3 SOFT_ERROR_BOOL(node, DTOR_MUST_BE_PROCEDURE, "Destructors are not allowed to be of type function")
58
59 // Check if all control paths in the function return
60 4246 bool doSetPredecessorsUnreachable = true;
61
3/4
✓ Branch 1 taken 4246 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4245 times.
4246 if (!node->returnsOnAllControlPaths(&doSetPredecessorsUnreachable))
62
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
3 SOFT_ERROR_BOOL(node, MISSING_RETURN_STMT, "Not all control paths of this function have a return statement")
63
64 // Change to function scope
65 4245 currentScope = node->scope;
66
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4245 times.
4245 assert(currentScope->type == ScopeType::FUNC_PROC_BODY);
67
68 // Retrieve function template types
69 4245 std::vector<GenericType> usedGenericTypes;
70
2/2
✓ Branch 0 taken 699 times.
✓ Branch 1 taken 3546 times.
4245 if (node->hasTemplateTypes) {
71
2/2
✓ Branch 5 taken 848 times.
✓ Branch 6 taken 698 times.
1546 for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) {
72 // Visit template type
73
2/4
✓ Branch 1 taken 848 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 848 times.
✗ Branch 5 not taken.
848 auto templateType = std::any_cast<QualType>(visit(dataType));
74
2/4
✓ Branch 1 taken 848 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 848 times.
848 if (templateType.is(TY_UNRESOLVED))
75 continue;
76 // Check if it is a generic type
77
3/4
✓ Branch 1 taken 848 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 847 times.
848 if (!templateType.is(TY_GENERIC))
78
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
3 throw SemanticError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types");
79 // Convert generic symbol type to generic type
80
2/4
✓ Branch 1 taken 847 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 847 times.
✗ Branch 5 not taken.
847 GenericType *genericType = node->scope->lookupGenericType(templateType.getSubType());
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 847 times.
847 assert(genericType != nullptr);
82
1/2
✓ Branch 1 taken 847 times.
✗ Branch 2 not taken.
847 usedGenericTypes.push_back(*genericType);
83 }
84 }
85
86 // Retrieve 'this' type
87
1/2
✓ Branch 1 taken 4244 times.
✗ Branch 2 not taken.
4244 QualType thisType(TY_DYN); // If the function is not a method, the default this type is TY_DYN
88 4244 QualType thisPtrType = thisType;
89
2/2
✓ Branch 0 taken 2362 times.
✓ Branch 1 taken 1882 times.
4244 if (node->isMethod) {
90 2362 Scope *structParentScope = node->structScope->parent;
91
1/2
✓ Branch 1 taken 2362 times.
✗ Branch 2 not taken.
2362 SymbolTableEntry *structEntry = structParentScope->lookupStrict(node->name->structName);
92
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2362 times.
2362 assert(structEntry != nullptr);
93 // Set struct to used
94 2362 structEntry->used = true;
95 // Get type and ptr type
96
1/2
✓ Branch 1 taken 2362 times.
✗ Branch 2 not taken.
2362 thisType = structEntry->getQualType();
97
1/2
✓ Branch 1 taken 2362 times.
✗ Branch 2 not taken.
2362 thisPtrType = thisType.toPtr(node);
98 // Collect template types of 'this' type
99
3/4
✓ Branch 1 taken 2362 times.
✗ Branch 2 not taken.
✓ Branch 7 taken 833 times.
✓ Branch 8 taken 2362 times.
3195 for (const QualType &templateType : thisType.getTemplateTypes()) {
100 196 const auto lambda = [&](const GenericType &genericType) { return genericType == templateType; };
101
3/4
✓ Branch 1 taken 833 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 829 times.
✓ Branch 4 taken 4 times.
833 if (std::ranges::none_of(usedGenericTypes, lambda))
102
1/2
✓ Branch 1 taken 829 times.
✗ Branch 2 not taken.
829 usedGenericTypes.emplace_back(templateType);
103 833 usedGenericTypes.back().used = true;
104 }
105
106 // Set type of 'this' variable
107
1/2
✓ Branch 1 taken 2362 times.
✗ Branch 2 not taken.
7086 SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2362 times.
2362 assert(thisEntry != nullptr);
109
1/2
✓ Branch 1 taken 2362 times.
✗ Branch 2 not taken.
2362 thisEntry->updateType(thisPtrType, false);
110 }
111
112 // Visit parameters
113 4244 QualTypeList paramTypes;
114 4244 ParamList paramList;
115
2/2
✓ Branch 0 taken 3001 times.
✓ Branch 1 taken 1243 times.
4244 if (node->hasParams) {
116 3001 std::vector<const char *> paramNames;
117 // Visit param list to retrieve the param names
118
2/4
✓ Branch 1 taken 3001 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 3001 times.
✗ Branch 5 not taken.
3001 auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst));
119
2/2
✓ Branch 5 taken 5322 times.
✓ Branch 6 taken 2999 times.
8321 for (const auto &[name, qualType, isOptional] : namedParamList) {
120
1/2
✓ Branch 1 taken 5322 times.
✗ Branch 2 not taken.
5322 paramNames.push_back(name);
121
1/2
✓ Branch 1 taken 5322 times.
✗ Branch 2 not taken.
5322 paramTypes.push_back(qualType);
122
1/2
✓ Branch 1 taken 5322 times.
✗ Branch 2 not taken.
5322 paramList.push_back({qualType, isOptional});
123 // Check if the type is present in the template for generic types
124
3/4
✓ Branch 1 taken 5322 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 5320 times.
5322 if (!qualType.isCoveredByGenericTypeList(usedGenericTypes))
125 throw SemanticError(node->paramLst, GENERIC_TYPE_NOT_IN_TEMPLATE,
126
2/4
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
6 "Generic param type not included in the template type list of the function");
127 }
128 3003 }
129
130 // Retrieve return type
131
2/4
✓ Branch 1 taken 4242 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4242 times.
✗ Branch 5 not taken.
4242 auto returnType = std::any_cast<QualType>(visit(node->returnType));
132
2/6
✓ Branch 1 taken 4242 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4242 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
4242 HANDLE_UNRESOLVED_TYPE_PTR(returnType)
133
3/4
✓ Branch 1 taken 4242 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4241 times.
4242 if (returnType.is(TY_DYN))
134
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
3 SOFT_ERROR_BOOL(node, UNEXPECTED_DYN_TYPE, "Dyn return types are not allowed")
135
3/4
✓ Branch 1 taken 4241 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 4240 times.
4241 if (!returnType.isCoveredByGenericTypeList(usedGenericTypes))
136
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
3 SOFT_ERROR_BOOL(node->returnType, GENERIC_TYPE_NOT_IN_TEMPLATE,
137 "Generic return type not included in the template type list of the function")
138
139 // Leave function body scope
140 4240 currentScope = node->scope->parent;
141
3/4
✓ Branch 0 taken 2362 times.
✓ Branch 1 taken 1878 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2362 times.
4240 assert(currentScope->type == ScopeType::GLOBAL || currentScope->type == ScopeType::STRUCT);
142
143 // Prepare type of function
144
2/4
✓ Branch 1 taken 4240 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4240 times.
✗ Branch 5 not taken.
4240 QualType functionType = QualType(TY_FUNCTION).getWithFunctionParamAndReturnTypes(returnType, paramTypes);
145 4240 functionType.setSpecifiers(node->specifiers);
146
147 // Update type of function entry
148
1/2
✓ Branch 1 taken 4240 times.
✗ Branch 2 not taken.
8480 SymbolTableEntry *functionEntry = currentScope->lookupStrict(node->getSymbolTableEntryName());
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4240 times.
4240 assert(functionEntry != nullptr);
150
1/2
✓ Branch 1 taken 4240 times.
✗ Branch 2 not taken.
4240 functionEntry->updateType(functionType, false);
151
152 // Build function object
153
3/6
✓ Branch 1 taken 4240 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 4240 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 4240 times.
✗ Branch 8 not taken.
4240 Function spiceFunc(node->name->name, functionEntry, thisType, returnType, paramList, usedGenericTypes, node);
154 4240 spiceFunc.bodyScope = node->scope;
155
2/2
✓ Branch 1 taken 4239 times.
✓ Branch 2 taken 1 times.
4240 FunctionManager::insert(currentScope, spiceFunc, &node->manifestations);
156
157 // Check function attributes
158
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 4228 times.
4239 if (node->attrs) {
159 11 const AttrLstNode *attrLst = node->attrs->attrLst;
160 11 Function *firstManifestation = node->manifestations.front();
161
3/6
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 11 times.
33 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE))
162 firstManifestation->mangleFunctionName = value->boolValue;
163
3/6
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 11 times.
33 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) {
164 const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset);
165 firstManifestation->predefinedMangledName = stringValue;
166 }
167
5/8
✓ Branch 1 taken 11 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 11 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 11 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 10 times.
✓ Branch 10 taken 1 times.
33 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_TEST); value && value->boolValue) {
168 // Make sure that the function has the correct signature
169
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 9 times.
10 if (node->hasParams)
170
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
3 throw SemanticError(node->paramLst, TEST_FUNCTION_WITH_PARAMS, "Test function may not have parameters");
171
3/4
✓ Branch 1 taken 9 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 8 times.
9 if (!returnType.is(TY_BOOL))
172
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
3 throw SemanticError(node->returnType, TEST_FUNCTION_WRONG_RETURN_TYPE, "Test function must return a bool");
173 // Add to test function list
174 8 firstManifestation->entry->used = true; // Avoid printing unused warnings
175 8 firstManifestation->used = true; // Always keep test functions, because they are called implicitly by the test main
176
1/2
✓ Branch 2 taken 8 times.
✗ Branch 3 not taken.
8 sourceFile->testFunctions.push_back(node->manifestations.front());
177 }
178 }
179
180 // Duplicate / rename the original child scope to reflect the substantiated versions of the function
181
2/2
✓ Branch 1 taken 535 times.
✓ Branch 2 taken 4237 times.
4772 for (size_t i = 1; i < node->manifestations.size(); i++) {
182
4/8
✓ Branch 1 taken 535 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 535 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 535 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 535 times.
✗ Branch 11 not taken.
535 Scope *scope = currentScope->copyChildScope(node->getScopeId(), node->manifestations.at(i)->getSignature(false));
183
1/2
✓ Branch 1 taken 535 times.
✗ Branch 2 not taken.
535 node->manifestations.at(i)->bodyScope = scope;
184 }
185
3/6
✓ Branch 2 taken 4237 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 4237 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 4237 times.
✗ Branch 9 not taken.
4237 currentScope->renameChildScope(node->getScopeId(), node->manifestations.front()->getSignature(false));
186
187 // Change to the root scope
188 4237 currentScope = rootScope;
189
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4237 times.
4237 assert(currentScope->type == ScopeType::GLOBAL);
190
191
1/2
✓ Branch 1 taken 4237 times.
✗ Branch 2 not taken.
4237 return nullptr;
192 4258 }
193
194 2701 std::any TypeChecker::visitProcDefPrepare(ProcDefNode *node) {
195 // Mark unreachable statements
196 2701 bool doSetPredecessorsUnreachable = true;
197
1/2
✓ Branch 1 taken 2701 times.
✗ Branch 2 not taken.
2701 node->returnsOnAllControlPaths(&doSetPredecessorsUnreachable);
198
199 // Check if dtor and has params
200
7/8
✓ Branch 0 taken 1914 times.
✓ Branch 1 taken 787 times.
✓ Branch 3 taken 1914 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1913 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 2700 times.
2701 if (node->hasParams && node->name->name == DTOR_FUNCTION_NAME)
201
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
3 throw SemanticError(node, DTOR_WITH_PARAMS, "It is not allowed to specify parameters for destructors");
202
203 // Change to procedure scope
204 2700 currentScope = node->scope;
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2700 times.
2700 assert(currentScope->type == ScopeType::FUNC_PROC_BODY);
206
207 // Retrieve procedure template types
208 2700 std::vector<GenericType> usedGenericTypes;
209
2/2
✓ Branch 0 taken 514 times.
✓ Branch 1 taken 2186 times.
2700 if (node->hasTemplateTypes) {
210
2/2
✓ Branch 5 taken 574 times.
✓ Branch 6 taken 513 times.
1087 for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) {
211 // Visit template type
212
2/4
✓ Branch 1 taken 574 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 574 times.
✗ Branch 5 not taken.
574 auto templateType = std::any_cast<QualType>(visit(dataType));
213
2/4
✓ Branch 1 taken 574 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 574 times.
574 if (templateType.is(TY_UNRESOLVED))
214 continue;
215 // Check if it is a generic type
216
3/4
✓ Branch 1 taken 574 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 573 times.
574 if (!templateType.is(TY_GENERIC))
217
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
3 throw SemanticError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types");
218 // Convert generic symbol type to generic type
219
2/4
✓ Branch 1 taken 573 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 573 times.
✗ Branch 5 not taken.
573 const GenericType *genericType = node->scope->lookupGenericType(templateType.getSubType());
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 573 times.
573 assert(genericType != nullptr);
221
1/2
✓ Branch 1 taken 573 times.
✗ Branch 2 not taken.
573 usedGenericTypes.push_back(*genericType);
222 }
223 }
224
225 // Retrieve 'this' type
226
1/2
✓ Branch 1 taken 2699 times.
✗ Branch 2 not taken.
2699 QualType thisType(TY_DYN); // If the procedure is not a method, the default this type is TY_DYN
227 2699 QualType thisPtrType = thisType;
228
2/2
✓ Branch 0 taken 2267 times.
✓ Branch 1 taken 432 times.
2699 if (node->isMethod) {
229 2267 Scope *structParentScope = node->structScope->parent;
230
1/2
✓ Branch 1 taken 2267 times.
✗ Branch 2 not taken.
2267 SymbolTableEntry *structEntry = structParentScope->lookupStrict(node->name->structName);
231
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2267 times.
2267 assert(structEntry != nullptr);
232 // Set struct to used
233 2267 structEntry->used = true;
234 // Get type and ptr type
235
1/2
✓ Branch 1 taken 2267 times.
✗ Branch 2 not taken.
2267 thisType = structEntry->getQualType();
236
1/2
✓ Branch 1 taken 2267 times.
✗ Branch 2 not taken.
2267 thisPtrType = thisType.toPtr(node);
237 // Collect template types of 'this' type
238
3/4
✓ Branch 1 taken 2267 times.
✗ Branch 2 not taken.
✓ Branch 7 taken 793 times.
✓ Branch 8 taken 2267 times.
3060 for (const QualType &templateType : thisType.getTemplateTypes()) {
239 250 const auto lambda = [&](const GenericType &genericType) { return genericType == templateType; };
240
3/4
✓ Branch 1 taken 793 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 736 times.
✓ Branch 4 taken 57 times.
793 if (std::ranges::none_of(usedGenericTypes, lambda))
241
1/2
✓ Branch 1 taken 736 times.
✗ Branch 2 not taken.
736 usedGenericTypes.emplace_back(templateType);
242 793 usedGenericTypes.back().used = true;
243 }
244
245 // Set type of 'this' variable
246
1/2
✓ Branch 1 taken 2267 times.
✗ Branch 2 not taken.
6801 SymbolTableEntry *thisEntry = currentScope->lookupStrict(THIS_VARIABLE_NAME);
247
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2267 times.
2267 assert(thisEntry != nullptr);
248
1/2
✓ Branch 1 taken 2267 times.
✗ Branch 2 not taken.
2267 thisEntry->updateType(thisPtrType, false);
249 }
250
251 // Visit parameters
252 2699 QualTypeList paramTypes;
253 2699 ParamList paramList;
254
2/2
✓ Branch 0 taken 1913 times.
✓ Branch 1 taken 786 times.
2699 if (node->hasParams) {
255 1913 std::vector<const char *> paramNames;
256 // Visit param list to retrieve the param names
257
2/4
✓ Branch 1 taken 1913 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1913 times.
✗ Branch 5 not taken.
1913 auto namedParamList = std::any_cast<NamedParamList>(visit(node->paramLst));
258
2/2
✓ Branch 5 taken 2389 times.
✓ Branch 6 taken 1912 times.
4301 for (const auto &[name, qualType, isOptional] : namedParamList) {
259
1/2
✓ Branch 1 taken 2389 times.
✗ Branch 2 not taken.
2389 paramNames.push_back(name);
260
1/2
✓ Branch 1 taken 2389 times.
✗ Branch 2 not taken.
2389 paramTypes.push_back(qualType);
261
1/2
✓ Branch 1 taken 2389 times.
✗ Branch 2 not taken.
2389 paramList.push_back({qualType, isOptional});
262 // Check if the type is present in the template for generic types
263
3/4
✓ Branch 1 taken 2389 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 2388 times.
2389 if (!qualType.isCoveredByGenericTypeList(usedGenericTypes))
264 throw SemanticError(node->paramLst, GENERIC_TYPE_NOT_IN_TEMPLATE,
265
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
3 "Generic param type not included in the template type list of the procedure");
266 }
267 1914 }
268
269 // Leave procedure body scope
270 2698 currentScope = node->scope->parent;
271
3/4
✓ Branch 0 taken 2267 times.
✓ Branch 1 taken 431 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2267 times.
2698 assert(currentScope->type == ScopeType::GLOBAL || currentScope->type == ScopeType::STRUCT);
272
273 // Prepare type of procedure
274
3/6
✓ Branch 1 taken 2698 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2698 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2698 times.
✗ Branch 8 not taken.
2698 QualType procedureType = QualType(TY_PROCEDURE).getWithFunctionParamAndReturnTypes(QualType(TY_DYN), paramTypes);
275 2698 procedureType.setSpecifiers(node->specifiers);
276
277 // Update type of procedure entry
278
1/2
✓ Branch 1 taken 2698 times.
✗ Branch 2 not taken.
5396 SymbolTableEntry *procedureEntry = currentScope->lookupStrict(node->getSymbolTableEntryName());
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2698 times.
2698 assert(procedureEntry != nullptr);
280
1/2
✓ Branch 1 taken 2698 times.
✗ Branch 2 not taken.
2698 procedureEntry->updateType(procedureType, false);
281
282 // Build procedure object
283
4/8
✓ Branch 1 taken 2698 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2698 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2698 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2698 times.
✗ Branch 11 not taken.
2698 Function spiceProc(node->name->name, procedureEntry, thisType, QualType(TY_DYN), paramList, usedGenericTypes, node);
284 2698 spiceProc.bodyScope = node->scope;
285
2/2
✓ Branch 1 taken 2697 times.
✓ Branch 2 taken 1 times.
2698 FunctionManager::insert(currentScope, spiceProc, &node->manifestations);
286
287 // Check procedure attributes
288
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2697 times.
2697 if (node->attrs) {
289 const AttrLstNode *attrLst = node->attrs->attrLst;
290 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE))
291 node->manifestations.front()->mangleFunctionName = value->boolValue;
292 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) {
293 const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset);
294 node->manifestations.front()->predefinedMangledName = stringValue;
295 }
296 }
297
298 // Duplicate / rename the original child scope to reflect the substantiated versions of the procedure
299
2/2
✓ Branch 1 taken 125 times.
✓ Branch 2 taken 2697 times.
2822 for (size_t i = 1; i < node->manifestations.size(); i++) {
300
4/8
✓ Branch 1 taken 125 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 125 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 125 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 125 times.
✗ Branch 11 not taken.
125 Scope *scope = currentScope->copyChildScope(node->getScopeId(), node->manifestations.at(i)->getSignature(false));
301
1/2
✓ Branch 1 taken 125 times.
✗ Branch 2 not taken.
125 node->manifestations.at(i)->bodyScope = scope;
302 }
303
3/6
✓ Branch 2 taken 2697 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2697 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 2697 times.
✗ Branch 9 not taken.
2697 currentScope->renameChildScope(node->getScopeId(), node->manifestations.front()->getSignature(false));
304
305 // Change to the root scope
306 2697 currentScope = rootScope;
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2697 times.
2697 assert(currentScope->type == ScopeType::GLOBAL);
308
309
1/2
✓ Branch 1 taken 2697 times.
✗ Branch 2 not taken.
5394 return nullptr;
310 2705 }
311
312 544 std::any TypeChecker::visitStructDefPrepare(StructDefNode *node) {
313 544 QualTypeList usedTemplateTypes;
314 544 std::vector<GenericType> templateTypesGeneric;
315
316 // Retrieve struct template types
317
2/2
✓ Branch 0 taken 172 times.
✓ Branch 1 taken 372 times.
544 if (node->hasTemplateTypes) {
318
1/2
✓ Branch 2 taken 172 times.
✗ Branch 3 not taken.
172 usedTemplateTypes.reserve(node->templateTypeLst->dataTypes.size());
319
1/2
✓ Branch 2 taken 172 times.
✗ Branch 3 not taken.
172 templateTypesGeneric.reserve(node->templateTypeLst->dataTypes.size());
320
2/2
✓ Branch 5 taken 234 times.
✓ Branch 6 taken 172 times.
406 for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) {
321 // Visit template type
322
2/4
✓ Branch 1 taken 234 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 234 times.
✗ Branch 5 not taken.
234 auto templateType = std::any_cast<QualType>(visit(dataType));
323
2/4
✓ Branch 1 taken 234 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 234 times.
234 if (templateType.is(TY_UNRESOLVED))
324 continue;
325 // Check if it is a generic type
326
2/4
✓ Branch 1 taken 234 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 234 times.
234 if (!templateType.is(TY_GENERIC))
327 throw SemanticError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types");
328 // Convert generic symbol type to generic type
329
2/4
✓ Branch 1 taken 234 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 234 times.
✗ Branch 5 not taken.
234 GenericType *genericType = currentScope->lookupGenericType(templateType.getSubType());
330
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 234 times.
234 assert(genericType != nullptr);
331
1/2
✓ Branch 1 taken 234 times.
✗ Branch 2 not taken.
234 usedTemplateTypes.push_back(*genericType);
332
1/2
✓ Branch 1 taken 234 times.
✗ Branch 2 not taken.
234 templateTypesGeneric.push_back(*genericType);
333 }
334 }
335
336 // Retrieve implemented interfaces
337 544 QualTypeList interfaceTypes;
338
2/2
✓ Branch 0 taken 87 times.
✓ Branch 1 taken 457 times.
544 if (node->hasInterfaces) {
339
1/2
✓ Branch 2 taken 87 times.
✗ Branch 3 not taken.
87 interfaceTypes.reserve(node->interfaceTypeLst->dataTypes.size());
340
2/2
✓ Branch 5 taken 87 times.
✓ Branch 6 taken 86 times.
173 for (DataTypeNode *interfaceNode : node->interfaceTypeLst->dataTypes) {
341 // Visit interface type
342
2/4
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 87 times.
✗ Branch 5 not taken.
87 auto interfaceType = std::any_cast<QualType>(visit(interfaceNode));
343
2/4
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 87 times.
87 if (interfaceType.is(TY_UNRESOLVED))
344 continue;
345 // Check if it is an interface type
346
2/4
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 87 times.
87 if (!interfaceType.is(TY_INTERFACE))
347 throw SemanticError(interfaceNode, EXPECTED_INTERFACE_TYPE,
348 "Expected interface type, got " + interfaceType.getName(false));
349 // Check for visibility
350
9/12
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 87 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 74 times.
✓ Branch 7 taken 13 times.
✓ Branch 9 taken 74 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 73 times.
✓ Branch 13 taken 1 times.
✓ Branch 14 taken 86 times.
87 if (interfaceType.getBodyScope()->isImportedBy(rootScope) && !interfaceType.isPublic())
351 throw SemanticError(node, INSUFFICIENT_VISIBILITY,
352
4/8
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
1 "Cannot access interface '" + interfaceType.getSubType() + "' due to its private visibility");
353 // Add to interface types
354
1/2
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
86 interfaceTypes.push_back(interfaceType);
355 // Update the type of the entry for that interface field
356 86 const std::string &interfaceName = interfaceNode->baseDataType->customDataType->typeNameFragments.back();
357
1/2
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
172 SymbolTableEntry *interfaceEntry = node->structScope->lookupStrict("this." + interfaceName);
358
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 86 times.
86 assert(interfaceEntry != nullptr);
359
1/2
✓ Branch 1 taken 86 times.
✗ Branch 2 not taken.
86 interfaceEntry->updateType(interfaceType, false);
360 }
361 }
362
363 // Update type of struct entry
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 543 times.
543 assert(node->entry != nullptr);
365 543 const TypeChainElementData data = {.bodyScope = node->structScope};
366
1/2
✓ Branch 1 taken 543 times.
✗ Branch 2 not taken.
543 const Type *type = TypeRegistry::getOrInsert(TY_STRUCT, node->structName, node->typeId, data, usedTemplateTypes);
367
2/4
✓ Branch 1 taken 543 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 543 times.
✗ Branch 5 not taken.
543 node->entry->updateType(QualType(type, node->structSpecifiers), false);
368
369 // Change to struct scope
370 543 currentScope = node->structScope;
371
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 543 times.
543 assert(currentScope->type == ScopeType::STRUCT);
372
373 // Retrieve field types
374 543 QualTypeList fieldTypes;
375
1/2
✓ Branch 2 taken 543 times.
✗ Branch 3 not taken.
543 fieldTypes.reserve(node->fields.size());
376
2/2
✓ Branch 5 taken 1182 times.
✓ Branch 6 taken 540 times.
1722 for (FieldNode *field : node->fields) {
377 // Visit field type
378
2/4
✓ Branch 1 taken 1182 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1182 times.
✗ Branch 5 not taken.
1182 auto fieldType = std::any_cast<QualType>(visit(field));
379
3/4
✓ Branch 1 taken 1182 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 1180 times.
1182 if (fieldType.is(TY_UNRESOLVED))
380
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 sourceFile->checkForSoftErrors(); // We get into trouble if we continue without the field type -> abort
381
382 // Check for struct with infinite size.
383 // This can happen if the struct A has a field with type A
384
6/10
✓ Branch 1 taken 1180 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 111 times.
✓ Branch 4 taken 1069 times.
✓ Branch 6 taken 111 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 111 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 1180 times.
1180 if (fieldType.is(TY_STRUCT) && fieldType.getBodyScope() == node->structScope)
385 throw SemanticError(field, STRUCT_INFINITE_SIZE, "Struct with infinite size detected");
386
387 // Add to field types
388
1/2
✓ Branch 1 taken 1180 times.
✗ Branch 2 not taken.
1180 fieldTypes.push_back(fieldType);
389
390 // Update type of field entry
391
1/2
✓ Branch 1 taken 1180 times.
✗ Branch 2 not taken.
1180 SymbolTableEntry *fieldEntry = currentScope->lookupStrict(field->fieldName);
392
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1180 times.
1180 assert(fieldEntry != nullptr);
393
1/2
✓ Branch 1 taken 1180 times.
✗ Branch 2 not taken.
1180 fieldEntry->updateType(fieldType, false);
394
395 // Check if the template type list contains this type
396
3/4
✓ Branch 1 taken 1180 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1179 times.
1180 if (!fieldType.isCoveredByGenericTypeList(templateTypesGeneric))
397
2/4
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
3 throw SemanticError(field->dataType, GENERIC_TYPE_NOT_IN_TEMPLATE, "Generic field type not included in struct template");
398 }
399
400 // Change to the root scope
401 540 currentScope = rootScope;
402
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 540 times.
540 assert(currentScope->type == ScopeType::GLOBAL);
403
404 // Build struct object
405
4/8
✓ Branch 1 taken 540 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 540 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 540 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 540 times.
✗ Branch 11 not taken.
540 Struct spiceStruct(node->structName, node->entry, node->structScope, fieldTypes, templateTypesGeneric, interfaceTypes, node);
406
1/2
✓ Branch 1 taken 540 times.
✗ Branch 2 not taken.
540 StructManager::insert(currentScope, spiceStruct, &node->structManifestations);
407 540 spiceStruct.scope = node->structScope;
408
409 // Request RTTI runtime if the struct is polymorphic
410 540 node->emitVTable |= node->hasInterfaces;
411
12/18
✓ Branch 0 taken 51 times.
✓ Branch 1 taken 489 times.
✓ Branch 3 taken 51 times.
✗ Branch 4 not taken.
✓ Branch 6 taken 51 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 50 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 51 times.
✓ Branch 11 taken 489 times.
✓ Branch 13 taken 51 times.
✓ Branch 14 taken 489 times.
✓ Branch 15 taken 50 times.
✓ Branch 16 taken 490 times.
✗ Branch 17 not taken.
✗ Branch 18 not taken.
✗ Branch 20 not taken.
✗ Branch 21 not taken.
642 if (node->attrs && node->attrs->attrLst->hasAttr(ATTR_CORE_COMPILER_EMIT_VTABLE))
412
2/4
✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 50 times.
✗ Branch 5 not taken.
150 node->emitVTable |= node->attrs->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_EMIT_VTABLE)->boolValue;
413
7/8
✓ Branch 0 taken 136 times.
✓ Branch 1 taken 404 times.
✓ Branch 3 taken 136 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 87 times.
✓ Branch 6 taken 49 times.
✓ Branch 7 taken 87 times.
✓ Branch 8 taken 453 times.
676 if (node->emitVTable && !sourceFile->isRttiRT())
414
1/2
✓ Branch 1 taken 87 times.
✗ Branch 2 not taken.
87 sourceFile->requestRuntimeModule(RTTI_RT);
415
416
1/2
✓ Branch 1 taken 540 times.
✗ Branch 2 not taken.
1080 return nullptr;
417 555 }
418
419 74 std::any TypeChecker::visitInterfaceDefPrepare(InterfaceDefNode *node) {
420 74 QualTypeList usedTemplateTypes;
421 74 std::vector<GenericType> templateTypesGeneric;
422
423 // Retrieve interface template types
424
2/2
✓ Branch 0 taken 56 times.
✓ Branch 1 taken 18 times.
74 if (node->hasTemplateTypes) {
425
1/2
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
56 usedTemplateTypes.reserve(node->templateTypeLst->dataTypes.size());
426
1/2
✓ Branch 2 taken 56 times.
✗ Branch 3 not taken.
56 templateTypesGeneric.reserve(node->templateTypeLst->dataTypes.size());
427
2/2
✓ Branch 5 taken 56 times.
✓ Branch 6 taken 56 times.
112 for (DataTypeNode *dataType : node->templateTypeLst->dataTypes) {
428 // Visit template type
429
2/4
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 56 times.
✗ Branch 5 not taken.
56 auto templateType = std::any_cast<QualType>(visit(dataType));
430
2/6
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 56 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
56 HANDLE_UNRESOLVED_TYPE_PTR(templateType)
431 // Check if it is a generic type
432
2/4
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 56 times.
56 if (!templateType.is(TY_GENERIC)) {
433 softError(dataType, EXPECTED_GENERIC_TYPE, "A template list can only contain generic types");
434 continue;
435 }
436 // Convert generic symbol type to generic type
437
2/4
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 56 times.
✗ Branch 5 not taken.
56 GenericType *genericType = currentScope->lookupGenericType(templateType.getSubType());
438
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 56 times.
56 assert(genericType != nullptr);
439
1/2
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
56 usedTemplateTypes.push_back(*genericType);
440
1/2
✓ Branch 1 taken 56 times.
✗ Branch 2 not taken.
56 templateTypesGeneric.push_back(*genericType);
441 }
442 }
443
444 // Update type of interface entry
445 74 const TypeChainElementData data = {.bodyScope = node->interfaceScope};
446
1/2
✓ Branch 1 taken 74 times.
✗ Branch 2 not taken.
74 const Type *type = TypeRegistry::getOrInsert(TY_INTERFACE, node->interfaceName, node->typeId, data, usedTemplateTypes);
447
1/2
✓ Branch 1 taken 74 times.
✗ Branch 2 not taken.
74 const QualType interfaceType(type, node->interfaceSpecifiers);
448
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 assert(node->entry != nullptr);
449
1/2
✓ Branch 1 taken 74 times.
✗ Branch 2 not taken.
74 node->entry->updateType(interfaceType, false);
450
451 // Change to interface scope
452 74 currentScope = node->interfaceScope;
453
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 74 times.
74 assert(currentScope->type == ScopeType::INTERFACE);
454
455 // Visit methods
456 74 size_t vtableIndex = 0;
457 74 std::vector<Function *> methods;
458
1/2
✓ Branch 2 taken 74 times.
✗ Branch 3 not taken.
74 methods.reserve(node->signatures.size());
459
2/2
✓ Branch 4 taken 172 times.
✓ Branch 5 taken 73 times.
245 for (SignatureNode *signature : node->signatures) {
460
2/4
✓ Branch 1 taken 172 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 172 times.
✗ Branch 5 not taken.
172 const auto method = std::any_cast<std::vector<Function *> *>(visit(signature));
461
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 171 times.
172 if (!method)
462
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 return nullptr;
463
464 // Set 'this' type
465
2/2
✓ Branch 4 taken 171 times.
✓ Branch 5 taken 171 times.
342 for (Function *m : *method) {
466 171 m->isVirtual = true; // Interface methods are always virtual
467 171 m->vtableIndex = vtableIndex;
468 171 m->thisType = interfaceType;
469 }
470
471
1/2
✓ Branch 5 taken 171 times.
✗ Branch 6 not taken.
171 methods.insert(methods.end(), method->begin(), method->end());
472 171 vtableIndex++;
473 }
474
475 // Change to root scope
476 73 currentScope = rootScope;
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 73 times.
73 assert(currentScope->type == ScopeType::GLOBAL);
478
479 // Build interface object
480
3/6
✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 73 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 73 times.
✗ Branch 8 not taken.
73 Interface spiceInterface(node->interfaceName, node->entry, node->interfaceScope, methods, templateTypesGeneric, node);
481
1/2
✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
73 InterfaceManager::insert(currentScope, spiceInterface, &node->interfaceManifestations);
482 73 spiceInterface.scope = node->interfaceScope;
483
484 // Request RTTI runtime, that is always required when dealing with interfaces due to polymorphism
485
2/4
✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 73 times.
✗ Branch 4 not taken.
146 if (!sourceFile->isRttiRT())
486
1/2
✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
73 sourceFile->requestRuntimeModule(RTTI_RT);
487
488
1/2
✓ Branch 1 taken 73 times.
✗ Branch 2 not taken.
73 return nullptr;
489 74 }
490
491 38 std::any TypeChecker::visitEnumDefPrepare(EnumDefNode *node) {
492 // Update type of enum entry
493 38 const TypeChainElementData data = {.bodyScope = node->enumScope};
494
1/2
✓ Branch 2 taken 38 times.
✗ Branch 3 not taken.
38 const Type *type = TypeRegistry::getOrInsert(TY_ENUM, node->enumName, node->typeId, data, {});
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 assert(node->entry != nullptr);
496
2/4
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 38 times.
✗ Branch 5 not taken.
38 node->entry->updateType(QualType(type, node->enumSpecifiers), false);
497
498 // Change to enum scope
499 38 currentScope = node->enumScope;
500
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 assert(currentScope->type == ScopeType::ENUM);
501
502 // Loop through all items with values
503 38 std::vector<std::string> names;
504 38 std::vector<uint32_t> values;
505
2/2
✓ Branch 5 taken 432 times.
✓ Branch 6 taken 38 times.
470 for (const EnumItemNode *enumItem : node->itemLst->items) {
506 // Save name
507
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 names.push_back(enumItem->itemName);
508 // Check for duplicate value
509
2/2
✓ Branch 0 taken 263 times.
✓ Branch 1 taken 169 times.
432 if (enumItem->hasValue) {
510
3/4
✓ Branch 2 taken 263 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 262 times.
263 if (std::ranges::find(values, enumItem->itemValue) != values.end()) {
511
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 softError(enumItem, DUPLICATE_ENUM_ITEM_VALUE, "Duplicate enum item value, please use another");
512 1 continue;
513 }
514
1/2
✓ Branch 1 taken 262 times.
✗ Branch 2 not taken.
262 values.push_back(enumItem->itemValue);
515 }
516 }
517
518 // Loop through all items without values
519 38 uint32_t nextValue = 0;
520
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
38 const QualType intSymbolType(TY_INT);
521
2/2
✓ Branch 5 taken 432 times.
✓ Branch 6 taken 38 times.
470 for (EnumItemNode *enumItem : node->itemLst->items) {
522 // Update type of enum item entry
523
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 SymbolTableEntry *itemEntry = currentScope->lookupStrict(enumItem->itemName);
524
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 432 times.
432 assert(itemEntry != nullptr);
525
1/2
✓ Branch 1 taken 432 times.
✗ Branch 2 not taken.
432 itemEntry->updateType(intSymbolType, false);
526 // Fill in value if not filled yet
527
2/2
✓ Branch 0 taken 169 times.
✓ Branch 1 taken 263 times.
432 if (!enumItem->hasValue) {
528
3/4
✓ Branch 2 taken 314 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 145 times.
✓ Branch 6 taken 169 times.
314 while (std::ranges::find(values, nextValue) != values.end())
529 145 nextValue++;
530 169 enumItem->itemValue = nextValue;
531
1/2
✓ Branch 1 taken 169 times.
✗ Branch 2 not taken.
169 values.push_back(nextValue);
532 }
533 }
534
535 // Change to root scope
536 38 currentScope = rootScope;
537
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 assert(currentScope->type == ScopeType::GLOBAL);
538
539
1/2
✓ Branch 1 taken 38 times.
✗ Branch 2 not taken.
76 return nullptr;
540 38 }
541
542 722 std::any TypeChecker::visitGenericTypeDefPrepare(GenericTypeDefNode *node) {
543 // Retrieve type conditions
544 722 QualTypeList typeConditions;
545
1/2
✓ Branch 2 taken 722 times.
✗ Branch 3 not taken.
722 typeConditions.reserve(node->typeAltsLst->dataTypes.size());
546
2/2
✓ Branch 5 taken 1398 times.
✓ Branch 6 taken 722 times.
2120 for (const auto &typeAlt : node->typeAltsLst->dataTypes) {
547
2/4
✓ Branch 1 taken 1398 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1398 times.
✗ Branch 5 not taken.
1398 auto typeCondition = std::any_cast<QualType>(visit(typeAlt));
548
2/6
✓ Branch 1 taken 1398 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1398 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1398 HANDLE_UNRESOLVED_TYPE_PTR(typeCondition)
549
3/4
✓ Branch 1 taken 1398 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1139 times.
✓ Branch 4 taken 259 times.
1398 if (!typeCondition.is(TY_DYN))
550
1/2
✓ Branch 1 taken 1139 times.
✗ Branch 2 not taken.
1139 typeConditions.push_back(typeCondition);
551 }
552
553 // Add generic type to the scope
554
2/4
✓ Branch 1 taken 722 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 722 times.
✗ Branch 5 not taken.
722 const GenericType genericType(node->typeName, typeConditions);
555
1/2
✓ Branch 1 taken 722 times.
✗ Branch 2 not taken.
722 rootScope->insertGenericType(node->typeName, genericType);
556
557 // Check if only one type condition is set
558
6/8
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 721 times.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 721 times.
722 if (typeConditions.size() == 1 && !typeConditions.front().is(TY_DYN))
559
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 sourceFile->compilerOutput.warnings.emplace_back(node->typeAltsLst->codeLoc, SINGLE_GENERIC_TYPE_CONDITION,
560 "Generic type is locked to one type");
561
562
1/2
✓ Branch 1 taken 722 times.
✗ Branch 2 not taken.
722 return nullptr;
563 722 }
564
565 30 std::any TypeChecker::visitAliasDefPrepare(AliasDefNode *node) {
566
2/4
✓ Branch 0 taken 30 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 assert(node->entry != nullptr && node->aliasedTypeContainerEntry != nullptr);
567
568 // Update type of alias entry
569
1/2
✓ Branch 2 taken 30 times.
✗ Branch 3 not taken.
30 const Type *type = TypeRegistry::getOrInsert(TY_ALIAS, node->aliasName, node->typeId, {}, {});
570
2/4
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
30 node->entry->updateType(QualType(type, node->aliasSpecifiers), false);
571
572 // Update type of the aliased type container entry
573
2/4
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 30 times.
✗ Branch 5 not taken.
30 const auto aliasedType = std::any_cast<QualType>(visit(node->dataType));
574
4/6
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 29 times.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
30 HANDLE_UNRESOLVED_TYPE_PTR(aliasedType)
575
1/2
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
29 node->aliasedTypeContainerEntry->updateType(aliasedType, false);
576 29 node->aliasedTypeContainerEntry->used = true; // The container type is always used per default
577
578
1/2
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
29 return nullptr;
579 }
580
581 734 std::any TypeChecker::visitGlobalVarDefPrepare(GlobalVarDefNode *node) {
582 // Insert variable name to symbol table
583
2/4
✓ Branch 1 taken 734 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 734 times.
✗ Branch 5 not taken.
734 auto globalVarType = std::any_cast<QualType>(visit(node->dataType));
584
2/6
✓ Branch 1 taken 734 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 734 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
734 HANDLE_UNRESOLVED_TYPE_PTR(globalVarType)
585
586
2/2
✓ Branch 0 taken 732 times.
✓ Branch 1 taken 2 times.
734 if (node->constant) { // Variable is initialized here
587
2/4
✓ Branch 1 taken 732 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 732 times.
✗ Branch 5 not taken.
732 const QualType rhsType = std::any_cast<ExprResult>(visit(node->constant)).type;
588
2/6
✓ Branch 1 taken 732 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 732 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
732 HANDLE_UNRESOLVED_TYPE_PTR(rhsType)
589
3/4
✓ Branch 1 taken 732 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 731 times.
732 if (globalVarType.is(TY_DYN)) { // Perform type inference
590 1 globalVarType = rhsType;
591
3/4
✓ Branch 1 taken 731 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 729 times.
731 } else if (!globalVarType.matches(rhsType, false, true, true)) { // Check if types are matching
592
7/14
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 2 times.
✗ Branch 8 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
✓ Branch 16 taken 2 times.
✗ Branch 17 not taken.
✓ Branch 24 taken 2 times.
✗ Branch 25 not taken.
2 SOFT_ERROR_BOOL(node->constant, OPERATOR_WRONG_DATA_TYPE,
593 "Expected " + globalVarType.getName(false) + ", but got " + rhsType.getName(false))
594 }
595 }
596
597 // Check if the type is still missing
598
3/4
✓ Branch 1 taken 732 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 731 times.
732 if (globalVarType.is(TY_DYN))
599
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
3 SOFT_ERROR_BOOL(node->dataType, GLOBAL_OF_TYPE_DYN, "Global variables must have an explicit data type")
600
601 // Check if we would need to insert instructions in the global scope to initialize the variable
602
2/4
✓ Branch 1 taken 731 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 731 times.
731 if (!globalVarType.isPrimitive())
603 SOFT_ERROR_BOOL(node->dataType, GLOBAL_OF_INVALID_TYPE, "Spice does only support global variables of primitive type")
604
605 // Update type of global var entry
606
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 731 times.
731 assert(node->entry != nullptr);
607
1/2
✓ Branch 1 taken 731 times.
✗ Branch 2 not taken.
731 node->entry->updateType(globalVarType, false);
608
609 // Check if a value is attached
610
6/8
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 730 times.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 730 times.
731 if (!node->constant && globalVarType.isConst())
611
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
3 SOFT_ERROR_BOOL(node, GLOBAL_CONST_WITHOUT_VALUE, "You must specify a value for constant global variables")
612
613
1/2
✓ Branch 1 taken 730 times.
✗ Branch 2 not taken.
730 return nullptr;
614 }
615
616 701 std::any TypeChecker::visitExtDeclPrepare(ExtDeclNode *node) {
617 // Collect argument types
618 701 QualTypeList argTypes;
619 701 ParamList argList;
620
2/2
✓ Branch 0 taken 677 times.
✓ Branch 1 taken 24 times.
701 if (node->hasArgs) {
621
1/2
✓ Branch 2 taken 677 times.
✗ Branch 3 not taken.
677 argList.reserve(node->argTypeLst->dataTypes.size());
622
2/2
✓ Branch 5 taken 1330 times.
✓ Branch 6 taken 677 times.
2007 for (DataTypeNode *arg : node->argTypeLst->dataTypes) {
623 // Visit argument
624
2/4
✓ Branch 1 taken 1330 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1330 times.
✗ Branch 5 not taken.
1330 auto argType = std::any_cast<QualType>(visit(arg));
625
2/6
✓ Branch 1 taken 1330 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 1330 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
1330 HANDLE_UNRESOLVED_TYPE_PTR(argType)
626 // Check if the argument type is 'dyn'
627
3/4
✓ Branch 1 taken 1330 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1329 times.
1330 if (argType.is(TY_DYN)) {
628
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
1 softError(arg, UNEXPECTED_DYN_TYPE, "Dyn data type is not allowed as arg type for external functions");
629 1 continue;
630 }
631 // Save argument
632
1/2
✓ Branch 1 taken 1329 times.
✗ Branch 2 not taken.
1329 argTypes.push_back(argType);
633
1/2
✓ Branch 1 taken 1329 times.
✗ Branch 2 not taken.
1329 argList.push_back({argType, false});
634 }
635 }
636
637 // Retrieve return type
638
1/2
✓ Branch 1 taken 701 times.
✗ Branch 2 not taken.
701 QualType returnType(TY_DYN);
639 701 const bool isFunction = node->returnType;
640
2/2
✓ Branch 0 taken 453 times.
✓ Branch 1 taken 248 times.
701 if (isFunction) { // External function
641
2/4
✓ Branch 1 taken 453 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 453 times.
✗ Branch 5 not taken.
453 returnType = std::any_cast<QualType>(visit(node->returnType));
642
2/6
✓ Branch 1 taken 453 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 453 times.
✗ Branch 6 not taken.
✗ Branch 7 not taken.
453 HANDLE_UNRESOLVED_TYPE_PTR(returnType)
643 // Check if return type is dyn
644
3/4
✓ Branch 1 taken 453 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 452 times.
453 if (returnType.is(TY_DYN))
645
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
3 SOFT_ERROR_BOOL(node->returnType, UNEXPECTED_DYN_TYPE, "dyn is not allowed as return type for external functions")
646 }
647
648 // Add function to current scope
649
3/6
✓ Branch 2 taken 700 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 700 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 700 times.
✗ Branch 9 not taken.
700 const Function spiceFunc(node->extFunctionName, node->entry, QualType(TY_DYN), returnType, argList, {}, node);
650
1/2
✓ Branch 1 taken 700 times.
✗ Branch 2 not taken.
700 node->extFunction = FunctionManager::insert(currentScope, spiceFunc, &node->extFunctionManifestations);
651 700 node->extFunction->mangleFunctionName = false;
652 700 node->extFunction->alreadyTypeChecked = true;
653
654 // Check procedure attributes
655
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 699 times.
700 if (node->attrs) {
656 1 const AttrLstNode *attrLst = node->attrs->attrLst;
657
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✗ Branch 7 not taken.
✓ Branch 8 taken 1 times.
3 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLE))
658 node->extFunction->mangleFunctionName = value->boolValue;
659
3/6
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 1 times.
✗ Branch 8 not taken.
3 if (const CompileTimeValue *value = attrLst->getAttrValueByName(ATTR_CORE_COMPILER_MANGLED_NAME)) {
660
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 const std::string &stringValue = resourceManager.compileTimeStringValues.at(value->stringValueOffset);
661
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 node->extFunction->predefinedMangledName = stringValue;
662 }
663 }
664
665 // Prepare ext function type
666
2/2
✓ Branch 0 taken 452 times.
✓ Branch 1 taken 248 times.
700 const SuperType superType = isFunction ? TY_FUNCTION : TY_PROCEDURE;
667
2/4
✓ Branch 1 taken 700 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 700 times.
✗ Branch 5 not taken.
700 const QualType extFunctionType = QualType(superType).getWithFunctionParamAndReturnTypes(returnType, argTypes);
668
669 // Set type of external function
670
1/2
✓ Branch 1 taken 700 times.
✗ Branch 2 not taken.
700 node->entry->updateType(extFunctionType, false);
671
672 // Rename the original child scope to reflect the substantiated versions of the external function
673
3/6
✓ Branch 1 taken 700 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 700 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 700 times.
✗ Branch 8 not taken.
700 currentScope->renameChildScope(node->getScopeId(), spiceFunc.getSignature(false));
674
675
1/2
✓ Branch 1 taken 700 times.
✗ Branch 2 not taken.
700 return nullptr;
676 701 }
677
678 361 std::any TypeChecker::visitImportDefPrepare(ImportDefNode *node) {
679 // Set entry to import type
680
1/2
✓ Branch 1 taken 361 times.
✗ Branch 2 not taken.
361 const QualType importType(TY_IMPORT, node->importName);
681
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 361 times.
361 assert(node->entry != nullptr);
682
1/2
✓ Branch 1 taken 361 times.
✗ Branch 2 not taken.
361 node->entry->updateType(importType, false);
683
684
1/2
✓ Branch 1 taken 361 times.
✗ Branch 2 not taken.
361 return nullptr;
685 }
686
687 } // namespace spice::compiler
688