| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "StructManager.h" | ||
| 4 | |||
| 5 | #include <ast/ASTNodes.h> | ||
| 6 | #include <exception/SemanticError.h> | ||
| 7 | #include <symboltablebuilder/Scope.h> | ||
| 8 | #include <typechecker/TypeMatcher.h> | ||
| 9 | #include <util/CodeLoc.h> | ||
| 10 | #include <util/CustomHashFunctions.h> | ||
| 11 | |||
| 12 | namespace spice::compiler { | ||
| 13 | |||
| 14 | // Static member initialization | ||
| 15 | std::unordered_map<uint64_t, Struct *> StructManager::lookupCache = {}; | ||
| 16 | size_t StructManager::lookupCacheHits = 0; | ||
| 17 | size_t StructManager::lookupCacheMisses = 0; | ||
| 18 | |||
| 19 | 717 | Struct *StructManager::insert(Scope *insertScope, Struct &spiceStruct, std::vector<Struct *> *nodeStructList) { | |
| 20 | // Open a new manifestation list. Which gets filled by the substantiated manifestations of the struct | ||
| 21 |
3/6✓ Branch 2 → 3 taken 717 times.
✗ Branch 2 → 22 not taken.
✓ Branch 3 → 4 taken 717 times.
✗ Branch 3 → 19 not taken.
✓ Branch 4 → 5 taken 717 times.
✗ Branch 4 → 17 not taken.
|
717 | const std::string structId = spiceStruct.name + ":" + spiceStruct.declNode->codeLoc.toPrettyLineAndColumn(); |
| 22 |
2/4✓ Branch 8 → 9 taken 717 times.
✗ Branch 8 → 25 not taken.
✓ Branch 9 → 10 taken 717 times.
✗ Branch 9 → 23 not taken.
|
717 | insertScope->structs.insert({structId, StructManifestationList()}); |
| 23 | |||
| 24 | // Save substantiation in declaration node | ||
| 25 |
1/2✓ Branch 12 → 13 taken 717 times.
✗ Branch 12 → 29 not taken.
|
717 | Struct *substantiation = insertSubstantiation(insertScope, spiceStruct, spiceStruct.declNode); |
| 26 |
1/2✓ Branch 13 → 14 taken 717 times.
✗ Branch 13 → 29 not taken.
|
717 | nodeStructList->push_back(substantiation); |
| 27 | |||
| 28 | 717 | return substantiation; | |
| 29 | 717 | } | |
| 30 | |||
| 31 | 1186 | Struct *StructManager::insertSubstantiation(Scope *insertScope, Struct &newManifestation, const ASTNode *declNode) { | |
| 32 |
1/2✓ Branch 2 → 3 taken 1186 times.
✗ Branch 2 → 42 not taken.
|
1186 | const std::string signature = newManifestation.getSignature(); |
| 33 | |||
| 34 | #ifndef NDEBUG | ||
| 35 | // Make sure that the manifestation does not exist already | ||
| 36 |
5/8✓ Branch 3 → 4 taken 1186 times.
✗ Branch 3 → 31 not taken.
✓ Branch 4 → 5 taken 1186 times.
✗ Branch 4 → 31 not taken.
✓ Branch 5 → 6 taken 1186 times.
✗ Branch 5 → 31 not taken.
✓ Branch 13 → 7 taken 1832 times.
✓ Branch 13 → 14 taken 1186 times.
|
3018 | for (const auto &val : insertScope->structs | std::views::values) |
| 37 |
2/4✓ Branch 8 → 9 taken 1832 times.
✗ Branch 8 → 31 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 1832 times.
|
1832 | assert(!val.contains(signature)); |
| 38 | #endif | ||
| 39 | |||
| 40 | // Retrieve the matching manifestation list of the scope | ||
| 41 |
3/6✓ Branch 14 → 15 taken 1186 times.
✗ Branch 14 → 37 not taken.
✓ Branch 15 → 16 taken 1186 times.
✗ Branch 15 → 34 not taken.
✓ Branch 16 → 17 taken 1186 times.
✗ Branch 16 → 32 not taken.
|
1186 | const std::string structId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn(); |
| 42 |
2/4✓ Branch 19 → 20 taken 1186 times.
✗ Branch 19 → 38 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 1186 times.
|
1186 | assert(insertScope->structs.contains(structId)); |
| 43 |
1/2✓ Branch 22 → 23 taken 1186 times.
✗ Branch 22 → 38 not taken.
|
1186 | StructManifestationList &manifestationList = insertScope->structs.at(structId); |
| 44 | |||
| 45 | // Add substantiated struct | ||
| 46 | 1186 | newManifestation.manifestationIndex = manifestationList.size(); | |
| 47 |
1/2✓ Branch 24 → 25 taken 1186 times.
✗ Branch 24 → 38 not taken.
|
1186 | manifestationList.emplace(signature, newManifestation); |
| 48 |
1/2✓ Branch 25 → 26 taken 1186 times.
✗ Branch 25 → 38 not taken.
|
2372 | return &manifestationList.at(signature); |
| 49 | 1186 | } | |
| 50 | |||
| 51 | /** | ||
| 52 | * Check if there is a struct in this scope, fulfilling all given requirements and if found, return it. | ||
| 53 | * If more than one struct matches the requirement, an error gets thrown | ||
| 54 | * | ||
| 55 | * @param matchScope Scope to match against | ||
| 56 | * @param qt Struct name requirement | ||
| 57 | * @param reqTemplateTypes Template types to substantiate generic types | ||
| 58 | * @param node Instantiation AST node for printing error messages | ||
| 59 | * @return Matched struct or nullptr | ||
| 60 | */ | ||
| 61 | 25720 | Struct *StructManager::match(Scope *matchScope, const std::string &qt, const QualTypeList &reqTemplateTypes, | |
| 62 | const ASTNode *node) { | ||
| 63 | // Do cache lookup | ||
| 64 | 25720 | const uint64_t cacheKey = getCacheKey(matchScope, qt, reqTemplateTypes); | |
| 65 |
3/4✓ Branch 3 → 4 taken 25720 times.
✗ Branch 3 → 213 not taken.
✓ Branch 4 → 5 taken 24391 times.
✓ Branch 4 → 7 taken 1329 times.
|
25720 | if (lookupCache.contains(cacheKey)) { |
| 66 | 24391 | lookupCacheHits++; | |
| 67 |
1/2✓ Branch 5 → 6 taken 24391 times.
✗ Branch 5 → 213 not taken.
|
24391 | return lookupCache.at(cacheKey); |
| 68 | } | ||
| 69 | 1329 | lookupCacheMisses++; | |
| 70 | |||
| 71 | // Copy the registry to prevent iterating over items, that are created within the loop | ||
| 72 |
1/2✓ Branch 7 → 8 taken 1329 times.
✗ Branch 7 → 213 not taken.
|
1329 | StructRegistry structRegistry = matchScope->structs; |
| 73 | // Loop over struct registry to find structs, that match the requirements of the instantiation | ||
| 74 | 1329 | std::vector<Struct *> matches; | |
| 75 |
2/2✓ Branch 145 → 10 taken 2110 times.
✓ Branch 145 → 146 taken 1329 times.
|
3439 | for (const auto &[structId, m] : structRegistry) { |
| 76 | // Copy the manifestation list to prevent iterating over items, that are created within the loop | ||
| 77 |
1/2✓ Branch 13 → 14 taken 2110 times.
✗ Branch 13 → 198 not taken.
|
2110 | const StructManifestationList manifestations = m; |
| 78 |
2/2✓ Branch 141 → 16 taken 2843 times.
✓ Branch 141 → 142 taken 924 times.
|
3767 | for (const auto &[mangledName, presetStruct] : manifestations) { |
| 79 | // Skip generic substantiations to prevent double matching of a struct | ||
| 80 |
3/4✓ Branch 19 → 20 taken 2843 times.
✗ Branch 19 → 194 not taken.
✓ Branch 20 → 21 taken 733 times.
✓ Branch 20 → 22 taken 2110 times.
|
2843 | if (presetStruct.isGenericSubstantiation()) |
| 81 | 1188 | continue; | |
| 82 | |||
| 83 | // Copy the struct to be able to substantiate types | ||
| 84 |
1/2✓ Branch 22 → 23 taken 2110 times.
✗ Branch 22 → 194 not taken.
|
2110 | Struct candidate = presetStruct; |
| 85 | |||
| 86 | // Check name requirement | ||
| 87 |
2/2✓ Branch 24 → 25 taken 833 times.
✓ Branch 24 → 26 taken 1277 times.
|
2110 | if (!matchName(candidate, qt)) |
| 88 | 833 | break; // Leave the whole manifestation list, because all manifestations in this list have the same name | |
| 89 | |||
| 90 | // Prepare mapping table from generic type name to concrete type | ||
| 91 | 1277 | TypeMapping &typeMapping = candidate.typeMapping; | |
| 92 | 1277 | typeMapping.clear(); | |
| 93 |
1/2✓ Branch 28 → 29 taken 1277 times.
✗ Branch 28 → 192 not taken.
|
1277 | typeMapping.reserve(candidate.templateTypes.size()); |
| 94 | |||
| 95 | // Check template types requirement | ||
| 96 |
3/4✓ Branch 29 → 30 taken 1277 times.
✗ Branch 29 → 192 not taken.
✓ Branch 30 → 31 taken 1 time.
✓ Branch 30 → 32 taken 1276 times.
|
1277 | if (!matchTemplateTypes(candidate, reqTemplateTypes, typeMapping, node)) |
| 97 | 1 | continue; // Leave this manifestation and continue with the next one | |
| 98 | |||
| 99 | // Map field types from generic to concrete | ||
| 100 |
1/2✓ Branch 32 → 33 taken 1276 times.
✗ Branch 32 → 192 not taken.
|
1276 | substantiateFieldTypes(candidate, typeMapping, node); |
| 101 | |||
| 102 | // We found a match! -> Set the actual candidate and its entry to used | ||
| 103 | 1276 | candidate.used = true; | |
| 104 | 1276 | candidate.entry->used = true; | |
| 105 | |||
| 106 | // Check if it needs to be substantiated | ||
| 107 |
2/2✓ Branch 34 → 35 taken 454 times.
✓ Branch 34 → 47 taken 822 times.
|
1276 | if (presetStruct.templateTypes.empty()) { |
| 108 |
5/10✓ Branch 35 → 36 taken 454 times.
✗ Branch 35 → 192 not taken.
✓ Branch 36 → 37 taken 454 times.
✗ Branch 36 → 41 not taken.
✓ Branch 37 → 38 taken 454 times.
✗ Branch 37 → 192 not taken.
✓ Branch 38 → 39 taken 454 times.
✗ Branch 38 → 192 not taken.
✓ Branch 39 → 40 taken 454 times.
✗ Branch 39 → 41 not taken.
|
454 | assert(matchScope->structs.contains(structId) && matchScope->structs.at(structId).contains(mangledName)); |
| 109 |
3/6✓ Branch 42 → 43 taken 454 times.
✗ Branch 42 → 167 not taken.
✓ Branch 43 → 44 taken 454 times.
✗ Branch 43 → 167 not taken.
✓ Branch 44 → 45 taken 454 times.
✗ Branch 44 → 167 not taken.
|
454 | matches.push_back(&matchScope->structs.at(structId).at(mangledName)); |
| 110 | 454 | matches.back()->used = true; | |
| 111 | 454 | continue; // Match was successful -> match the next struct | |
| 112 | } | ||
| 113 | |||
| 114 | // Check if we already have this manifestation and can simply re-use it | ||
| 115 |
4/6✓ Branch 47 → 48 taken 822 times.
✗ Branch 47 → 170 not taken.
✓ Branch 48 → 49 taken 822 times.
✗ Branch 48 → 168 not taken.
✓ Branch 50 → 51 taken 353 times.
✓ Branch 50 → 58 taken 469 times.
|
822 | if (manifestations.contains(candidate.getSignature())) { |
| 116 |
4/8✓ Branch 51 → 52 taken 353 times.
✗ Branch 51 → 174 not taken.
✓ Branch 52 → 53 taken 353 times.
✗ Branch 52 → 173 not taken.
✓ Branch 53 → 54 taken 353 times.
✗ Branch 53 → 171 not taken.
✓ Branch 54 → 55 taken 353 times.
✗ Branch 54 → 171 not taken.
|
353 | matches.push_back(&matchScope->structs.at(structId).at(candidate.getSignature())); |
| 117 | 353 | matches.back()->used = true; | |
| 118 | 353 | break; // Leave the whole manifestation list to not double-match the manifestation | |
| 119 | } | ||
| 120 | |||
| 121 | // Insert the substantiated version if required | ||
| 122 |
1/2✓ Branch 58 → 59 taken 469 times.
✗ Branch 58 → 192 not taken.
|
469 | Struct *substantiatedStruct = insertSubstantiation(matchScope, candidate, presetStruct.declNode); |
| 123 |
2/4✓ Branch 59 → 60 taken 469 times.
✗ Branch 59 → 192 not taken.
✓ Branch 60 → 61 taken 469 times.
✗ Branch 60 → 192 not taken.
|
469 | substantiatedStruct->genericPreset = &matchScope->structs.at(structId).at(mangledName); |
| 124 |
2/4✓ Branch 61 → 62 taken 469 times.
✗ Branch 61 → 192 not taken.
✓ Branch 62 → 63 taken 469 times.
✗ Branch 62 → 192 not taken.
|
469 | substantiatedStruct->declNode->getStructManifestations()->push_back(substantiatedStruct); |
| 125 | |||
| 126 | // Copy struct entry | ||
| 127 |
1/2✓ Branch 63 → 64 taken 469 times.
✗ Branch 63 → 192 not taken.
|
469 | const std::string newSignature = substantiatedStruct->getSignature(); |
| 128 |
1/2✓ Branch 64 → 65 taken 469 times.
✗ Branch 64 → 190 not taken.
|
469 | matchScope->lookupStrict(substantiatedStruct->name)->used = true; |
| 129 |
1/2✓ Branch 67 → 68 taken 469 times.
✗ Branch 67 → 190 not taken.
|
469 | substantiatedStruct->entry = matchScope->symbolTable.copySymbol(substantiatedStruct->name, newSignature); |
| 130 |
1/2✗ Branch 68 → 69 not taken.
✓ Branch 68 → 70 taken 469 times.
|
469 | assert(substantiatedStruct->entry != nullptr); |
| 131 | |||
| 132 | // Copy struct scope | ||
| 133 |
1/2✓ Branch 70 → 71 taken 469 times.
✗ Branch 70 → 190 not taken.
|
469 | const std::string &oldScopeName = presetStruct.getScopeName(); |
| 134 |
1/2✓ Branch 71 → 72 taken 469 times.
✗ Branch 71 → 188 not taken.
|
469 | const std::string &newScopeName = substantiatedStruct->getScopeName(); |
| 135 |
1/2✓ Branch 72 → 73 taken 469 times.
✗ Branch 72 → 186 not taken.
|
469 | substantiatedStruct->scope = matchScope->copyChildScope(oldScopeName, newScopeName); |
| 136 |
1/2✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 469 times.
|
469 | assert(substantiatedStruct->scope != nullptr); |
| 137 | 469 | substantiatedStruct->scope->isGenericScope = false; | |
| 138 | |||
| 139 | // Attach the template types to the new struct entry | ||
| 140 |
1/2✓ Branch 75 → 76 taken 469 times.
✗ Branch 75 → 178 not taken.
|
469 | QualType entryType = substantiatedStruct->entry->getQualType() |
| 141 |
2/4✓ Branch 76 → 77 taken 469 times.
✗ Branch 76 → 177 not taken.
✓ Branch 77 → 78 taken 469 times.
✗ Branch 77 → 175 not taken.
|
938 | .getWithTemplateTypes(substantiatedStruct->getTemplateTypes()) |
| 142 |
1/2✓ Branch 78 → 79 taken 469 times.
✗ Branch 78 → 175 not taken.
|
469 | .getWithBodyScope(substantiatedStruct->scope); |
| 143 |
1/2✓ Branch 80 → 81 taken 469 times.
✗ Branch 80 → 186 not taken.
|
469 | substantiatedStruct->entry->updateType(entryType, true); |
| 144 | |||
| 145 | // Replace symbol types of field entries with concrete types | ||
| 146 |
1/2✗ Branch 81 → 82 not taken.
✓ Branch 81 → 83 taken 469 times.
|
469 | assert(substantiatedStruct->scope != nullptr); |
| 147 | 469 | const size_t fieldCount = substantiatedStruct->fieldTypes.size(); | |
| 148 |
1/2✓ Branch 84 → 85 taken 469 times.
✗ Branch 84 → 186 not taken.
|
469 | const size_t explicitFieldsStartIdx = substantiatedStruct->scope->getFieldCount() - fieldCount; |
| 149 |
2/2✓ Branch 109 → 86 taken 1089 times.
✓ Branch 109 → 110 taken 469 times.
|
1558 | for (size_t i = 0; i < fieldCount; i++) { |
| 150 | // Replace field type with concrete template type | ||
| 151 |
1/2✗ Branch 86 → 87 not taken.
✓ Branch 86 → 88 taken 1089 times.
|
1089 | SymbolTableEntry *fieldEntry = substantiatedStruct->scope->lookupField(explicitFieldsStartIdx + i); |
| 152 |
3/6✓ Branch 91 → 92 taken 1089 times.
✗ Branch 91 → 95 not taken.
✓ Branch 92 → 93 taken 1089 times.
✗ Branch 92 → 181 not taken.
✓ Branch 93 → 94 taken 1089 times.
✗ Branch 93 → 95 not taken.
|
1089 | assert(fieldEntry != nullptr && fieldEntry->isField()); |
| 153 |
1/2✓ Branch 96 → 97 taken 1089 times.
✗ Branch 96 → 181 not taken.
|
1089 | QualType &fieldType = substantiatedStruct->fieldTypes.at(i); |
| 154 |
1/2✓ Branch 97 → 98 taken 1089 times.
✗ Branch 97 → 181 not taken.
|
1089 | QualType baseType = fieldType.getBase(); |
| 155 | |||
| 156 | // Set the body scope of fields that are of type <candidate-struct>* | ||
| 157 |
4/6✓ Branch 98 → 99 taken 1089 times.
✗ Branch 98 → 181 not taken.
✓ Branch 99 → 100 taken 1089 times.
✗ Branch 99 → 181 not taken.
✓ Branch 100 → 101 taken 33 times.
✓ Branch 100 → 104 taken 1056 times.
|
1089 | if (baseType.matches(substantiatedStruct->entry->getQualType(), false, true, true)) |
| 158 |
2/4✓ Branch 101 → 102 taken 33 times.
✗ Branch 101 → 179 not taken.
✓ Branch 102 → 103 taken 33 times.
✗ Branch 102 → 179 not taken.
|
33 | fieldType = fieldType.replaceBaseType(baseType.getWithBodyScope(substantiatedStruct->scope)); |
| 159 | |||
| 160 |
1/2✓ Branch 104 → 105 taken 1089 times.
✗ Branch 104 → 181 not taken.
|
1089 | fieldEntry->updateType(fieldType, /*overwriteExistingType=*/true); |
| 161 | |||
| 162 | // Instantiate structs | ||
| 163 |
3/4✓ Branch 105 → 106 taken 1089 times.
✗ Branch 105 → 181 not taken.
✓ Branch 106 → 107 taken 420 times.
✓ Branch 106 → 108 taken 669 times.
|
1089 | if (baseType.is(TY_STRUCT)) |
| 164 |
1/2✓ Branch 107 → 108 taken 420 times.
✗ Branch 107 → 181 not taken.
|
420 | (void)baseType.getStruct(node); |
| 165 | } | ||
| 166 | |||
| 167 | // Instantiate implemented interfaces if required | ||
| 168 |
2/2✓ Branch 127 → 112 taken 165 times.
✓ Branch 127 → 128 taken 469 times.
|
634 | for (QualType &interfaceType : substantiatedStruct->interfaceTypes) { |
| 169 | // Skip non-generic interfaces | ||
| 170 |
2/4✓ Branch 113 → 114 taken 165 times.
✗ Branch 113 → 184 not taken.
✗ Branch 114 → 115 not taken.
✓ Branch 114 → 116 taken 165 times.
|
165 | if (!interfaceType.hasAnyGenericParts()) |
| 171 | ✗ | continue; | |
| 172 | |||
| 173 | // Build template types | ||
| 174 |
2/4✓ Branch 116 → 117 taken 165 times.
✗ Branch 116 → 184 not taken.
✓ Branch 117 → 118 taken 165 times.
✗ Branch 117 → 184 not taken.
|
165 | QualTypeList templateTypes = interfaceType.getTemplateTypes(); |
| 175 |
1/2✓ Branch 118 → 119 taken 165 times.
✗ Branch 118 → 182 not taken.
|
165 | TypeMatcher::substantiateTypesWithTypeMapping(templateTypes, typeMapping, node); |
| 176 | |||
| 177 | // Instantiate interface | ||
| 178 |
1/2✓ Branch 119 → 120 taken 165 times.
✗ Branch 119 → 182 not taken.
|
165 | Interface *spiceInterface = interfaceType.getInterface(node, templateTypes); |
| 179 |
1/2✗ Branch 120 → 121 not taken.
✓ Branch 120 → 122 taken 165 times.
|
165 | assert(spiceInterface != nullptr); |
| 180 |
1/2✓ Branch 122 → 123 taken 165 times.
✗ Branch 122 → 182 not taken.
|
165 | interfaceType = spiceInterface->entry->getQualType(); |
| 181 | 165 | } | |
| 182 | |||
| 183 | // Add to matched structs | ||
| 184 |
1/2✓ Branch 128 → 129 taken 469 times.
✗ Branch 128 → 186 not taken.
|
469 | matches.push_back(substantiatedStruct); |
| 185 |
3/3✓ Branch 134 → 135 taken 469 times.
✓ Branch 134 → 137 taken 455 times.
✓ Branch 134 → 138 taken 1186 times.
|
2110 | } |
| 186 | 2110 | } | |
| 187 | |||
| 188 | // If no matches were found, return a nullptr | ||
| 189 |
2/2✓ Branch 147 → 148 taken 53 times.
✓ Branch 147 → 149 taken 1276 times.
|
1329 | if (matches.empty()) |
| 190 | 53 | return nullptr; | |
| 191 | |||
| 192 | // Check if more than one struct matches the requirements | ||
| 193 |
1/2✗ Branch 150 → 151 not taken.
✓ Branch 150 → 159 taken 1276 times.
|
1276 | if (matches.size() > 1) |
| 194 | ✗ | throw SemanticError(node, STRUCT_AMBIGUITY, "Multiple structs match the requested signature"); | |
| 195 | |||
| 196 | // Insert into cache | ||
| 197 |
1/2✓ Branch 160 → 161 taken 1276 times.
✗ Branch 160 → 209 not taken.
|
1276 | lookupCache[cacheKey] = matches.front(); |
| 198 | |||
| 199 | 1276 | return matches.front(); | |
| 200 | 1329 | } | |
| 201 | |||
| 202 | /** | ||
| 203 | * Checks if the matching candidate fulfills the name requirement | ||
| 204 | * | ||
| 205 | * @param candidate Matching candidate struct | ||
| 206 | * @param reqName Requested struct name | ||
| 207 | * @return Fulfilled or not | ||
| 208 | */ | ||
| 209 | 2110 | bool StructManager::matchName(const Struct &candidate, const std::string &reqName) { return candidate.name == reqName; } | |
| 210 | |||
| 211 | /** | ||
| 212 | * Checks if the matching candidate fulfills the template types requirement | ||
| 213 | * | ||
| 214 | * @param candidate Matching candidate struct | ||
| 215 | * @param reqTemplateTypes Requested struct template types | ||
| 216 | * @param typeMapping Generic type mapping | ||
| 217 | * @param node Instantiation AST node for printing error messages | ||
| 218 | * @return Fulfilled or not | ||
| 219 | */ | ||
| 220 | 1277 | bool StructManager::matchTemplateTypes(Struct &candidate, const QualTypeList &reqTemplateTypes, TypeMapping &typeMapping, | |
| 221 | const ASTNode *node) { | ||
| 222 | // Check if the number of types match | ||
| 223 | 1277 | const size_t typeCount = reqTemplateTypes.size(); | |
| 224 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 1276 times.
|
1277 | if (typeCount != candidate.templateTypes.size()) |
| 225 | 1 | return false; | |
| 226 | |||
| 227 | // Give the type matcher a way to retrieve instances of GenericType by their name | ||
| 228 | 3748 | TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) { | |
| 229 | 1196 | return getGenericTypeOfCandidateByName(candidate, genericTypeName); | |
| 230 | 1276 | }; | |
| 231 | |||
| 232 | // Loop over all template types | ||
| 233 |
2/2✓ Branch 17 → 8 taken 1196 times.
✓ Branch 17 → 18 taken 1276 times.
|
2472 | for (size_t i = 0; i < typeCount; i++) { |
| 234 |
1/2✓ Branch 8 → 9 taken 1196 times.
✗ Branch 8 → 22 not taken.
|
1196 | const QualType &reqType = reqTemplateTypes.at(i); |
| 235 |
1/2✓ Branch 9 → 10 taken 1196 times.
✗ Branch 9 → 22 not taken.
|
1196 | QualType &candidateType = candidate.templateTypes.at(i); |
| 236 | |||
| 237 | // Check if the requested template type matches the candidate template type. The type mapping may be extended | ||
| 238 |
2/4✓ Branch 10 → 11 taken 1196 times.
✗ Branch 10 → 22 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 1196 times.
|
1196 | if (!TypeMatcher::matchRequestedToCandidateType(candidateType, reqType, typeMapping, genericTypeResolver, false)) |
| 239 | ✗ | return false; | |
| 240 | |||
| 241 | // Substantiate the candidate param type, based on the type mapping | ||
| 242 |
2/4✓ Branch 13 → 14 taken 1196 times.
✗ Branch 13 → 22 not taken.
✓ Branch 14 → 15 taken 1196 times.
✗ Branch 14 → 16 not taken.
|
1196 | if (candidateType.hasAnyGenericParts()) |
| 243 |
1/2✓ Branch 15 → 16 taken 1196 times.
✗ Branch 15 → 22 not taken.
|
1196 | TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, node); |
| 244 | } | ||
| 245 | |||
| 246 | 1276 | return true; | |
| 247 | 1276 | } | |
| 248 | |||
| 249 | /** | ||
| 250 | * Come up with the concrete field types, by applying the type mapping onto the generic field types | ||
| 251 | * | ||
| 252 | * @param candidate Candidate struct | ||
| 253 | * @param typeMapping Generic type mapping | ||
| 254 | * @param node Instantiation AST node for printing error messages | ||
| 255 | */ | ||
| 256 | 1276 | void StructManager::substantiateFieldTypes(Struct &candidate, const TypeMapping &typeMapping, const ASTNode *node) { | |
| 257 | // Loop over all implicit fields and substantiate the generic ones | ||
| 258 | 1276 | const size_t fieldCount = candidate.scope->getFieldCount() - candidate.fieldTypes.size(); | |
| 259 |
2/2✓ Branch 16 → 5 taken 332 times.
✓ Branch 16 → 17 taken 1276 times.
|
1608 | for (size_t i = 0; i < fieldCount; i++) { |
| 260 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 332 times.
|
332 | SymbolTableEntry *fieldEntry = candidate.scope->lookupField(i); |
| 261 |
1/2✓ Branch 10 → 11 taken 332 times.
✗ Branch 10 → 27 not taken.
|
332 | QualType fieldType = fieldEntry->getQualType(); |
| 262 |
3/4✓ Branch 11 → 12 taken 332 times.
✗ Branch 11 → 27 not taken.
✓ Branch 12 → 13 taken 271 times.
✓ Branch 12 → 15 taken 61 times.
|
332 | if (fieldType.hasAnyGenericParts()) { |
| 263 |
1/2✓ Branch 13 → 14 taken 271 times.
✗ Branch 13 → 27 not taken.
|
271 | TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node); |
| 264 |
1/2✓ Branch 14 → 15 taken 271 times.
✗ Branch 14 → 27 not taken.
|
271 | fieldEntry->updateType(fieldType, true); |
| 265 | } | ||
| 266 | } | ||
| 267 | |||
| 268 | // Loop over all explicit field types and substantiate the generic ones | ||
| 269 |
2/2✓ Branch 25 → 19 taken 2873 times.
✓ Branch 25 → 26 taken 1276 times.
|
4149 | for (QualType &fieldType : candidate.fieldTypes) |
| 270 |
3/4✓ Branch 20 → 21 taken 2873 times.
✗ Branch 20 → 28 not taken.
✓ Branch 21 → 22 taken 1320 times.
✓ Branch 21 → 23 taken 1553 times.
|
2873 | if (fieldType.hasAnyGenericParts()) |
| 271 |
1/2✓ Branch 22 → 23 taken 1320 times.
✗ Branch 22 → 28 not taken.
|
1320 | TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node); |
| 272 | 1276 | } | |
| 273 | |||
| 274 | /** | ||
| 275 | * Searches the candidate template types for a generic type object with a certain name and return it | ||
| 276 | * | ||
| 277 | * @param candidate Matching candidate struct | ||
| 278 | * @param templateTypeName Template type name | ||
| 279 | * @return Generic type object | ||
| 280 | */ | ||
| 281 | 1196 | const GenericType *StructManager::getGenericTypeOfCandidateByName(const Struct &candidate, const std::string &templateTypeName) { | |
| 282 |
1/2✓ Branch 14 → 4 taken 1572 times.
✗ Branch 14 → 15 not taken.
|
1572 | for (const GenericType &templateType : candidate.templateTypes) { |
| 283 |
3/4✓ Branch 5 → 6 taken 1572 times.
✗ Branch 5 → 17 not taken.
✓ Branch 6 → 7 taken 233 times.
✓ Branch 6 → 8 taken 1339 times.
|
1572 | if (!templateType.is(TY_GENERIC)) |
| 284 | 233 | continue; | |
| 285 |
3/4✓ Branch 8 → 9 taken 1339 times.
✗ Branch 8 → 17 not taken.
✓ Branch 10 → 11 taken 1196 times.
✓ Branch 10 → 12 taken 143 times.
|
1339 | if (templateType.getSubType() == templateTypeName) |
| 286 | 1196 | return &templateType; | |
| 287 | } | ||
| 288 | ✗ | return nullptr; | |
| 289 | } | ||
| 290 | |||
| 291 | /** | ||
| 292 | * Calculate the cache key for the struct lookup cache | ||
| 293 | * | ||
| 294 | * @param scope Scope to match against | ||
| 295 | * @param name Struct name requirement | ||
| 296 | * @param templateTypes Template types to substantiate generic types | ||
| 297 | * @return Cache key | ||
| 298 | */ | ||
| 299 | 25720 | uint64_t StructManager::getCacheKey(const Scope *scope, const std::string &name, const QualTypeList &templateTypes) { | |
| 300 | 25720 | uint64_t hash = 0; | |
| 301 | 25720 | hashCombine64(hash, hashPointer(scope)); | |
| 302 | 25720 | hashCombine64(hash, std::hash<std::string>{}(name)); | |
| 303 | 25720 | hashCombine64(hash, hashVector(templateTypes)); | |
| 304 | 25720 | return hash; | |
| 305 | } | ||
| 306 | |||
| 307 | /** | ||
| 308 | * Clear the lookup cache | ||
| 309 | */ | ||
| 310 | 439 | void StructManager::cleanup() { | |
| 311 | 439 | lookupCache.clear(); | |
| 312 | 439 | lookupCacheHits = 0; | |
| 313 | 439 | lookupCacheMisses = 0; | |
| 314 | 439 | } | |
| 315 | |||
| 316 | /** | ||
| 317 | * Dump usage statistics for the lookup cache | ||
| 318 | */ | ||
| 319 | 204 | std::string StructManager::dumpLookupCacheStatistics() { | |
| 320 |
1/2✓ Branch 2 → 3 taken 204 times.
✗ Branch 2 → 22 not taken.
|
204 | std::stringstream stats; |
| 321 |
2/4✓ Branch 3 → 4 taken 204 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 204 times.
✗ Branch 4 → 20 not taken.
|
204 | stats << "StructManager lookup cache statistics:" << std::endl; |
| 322 |
3/6✓ Branch 5 → 6 taken 204 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 204 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 204 times.
✗ Branch 8 → 20 not taken.
|
204 | stats << " lookup cache entries: " << lookupCache.size() << std::endl; |
| 323 |
3/6✓ Branch 9 → 10 taken 204 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 204 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 204 times.
✗ Branch 11 → 20 not taken.
|
204 | stats << " lookup cache hits: " << lookupCacheHits << std::endl; |
| 324 |
3/6✓ Branch 12 → 13 taken 204 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 204 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 204 times.
✗ Branch 14 → 20 not taken.
|
204 | stats << " lookup cache misses: " << lookupCacheMisses << std::endl; |
| 325 |
1/2✓ Branch 15 → 16 taken 204 times.
✗ Branch 15 → 20 not taken.
|
408 | return stats.str(); |
| 326 | 204 | } | |
| 327 | |||
| 328 | } // namespace spice::compiler | ||
| 329 |