src/typechecker/TypeCheckerTopLevelDefinitionsCheck.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "TypeChecker.h" | ||
| 4 | |||
| 5 | #include <SourceFile.h> | ||
| 6 | #include <ast/ASTNodes.h> | ||
| 7 | #include <exception/SemanticError.h> | ||
| 8 | #include <model/Interface.h> | ||
| 9 | #include <model/Union.h> | ||
| 10 | #include <symboltablebuilder/Scope.h> | ||
| 11 | #include <symboltablebuilder/SymbolTableBuilder.h> | ||
| 12 | #include <typechecker/FunctionManager.h> | ||
| 13 | #include <typechecker/TypeMatcher.h> | ||
| 14 | |||
| 15 | namespace spice::compiler { | ||
| 16 | |||
| 17 | 1452 | std::any TypeChecker::visitMainFctDefCheck(MainFctDefNode *node) { | |
| 18 | // Skip if already type-checked | ||
| 19 |
2/2✓ Branch 2 → 3 taken 177 times.
✓ Branch 2 → 6 taken 1275 times.
|
1452 | if (typeCheckedMainFct) |
| 20 |
1/2✓ Branch 3 → 4 taken 177 times.
✗ Branch 3 → 13 not taken.
|
354 | return nullptr; |
| 21 | |||
| 22 | 1275 | node->resizeToNumberOfManifestations(1); | |
| 23 | |||
| 24 | // Change to function body scope | ||
| 25 | 1275 | currentScope = node->bodyScope; | |
| 26 | // Visit statements in new scope | ||
| 27 |
2/2✓ Branch 7 → 8 taken 1209 times.
✓ Branch 7 → 14 taken 66 times.
|
1275 | visit(node->body); |
| 28 | // Leave main function body scope | ||
| 29 | 1209 | currentScope = rootScope; | |
| 30 | |||
| 31 | // Set to type-checked | ||
| 32 | 1209 | typeCheckedMainFct = true; | |
| 33 |
1/2✓ Branch 9 → 10 taken 1209 times.
✗ Branch 9 → 15 not taken.
|
2418 | return nullptr; |
| 34 | } | ||
| 35 | |||
| 36 | 331525 | std::any TypeChecker::visitFctDefCheck(FctDefNode *node) { | |
| 37 | 331525 | const size_t manifestationCount = node->manifestations.size(); | |
| 38 | 331525 | node->resizeToNumberOfManifestations(manifestationCount); | |
| 39 | 331525 | manIdx = 0; // Reset the manifestation index | |
| 40 | |||
| 41 | // Get all manifestations for this function definition. Type-checking a body can add new manifestations of this function | ||
| 42 | // (e.g. a generic struct method that uses another substantiation of its own struct), which would invalidate iterators. | ||
| 43 | // Those are type-checked in the next run, so only visit the manifestations that exist up front, accessing them by index. | ||
| 44 |
2/2✓ Branch 69 → 5 taken 546875 times.
✓ Branch 69 → 70 taken 331521 times.
|
878396 | while (manIdx < manifestationCount) { |
| 45 | 546875 | Function *manifestation = node->manifestations.at(manIdx); | |
| 46 | // Skip non-substantiated or already checked functions | ||
| 47 |
6/6✓ Branch 7 → 8 taken 494520 times.
✓ Branch 7 → 9 taken 52355 times.
✓ Branch 8 → 9 taken 266078 times.
✓ Branch 8 → 10 taken 228442 times.
✓ Branch 11 → 12 taken 318433 times.
✓ Branch 11 → 13 taken 228442 times.
|
546875 | if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) { |
| 48 | 318433 | manIdx++; // Increase the manifestation index | |
| 49 | 318433 | continue; | |
| 50 | } | ||
| 51 | |||
| 52 | // Change scope to concrete struct specialization scope | ||
| 53 |
2/2✓ Branch 13 → 14 taken 177190 times.
✓ Branch 13 → 19 taken 51252 times.
|
228442 | if (node->isMethod) { |
| 54 |
2/4✓ Branch 14 → 15 taken 177190 times.
✗ Branch 14 → 76 not taken.
✓ Branch 15 → 16 taken 177190 times.
✗ Branch 15 → 76 not taken.
|
177190 | const std::string &scopeName = Struct::getScopeName(node->name->structName, manifestation->thisType.getTemplateTypes()); |
| 55 |
1/2✓ Branch 16 → 17 taken 177190 times.
✗ Branch 16 → 74 not taken.
|
177190 | changeToScope(scopeName, ScopeType::STRUCT); |
| 56 | 177190 | } | |
| 57 | |||
| 58 | // Change to function scope | ||
| 59 | 228442 | changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY); | |
| 60 | |||
| 61 | // Mount type mapping for this manifestation | ||
| 62 |
1/2✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 228442 times.
|
228442 | assert(typeMapping.empty()); |
| 63 | 228442 | typeMapping = manifestation->typeMapping; | |
| 64 | |||
| 65 | // Set return type to the result variable | ||
| 66 |
1/2✓ Branch 26 → 27 taken 228442 times.
✗ Branch 26 → 79 not taken.
|
685326 | SymbolTableEntry *resultVarEntry = currentScope->lookupStrict(RETURN_VARIABLE_NAME); |
| 67 |
1/2✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 228442 times.
|
228442 | assert(resultVarEntry != nullptr); |
| 68 | 228442 | resultVarEntry->updateType(manifestation->returnType, false); | |
| 69 | 228442 | resultVarEntry->used = true; | |
| 70 | |||
| 71 | // Visit parameters | ||
| 72 | // This happens once in the type checker prepare stage. This second time is only required if we have a generic function | ||
| 73 |
2/2✓ Branch 35 → 36 taken 132456 times.
✓ Branch 35 → 62 taken 95986 times.
|
228442 | if (node->hasParams) { |
| 74 |
1/2✓ Branch 36 → 37 taken 132456 times.
✗ Branch 36 → 83 not taken.
|
132456 | visit(node->paramLst); |
| 75 | // Annotate function/procedure-typed params with lambda-capture info from the resolved manifestation type. | ||
| 76 | // This must happen in the TypeChecker so that the IRGenerator can treat SymbolTableEntry as immutable. | ||
| 77 |
2/2✓ Branch 61 → 39 taken 187493 times.
✓ Branch 61 → 62 taken 132456 times.
|
319949 | for (size_t i = 0; i < manifestation->paramList.size(); i++) { |
| 78 |
1/2✓ Branch 39 → 40 taken 187493 times.
✗ Branch 39 → 89 not taken.
|
187493 | const DeclStmtNode *param = node->paramLst->params.at(i); |
| 79 |
2/4✓ Branch 40 → 41 taken 187493 times.
✗ Branch 40 → 86 not taken.
✓ Branch 41 → 42 taken 187493 times.
✗ Branch 41 → 84 not taken.
|
187493 | const QualType paramType = manifestation->getParamTypes().at(i); |
| 80 |
8/10✓ Branch 43 → 44 taken 187493 times.
✗ Branch 43 → 87 not taken.
✓ Branch 44 → 45 taken 152 times.
✓ Branch 44 → 48 taken 187341 times.
✓ Branch 45 → 46 taken 152 times.
✗ Branch 45 → 87 not taken.
✓ Branch 46 → 47 taken 122 times.
✓ Branch 46 → 48 taken 30 times.
✓ Branch 49 → 50 taken 122 times.
✓ Branch 49 → 59 taken 187371 times.
|
187493 | if (paramType.isOneOf({TY_FUNCTION, TY_PROCEDURE}) && paramType.hasLambdaCaptures()) { |
| 81 |
1/2✓ Branch 50 → 51 taken 122 times.
✗ Branch 50 → 89 not taken.
|
122 | SymbolTableEntry *paramSymbol = currentScope->lookupStrict(param->varName); |
| 82 |
1/2✗ Branch 53 → 54 not taken.
✓ Branch 53 → 55 taken 122 times.
|
122 | assert(paramSymbol != nullptr); |
| 83 |
3/6✓ Branch 55 → 56 taken 122 times.
✗ Branch 55 → 88 not taken.
✓ Branch 56 → 57 taken 122 times.
✗ Branch 56 → 88 not taken.
✓ Branch 57 → 58 taken 122 times.
✗ Branch 57 → 88 not taken.
|
122 | paramSymbol->updateType(paramSymbol->getQualType().getWithLambdaCaptures(), true); |
| 84 | } | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | // Visit statements in new scope | ||
| 89 |
2/2✓ Branch 62 → 63 taken 228438 times.
✓ Branch 62 → 90 taken 4 times.
|
228442 | visit(node->body); |
| 90 | |||
| 91 | // Clear type mapping | ||
| 92 | 228438 | typeMapping.clear(); | |
| 93 | |||
| 94 | // Change to root scope | ||
| 95 | 228438 | currentScope = rootScope; | |
| 96 |
1/2✗ Branch 65 → 66 not taken.
✓ Branch 65 → 67 taken 228438 times.
|
228438 | assert(currentScope->type == ScopeType::GLOBAL); |
| 97 | |||
| 98 | // Do not type-check this manifestation again | ||
| 99 | 228438 | manifestation->alreadyTypeChecked = true; | |
| 100 | |||
| 101 | 228438 | manIdx++; // Increase the manifestation index | |
| 102 | } | ||
| 103 | 331521 | manIdx = 0; // Reset the manifestation index | |
| 104 | |||
| 105 |
1/2✓ Branch 70 → 71 taken 331521 times.
✗ Branch 70 → 91 not taken.
|
663042 | return nullptr; |
| 106 | } | ||
| 107 | |||
| 108 | 142944 | std::any TypeChecker::visitProcDefCheck(ProcDefNode *node) { | |
| 109 | 142944 | const size_t manifestationCount = node->manifestations.size(); | |
| 110 | 142944 | node->resizeToNumberOfManifestations(manifestationCount); | |
| 111 | 142944 | manIdx = 0; // Reset the manifestation index | |
| 112 | |||
| 113 | // Get all manifestations for this procedure definition. Type-checking a body can add new manifestations of this procedure | ||
| 114 | // (e.g. a generic struct method that uses another substantiation of its own struct), which would invalidate iterators. | ||
| 115 | // Those are type-checked in the next run, so only visit the manifestations that exist up front, accessing them by index. | ||
| 116 |
2/2✓ Branch 62 → 5 taken 359170 times.
✓ Branch 62 → 63 taken 142944 times.
|
502114 | while (manIdx < manifestationCount) { |
| 117 | 359170 | Function *manifestation = node->manifestations.at(manIdx); | |
| 118 | // Skip non-substantiated or already checked procedures | ||
| 119 |
6/6✓ Branch 7 → 8 taken 309108 times.
✓ Branch 7 → 9 taken 50062 times.
✓ Branch 8 → 9 taken 167317 times.
✓ Branch 8 → 10 taken 141791 times.
✓ Branch 11 → 12 taken 217379 times.
✓ Branch 11 → 13 taken 141791 times.
|
359170 | if (!manifestation->isFullySubstantiated() || manifestation->alreadyTypeChecked) { |
| 120 | 217379 | manIdx++; // Increase the manifestation index | |
| 121 | 217379 | continue; | |
| 122 | } | ||
| 123 | |||
| 124 | // Change scope to concrete struct specialization scope | ||
| 125 |
2/2✓ Branch 13 → 14 taken 117054 times.
✓ Branch 13 → 19 taken 24737 times.
|
141791 | if (node->isMethod) { |
| 126 |
2/4✓ Branch 14 → 15 taken 117054 times.
✗ Branch 14 → 69 not taken.
✓ Branch 15 → 16 taken 117054 times.
✗ Branch 15 → 69 not taken.
|
117054 | const std::string &scopeName = Struct::getScopeName(node->name->structName, manifestation->thisType.getTemplateTypes()); |
| 127 |
1/2✓ Branch 16 → 17 taken 117054 times.
✗ Branch 16 → 67 not taken.
|
117054 | changeToScope(scopeName, ScopeType::STRUCT); |
| 128 | 117054 | } | |
| 129 | |||
| 130 | // Change to procedure scope | ||
| 131 | 141791 | changeToScope(manifestation->bodyScope, ScopeType::FUNC_PROC_BODY); | |
| 132 | |||
| 133 | // Mount type mapping for this manifestation | ||
| 134 |
1/2✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 141791 times.
|
141791 | assert(typeMapping.empty()); |
| 135 | 141791 | typeMapping = manifestation->typeMapping; | |
| 136 | |||
| 137 | // Visit parameters | ||
| 138 | // This happens once in the type checker prepare stage. This second time is only required if we have a generic procedure | ||
| 139 |
2/2✓ Branch 24 → 25 taken 106628 times.
✓ Branch 24 → 51 taken 35163 times.
|
141791 | if (node->hasParams) { |
| 140 |
1/2✓ Branch 25 → 26 taken 106628 times.
✗ Branch 25 → 70 not taken.
|
106628 | visit(node->paramLst); |
| 141 | // Annotate function/procedure-typed params with lambda-capture info from the resolved manifestation type. | ||
| 142 | // This must happen in the TypeChecker so that the IRGenerator can treat SymbolTableEntry as immutable. | ||
| 143 |
2/2✓ Branch 50 → 28 taken 145433 times.
✓ Branch 50 → 51 taken 106628 times.
|
252061 | for (size_t i = 0; i < manifestation->paramList.size(); i++) { |
| 144 |
1/2✓ Branch 28 → 29 taken 145433 times.
✗ Branch 28 → 76 not taken.
|
145433 | const DeclStmtNode *param = node->paramLst->params.at(i); |
| 145 |
2/4✓ Branch 29 → 30 taken 145433 times.
✗ Branch 29 → 73 not taken.
✓ Branch 30 → 31 taken 145433 times.
✗ Branch 30 → 71 not taken.
|
145433 | const QualType paramType = manifestation->getParamTypes().at(i); |
| 146 |
8/10✓ Branch 32 → 33 taken 145433 times.
✗ Branch 32 → 74 not taken.
✓ Branch 33 → 34 taken 662 times.
✓ Branch 33 → 37 taken 144771 times.
✓ Branch 34 → 35 taken 662 times.
✗ Branch 34 → 74 not taken.
✓ Branch 35 → 36 taken 386 times.
✓ Branch 35 → 37 taken 276 times.
✓ Branch 38 → 39 taken 386 times.
✓ Branch 38 → 48 taken 145047 times.
|
145433 | if (paramType.isOneOf({TY_FUNCTION, TY_PROCEDURE}) && paramType.hasLambdaCaptures()) { |
| 147 |
1/2✓ Branch 39 → 40 taken 386 times.
✗ Branch 39 → 76 not taken.
|
386 | SymbolTableEntry *paramSymbol = currentScope->lookupStrict(param->varName); |
| 148 |
1/2✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 386 times.
|
386 | assert(paramSymbol != nullptr); |
| 149 |
3/6✓ Branch 44 → 45 taken 386 times.
✗ Branch 44 → 75 not taken.
✓ Branch 45 → 46 taken 386 times.
✗ Branch 45 → 75 not taken.
✓ Branch 46 → 47 taken 386 times.
✗ Branch 46 → 75 not taken.
|
386 | paramSymbol->updateType(paramSymbol->getQualType().getWithLambdaCaptures(), true); |
| 150 | } | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | // Prepare generation of special ctor preamble to store VTable, default field values, etc. if required | ||
| 155 |
2/2✓ Branch 51 → 52 taken 52510 times.
✓ Branch 51 → 53 taken 89281 times.
|
141791 | if (node->isCtor) |
| 156 | 52510 | createCtorBodyPreamble(manifestation->bodyScope); | |
| 157 | |||
| 158 | // Visit statements in new scope | ||
| 159 |
1/2✓ Branch 53 → 54 taken 141791 times.
✗ Branch 53 → 77 not taken.
|
141791 | visit(node->body); |
| 160 | |||
| 161 | // Clear type mapping | ||
| 162 | 141791 | typeMapping.clear(); | |
| 163 | |||
| 164 | // Change to root scope | ||
| 165 | 141791 | currentScope = rootScope; | |
| 166 |
2/4✓ Branch 56 → 57 taken 141791 times.
✗ Branch 56 → 59 not taken.
✓ Branch 57 → 58 taken 141791 times.
✗ Branch 57 → 59 not taken.
|
141791 | assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL); |
| 167 | |||
| 168 | // Do not type-check this manifestation again | ||
| 169 | 141791 | manifestation->alreadyTypeChecked = true; | |
| 170 | |||
| 171 | 141791 | manIdx++; // Increase the manifestation index | |
| 172 | } | ||
| 173 | 142944 | manIdx = 0; // Reset the manifestation index | |
| 174 | |||
| 175 |
1/2✓ Branch 63 → 64 taken 142944 times.
✗ Branch 63 → 78 not taken.
|
285888 | return nullptr; |
| 176 | } | ||
| 177 | |||
| 178 | 37692 | std::any TypeChecker::visitStructDefCheck(StructDefNode *node) { | |
| 179 | 37692 | node->resizeToNumberOfManifestations(node->structManifestations.size()); | |
| 180 | 37692 | manIdx = 0; // Reset the manifestation index | |
| 181 | |||
| 182 | // Get all manifestations for this procedure definition | ||
| 183 |
2/2✓ Branch 215 → 6 taken 99441 times.
✓ Branch 215 → 216 taken 37692 times.
|
174825 | for (Struct *manifestation : node->structManifestations) { |
| 184 | // Skip non-substantiated or already checked procedures | ||
| 185 |
3/4✓ Branch 8 → 9 taken 99441 times.
✗ Branch 8 → 299 not taken.
✓ Branch 9 → 10 taken 8714 times.
✓ Branch 9 → 11 taken 90727 times.
|
99441 | if (!manifestation->isFullySubstantiated()) { |
| 186 | 8714 | manIdx++; // Increase the manifestation index | |
| 187 | 8714 | continue; | |
| 188 | } | ||
| 189 | |||
| 190 | // Fallback for manifestations that could not be decided on when they were created, because a by-value struct field | ||
| 191 | // was not manifested yet at that point (circular import). By now the whole import graph is prepared, so the | ||
| 192 | // decision can be taken - late, but still before the struct's own default member bodies are prepared below. | ||
| 193 |
1/2✓ Branch 11 → 12 taken 90727 times.
✗ Branch 11 → 299 not taken.
|
90727 | createImplicitDefaultMembers(*manifestation, node, /*withMoveCtor=*/false); |
| 194 | |||
| 195 | // Change to struct scope | ||
| 196 |
1/2✓ Branch 12 → 13 taken 90727 times.
✗ Branch 12 → 299 not taken.
|
90727 | changeToScope(manifestation->scope, ScopeType::STRUCT); |
| 197 | |||
| 198 | // Mount type mapping for this manifestation, so that the body preamble helpers below can substantiate | ||
| 199 | // generic field types (e.g. `heap T*` on `Vector<T>`). Without this, an auto-generated body preamble that | ||
| 200 | // touches a still-generic field would assert in TypeMatcher::substantiateTypeWithTypeMapping. | ||
| 201 |
1/2✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 90727 times.
|
90727 | assert(typeMapping.empty()); |
| 202 |
1/2✓ Branch 16 → 17 taken 90727 times.
✗ Branch 16 → 299 not taken.
|
90727 | typeMapping = manifestation->typeMapping; |
| 203 | |||
| 204 | // Re-visit all default values. This is required, since the type of the default value might vary for different manifestations | ||
| 205 |
2/2✓ Branch 40 → 19 taken 289384 times.
✓ Branch 40 → 41 taken 90727 times.
|
470838 | for (const FieldNode *field : node->fields) { |
| 206 |
2/2✓ Branch 21 → 22 taken 123419 times.
✓ Branch 21 → 31 taken 165965 times.
|
289384 | if (field->defaultValue != nullptr) { |
| 207 |
1/2✓ Branch 22 → 23 taken 123419 times.
✗ Branch 22 → 220 not taken.
|
123419 | visit(field->defaultValue); |
| 208 |
1/2✓ Branch 24 → 25 taken 123419 times.
✗ Branch 24 → 222 not taken.
|
123419 | SymbolTableEntry *fieldEntry = manifestation->scope->lookupStrict(field->fieldName); |
| 209 |
1/2✗ Branch 27 → 28 not taken.
✓ Branch 27 → 29 taken 123419 times.
|
123419 | assert(fieldEntry != nullptr); |
| 210 |
1/2✓ Branch 29 → 30 taken 123419 times.
✗ Branch 29 → 221 not taken.
|
123419 | fieldEntry->updateState(INITIALIZED, field); |
| 211 | } | ||
| 212 | } | ||
| 213 | |||
| 214 | // Build struct type | ||
| 215 |
1/2✓ Branch 41 → 42 taken 90727 times.
✗ Branch 41 → 299 not taken.
|
90727 | const QualType structType = manifestation->entry->getQualType(); |
| 216 | |||
| 217 | // Check if the struct implements all methods of all attached interfaces | ||
| 218 | 90727 | size_t vtableIndex = 0; | |
| 219 |
2/2✓ Branch 146 → 44 taken 48432 times.
✓ Branch 146 → 147 taken 90727 times.
|
229886 | for (const QualType &interfaceType : manifestation->interfaceTypes) { |
| 220 |
1/2✓ Branch 46 → 47 taken 48432 times.
✗ Branch 46 → 266 not taken.
|
48432 | const Interface *interface = interfaceType.getInterface(node); |
| 221 |
1/2✗ Branch 47 → 48 not taken.
✓ Branch 47 → 49 taken 48432 times.
|
48432 | assert(interface != nullptr); |
| 222 | |||
| 223 | // Check for all methods, that it is implemented by the struct | ||
| 224 |
2/2✓ Branch 136 → 51 taken 168914 times.
✓ Branch 136 → 137 taken 48432 times.
|
265778 | for (const Function *expMethod : interface->methods) { |
| 225 |
1/2✓ Branch 53 → 54 taken 168914 times.
✗ Branch 53 → 264 not taken.
|
168914 | const std::string methodName = expMethod->name; |
| 226 |
1/2✓ Branch 54 → 55 taken 168914 times.
✗ Branch 54 → 262 not taken.
|
168914 | QualTypeList params = expMethod->getParamTypes(); |
| 227 | 168914 | QualType returnType = expMethod->returnType; | |
| 228 | |||
| 229 | // Substantiate param and return types | ||
| 230 |
1/2✓ Branch 55 → 56 taken 168914 times.
✗ Branch 55 → 260 not taken.
|
168914 | TypeMatcher::substantiateTypesWithTypeMapping(params, interface->typeMapping, node); |
| 231 |
3/4✓ Branch 56 → 57 taken 168914 times.
✗ Branch 56 → 260 not taken.
✓ Branch 57 → 58 taken 52470 times.
✓ Branch 57 → 59 taken 116444 times.
|
168914 | if (returnType.hasAnyGenericParts()) |
| 232 |
1/2✓ Branch 58 → 59 taken 52470 times.
✗ Branch 58 → 260 not taken.
|
52470 | TypeMatcher::substantiateTypeWithTypeMapping(returnType, interface->typeMapping, node); |
| 233 | |||
| 234 | // Build args list | ||
| 235 | 168914 | ArgList args; | |
| 236 |
1/2✓ Branch 60 → 61 taken 168914 times.
✗ Branch 60 → 258 not taken.
|
168914 | args.reserve(params.size()); |
| 237 |
2/2✓ Branch 75 → 63 taken 55860 times.
✓ Branch 75 → 76 taken 168914 times.
|
393688 | for (const QualType ¶m : params) |
| 238 |
1/2✓ Branch 65 → 66 taken 55860 times.
✗ Branch 65 → 223 not taken.
|
55860 | args.emplace_back(param, nullptr); |
| 239 | |||
| 240 | // Search for method that has the required signature | ||
| 241 |
1/2✓ Branch 77 → 78 taken 168914 times.
✗ Branch 77 → 225 not taken.
|
168914 | Function *spiceFunction = FunctionManager::match(currentScope, methodName, structType, args, {}, true, node); |
| 242 |
2/2✓ Branch 79 → 80 taken 8 times.
✓ Branch 79 → 92 taken 168906 times.
|
168914 | if (spiceFunction == nullptr) { |
| 243 |
1/2✓ Branch 85 → 86 taken 8 times.
✗ Branch 85 → 228 not taken.
|
8 | softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED, |
| 244 |
5/10✓ Branch 80 → 81 taken 8 times.
✗ Branch 80 → 240 not taken.
✓ Branch 81 → 82 taken 8 times.
✗ Branch 81 → 236 not taken.
✓ Branch 82 → 83 taken 8 times.
✗ Branch 82 → 234 not taken.
✓ Branch 83 → 84 taken 8 times.
✗ Branch 83 → 232 not taken.
✓ Branch 84 → 85 taken 8 times.
✗ Branch 84 → 230 not taken.
|
16 | "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() + "'."); |
| 245 | 8 | continue; | |
| 246 | } | ||
| 247 | |||
| 248 | // Check return type | ||
| 249 |
5/6✓ Branch 92 → 93 taken 168906 times.
✗ Branch 92 → 258 not taken.
✓ Branch 93 → 94 taken 17374 times.
✓ Branch 93 → 97 taken 151532 times.
✓ Branch 98 → 99 taken 4 times.
✓ Branch 98 → 111 taken 168902 times.
|
186280 | if (spiceFunction->returnType != returnType && |
| 250 |
3/4✓ Branch 94 → 95 taken 17374 times.
✗ Branch 94 → 258 not taken.
✓ Branch 95 → 96 taken 4 times.
✓ Branch 95 → 97 taken 17370 times.
|
17374 | !returnType.matchesInterfaceImplementedByStruct(spiceFunction->returnType)) { |
| 251 |
1/2✓ Branch 104 → 105 taken 4 times.
✗ Branch 104 → 243 not taken.
|
4 | softError(node, INTERFACE_METHOD_NOT_IMPLEMENTED, |
| 252 |
4/8✓ Branch 99 → 100 taken 4 times.
✗ Branch 99 → 255 not taken.
✓ Branch 100 → 101 taken 4 times.
✗ Branch 100 → 251 not taken.
✓ Branch 101 → 102 taken 4 times.
✗ Branch 101 → 249 not taken.
✓ Branch 102 → 103 taken 4 times.
✗ Branch 102 → 247 not taken.
|
8 | "The struct '" + node->structName + "' does not implement method '" + expMethod->getSignature() + |
| 253 |
1/2✓ Branch 103 → 104 taken 4 times.
✗ Branch 103 → 245 not taken.
|
4 | "'. The return type does not match."); |
| 254 | 4 | continue; | |
| 255 | } | ||
| 256 | // Set to virtual, since it overrides the interface method | ||
| 257 | 168902 | spiceFunction->isVirtual = true; | |
| 258 | 168902 | spiceFunction->vtableIndex = vtableIndex++; | |
| 259 |
6/6✓ Branch 113 → 114 taken 168902 times.
✓ Branch 113 → 115 taken 12 times.
✓ Branch 118 → 119 taken 168902 times.
✓ Branch 118 → 120 taken 12 times.
✓ Branch 123 → 124 taken 168902 times.
✓ Branch 123 → 126 taken 12 times.
|
506742 | } |
| 260 | } | ||
| 261 | |||
| 262 | // Check default ctor body if required | ||
| 263 |
2/4✓ Branch 150 → 151 taken 90727 times.
✗ Branch 150 → 269 not taken.
✓ Branch 151 → 152 taken 90727 times.
✗ Branch 151 → 267 not taken.
|
272181 | const Function *ctorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, {}, true); |
| 264 |
4/4✓ Branch 155 → 156 taken 28147 times.
✓ Branch 155 → 161 taken 62580 times.
✓ Branch 156 → 157 taken 5246 times.
✓ Branch 156 → 161 taken 22901 times.
|
90727 | if (ctorFunc != nullptr && ctorFunc->implicitDefault) { |
| 265 |
1/2✓ Branch 157 → 158 taken 5246 times.
✗ Branch 157 → 299 not taken.
|
5246 | createCtorBodyPreamble(ctorFunc->bodyScope); |
| 266 |
2/4✓ Branch 158 → 159 taken 5246 times.
✗ Branch 158 → 299 not taken.
✗ Branch 159 → 160 not taken.
✓ Branch 159 → 161 taken 5246 times.
|
5246 | assert(manifestation->areAllFieldsInitialized() == nullptr); |
| 267 | } | ||
| 268 | |||
| 269 | // Check default copy ctor body if required | ||
| 270 |
2/4✓ Branch 161 → 162 taken 90727 times.
✗ Branch 161 → 280 not taken.
✓ Branch 165 → 166 taken 90727 times.
✗ Branch 165 → 276 not taken.
|
272181 | const ArgList args = {{structType.toConstRef(node), false /* always non-temporary */}}; |
| 271 |
2/4✓ Branch 169 → 170 taken 90727 times.
✗ Branch 169 → 284 not taken.
✓ Branch 170 → 171 taken 90727 times.
✗ Branch 170 → 282 not taken.
|
90727 | const Function *copyCtorFunc = FunctionManager::lookup(currentScope, CTOR_FUNCTION_NAME, structType, args, true); |
| 272 |
4/4✓ Branch 173 → 174 taken 44441 times.
✓ Branch 173 → 179 taken 46286 times.
✓ Branch 174 → 175 taken 30585 times.
✓ Branch 174 → 179 taken 13856 times.
|
90727 | if (copyCtorFunc != nullptr && copyCtorFunc->implicitDefault) { |
| 273 |
1/2✓ Branch 175 → 176 taken 30585 times.
✗ Branch 175 → 297 not taken.
|
30585 | createCopyCtorBodyPreamble(copyCtorFunc->bodyScope); |
| 274 |
2/4✓ Branch 176 → 177 taken 30585 times.
✗ Branch 176 → 297 not taken.
✗ Branch 177 → 178 not taken.
✓ Branch 177 → 179 taken 30585 times.
|
30585 | assert(manifestation->areAllFieldsInitialized() == nullptr); |
| 275 | } | ||
| 276 | |||
| 277 | // Check default move ctor body if required. findMoveCtor scans the manifestations directly to avoid the | ||
| 278 | // constify-based false-positive that FunctionManager::lookup with a non-const ref arg can produce. | ||
| 279 |
3/4✓ Branch 179 → 180 taken 90727 times.
✗ Branch 179 → 297 not taken.
✓ Branch 180 → 181 taken 2141 times.
✓ Branch 180 → 186 taken 88586 times.
|
90727 | if (const Function *moveCtorFunc = FunctionManager::findMoveCtor(currentScope); |
| 280 |
2/2✓ Branch 181 → 182 taken 142 times.
✓ Branch 181 → 186 taken 1999 times.
|
2141 | moveCtorFunc && moveCtorFunc->implicitDefault) { |
| 281 |
1/2✓ Branch 182 → 183 taken 142 times.
✗ Branch 182 → 297 not taken.
|
142 | createMoveCtorBodyPreamble(moveCtorFunc->bodyScope); |
| 282 |
2/4✓ Branch 183 → 184 taken 142 times.
✗ Branch 183 → 297 not taken.
✗ Branch 184 → 185 not taken.
✓ Branch 184 → 186 taken 142 times.
|
142 | assert(manifestation->areAllFieldsInitialized() == nullptr); |
| 283 | } | ||
| 284 | |||
| 285 | // Check default dtor body if required | ||
| 286 |
2/4✓ Branch 189 → 190 taken 90727 times.
✗ Branch 189 → 290 not taken.
✓ Branch 190 → 191 taken 90727 times.
✗ Branch 190 → 288 not taken.
|
272181 | const Function *dtorFunc = FunctionManager::lookup(currentScope, DTOR_FUNCTION_NAME, structType, {}, true); |
| 287 |
4/4✓ Branch 194 → 195 taken 49217 times.
✓ Branch 194 → 197 taken 41510 times.
✓ Branch 195 → 196 taken 31525 times.
✓ Branch 195 → 197 taken 17692 times.
|
90727 | if (dtorFunc != nullptr && dtorFunc->implicitDefault) |
| 288 |
1/2✓ Branch 196 → 197 taken 31525 times.
✗ Branch 196 → 297 not taken.
|
31525 | createDtorBodyPreamble(dtorFunc->bodyScope, node); |
| 289 | |||
| 290 | // Reset field symbols to declared state for the next manifestation | ||
| 291 |
1/2✓ Branch 197 → 198 taken 90727 times.
✗ Branch 197 → 297 not taken.
|
90727 | manifestation->resetFieldSymbolsToDeclared(node); |
| 292 | |||
| 293 | // Clear type mapping | ||
| 294 | 90727 | typeMapping.clear(); | |
| 295 | |||
| 296 | // Return to the root scope | ||
| 297 | 90727 | currentScope = rootScope; | |
| 298 |
2/4✓ Branch 199 → 200 taken 90727 times.
✗ Branch 199 → 202 not taken.
✓ Branch 200 → 201 taken 90727 times.
✗ Branch 200 → 202 not taken.
|
90727 | assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL); |
| 299 | |||
| 300 | 90727 | manIdx++; // Increase the manifestation index | |
| 301 | 90727 | } | |
| 302 | 37692 | manIdx = 0; // Reset the manifestation index | |
| 303 | |||
| 304 |
1/2✓ Branch 216 → 217 taken 37692 times.
✗ Branch 216 → 301 not taken.
|
75384 | return nullptr; |
| 305 | } | ||
| 306 | |||
| 307 | 30 | std::any TypeChecker::visitUnionDefCheck(UnionDefNode *node) { | |
| 308 | 30 | node->resizeToNumberOfManifestations(node->unionManifestations.size()); | |
| 309 | 30 | manIdx = 0; // Reset the manifestation index | |
| 310 | |||
| 311 | // Get all manifestations for this union definition | ||
| 312 |
2/2✓ Branch 55 → 6 taken 30 times.
✓ Branch 55 → 56 taken 30 times.
|
90 | for (Union *manifestation : node->unionManifestations) { |
| 313 | // Skip non-substantiated manifestations | ||
| 314 |
3/4✓ Branch 8 → 9 taken 30 times.
✗ Branch 8 → 63 not taken.
✓ Branch 9 → 10 taken 4 times.
✓ Branch 9 → 11 taken 26 times.
|
30 | if (!manifestation->isFullySubstantiated()) { |
| 315 | 4 | manIdx++; // Increase the manifestation index | |
| 316 | 4 | continue; | |
| 317 | } | ||
| 318 | |||
| 319 | // Change to union scope | ||
| 320 |
1/2✓ Branch 11 → 12 taken 26 times.
✗ Branch 11 → 63 not taken.
|
26 | changeToScope(manifestation->scope, ScopeType::UNION); |
| 321 | |||
| 322 | // Mount type mapping for this manifestation, so that the default value below can substantiate generic field types | ||
| 323 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 26 times.
|
26 | assert(typeMapping.empty()); |
| 324 |
1/2✓ Branch 15 → 16 taken 26 times.
✗ Branch 15 → 63 not taken.
|
26 | typeMapping = manifestation->typeMapping; |
| 325 | |||
| 326 | // Re-visit the default value (at most one field may have one). This is required, since the type of the default | ||
| 327 | // value might vary for different manifestations | ||
| 328 |
2/2✓ Branch 39 → 18 taken 52 times.
✓ Branch 39 → 40 taken 26 times.
|
104 | for (const FieldNode *field : node->fields) { |
| 329 |
2/2✓ Branch 20 → 21 taken 8 times.
✓ Branch 20 → 30 taken 44 times.
|
52 | if (field->defaultValue != nullptr) { |
| 330 |
1/2✓ Branch 21 → 22 taken 8 times.
✗ Branch 21 → 60 not taken.
|
8 | visit(field->defaultValue); |
| 331 |
1/2✓ Branch 23 → 24 taken 8 times.
✗ Branch 23 → 62 not taken.
|
8 | SymbolTableEntry *fieldEntry = manifestation->scope->lookupStrict(field->fieldName); |
| 332 |
1/2✗ Branch 26 → 27 not taken.
✓ Branch 26 → 28 taken 8 times.
|
8 | assert(fieldEntry != nullptr); |
| 333 |
1/2✓ Branch 28 → 29 taken 8 times.
✗ Branch 28 → 61 not taken.
|
8 | fieldEntry->updateState(INITIALIZED, field); |
| 334 | } | ||
| 335 | } | ||
| 336 | |||
| 337 | // Clear type mapping | ||
| 338 | 26 | typeMapping.clear(); | |
| 339 | |||
| 340 | // Return to the root scope | ||
| 341 | 26 | currentScope = rootScope; | |
| 342 |
2/4✓ Branch 41 → 42 taken 26 times.
✗ Branch 41 → 44 not taken.
✓ Branch 42 → 43 taken 26 times.
✗ Branch 42 → 44 not taken.
|
26 | assert(currentScope != nullptr && currentScope->type == ScopeType::GLOBAL); |
| 343 | |||
| 344 | 26 | manIdx++; // Increase the manifestation index | |
| 345 | } | ||
| 346 | 30 | manIdx = 0; // Reset the manifestation index | |
| 347 | |||
| 348 |
1/2✓ Branch 56 → 57 taken 30 times.
✗ Branch 56 → 64 not taken.
|
60 | return nullptr; |
| 349 | } | ||
| 350 | |||
| 351 | } // namespace spice::compiler | ||
| 352 |