src/typechecker/StructManager.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "StructManager.h" | ||
| 4 | |||
| 5 | #include <SourceFile.h> | ||
| 6 | #include <ast/ASTNodes.h> | ||
| 7 | #include <exception/SemanticError.h> | ||
| 8 | #include <model/GenericType.h> | ||
| 9 | #include <model/Interface.h> | ||
| 10 | #include <symboltablebuilder/Scope.h> | ||
| 11 | #include <typechecker/TypeChecker.h> | ||
| 12 | #include <typechecker/TypeMatcher.h> | ||
| 13 | #include <util/CodeLoc.h> | ||
| 14 | #include <util/Concurrency.h> | ||
| 15 | #include <util/CustomHashFunctions.h> | ||
| 16 | |||
| 17 | namespace spice::compiler { | ||
| 18 | |||
| 19 | // Static member initialization | ||
| 20 | std::unordered_map<uint64_t, Struct *> StructManager::lookupCache = {}; | ||
| 21 | size_t StructManager::lookupCacheHits = 0; | ||
| 22 | size_t StructManager::lookupCacheMisses = 0; | ||
| 23 | |||
| 24 | 3256 | Struct *StructManager::insert(Scope *insertScope, Struct &spiceStruct, std::vector<Struct *> *nodeStructList) { | |
| 25 | // Open a new manifestation list. Which gets filled by the substantiated manifestations of the struct | ||
| 26 |
3/6✓ Branch 2 → 3 taken 3256 times.
✗ Branch 2 → 20 not taken.
✓ Branch 3 → 4 taken 3256 times.
✗ Branch 3 → 17 not taken.
✓ Branch 4 → 5 taken 3256 times.
✗ Branch 4 → 15 not taken.
|
3256 | const std::string structId = spiceStruct.name + ":" + spiceStruct.declNode->codeLoc.toPrettyLineAndColumn(); |
| 27 |
1/2✓ Branch 8 → 9 taken 3256 times.
✗ Branch 8 → 21 not taken.
|
3256 | insertScope->structs.emplace(structId, StructManifestationList()); |
| 28 | |||
| 29 | // Save substantiation in declaration node | ||
| 30 |
1/2✓ Branch 10 → 11 taken 3256 times.
✗ Branch 10 → 24 not taken.
|
3256 | Struct *substantiation = insertSubstantiation(insertScope, spiceStruct, spiceStruct.declNode); |
| 31 |
1/2✓ Branch 11 → 12 taken 3256 times.
✗ Branch 11 → 24 not taken.
|
3256 | nodeStructList->push_back(substantiation); |
| 32 | |||
| 33 | 3256 | return substantiation; | |
| 34 | 3256 | } | |
| 35 | |||
| 36 | 5602 | Struct *StructManager::insertSubstantiation(Scope *insertScope, Struct &newManifestation, const ASTNode *declNode) { | |
| 37 |
1/2✓ Branch 2 → 3 taken 5602 times.
✗ Branch 2 → 42 not taken.
|
5602 | const std::string signature = newManifestation.getSignature(); |
| 38 | |||
| 39 | #ifndef NDEBUG | ||
| 40 | // Make sure that the manifestation does not exist already | ||
| 41 |
5/8✓ Branch 3 → 4 taken 5602 times.
✗ Branch 3 → 31 not taken.
✓ Branch 4 → 5 taken 5602 times.
✗ Branch 4 → 31 not taken.
✓ Branch 5 → 6 taken 5602 times.
✗ Branch 5 → 31 not taken.
✓ Branch 13 → 7 taken 61655 times.
✓ Branch 13 → 14 taken 5602 times.
|
67257 | for (const auto &val : insertScope->structs | std::views::values) |
| 42 |
2/4✓ Branch 8 → 9 taken 61655 times.
✗ Branch 8 → 31 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 61655 times.
|
61655 | assert(!val.contains(signature)); |
| 43 | #endif | ||
| 44 | |||
| 45 | // Retrieve the matching manifestation list of the scope | ||
| 46 |
3/6✓ Branch 14 → 15 taken 5602 times.
✗ Branch 14 → 37 not taken.
✓ Branch 15 → 16 taken 5602 times.
✗ Branch 15 → 34 not taken.
✓ Branch 16 → 17 taken 5602 times.
✗ Branch 16 → 32 not taken.
|
5602 | const std::string structId = newManifestation.name + ":" + declNode->codeLoc.toPrettyLineAndColumn(); |
| 47 |
2/4✓ Branch 19 → 20 taken 5602 times.
✗ Branch 19 → 38 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 5602 times.
|
5602 | assert(insertScope->structs.contains(structId)); |
| 48 |
1/2✓ Branch 22 → 23 taken 5602 times.
✗ Branch 22 → 38 not taken.
|
5602 | StructManifestationList &manifestationList = insertScope->structs.at(structId); |
| 49 | |||
| 50 | // Add substantiated struct | ||
| 51 | 5602 | newManifestation.manifestationIndex = manifestationList.size(); | |
| 52 |
1/2✓ Branch 24 → 25 taken 5602 times.
✗ Branch 24 → 38 not taken.
|
5602 | manifestationList.emplace(signature, newManifestation); |
| 53 |
1/2✓ Branch 25 → 26 taken 5602 times.
✗ Branch 25 → 38 not taken.
|
11204 | return &manifestationList.at(signature); |
| 54 | 5602 | } | |
| 55 | |||
| 56 | /** | ||
| 57 | * Check if there is a struct in this scope, fulfilling all given requirements and if found, return it. | ||
| 58 | * If more than one struct matches the requirement, an error gets thrown | ||
| 59 | * | ||
| 60 | * @param matchScope Scope to match against | ||
| 61 | * @param qt Struct name requirement | ||
| 62 | * @param reqTemplateTypes Template types to substantiate generic types | ||
| 63 | * @param node Instantiation AST node for printing error messages | ||
| 64 | * @return Matched struct or nullptr | ||
| 65 | */ | ||
| 66 | 147555 | Struct *StructManager::match(Scope *matchScope, const std::string &qt, const QualTypeList &reqTemplateTypes, | |
| 67 | const ASTNode *node) { | ||
| 68 | // The IR generator lowers struct types through QualType::getStruct, so this may run on multiple threads at once. The | ||
| 69 | // lock is recursive, because matching a struct recurses into matching its field and interface types. | ||
| 70 |
1/2✓ Branch 2 → 3 taken 147555 times.
✗ Branch 2 → 224 not taken.
|
147555 | const ConditionalLock lock(symbolRegistryMutex); |
| 71 | |||
| 72 | // Do cache lookup | ||
| 73 | 147555 | const uint64_t cacheKey = getCacheKey(matchScope, qt, reqTemplateTypes); | |
| 74 |
3/4✓ Branch 4 → 5 taken 147555 times.
✗ Branch 4 → 182 not taken.
✓ Branch 7 → 8 taken 140525 times.
✓ Branch 7 → 10 taken 7030 times.
|
147555 | if (const auto it = lookupCache.find(cacheKey); it != lookupCache.end()) { |
| 75 | 140525 | lookupCacheHits++; | |
| 76 | 140525 | return it->second; | |
| 77 | } | ||
| 78 | 7030 | lookupCacheMisses++; | |
| 79 | |||
| 80 | // Loop over struct registry to find structs, that match the requirements of the instantiation | ||
| 81 | 7030 | std::vector<Struct *> matches; | |
| 82 |
2/2✓ Branch 161 → 12 taken 108541 times.
✓ Branch 161 → 162 taken 7030 times.
|
115571 | for (auto &[structId, manifestations] : matchScope->structs) { |
| 83 |
2/2✓ Branch 158 → 17 taken 123872 times.
✓ Branch 158 → 159 taken 4866 times.
|
128738 | for (const auto &[mangledName, presetStruct] : manifestations) { |
| 84 | // Skip generic and newly inserted substantiations to prevent double matching of a struct | ||
| 85 |
6/8✓ Branch 20 → 21 taken 123872 times.
✗ Branch 20 → 208 not taken.
✓ Branch 21 → 22 taken 108541 times.
✓ Branch 21 → 23 taken 15331 times.
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 108541 times.
✓ Branch 25 → 26 taken 15331 times.
✓ Branch 25 → 27 taken 108541 times.
|
123872 | if (presetStruct.isGenericSubstantiation() || presetStruct.isNewlyInserted) |
| 86 | 17851 | continue; | |
| 87 | |||
| 88 | // Copy the struct to be able to substantiate types | ||
| 89 |
1/2✓ Branch 27 → 28 taken 108541 times.
✗ Branch 27 → 208 not taken.
|
108541 | Struct candidate = presetStruct; |
| 90 | |||
| 91 | // Check name requirement | ||
| 92 |
2/2✓ Branch 29 → 30 taken 102738 times.
✓ Branch 29 → 31 taken 5803 times.
|
108541 | if (!matchName(candidate, qt)) |
| 93 | 102738 | break; // Leave the whole manifestation list, because all manifestations in this list have the same name | |
| 94 | |||
| 95 | // Prepare mapping table from generic type name to concrete type | ||
| 96 | 5803 | TypeMapping &typeMapping = candidate.typeMapping; | |
| 97 | 5803 | typeMapping.clear(); | |
| 98 |
1/2✓ Branch 33 → 34 taken 5803 times.
✗ Branch 33 → 206 not taken.
|
5803 | typeMapping.reserve(candidate.templateTypes.size()); |
| 99 | |||
| 100 | // Check template types requirement | ||
| 101 |
3/4✓ Branch 34 → 35 taken 5803 times.
✗ Branch 34 → 206 not taken.
✓ Branch 35 → 36 taken 1 time.
✓ Branch 35 → 37 taken 5802 times.
|
5803 | if (!matchTemplateTypes(candidate, reqTemplateTypes, typeMapping, node)) |
| 102 | 1 | continue; // Leave this manifestation and continue with the next one | |
| 103 | |||
| 104 | // Map field types from generic to concrete | ||
| 105 |
1/2✓ Branch 37 → 38 taken 5802 times.
✗ Branch 37 → 206 not taken.
|
5802 | substantiateFieldTypes(candidate, typeMapping, node); |
| 106 | |||
| 107 | // We found a match! -> Set the actual candidate and its entry to used | ||
| 108 | 5802 | candidate.used = true; | |
| 109 | 5802 | candidate.entry->used = true; | |
| 110 | |||
| 111 | // Check if it needs to be substantiated | ||
| 112 |
2/2✓ Branch 39 → 40 taken 2519 times.
✓ Branch 39 → 52 taken 3283 times.
|
5802 | if (presetStruct.templateTypes.empty()) { |
| 113 |
5/10✓ Branch 40 → 41 taken 2519 times.
✗ Branch 40 → 183 not taken.
✓ Branch 41 → 42 taken 2519 times.
✗ Branch 41 → 46 not taken.
✓ Branch 42 → 43 taken 2519 times.
✗ Branch 42 → 183 not taken.
✓ Branch 43 → 44 taken 2519 times.
✗ Branch 43 → 183 not taken.
✓ Branch 44 → 45 taken 2519 times.
✗ Branch 44 → 46 not taken.
|
2519 | assert(matchScope->structs.contains(structId) && matchScope->structs.at(structId).contains(mangledName)); |
| 114 |
2/4✓ Branch 47 → 48 taken 2519 times.
✗ Branch 47 → 183 not taken.
✓ Branch 48 → 49 taken 2519 times.
✗ Branch 48 → 183 not taken.
|
2519 | Struct *match = &matchScope->structs.at(structId).at(mangledName); |
| 115 | 2519 | match->used = true; | |
| 116 |
1/2✓ Branch 49 → 50 taken 2519 times.
✗ Branch 49 → 183 not taken.
|
2519 | matches.push_back(match); |
| 117 | 2519 | continue; // Match was successful -> match the next struct | |
| 118 | 2519 | } | |
| 119 | |||
| 120 | // Check if we already have this manifestation and can simply re-use it | ||
| 121 |
4/6✓ Branch 52 → 53 taken 3283 times.
✗ Branch 52 → 186 not taken.
✓ Branch 53 → 54 taken 3283 times.
✗ Branch 53 → 184 not taken.
✓ Branch 57 → 58 taken 937 times.
✓ Branch 57 → 62 taken 2346 times.
|
3283 | if (const auto it = manifestations.find(candidate.getSignature()); it != manifestations.end()) { |
| 122 | 937 | it->second.used = true; | |
| 123 |
1/2✓ Branch 60 → 61 taken 937 times.
✗ Branch 60 → 187 not taken.
|
937 | matches.push_back(&it->second); |
| 124 | 937 | break; // Leave the whole manifestation list to not double-match the manifestation | |
| 125 | } | ||
| 126 | |||
| 127 | // Insert the substantiated version if required | ||
| 128 |
1/2✓ Branch 62 → 64 taken 2346 times.
✗ Branch 62 → 206 not taken.
|
2346 | Struct *substantiatedStruct = insertSubstantiation(matchScope, candidate, presetStruct.declNode); |
| 129 |
2/4✓ Branch 64 → 65 taken 2346 times.
✗ Branch 64 → 206 not taken.
✓ Branch 65 → 66 taken 2346 times.
✗ Branch 65 → 206 not taken.
|
2346 | substantiatedStruct->genericPreset = &matchScope->structs.at(structId).at(mangledName); |
| 130 |
2/4✓ Branch 66 → 67 taken 2346 times.
✗ Branch 66 → 206 not taken.
✓ Branch 67 → 68 taken 2346 times.
✗ Branch 67 → 206 not taken.
|
2346 | substantiatedStruct->declNode->getStructManifestations()->push_back(substantiatedStruct); |
| 131 | 2346 | substantiatedStruct->isNewlyInserted = true; // To not iterate over it in the same matching | |
| 132 | // The generic preset was already decided on, but this manifestation carries concrete field types and therefore | ||
| 133 | // needs its own decision (taken further below, once its fields and interfaces are substantiated) | ||
| 134 | 2346 | substantiatedStruct->implicitDefaultMembersDecided = false; | |
| 135 | |||
| 136 | // Copy struct entry | ||
| 137 |
1/2✓ Branch 68 → 69 taken 2346 times.
✗ Branch 68 → 206 not taken.
|
2346 | const std::string newSignature = substantiatedStruct->getSignature(); |
| 138 |
1/2✓ Branch 69 → 70 taken 2346 times.
✗ Branch 69 → 204 not taken.
|
2346 | matchScope->lookupStrict(substantiatedStruct->name)->used = true; |
| 139 |
1/2✓ Branch 72 → 73 taken 2346 times.
✗ Branch 72 → 204 not taken.
|
2346 | substantiatedStruct->entry = matchScope->symbolTable.copySymbol(substantiatedStruct->name, newSignature); |
| 140 |
1/2✗ Branch 73 → 74 not taken.
✓ Branch 73 → 75 taken 2346 times.
|
2346 | assert(substantiatedStruct->entry != nullptr); |
| 141 | |||
| 142 | // Copy struct scope | ||
| 143 |
1/2✓ Branch 75 → 76 taken 2346 times.
✗ Branch 75 → 204 not taken.
|
2346 | const std::string &oldScopeName = presetStruct.getScopeName(); |
| 144 |
1/2✓ Branch 76 → 77 taken 2346 times.
✗ Branch 76 → 202 not taken.
|
2346 | const std::string &newScopeName = substantiatedStruct->getScopeName(); |
| 145 |
1/2✓ Branch 77 → 78 taken 2346 times.
✗ Branch 77 → 200 not taken.
|
2346 | substantiatedStruct->scope = matchScope->copyChildScope(oldScopeName, newScopeName); |
| 146 |
1/2✗ Branch 78 → 79 not taken.
✓ Branch 78 → 80 taken 2346 times.
|
2346 | assert(substantiatedStruct->scope != nullptr); |
| 147 | 2346 | substantiatedStruct->scope->isGenericScope = false; | |
| 148 | |||
| 149 | // Attach the template types to the new struct entry | ||
| 150 |
1/2✓ Branch 80 → 81 taken 2346 times.
✗ Branch 80 → 192 not taken.
|
2346 | QualType entryType = substantiatedStruct->entry->getQualType() |
| 151 |
2/4✓ Branch 81 → 82 taken 2346 times.
✗ Branch 81 → 191 not taken.
✓ Branch 82 → 83 taken 2346 times.
✗ Branch 82 → 189 not taken.
|
4692 | .getWithTemplateTypes(substantiatedStruct->getTemplateTypes()) |
| 152 |
1/2✓ Branch 83 → 84 taken 2346 times.
✗ Branch 83 → 189 not taken.
|
2346 | .getWithBodyScope(substantiatedStruct->scope); |
| 153 |
1/2✓ Branch 85 → 86 taken 2346 times.
✗ Branch 85 → 200 not taken.
|
2346 | substantiatedStruct->entry->updateType(entryType, true); |
| 154 | |||
| 155 | // Replace symbol types of field entries with concrete types | ||
| 156 |
1/2✗ Branch 86 → 87 not taken.
✓ Branch 86 → 88 taken 2346 times.
|
2346 | assert(substantiatedStruct->scope != nullptr); |
| 157 | 2346 | const size_t fieldCount = substantiatedStruct->fieldTypes.size(); | |
| 158 |
1/2✓ Branch 89 → 90 taken 2346 times.
✗ Branch 89 → 200 not taken.
|
2346 | const size_t explicitFieldsStartIdx = substantiatedStruct->scope->getFieldCount() - fieldCount; |
| 159 |
2/2✓ Branch 114 → 91 taken 5486 times.
✓ Branch 114 → 115 taken 2346 times.
|
7832 | for (size_t i = 0; i < fieldCount; i++) { |
| 160 | // Replace field type with concrete template type | ||
| 161 |
1/2✗ Branch 91 → 92 not taken.
✓ Branch 91 → 93 taken 5486 times.
|
5486 | SymbolTableEntry *fieldEntry = substantiatedStruct->scope->lookupField(explicitFieldsStartIdx + i); |
| 162 |
3/6✓ Branch 96 → 97 taken 5486 times.
✗ Branch 96 → 100 not taken.
✓ Branch 97 → 98 taken 5486 times.
✗ Branch 97 → 195 not taken.
✓ Branch 98 → 99 taken 5486 times.
✗ Branch 98 → 100 not taken.
|
5486 | assert(fieldEntry != nullptr && fieldEntry->isField()); |
| 163 |
1/2✓ Branch 101 → 102 taken 5486 times.
✗ Branch 101 → 195 not taken.
|
5486 | QualType &fieldType = substantiatedStruct->fieldTypes.at(i); |
| 164 |
1/2✓ Branch 102 → 103 taken 5486 times.
✗ Branch 102 → 195 not taken.
|
5486 | QualType baseType = fieldType.getBase(); |
| 165 | |||
| 166 | // Set the body scope of fields that are of type <candidate-struct>* | ||
| 167 |
4/6✓ Branch 103 → 104 taken 5486 times.
✗ Branch 103 → 195 not taken.
✓ Branch 104 → 105 taken 5486 times.
✗ Branch 104 → 195 not taken.
✓ Branch 105 → 106 taken 81 times.
✓ Branch 105 → 109 taken 5405 times.
|
5486 | if (baseType.matches(substantiatedStruct->entry->getQualType(), false, true, true)) |
| 168 |
2/4✓ Branch 106 → 107 taken 81 times.
✗ Branch 106 → 193 not taken.
✓ Branch 107 → 108 taken 81 times.
✗ Branch 107 → 193 not taken.
|
81 | fieldType = fieldType.replaceBaseType(baseType.getWithBodyScope(substantiatedStruct->scope)); |
| 169 | |||
| 170 |
1/2✓ Branch 109 → 110 taken 5486 times.
✗ Branch 109 → 195 not taken.
|
5486 | fieldEntry->updateType(fieldType, /*overwriteExistingType=*/true); |
| 171 | |||
| 172 | // Instantiate structs | ||
| 173 |
3/4✓ Branch 110 → 111 taken 5486 times.
✗ Branch 110 → 195 not taken.
✓ Branch 111 → 112 taken 2116 times.
✓ Branch 111 → 113 taken 3370 times.
|
5486 | if (baseType.is(TY_STRUCT)) |
| 174 |
1/2✓ Branch 112 → 113 taken 2116 times.
✗ Branch 112 → 195 not taken.
|
2116 | (void)baseType.getStruct(node); |
| 175 | } | ||
| 176 | |||
| 177 | // Instantiate implemented interfaces if required | ||
| 178 |
2/2✓ Branch 141 → 117 taken 997 times.
✓ Branch 141 → 142 taken 2346 times.
|
5689 | for (QualType &interfaceType : substantiatedStruct->interfaceTypes) { |
| 179 | // Skip non-generic interfaces | ||
| 180 |
3/4✓ Branch 119 → 120 taken 997 times.
✗ Branch 119 → 198 not taken.
✓ Branch 120 → 121 taken 3 times.
✓ Branch 120 → 122 taken 994 times.
|
997 | if (!interfaceType.hasAnyGenericParts()) |
| 181 | 3 | continue; | |
| 182 | |||
| 183 | // Build template types | ||
| 184 |
2/4✓ Branch 122 → 123 taken 994 times.
✗ Branch 122 → 198 not taken.
✓ Branch 123 → 124 taken 994 times.
✗ Branch 123 → 198 not taken.
|
994 | QualTypeList templateTypes = interfaceType.getTemplateTypes(); |
| 185 |
1/2✓ Branch 124 → 125 taken 994 times.
✗ Branch 124 → 196 not taken.
|
994 | TypeMatcher::substantiateTypesWithTypeMapping(templateTypes, typeMapping, node); |
| 186 | |||
| 187 | // Instantiate interface | ||
| 188 |
1/2✓ Branch 125 → 126 taken 994 times.
✗ Branch 125 → 196 not taken.
|
994 | Interface *spiceInterface = interfaceType.getInterface(node, templateTypes); |
| 189 |
1/2✗ Branch 126 → 127 not taken.
✓ Branch 126 → 128 taken 994 times.
|
994 | assert(spiceInterface != nullptr); |
| 190 |
1/2✓ Branch 128 → 129 taken 994 times.
✗ Branch 128 → 196 not taken.
|
994 | interfaceType = spiceInterface->entry->getQualType(); |
| 191 | 994 | } | |
| 192 | |||
| 193 | // Decide which compiler-generated default members this brand-new manifestation needs. This has to happen right | ||
| 194 | // here, because the decision depends on the concrete field types and every reactive lookup that asks the struct | ||
| 195 | // for its dtor/ctor from now on has to see the answer (see TypeChecker::createImplicitDefaultMembers). The nested | ||
| 196 | // field manifestations created above are decided on first, by their own recursive match() call, so a field's dtor | ||
| 197 | // is already in place when the outer struct is asked whether it needs one. | ||
| 198 | // A manifestation that is created while its own file is still being prepared is left alone: the generic preset | ||
| 199 | // has not been decided on yet at that point, so this manifestation could neither inherit its default members | ||
| 200 | // through the copied scope nor come to the same conclusion on its own. Those manifestations are covered by the | ||
| 201 | // one-shot sweep at the end of the file's prepare run (TypeChecker::visitEntry), which sees all of them. | ||
| 202 |
3/4✓ Branch 142 → 143 taken 2346 times.
✗ Branch 142 → 145 not taken.
✓ Branch 143 → 144 taken 2340 times.
✓ Branch 143 → 145 taken 6 times.
|
2346 | if (structDefaultMembersDecidable && substantiatedStruct->scope->sourceFile->previousStage >= TYPE_CHECKER_PRE) |
| 203 |
1/2✓ Branch 144 → 145 taken 2340 times.
✗ Branch 144 → 200 not taken.
|
2340 | TypeChecker::createImplicitDefaultMembers(*substantiatedStruct, node, /*withMoveCtor=*/false); |
| 204 | |||
| 205 | // Add to matched structs | ||
| 206 |
1/2✓ Branch 145 → 146 taken 2346 times.
✗ Branch 145 → 200 not taken.
|
2346 | matches.push_back(substantiatedStruct); |
| 207 |
3/3✓ Branch 151 → 152 taken 2346 times.
✓ Branch 151 → 154 taken 2520 times.
✓ Branch 151 → 155 taken 103675 times.
|
108541 | } |
| 208 | } | ||
| 209 | |||
| 210 | // If no matches were found, return a nullptr | ||
| 211 |
2/2✓ Branch 163 → 164 taken 1228 times.
✓ Branch 163 → 165 taken 5802 times.
|
7030 | if (matches.empty()) |
| 212 | 1228 | return nullptr; | |
| 213 | |||
| 214 | // Check if more than one struct matches the requirements | ||
| 215 |
1/2✗ Branch 166 → 167 not taken.
✓ Branch 166 → 175 taken 5802 times.
|
5802 | if (matches.size() > 1) |
| 216 | ✗ | throw SemanticError(node, STRUCT_AMBIGUITY, "Multiple structs match the requested signature"); | |
| 217 | 5802 | Struct *matchedStruct = matches.front(); | |
| 218 | 5802 | matchedStruct->isNewlyInserted = false; | |
| 219 | |||
| 220 | // Insert into cache | ||
| 221 |
1/2✓ Branch 176 → 177 taken 5802 times.
✗ Branch 176 → 220 not taken.
|
5802 | lookupCache[cacheKey] = matchedStruct; |
| 222 | |||
| 223 | 5802 | return matchedStruct; | |
| 224 | 147555 | } | |
| 225 | |||
| 226 | /** | ||
| 227 | * Checks if the matching candidate fulfills the name requirement | ||
| 228 | * | ||
| 229 | * @param candidate Matching candidate struct | ||
| 230 | * @param reqName Requested struct name | ||
| 231 | * @return Fulfilled or not | ||
| 232 | */ | ||
| 233 | 108541 | bool StructManager::matchName(const Struct &candidate, const std::string &reqName) { return candidate.name == reqName; } | |
| 234 | |||
| 235 | /** | ||
| 236 | * Checks if the matching candidate fulfills the template types requirement | ||
| 237 | * | ||
| 238 | * @param candidate Matching candidate struct | ||
| 239 | * @param reqTemplateTypes Requested struct template types | ||
| 240 | * @param typeMapping Generic type mapping | ||
| 241 | * @param node Instantiation AST node for printing error messages | ||
| 242 | * @return Fulfilled or not | ||
| 243 | */ | ||
| 244 | 5803 | bool StructManager::matchTemplateTypes(Struct &candidate, const QualTypeList &reqTemplateTypes, TypeMapping &typeMapping, | |
| 245 | const ASTNode *node) { | ||
| 246 | // Check if the number of types match | ||
| 247 | 5803 | const size_t typeCount = reqTemplateTypes.size(); | |
| 248 |
2/2✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 5802 times.
|
5803 | if (typeCount != candidate.templateTypes.size()) |
| 249 | 1 | return false; | |
| 250 | |||
| 251 | // Give the type matcher a way to retrieve instances of GenericType by their name | ||
| 252 | 16394 | TypeMatcher::ResolverFct genericTypeResolver = [&](const std::string &genericTypeName) { | |
| 253 | 4790 | return getGenericTypeOfCandidateByName(candidate, genericTypeName); | |
| 254 | 5802 | }; | |
| 255 | |||
| 256 | // Loop over all template types | ||
| 257 |
2/2✓ Branch 17 → 8 taken 4790 times.
✓ Branch 17 → 18 taken 5802 times.
|
10592 | for (size_t i = 0; i < typeCount; i++) { |
| 258 |
1/2✓ Branch 8 → 9 taken 4790 times.
✗ Branch 8 → 22 not taken.
|
4790 | const QualType &reqType = reqTemplateTypes.at(i); |
| 259 |
1/2✓ Branch 9 → 10 taken 4790 times.
✗ Branch 9 → 22 not taken.
|
4790 | QualType &candidateType = candidate.templateTypes.at(i); |
| 260 | |||
| 261 | // Check if the requested template type matches the candidate template type. The type mapping may be extended | ||
| 262 |
2/4✓ Branch 10 → 11 taken 4790 times.
✗ Branch 10 → 22 not taken.
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 4790 times.
|
4790 | if (!TypeMatcher::matchRequestedToCandidateType(candidateType, reqType, typeMapping, genericTypeResolver, false)) |
| 263 | ✗ | return false; | |
| 264 | |||
| 265 | // Substantiate the candidate param type, based on the type mapping | ||
| 266 |
2/4✓ Branch 13 → 14 taken 4790 times.
✗ Branch 13 → 22 not taken.
✓ Branch 14 → 15 taken 4790 times.
✗ Branch 14 → 16 not taken.
|
4790 | if (candidateType.hasAnyGenericParts()) |
| 267 |
1/2✓ Branch 15 → 16 taken 4790 times.
✗ Branch 15 → 22 not taken.
|
4790 | TypeMatcher::substantiateTypeWithTypeMapping(candidateType, typeMapping, node); |
| 268 | } | ||
| 269 | |||
| 270 | 5802 | return true; | |
| 271 | 5802 | } | |
| 272 | |||
| 273 | /** | ||
| 274 | * Come up with the concrete field types, by applying the type mapping onto the generic field types | ||
| 275 | * | ||
| 276 | * @param candidate Candidate struct | ||
| 277 | * @param typeMapping Generic type mapping | ||
| 278 | * @param node Instantiation AST node for printing error messages | ||
| 279 | */ | ||
| 280 | 5802 | void StructManager::substantiateFieldTypes(Struct &candidate, const TypeMapping &typeMapping, const ASTNode *node) { | |
| 281 | // Loop over all implicit fields and substantiate the generic ones | ||
| 282 | 5802 | const size_t implicitFieldCount = candidate.scope->getFieldCount() - candidate.fieldTypes.size(); | |
| 283 |
2/2✓ Branch 16 → 5 taken 1408 times.
✓ Branch 16 → 17 taken 5802 times.
|
7210 | for (size_t i = 0; i < implicitFieldCount; i++) { |
| 284 |
1/2✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 1408 times.
|
1408 | SymbolTableEntry *fieldEntry = candidate.scope->lookupField(i); |
| 285 |
1/2✓ Branch 10 → 11 taken 1408 times.
✗ Branch 10 → 35 not taken.
|
1408 | QualType fieldType = fieldEntry->getQualType(); |
| 286 |
3/4✓ Branch 11 → 12 taken 1408 times.
✗ Branch 11 → 35 not taken.
✓ Branch 12 → 13 taken 648 times.
✓ Branch 12 → 15 taken 760 times.
|
1408 | if (fieldType.hasAnyGenericParts()) { |
| 287 |
1/2✓ Branch 13 → 14 taken 648 times.
✗ Branch 13 → 35 not taken.
|
648 | TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node); |
| 288 |
1/2✓ Branch 14 → 15 taken 648 times.
✗ Branch 14 → 35 not taken.
|
648 | fieldEntry->updateType(fieldType, true); |
| 289 | } | ||
| 290 | } | ||
| 291 | |||
| 292 | // Loop over all explicit field types and substantiate the generic ones | ||
| 293 |
2/2✓ Branch 33 → 19 taken 12213 times.
✓ Branch 33 → 34 taken 5802 times.
|
23817 | for (QualType &fieldType : candidate.fieldTypes) |
| 294 |
3/4✓ Branch 21 → 22 taken 12213 times.
✗ Branch 21 → 36 not taken.
✓ Branch 22 → 23 taken 4859 times.
✓ Branch 22 → 24 taken 7354 times.
|
12213 | if (fieldType.hasAnyGenericParts()) |
| 295 |
1/2✓ Branch 23 → 24 taken 4859 times.
✗ Branch 23 → 36 not taken.
|
4859 | TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, node); |
| 296 | 5802 | } | |
| 297 | |||
| 298 | /** | ||
| 299 | * Searches the candidate template types for a generic type object with a certain name and return it | ||
| 300 | * | ||
| 301 | * @param candidate Matching candidate struct | ||
| 302 | * @param templateTypeName Template type name | ||
| 303 | * @return Generic type object | ||
| 304 | */ | ||
| 305 | 4790 | const GenericType *StructManager::getGenericTypeOfCandidateByName(const Struct &candidate, const std::string &templateTypeName) { | |
| 306 |
1/2✓ Branch 22 → 4 taken 6304 times.
✗ Branch 22 → 23 not taken.
|
11094 | for (const GenericType &templateType : candidate.templateTypes) { |
| 307 |
3/4✓ Branch 6 → 7 taken 6304 times.
✗ Branch 6 → 25 not taken.
✓ Branch 7 → 8 taken 1073 times.
✓ Branch 7 → 9 taken 5231 times.
|
6304 | if (!templateType.is(TY_GENERIC)) |
| 308 | 1073 | continue; | |
| 309 |
3/4✓ Branch 9 → 10 taken 5231 times.
✗ Branch 9 → 25 not taken.
✓ Branch 11 → 12 taken 4790 times.
✓ Branch 11 → 13 taken 441 times.
|
5231 | if (templateType.getSubType() == templateTypeName) |
| 310 | 4790 | return &templateType; | |
| 311 | } | ||
| 312 | ✗ | return nullptr; | |
| 313 | } | ||
| 314 | |||
| 315 | /** | ||
| 316 | * Calculate the cache key for the struct lookup cache | ||
| 317 | * | ||
| 318 | * @param scope Scope to match against | ||
| 319 | * @param name Struct name requirement | ||
| 320 | * @param templateTypes Template types to substantiate generic types | ||
| 321 | * @return Cache key | ||
| 322 | */ | ||
| 323 | 147555 | uint64_t StructManager::getCacheKey(const Scope *scope, const std::string &name, const QualTypeList &templateTypes) { | |
| 324 | 147555 | uint64_t hash = 0; | |
| 325 | 147555 | hashCombine64(hash, hashPointer(scope)); | |
| 326 | 147555 | hashCombine64(hash, std::hash<std::string>{}(name)); | |
| 327 | 147555 | hashCombine64(hash, hashVector(templateTypes)); | |
| 328 | 147555 | return hash; | |
| 329 | } | ||
| 330 | |||
| 331 | /** | ||
| 332 | * Clear the lookup cache | ||
| 333 | */ | ||
| 334 | 621 | void StructManager::cleanup() { | |
| 335 | 621 | lookupCache.clear(); | |
| 336 | 621 | lookupCacheHits = 0; | |
| 337 | 621 | lookupCacheMisses = 0; | |
| 338 | 621 | } | |
| 339 | |||
| 340 | /** | ||
| 341 | * Dump usage statistics for the lookup cache | ||
| 342 | */ | ||
| 343 | 365 | std::string StructManager::dumpLookupCacheStatistics() { | |
| 344 |
1/2✓ Branch 2 → 3 taken 365 times.
✗ Branch 2 → 22 not taken.
|
365 | std::stringstream stats; |
| 345 |
2/4✓ Branch 3 → 4 taken 365 times.
✗ Branch 3 → 20 not taken.
✓ Branch 4 → 5 taken 365 times.
✗ Branch 4 → 20 not taken.
|
365 | stats << "StructManager lookup cache statistics:" << std::endl; |
| 346 |
3/6✓ Branch 5 → 6 taken 365 times.
✗ Branch 5 → 20 not taken.
✓ Branch 7 → 8 taken 365 times.
✗ Branch 7 → 20 not taken.
✓ Branch 8 → 9 taken 365 times.
✗ Branch 8 → 20 not taken.
|
365 | stats << " lookup cache entries: " << lookupCache.size() << std::endl; |
| 347 |
3/6✓ Branch 9 → 10 taken 365 times.
✗ Branch 9 → 20 not taken.
✓ Branch 10 → 11 taken 365 times.
✗ Branch 10 → 20 not taken.
✓ Branch 11 → 12 taken 365 times.
✗ Branch 11 → 20 not taken.
|
365 | stats << " lookup cache hits: " << lookupCacheHits << std::endl; |
| 348 |
3/6✓ Branch 12 → 13 taken 365 times.
✗ Branch 12 → 20 not taken.
✓ Branch 13 → 14 taken 365 times.
✗ Branch 13 → 20 not taken.
✓ Branch 14 → 15 taken 365 times.
✗ Branch 14 → 20 not taken.
|
365 | stats << " lookup cache misses: " << lookupCacheMisses << std::endl; |
| 349 |
1/2✓ Branch 15 → 16 taken 365 times.
✗ Branch 15 → 20 not taken.
|
730 | return stats.str(); |
| 350 | 365 | } | |
| 351 | |||
| 352 | } // namespace spice::compiler | ||
| 353 |