| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "TypeChecker.h" | ||
| 4 | |||
| 5 | #include <SourceFile.h> | ||
| 6 | #include <ast/ASTBuilder.h> | ||
| 7 | #include <ast/ASTNodes.h> | ||
| 8 | #include <global/GlobalResourceManager.h> | ||
| 9 | #include <symboltablebuilder/SymbolTableBuilder.h> | ||
| 10 | #include <typechecker/TypeMatcher.h> | ||
| 11 | |||
| 12 | namespace spice::compiler { | ||
| 13 | |||
| 14 | static const char *const FCT_NAME_DEALLOC = "sDealloc"; | ||
| 15 | |||
| 16 | /** | ||
| 17 | * Create a default struct method | ||
| 18 | * Checks if the given struct scope already has a user-defined constructor and creates a default one if not. | ||
| 19 | * | ||
| 20 | * @param spiceStruct Struct instance | ||
| 21 | * @param entryName Name of the symbol table entry | ||
| 22 | * @param name Name of the method to create | ||
| 23 | * @param params Parameter types of the method | ||
| 24 | */ | ||
| 25 | 359 | void TypeChecker::createDefaultStructMethod(const Struct &spiceStruct, const std::string &entryName, const std::string &name, | |
| 26 | const ParamList ¶ms) const { | ||
| 27 | 359 | Scope *structScope = spiceStruct.scope; | |
| 28 | 359 | ASTNode *node = spiceStruct.declNode; | |
| 29 | 359 | const SymbolTableEntry *structEntry = spiceStruct.entry; | |
| 30 |
1/2✓ Branch 2 → 3 taken 359 times.
✗ Branch 2 → 68 not taken.
|
359 | const QualType &structType = structEntry->getQualType(); |
| 31 |
3/6✓ Branch 3 → 4 taken 359 times.
✗ Branch 3 → 44 not taken.
✓ Branch 4 → 5 taken 359 times.
✗ Branch 4 → 44 not taken.
✓ Branch 5 → 6 taken 359 times.
✗ Branch 5 → 42 not taken.
|
359 | const std::string fqFctName = structType.getSubType() + MEMBER_ACCESS_TOKEN + name; |
| 32 | |||
| 33 | // Procedure type | ||
| 34 |
1/2✓ Branch 7 → 8 taken 359 times.
✗ Branch 7 → 66 not taken.
|
359 | QualType procedureType(TY_PROCEDURE); |
| 35 |
1/2✓ Branch 8 → 9 taken 359 times.
✗ Branch 8 → 66 not taken.
|
359 | procedureType.makePublic(); // Always public |
| 36 | |||
| 37 | // Insert symbol for function into the symbol table | ||
| 38 |
1/2✓ Branch 9 → 10 taken 359 times.
✗ Branch 9 → 66 not taken.
|
359 | SymbolTableEntry *procEntry = structScope->insert(entryName, structEntry->declNode); |
| 39 |
1/2✓ Branch 12 → 13 taken 359 times.
✗ Branch 12 → 66 not taken.
|
359 | procEntry->updateType(procedureType, true); |
| 40 | |||
| 41 | // Add to external name registry | ||
| 42 |
1/2✓ Branch 13 → 14 taken 359 times.
✗ Branch 13 → 66 not taken.
|
359 | sourceFile->addNameRegistryEntry(fqFctName, TY_PROCEDURE, procEntry, structScope, true); |
| 43 | |||
| 44 | // Create the default method | ||
| 45 |
1/2✓ Branch 14 → 15 taken 359 times.
✗ Branch 14 → 66 not taken.
|
359 | const std::vector<GenericType> templateTypes = spiceStruct.templateTypes; |
| 46 |
1/2✓ Branch 15 → 16 taken 359 times.
✗ Branch 15 → 64 not taken.
|
359 | const QualType returnType(TY_DYN); |
| 47 |
3/6✓ Branch 16 → 17 taken 359 times.
✗ Branch 16 → 51 not taken.
✓ Branch 17 → 18 taken 359 times.
✗ Branch 17 → 48 not taken.
✓ Branch 18 → 19 taken 359 times.
✗ Branch 18 → 45 not taken.
|
359 | Function defaultMethod(name, procEntry, structType, returnType, params, templateTypes, structEntry->declNode); |
| 48 | 359 | defaultMethod.implicitDefault = true; | |
| 49 | |||
| 50 | // Create function scope | ||
| 51 |
2/4✓ Branch 23 → 24 taken 359 times.
✗ Branch 23 → 54 not taken.
✓ Branch 24 → 25 taken 359 times.
✗ Branch 24 → 52 not taken.
|
359 | Scope *procScope = structScope->createChildScope(defaultMethod.getSignature(false), ScopeType::FUNC_PROC_BODY, &node->codeLoc); |
| 52 | 359 | defaultMethod.bodyScope = procScope; | |
| 53 | |||
| 54 | // Create 'this' symbol in the function scope | ||
| 55 |
1/2✓ Branch 28 → 29 taken 359 times.
✗ Branch 28 → 57 not taken.
|
1077 | SymbolTableEntry *thisEntry = procScope->insert(THIS_VARIABLE_NAME, node); |
| 56 |
2/4✓ Branch 34 → 35 taken 359 times.
✗ Branch 34 → 61 not taken.
✓ Branch 35 → 36 taken 359 times.
✗ Branch 35 → 61 not taken.
|
359 | thisEntry->updateType(structType.toPtr(node), true); |
| 57 | 359 | thisEntry->used = true; // Always set to used to not print warnings for non-existing code | |
| 58 | |||
| 59 | // Hand it off to the function manager to register the function | ||
| 60 |
2/4✓ Branch 36 → 37 taken 359 times.
✗ Branch 36 → 62 not taken.
✓ Branch 37 → 38 taken 359 times.
✗ Branch 37 → 62 not taken.
|
359 | FunctionManager::insert(structScope, defaultMethod, structEntry->declNode->getFctManifestations(name)); |
| 61 | 359 | } | |
| 62 | |||
| 63 | /** | ||
| 64 | * Checks if the given struct scope already has a user-defined constructor and creates a default one if not. | ||
| 65 | * | ||
| 66 | * For generating a default ctor, the following conditions need to be met: | ||
| 67 | * - No user-defined constructors | ||
| 68 | * | ||
| 69 | * @param spiceStruct Struct instance | ||
| 70 | * @param structScope Scope of the struct | ||
| 71 | */ | ||
| 72 | 674 | void TypeChecker::createDefaultCtorIfRequired(const Struct &spiceStruct, Scope *structScope) const { | |
| 73 |
1/2✓ Branch 2 → 3 taken 674 times.
✗ Branch 2 → 4 not taken.
|
674 | const auto node = spice_pointer_cast<StructDefNode *>(spiceStruct.declNode); |
| 74 |
2/4✓ Branch 9 → 10 taken 674 times.
✗ Branch 9 → 12 not taken.
✓ Branch 10 → 11 taken 674 times.
✗ Branch 10 → 12 not taken.
|
674 | assert(structScope != nullptr && structScope->type == ScopeType::STRUCT); |
| 75 | |||
| 76 | // Abort if the struct already has a user-defined constructor | ||
| 77 | 674 | const SymbolTableEntry *structEntry = spiceStruct.entry; | |
| 78 |
1/2✓ Branch 13 → 14 taken 674 times.
✗ Branch 13 → 128 not taken.
|
674 | const QualType &structType = structEntry->getQualType(); |
| 79 |
3/6✓ Branch 14 → 15 taken 674 times.
✗ Branch 14 → 101 not taken.
✓ Branch 15 → 16 taken 674 times.
✗ Branch 15 → 101 not taken.
✓ Branch 16 → 17 taken 674 times.
✗ Branch 16 → 99 not taken.
|
674 | const std::string fqFctName = structType.getSubType() + MEMBER_ACCESS_TOKEN + CTOR_FUNCTION_NAME; |
| 80 |
3/4✓ Branch 18 → 19 taken 674 times.
✗ Branch 18 → 126 not taken.
✓ Branch 19 → 20 taken 515 times.
✓ Branch 19 → 21 taken 159 times.
|
674 | if (sourceFile->getNameRegistryEntry(fqFctName)) |
| 81 | 515 | return; | |
| 82 | |||
| 83 | // Check if we have fields, that require us to do anything in the ctor | ||
| 84 |
1/2✓ Branch 21 → 22 taken 159 times.
✗ Branch 21 → 126 not taken.
|
159 | const size_t fieldCount = structScope->getFieldCount(); |
| 85 | 159 | bool hasFieldsWithDefaultValue = false; | |
| 86 | 159 | bool hasFieldsToConstruct = false; | |
| 87 |
2/2✓ Branch 77 → 23 taken 352 times.
✓ Branch 77 → 78 taken 153 times.
|
505 | for (size_t i = 0; i < fieldCount; i++) { |
| 88 |
1/2✗ Branch 23 → 24 not taken.
✓ Branch 23 → 25 taken 352 times.
|
352 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(i); |
| 89 |
1/2✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 352 times.
|
352 | assert(fieldSymbol != nullptr); |
| 90 |
1/2✓ Branch 30 → 31 taken 352 times.
✗ Branch 30 → 114 not taken.
|
352 | const QualType &thisType = fieldSymbol->getQualType(); |
| 91 | |||
| 92 | // Abort if we have a field, that is a reference | ||
| 93 |
3/4✓ Branch 31 → 32 taken 352 times.
✗ Branch 31 → 114 not taken.
✓ Branch 32 → 33 taken 5 times.
✓ Branch 32 → 34 taken 347 times.
|
352 | if (thisType.isRef()) |
| 94 | 6 | return; | |
| 95 | |||
| 96 |
3/4✓ Branch 34 → 35 taken 347 times.
✗ Branch 34 → 36 not taken.
✓ Branch 37 → 38 taken 318 times.
✓ Branch 37 → 39 taken 29 times.
|
347 | if (const auto fieldNode = dynamic_cast<FieldNode *>(fieldSymbol->declNode)) { |
| 97 | 318 | hasFieldsWithDefaultValue |= fieldNode->defaultValue != nullptr; | |
| 98 | } else { | ||
| 99 |
2/4✓ Branch 39 → 40 taken 29 times.
✗ Branch 39 → 41 not taken.
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 29 times.
|
29 | assert(dynamic_cast<DataTypeNode *>(fieldSymbol->declNode) != nullptr); |
| 100 | } | ||
| 101 | |||
| 102 |
1/2✓ Branch 44 → 45 taken 347 times.
✗ Branch 44 → 114 not taken.
|
347 | const QualType fieldType = fieldSymbol->getQualType(); |
| 103 |
3/4✓ Branch 45 → 46 taken 347 times.
✗ Branch 45 → 114 not taken.
✓ Branch 46 → 47 taken 26 times.
✓ Branch 46 → 75 taken 321 times.
|
347 | if (fieldType.is(TY_STRUCT)) { |
| 104 |
1/2✓ Branch 47 → 48 taken 26 times.
✗ Branch 47 → 114 not taken.
|
26 | Scope *bodyScope = fieldType.getBodyScope(); |
| 105 |
1/2✓ Branch 48 → 49 taken 26 times.
✗ Branch 48 → 114 not taken.
|
26 | const Struct *fieldStruct = fieldType.getStruct(node); |
| 106 | // Check if we are required to call a ctor | ||
| 107 |
1/2✓ Branch 49 → 50 taken 26 times.
✗ Branch 49 → 51 not taken.
|
26 | const auto structDeclNode = spice_pointer_cast<StructDefNode *>(fieldStruct->declNode); |
| 108 |
5/6✓ Branch 56 → 57 taken 26 times.
✗ Branch 56 → 114 not taken.
✓ Branch 57 → 58 taken 25 times.
✓ Branch 57 → 59 taken 1 time.
✓ Branch 58 → 59 taken 7 times.
✓ Branch 58 → 60 taken 18 times.
|
26 | const bool isCtorCallRequired = bodyScope->hasRefFields() || structDeclNode->emitVTable; |
| 109 | // Lookup ctor function | ||
| 110 |
2/4✓ Branch 65 → 66 taken 26 times.
✗ Branch 65 → 104 not taken.
✓ Branch 66 → 67 taken 26 times.
✗ Branch 66 → 102 not taken.
|
78 | const Function *ctorFct = FunctionManager::match(bodyScope, CTOR_FUNCTION_NAME, thisType, {}, {}, true, node); |
| 111 | // If we are required to construct, but no constructor is found, we can't generate a default ctor for the outer struct | ||
| 112 |
4/4✓ Branch 71 → 72 taken 12 times.
✓ Branch 71 → 74 taken 14 times.
✓ Branch 72 → 73 taken 1 time.
✓ Branch 72 → 74 taken 11 times.
|
26 | if (!ctorFct && isCtorCallRequired) |
| 113 | 1 | return; | |
| 114 | 25 | hasFieldsToConstruct |= ctorFct != nullptr; | |
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | // If we don't have any fields, that require us to do anything in the ctor, we can skip it | ||
| 119 |
6/6✓ Branch 78 → 79 taken 112 times.
✓ Branch 78 → 82 taken 41 times.
✓ Branch 79 → 80 taken 98 times.
✓ Branch 79 → 82 taken 14 times.
✓ Branch 80 → 81 taken 93 times.
✓ Branch 80 → 82 taken 5 times.
|
153 | if (!hasFieldsWithDefaultValue && !hasFieldsToConstruct && !node->emitVTable) |
| 120 | 93 | return; | |
| 121 | |||
| 122 | // Create the default ctor function | ||
| 123 |
1/2✓ Branch 82 → 83 taken 60 times.
✗ Branch 82 → 126 not taken.
|
60 | const std::string entryName = Function::getSymbolTableEntryNameDefaultCtor(node->codeLoc); |
| 124 |
2/4✓ Branch 86 → 87 taken 60 times.
✗ Branch 86 → 117 not taken.
✓ Branch 87 → 88 taken 60 times.
✗ Branch 87 → 115 not taken.
|
180 | createDefaultStructMethod(spiceStruct, entryName, CTOR_FUNCTION_NAME, {}); |
| 125 |
2/2✓ Branch 94 → 95 taken 60 times.
✓ Branch 94 → 97 taken 614 times.
|
674 | } |
| 126 | |||
| 127 | /** | ||
| 128 | * Checks if the given struct scope already has a user-defined constructor and creates a default one if not. | ||
| 129 | * | ||
| 130 | * For generating a default copy ctor, the following conditions need to be met: | ||
| 131 | * - No user-defined copy ctor | ||
| 132 | * - No user-defined move ctor | ||
| 133 | * | ||
| 134 | * @param spiceStruct Struct instance | ||
| 135 | * @param structScope Scope of the struct | ||
| 136 | */ | ||
| 137 | 674 | void TypeChecker::createDefaultCopyCtorIfRequired(const Struct &spiceStruct, Scope *structScope) const { | |
| 138 |
1/2✓ Branch 2 → 3 taken 674 times.
✗ Branch 2 → 4 not taken.
|
674 | const auto node = spice_pointer_cast<const StructDefNode *>(spiceStruct.declNode); |
| 139 |
2/4✓ Branch 9 → 10 taken 674 times.
✗ Branch 9 → 12 not taken.
✓ Branch 10 → 11 taken 674 times.
✗ Branch 10 → 12 not taken.
|
674 | assert(structScope != nullptr && structScope->type == ScopeType::STRUCT); |
| 140 | |||
| 141 | // Abort if the struct already has a user-defined copy constructor | ||
| 142 |
1/2✓ Branch 13 → 14 taken 674 times.
✗ Branch 13 → 153 not taken.
|
674 | const QualType structType = spiceStruct.entry->getQualType(); |
| 143 |
2/4✓ Branch 14 → 15 taken 674 times.
✗ Branch 14 → 111 not taken.
✓ Branch 18 → 19 taken 674 times.
✗ Branch 18 → 107 not taken.
|
2022 | const ArgList lookupArgs = {{structType.toConstRef(node), true}}; |
| 144 |
2/4✓ Branch 22 → 23 taken 674 times.
✗ Branch 22 → 115 not taken.
✓ Branch 23 → 24 taken 674 times.
✗ Branch 23 → 113 not taken.
|
674 | const Function *copyCtor = FunctionManager::lookup(structScope, CTOR_FUNCTION_NAME, structType, lookupArgs, true); |
| 145 |
2/2✓ Branch 26 → 27 taken 124 times.
✓ Branch 26 → 28 taken 550 times.
|
674 | if (copyCtor != nullptr) |
| 146 | 124 | return; | |
| 147 | |||
| 148 | // Abort if the struct has a user-defined move constructor | ||
| 149 | // ToDo: Check for move ctor | ||
| 150 | |||
| 151 | // Check if we have fields, that require us to do anything in the ctor | ||
| 152 |
1/2✓ Branch 28 → 29 taken 550 times.
✗ Branch 28 → 151 not taken.
|
550 | const size_t fieldCount = structScope->getFieldCount(); |
| 153 | 550 | bool copyCtorRequired = false; | |
| 154 |
2/2✓ Branch 82 → 30 taken 1182 times.
✓ Branch 82 → 83 taken 532 times.
|
1714 | for (size_t i = 0; i < fieldCount; i++) { |
| 155 |
1/2✗ Branch 30 → 31 not taken.
✓ Branch 30 → 32 taken 1182 times.
|
1182 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(i); |
| 156 |
1/2✓ Branch 35 → 36 taken 1182 times.
✗ Branch 35 → 151 not taken.
|
1182 | const QualType &fieldType = fieldSymbol->getQualType(); |
| 157 | |||
| 158 | // If the field is of type struct, check if this struct has a copy ctor that has to be called | ||
| 159 |
3/4✓ Branch 36 → 37 taken 1182 times.
✗ Branch 36 → 151 not taken.
✓ Branch 37 → 38 taken 163 times.
✓ Branch 37 → 75 taken 1019 times.
|
1182 | if (fieldType.is(TY_STRUCT)) { |
| 160 |
1/2✓ Branch 38 → 39 taken 163 times.
✗ Branch 38 → 136 not taken.
|
163 | Scope *bodyScope = fieldType.getBodyScope(); |
| 161 |
1/2✓ Branch 39 → 40 taken 163 times.
✗ Branch 39 → 136 not taken.
|
163 | const Struct *fieldStruct = fieldType.getStruct(node); |
| 162 | // Check if we are required to call a ctor | ||
| 163 |
1/2✓ Branch 40 → 41 taken 163 times.
✗ Branch 40 → 42 not taken.
|
163 | const auto structDeclNode = spice_pointer_cast<StructDefNode *>(fieldStruct->declNode); |
| 164 |
5/6✓ Branch 47 → 48 taken 163 times.
✗ Branch 47 → 136 not taken.
✓ Branch 48 → 49 taken 153 times.
✓ Branch 48 → 50 taken 10 times.
✓ Branch 49 → 50 taken 37 times.
✓ Branch 49 → 51 taken 116 times.
|
163 | const bool isCtorCallRequired = bodyScope->hasRefFields() || structDeclNode->emitVTable; |
| 165 | // Lookup copy ctor function | ||
| 166 |
2/4✓ Branch 52 → 53 taken 163 times.
✗ Branch 52 → 123 not taken.
✓ Branch 56 → 57 taken 163 times.
✗ Branch 56 → 119 not taken.
|
326 | const ArgList args = {{fieldType.toConstRef(node), false /* we always have the field as storage */}}; |
| 167 |
2/4✓ Branch 61 → 62 taken 163 times.
✗ Branch 61 → 127 not taken.
✓ Branch 62 → 63 taken 163 times.
✗ Branch 62 → 125 not taken.
|
489 | const Function *ctorFct = FunctionManager::match(bodyScope, CTOR_FUNCTION_NAME, fieldType, args, {}, true, node); |
| 168 | // If we are required to construct, but no constructor is found, we can't generate a default ctor for the outer struct | ||
| 169 |
4/4✓ Branch 66 → 67 taken 94 times.
✓ Branch 66 → 69 taken 69 times.
✓ Branch 67 → 68 taken 18 times.
✓ Branch 67 → 69 taken 76 times.
|
163 | if (!ctorFct && isCtorCallRequired) |
| 170 | 18 | return; | |
| 171 | 145 | copyCtorRequired |= ctorFct != nullptr; | |
| 172 |
2/2✓ Branch 71 → 72 taken 145 times.
✓ Branch 71 → 74 taken 18 times.
|
163 | } |
| 173 | |||
| 174 | // If we have an owning heap pointer, we need to do a memcpy of the heap storage and therefore need a default copy ctor | ||
| 175 |
3/4✓ Branch 75 → 76 taken 1164 times.
✗ Branch 75 → 151 not taken.
✓ Branch 76 → 77 taken 32 times.
✓ Branch 76 → 81 taken 1132 times.
|
1164 | if (fieldType.isHeap()) { |
| 176 |
2/4✓ Branch 77 → 78 taken 32 times.
✗ Branch 77 → 151 not taken.
✗ Branch 78 → 79 not taken.
✓ Branch 78 → 80 taken 32 times.
|
32 | assert(fieldType.isPtr()); |
| 177 | 32 | copyCtorRequired = true; | |
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | // If we don't have any fields, that require us to do anything in the copy ctor, we can skip it | ||
| 182 |
4/4✓ Branch 83 → 84 taken 460 times.
✓ Branch 83 → 86 taken 72 times.
✓ Branch 84 → 85 taken 336 times.
✓ Branch 84 → 86 taken 124 times.
|
532 | if (!copyCtorRequired && !node->emitVTable) |
| 183 | 336 | return; | |
| 184 | |||
| 185 | // Create the default copy ctor function | ||
| 186 |
1/2✓ Branch 86 → 87 taken 196 times.
✗ Branch 86 → 151 not taken.
|
196 | const std::string entryName = Function::getSymbolTableEntryNameDefaultCopyCtor(node->codeLoc); |
| 187 |
2/4✓ Branch 87 → 88 taken 196 times.
✗ Branch 87 → 140 not taken.
✓ Branch 90 → 91 taken 196 times.
✗ Branch 90 → 137 not taken.
|
588 | const ParamList paramTypes = {{structType.toConstRef(node), false}}; |
| 188 |
2/4✓ Branch 94 → 95 taken 196 times.
✗ Branch 94 → 143 not taken.
✓ Branch 95 → 96 taken 196 times.
✗ Branch 95 → 141 not taken.
|
196 | createDefaultStructMethod(spiceStruct, entryName, CTOR_FUNCTION_NAME, paramTypes); |
| 189 |
2/2✓ Branch 102 → 103 taken 196 times.
✓ Branch 102 → 105 taken 478 times.
|
674 | } |
| 190 | |||
| 191 | /** | ||
| 192 | * Checks if the given struct scope already has a user-defined destructor and creates a default one if not. | ||
| 193 | * | ||
| 194 | * For generating a default dtor, the following conditions need to be met: | ||
| 195 | * - No user-defined dtor | ||
| 196 | * | ||
| 197 | * @param spiceStruct Struct instance | ||
| 198 | * @param structScope Scope of the struct | ||
| 199 | */ | ||
| 200 | 674 | void TypeChecker::createDefaultDtorIfRequired(const Struct &spiceStruct, Scope *structScope) const { | |
| 201 | 674 | const ASTNode *node = spiceStruct.declNode; | |
| 202 |
2/4✓ Branch 2 → 3 taken 674 times.
✗ Branch 2 → 5 not taken.
✓ Branch 3 → 4 taken 674 times.
✗ Branch 3 → 5 not taken.
|
674 | assert(structScope != nullptr && structScope->type == ScopeType::STRUCT); |
| 203 | |||
| 204 | // Abort if the struct already has a user-defined destructor | ||
| 205 | 674 | const SymbolTableEntry *structEntry = spiceStruct.entry; | |
| 206 |
1/2✓ Branch 6 → 7 taken 674 times.
✗ Branch 6 → 143 not taken.
|
674 | const QualType &structType = structEntry->getQualType(); |
| 207 |
3/6✓ Branch 7 → 8 taken 674 times.
✗ Branch 7 → 98 not taken.
✓ Branch 8 → 9 taken 674 times.
✗ Branch 8 → 98 not taken.
✓ Branch 9 → 10 taken 674 times.
✗ Branch 9 → 96 not taken.
|
674 | const std::string fqFctName = structType.getSubType() + MEMBER_ACCESS_TOKEN + DTOR_FUNCTION_NAME; |
| 208 |
3/4✓ Branch 11 → 12 taken 674 times.
✗ Branch 11 → 141 not taken.
✓ Branch 12 → 13 taken 110 times.
✓ Branch 12 → 14 taken 564 times.
|
674 | if (sourceFile->getNameRegistryEntry(fqFctName)) |
| 209 | 110 | return; | |
| 210 | |||
| 211 | // Check we have field types, that require use to do anything in the destructor | ||
| 212 |
1/2✓ Branch 14 → 15 taken 564 times.
✗ Branch 14 → 141 not taken.
|
564 | const size_t fieldCount = structScope->getFieldCount(); |
| 213 | 564 | bool hasFieldsToDeAllocate = false; | |
| 214 | 564 | bool hasFieldsToDestruct = false; | |
| 215 |
2/2✓ Branch 41 → 16 taken 1292 times.
✓ Branch 41 → 42 taken 564 times.
|
1856 | for (size_t i = 0; i < fieldCount; i++) { |
| 216 |
1/2✗ Branch 16 → 17 not taken.
✓ Branch 16 → 18 taken 1292 times.
|
1292 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(i); |
| 217 |
2/4✓ Branch 21 → 22 taken 1292 times.
✗ Branch 21 → 141 not taken.
✓ Branch 22 → 23 taken 1292 times.
✗ Branch 22 → 141 not taken.
|
1292 | hasFieldsToDeAllocate |= fieldSymbol->getQualType().needsDeAllocation(); |
| 218 |
4/6✓ Branch 23 → 24 taken 1292 times.
✗ Branch 23 → 141 not taken.
✓ Branch 24 → 25 taken 1292 times.
✗ Branch 24 → 141 not taken.
✓ Branch 25 → 26 taken 162 times.
✓ Branch 25 → 40 taken 1130 times.
|
1292 | if (fieldSymbol->getQualType().is(TY_STRUCT)) { |
| 219 |
2/4✓ Branch 26 → 27 taken 162 times.
✗ Branch 26 → 141 not taken.
✓ Branch 27 → 28 taken 162 times.
✗ Branch 27 → 141 not taken.
|
162 | Scope *fieldScope = fieldSymbol->getQualType().getBodyScope(); |
| 220 | // Lookup dtor function | ||
| 221 |
1/2✓ Branch 28 → 29 taken 162 times.
✗ Branch 28 → 141 not taken.
|
162 | const QualType &thisType = fieldSymbol->getQualType(); |
| 222 |
2/4✓ Branch 33 → 34 taken 162 times.
✗ Branch 33 → 101 not taken.
✓ Branch 34 → 35 taken 162 times.
✗ Branch 34 → 99 not taken.
|
486 | const Function *dtorFct = FunctionManager::match(fieldScope, DTOR_FUNCTION_NAME, thisType, {}, {}, true, node); |
| 223 | 162 | hasFieldsToDestruct |= dtorFct != nullptr; | |
| 224 |
1/2✓ Branch 39 → 40 taken 162 times.
✗ Branch 39 → 141 not taken.
|
162 | requestRevisitIfRequired(dtorFct); |
| 225 | } | ||
| 226 | } | ||
| 227 | |||
| 228 | // If we don't have any fields, that require us to do anything in the dtor, we can skip it | ||
| 229 |
4/4✓ Branch 42 → 43 taken 505 times.
✓ Branch 42 → 45 taken 59 times.
✓ Branch 43 → 44 taken 461 times.
✓ Branch 43 → 45 taken 44 times.
|
564 | if (!hasFieldsToDeAllocate && !hasFieldsToDestruct) |
| 230 | 461 | return; | |
| 231 | |||
| 232 | // Create the default dtor function | ||
| 233 |
1/2✓ Branch 45 → 46 taken 103 times.
✗ Branch 45 → 141 not taken.
|
103 | const std::string entryName = Function::getSymbolTableEntryNameDefaultDtor(node->codeLoc); |
| 234 |
2/4✓ Branch 49 → 50 taken 103 times.
✗ Branch 49 → 113 not taken.
✓ Branch 50 → 51 taken 103 times.
✗ Branch 50 → 111 not taken.
|
309 | createDefaultStructMethod(spiceStruct, entryName, DTOR_FUNCTION_NAME, {}); |
| 235 | |||
| 236 | // Request memory runtime if we have fields, that are allocated on the heap | ||
| 237 | // The string runtime does not use it, but allocates manually to avoid circular dependencies | ||
| 238 |
6/8✓ Branch 54 → 55 taken 59 times.
✓ Branch 54 → 60 taken 44 times.
✓ Branch 55 → 56 taken 59 times.
✗ Branch 55 → 139 not taken.
✓ Branch 58 → 59 taken 59 times.
✗ Branch 58 → 60 not taken.
✓ Branch 61 → 62 taken 59 times.
✓ Branch 61 → 88 taken 44 times.
|
162 | if (hasFieldsToDeAllocate && !sourceFile->isStringRT()) { |
| 239 |
1/2✓ Branch 62 → 63 taken 59 times.
✗ Branch 62 → 138 not taken.
|
59 | const SourceFile *memoryRT = sourceFile->requestRuntimeModule(MEMORY_RT); |
| 240 |
1/2✗ Branch 63 → 64 not taken.
✓ Branch 63 → 65 taken 59 times.
|
59 | assert(memoryRT != nullptr); |
| 241 | 59 | Scope *matchScope = memoryRT->globalScope.get(); | |
| 242 | // Set dealloc function to used | ||
| 243 |
1/2✓ Branch 66 → 67 taken 59 times.
✗ Branch 66 → 138 not taken.
|
59 | const QualType thisType(TY_DYN); |
| 244 |
3/6✓ Branch 67 → 68 taken 59 times.
✗ Branch 67 → 120 not taken.
✓ Branch 68 → 69 taken 59 times.
✗ Branch 68 → 120 not taken.
✓ Branch 69 → 70 taken 59 times.
✗ Branch 69 → 120 not taken.
|
59 | QualType bytePtrRefType = QualType(TY_BYTE).toPtr(node).toRef(node); |
| 245 |
1/2✓ Branch 70 → 71 taken 59 times.
✗ Branch 70 → 138 not taken.
|
59 | bytePtrRefType.makeHeap(); |
| 246 |
1/2✓ Branch 74 → 75 taken 59 times.
✗ Branch 74 → 122 not taken.
|
118 | const ArgList args = {{bytePtrRefType, false /* we always have the field as storage */}}; |
| 247 |
2/4✓ Branch 79 → 80 taken 59 times.
✗ Branch 79 → 129 not taken.
✓ Branch 80 → 81 taken 59 times.
✗ Branch 80 → 127 not taken.
|
177 | Function *deallocFct = FunctionManager::match(matchScope, FCT_NAME_DEALLOC, thisType, args, {}, true, node); |
| 248 |
1/2✗ Branch 84 → 85 not taken.
✓ Branch 84 → 86 taken 59 times.
|
59 | assert(deallocFct != nullptr); |
| 249 | 59 | deallocFct->used = true; | |
| 250 | 59 | } | |
| 251 |
2/2✓ Branch 91 → 92 taken 103 times.
✓ Branch 91 → 94 taken 571 times.
|
674 | } |
| 252 | |||
| 253 | /** | ||
| 254 | * Prepare the generation of the ctor body preamble. This preamble is used to initialize the VTable, construct or initialize | ||
| 255 | * fields. | ||
| 256 | */ | ||
| 257 | 1378 | void TypeChecker::createCtorBodyPreamble(const Scope *bodyScope) const { | |
| 258 | // Retrieve struct scope | ||
| 259 | 1378 | Scope *structScope = bodyScope->parent; | |
| 260 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 1378 times.
|
1378 | assert(structScope != nullptr); |
| 261 | |||
| 262 | 1378 | const size_t fieldCount = structScope->getFieldCount(); | |
| 263 |
2/2✓ Branch 49 → 6 taken 3478 times.
✓ Branch 49 → 50 taken 1378 times.
|
4856 | for (size_t i = 0; i < fieldCount; i++) { |
| 264 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 3478 times.
|
3478 | SymbolTableEntry *fieldSymbol = structScope->lookupField(i); |
| 265 |
3/6✓ Branch 11 → 12 taken 3478 times.
✗ Branch 11 → 15 not taken.
✓ Branch 12 → 13 taken 3478 times.
✗ Branch 12 → 64 not taken.
✓ Branch 13 → 14 taken 3478 times.
✗ Branch 13 → 15 not taken.
|
3478 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 266 |
2/2✓ Branch 16 → 17 taken 235 times.
✓ Branch 16 → 18 taken 3243 times.
|
3478 | if (fieldSymbol->isImplicitField) |
| 267 | 235 | continue; | |
| 268 |
1/2✓ Branch 18 → 19 taken 3243 times.
✗ Branch 18 → 64 not taken.
|
3243 | QualType fieldType = fieldSymbol->getQualType(); |
| 269 |
3/4✓ Branch 19 → 20 taken 3243 times.
✗ Branch 19 → 64 not taken.
✓ Branch 20 → 21 taken 502 times.
✓ Branch 20 → 22 taken 2741 times.
|
3243 | if (fieldType.hasAnyGenericParts()) |
| 270 |
1/2✓ Branch 21 → 22 taken 502 times.
✗ Branch 21 → 64 not taken.
|
502 | TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, fieldSymbol->declNode); |
| 271 | |||
| 272 |
3/4✓ Branch 22 → 23 taken 3243 times.
✗ Branch 22 → 64 not taken.
✓ Branch 23 → 24 taken 439 times.
✓ Branch 23 → 47 taken 2804 times.
|
3243 | if (fieldType.is(TY_STRUCT)) { |
| 273 |
1/2✓ Branch 24 → 25 taken 439 times.
✗ Branch 24 → 26 not taken.
|
439 | const auto fieldNode = spice_pointer_cast<const FieldNode *>(fieldSymbol->declNode); |
| 274 | // Match ctor function, create the concrete manifestation and set it to used | ||
| 275 |
1/2✓ Branch 31 → 32 taken 439 times.
✗ Branch 31 → 64 not taken.
|
439 | Scope *matchScope = fieldType.getBodyScope(); |
| 276 | const Function *spiceFunc = | ||
| 277 |
2/4✓ Branch 36 → 37 taken 439 times.
✗ Branch 36 → 53 not taken.
✓ Branch 37 → 38 taken 439 times.
✗ Branch 37 → 51 not taken.
|
1317 | FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, fieldType, {}, {}, false, fieldNode); |
| 278 |
2/2✓ Branch 42 → 43 taken 388 times.
✓ Branch 42 → 47 taken 51 times.
|
439 | if (spiceFunc != nullptr) |
| 279 |
3/6✓ Branch 43 → 44 taken 388 times.
✗ Branch 43 → 63 not taken.
✓ Branch 44 → 45 taken 388 times.
✗ Branch 44 → 63 not taken.
✓ Branch 45 → 46 taken 388 times.
✗ Branch 45 → 63 not taken.
|
388 | fieldSymbol->updateType(fieldType.getWithBodyScope(spiceFunc->thisType.getBodyScope()), true); |
| 280 | } | ||
| 281 | } | ||
| 282 | 1378 | } | |
| 283 | |||
| 284 | /** | ||
| 285 | * Prepare the generation of the copy ctor body preamble. This preamble is used to initialize the VTable, construct or initialize | ||
| 286 | * fields. | ||
| 287 | */ | ||
| 288 | 139 | void TypeChecker::createCopyCtorBodyPreamble(const Scope *bodyScope) const { | |
| 289 | // Retrieve struct scope | ||
| 290 | 139 | Scope *structScope = bodyScope->parent; | |
| 291 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 139 times.
|
139 | assert(structScope != nullptr); |
| 292 | |||
| 293 | 139 | const size_t fieldCount = structScope->getFieldCount(); | |
| 294 |
2/2✓ Branch 55 → 6 taken 294 times.
✓ Branch 55 → 56 taken 139 times.
|
433 | for (size_t i = 0; i < fieldCount; i++) { |
| 295 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 294 times.
|
294 | SymbolTableEntry *fieldSymbol = structScope->lookupField(i); |
| 296 |
3/6✓ Branch 11 → 12 taken 294 times.
✗ Branch 11 → 15 not taken.
✓ Branch 12 → 13 taken 294 times.
✗ Branch 12 → 76 not taken.
✓ Branch 13 → 14 taken 294 times.
✗ Branch 13 → 15 not taken.
|
294 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 297 |
2/2✓ Branch 16 → 17 taken 33 times.
✓ Branch 16 → 18 taken 261 times.
|
294 | if (fieldSymbol->isImplicitField) |
| 298 | 33 | continue; | |
| 299 | |||
| 300 |
1/2✓ Branch 18 → 19 taken 261 times.
✗ Branch 18 → 76 not taken.
|
261 | QualType fieldType = fieldSymbol->getQualType(); |
| 301 |
2/4✓ Branch 19 → 20 taken 261 times.
✗ Branch 19 → 76 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 261 times.
|
261 | if (fieldType.hasAnyGenericParts()) |
| 302 | ✗ | TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, fieldSymbol->declNode); | |
| 303 | |||
| 304 |
3/4✓ Branch 22 → 23 taken 261 times.
✗ Branch 22 → 76 not taken.
✓ Branch 23 → 24 taken 117 times.
✓ Branch 23 → 53 taken 144 times.
|
261 | if (fieldType.is(TY_STRUCT)) { |
| 305 |
1/2✓ Branch 24 → 25 taken 117 times.
✗ Branch 24 → 26 not taken.
|
117 | const auto fieldNode = spice_pointer_cast<const FieldNode *>(fieldSymbol->declNode); |
| 306 | // Match ctor function, create the concrete manifestation and set it to used | ||
| 307 |
1/2✓ Branch 31 → 32 taken 117 times.
✗ Branch 31 → 75 not taken.
|
117 | Scope *matchScope = fieldType.getBodyScope(); |
| 308 |
2/4✓ Branch 32 → 33 taken 117 times.
✗ Branch 32 → 61 not taken.
✓ Branch 36 → 37 taken 117 times.
✗ Branch 36 → 57 not taken.
|
234 | const ArgList args = {{fieldType.toConstRef(fieldNode), false /* we always have the field as storage */}}; |
| 309 | const Function *copyCtorFct = | ||
| 310 |
2/4✓ Branch 41 → 42 taken 117 times.
✗ Branch 41 → 65 not taken.
✓ Branch 42 → 43 taken 117 times.
✗ Branch 42 → 63 not taken.
|
351 | FunctionManager::match(matchScope, CTOR_FUNCTION_NAME, fieldType, args, {}, false, fieldNode); |
| 311 |
2/2✓ Branch 46 → 47 taken 106 times.
✓ Branch 46 → 51 taken 11 times.
|
117 | if (copyCtorFct != nullptr) |
| 312 |
3/6✓ Branch 47 → 48 taken 106 times.
✗ Branch 47 → 72 not taken.
✓ Branch 48 → 49 taken 106 times.
✗ Branch 48 → 72 not taken.
✓ Branch 49 → 50 taken 106 times.
✗ Branch 49 → 72 not taken.
|
106 | fieldSymbol->updateType(fieldType.getWithBodyScope(copyCtorFct->thisType.getBodyScope()), true); |
| 313 | 117 | } | |
| 314 | } | ||
| 315 | 139 | } | |
| 316 | |||
| 317 | /** | ||
| 318 | * Prepare the generation of the dtor body preamble. This preamble is used to destruct all fields and to free all heap fields. | ||
| 319 | */ | ||
| 320 | 223 | void TypeChecker::createDtorBodyPreamble(const Scope *bodyScope) const { | |
| 321 | // Retrieve struct scope | ||
| 322 | 223 | Scope *structScope = bodyScope->parent; | |
| 323 |
1/2✗ Branch 2 → 3 not taken.
✓ Branch 2 → 4 taken 223 times.
|
223 | assert(structScope != nullptr); |
| 324 | |||
| 325 | 223 | const size_t fieldCount = structScope->getFieldCount(); | |
| 326 |
2/2✓ Branch 45 → 6 taken 781 times.
✓ Branch 45 → 46 taken 223 times.
|
1004 | for (size_t i = 0; i < fieldCount; i++) { |
| 327 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 781 times.
|
781 | const SymbolTableEntry *fieldSymbol = structScope->lookupField(i); |
| 328 |
3/6✓ Branch 11 → 12 taken 781 times.
✗ Branch 11 → 15 not taken.
✓ Branch 12 → 13 taken 781 times.
✗ Branch 12 → 59 not taken.
✓ Branch 13 → 14 taken 781 times.
✗ Branch 13 → 15 not taken.
|
781 | assert(fieldSymbol != nullptr && fieldSymbol->isField()); |
| 329 |
2/2✓ Branch 16 → 17 taken 142 times.
✓ Branch 16 → 18 taken 639 times.
|
781 | if (fieldSymbol->isImplicitField) |
| 330 | 142 | continue; | |
| 331 |
1/2✓ Branch 18 → 19 taken 639 times.
✗ Branch 18 → 59 not taken.
|
639 | QualType fieldType = fieldSymbol->getQualType(); |
| 332 |
2/4✓ Branch 19 → 20 taken 639 times.
✗ Branch 19 → 59 not taken.
✗ Branch 20 → 21 not taken.
✓ Branch 20 → 22 taken 639 times.
|
639 | if (fieldType.hasAnyGenericParts()) |
| 333 | ✗ | TypeMatcher::substantiateTypeWithTypeMapping(fieldType, typeMapping, fieldSymbol->declNode); | |
| 334 | |||
| 335 |
3/4✓ Branch 22 → 23 taken 639 times.
✗ Branch 22 → 59 not taken.
✓ Branch 23 → 24 taken 159 times.
✓ Branch 23 → 43 taken 480 times.
|
639 | if (fieldType.is(TY_STRUCT)) { |
| 336 |
1/2✓ Branch 24 → 25 taken 159 times.
✗ Branch 24 → 26 not taken.
|
159 | const auto fieldNode = spice_pointer_cast<const FieldNode *>(fieldSymbol->declNode); |
| 337 | // Match ctor function, create the concrete manifestation and set it to used | ||
| 338 |
1/2✓ Branch 31 → 32 taken 159 times.
✗ Branch 31 → 59 not taken.
|
159 | Scope *matchScope = fieldType.getBodyScope(); |
| 339 |
2/4✓ Branch 36 → 37 taken 159 times.
✗ Branch 36 → 49 not taken.
✓ Branch 37 → 38 taken 159 times.
✗ Branch 37 → 47 not taken.
|
477 | FunctionManager::match(matchScope, DTOR_FUNCTION_NAME, fieldType, {}, {}, false, fieldNode); |
| 340 | } | ||
| 341 | } | ||
| 342 | 223 | } | |
| 343 | |||
| 344 | /** | ||
| 345 | * Prepare the generation of a call to a method of a given struct | ||
| 346 | * | ||
| 347 | * @param entry Symbol entry to use as 'this' pointer for the method call | ||
| 348 | * @param methodName Name of the method to call | ||
| 349 | * @param args Provided arguments by the caller | ||
| 350 | * @param node AST node | ||
| 351 | */ | ||
| 352 | 1376 | Function *TypeChecker::implicitlyCallStructMethod(const SymbolTableEntry *entry, const std::string &methodName, | |
| 353 | const ArgList &args, const ASTNode *node) { | ||
| 354 |
3/6✓ Branch 2 → 3 taken 1376 times.
✗ Branch 2 → 9 not taken.
✓ Branch 3 → 4 taken 1376 times.
✗ Branch 3 → 9 not taken.
✓ Branch 4 → 5 taken 1376 times.
✗ Branch 4 → 9 not taken.
|
1376 | const QualType thisType = entry->getQualType().removeReferenceWrapper().toNonConst(); |
| 355 |
1/2✓ Branch 5 → 6 taken 1376 times.
✗ Branch 5 → 10 not taken.
|
2752 | return implicitlyCallStructMethod(thisType, methodName, args, node); |
| 356 | } | ||
| 357 | |||
| 358 | /** | ||
| 359 | * Prepare the generation of a call to a method of a given struct | ||
| 360 | * | ||
| 361 | * @param thisType Struct type to call the method on | ||
| 362 | * @param methodName Name of the method to call | ||
| 363 | * @param args Provided arguments by the caller | ||
| 364 | * @param node AST node | ||
| 365 | */ | ||
| 366 | 1624 | Function *TypeChecker::implicitlyCallStructMethod(QualType thisType, const std::string &methodName, const ArgList &args, | |
| 367 | const ASTNode *node) const { | ||
| 368 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1624 times.
|
1624 | assert(thisType.is(TY_STRUCT)); |
| 369 | 1624 | Scope *matchScope = thisType.getBodyScope(); | |
| 370 |
1/2✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 1624 times.
|
1624 | assert(matchScope->type == ScopeType::STRUCT); |
| 371 | |||
| 372 | // Search for dtor | ||
| 373 |
2/2✓ Branch 9 → 10 taken 882 times.
✓ Branch 9 → 12 taken 742 times.
|
1624 | if (matchScope->isImportedBy(rootScope)) |
| 374 |
1/2✓ Branch 10 → 11 taken 882 times.
✗ Branch 10 → 18 not taken.
|
882 | thisType = mapLocalTypeToImportedScopeType(matchScope, thisType); |
| 375 |
1/2✓ Branch 13 → 14 taken 1624 times.
✗ Branch 13 → 19 not taken.
|
1624 | return FunctionManager::match(matchScope, methodName, thisType, args, {}, true, node); |
| 376 | } | ||
| 377 | |||
| 378 | /** | ||
| 379 | * Prepare the generation of a call to the copy ctor of a given struct | ||
| 380 | * | ||
| 381 | * @param entry Symbol entry to use as 'this' pointer for the copy ctor call | ||
| 382 | * @param node Current AST node | ||
| 383 | */ | ||
| 384 | 5 | Function *TypeChecker::implicitlyCallStructCopyCtor(const SymbolTableEntry *entry, const ASTNode *node) const { | |
| 385 |
2/4✓ Branch 2 → 3 taken 5 times.
✗ Branch 2 → 7 not taken.
✓ Branch 5 → 6 taken 5 times.
✗ Branch 5 → 7 not taken.
|
5 | assert(entry != nullptr && entry->getQualType().is(TY_STRUCT)); |
| 386 | 5 | return implicitlyCallStructCopyCtor(entry->getQualType(), node); | |
| 387 | } | ||
| 388 | |||
| 389 | /** | ||
| 390 | * Prepare the generation of a call to the copy ctor of a given struct | ||
| 391 | * | ||
| 392 | * @param thisType Struct type to call the copy ctor on | ||
| 393 | * @param node Current AST node | ||
| 394 | */ | ||
| 395 | 248 | Function *TypeChecker::implicitlyCallStructCopyCtor(const QualType &thisType, const ASTNode *node) const { | |
| 396 |
2/4✓ Branch 2 → 3 taken 248 times.
✗ Branch 2 → 19 not taken.
✓ Branch 3 → 4 taken 248 times.
✗ Branch 3 → 19 not taken.
|
248 | const QualType argType = thisType.removeReferenceWrapper().toConstRef(node); |
| 397 |
1/2✓ Branch 7 → 8 taken 248 times.
✗ Branch 7 → 20 not taken.
|
744 | const ArgList args = {{argType, false /* we always have an entry here */}}; |
| 398 |
2/4✓ Branch 11 → 12 taken 248 times.
✗ Branch 11 → 27 not taken.
✓ Branch 12 → 13 taken 248 times.
✗ Branch 12 → 25 not taken.
|
496 | return implicitlyCallStructMethod(thisType, CTOR_FUNCTION_NAME, args, node); |
| 399 | 248 | } | |
| 400 | |||
| 401 | /** | ||
| 402 | * Prepare the generation of a call to the dtor of a given struct | ||
| 403 | * | ||
| 404 | * @param entry Symbol entry to use as 'this' pointer for the dtor call | ||
| 405 | * @param node StmtLstNode for the current scope | ||
| 406 | */ | ||
| 407 | 1376 | void TypeChecker::implicitlyCallStructDtor(SymbolTableEntry *entry, StmtLstNode *node) { | |
| 408 | // Add the dtor to the stmt list node to call it later in codegen | ||
| 409 |
4/6✓ Branch 5 → 6 taken 1376 times.
✗ Branch 5 → 16 not taken.
✓ Branch 6 → 7 taken 1376 times.
✗ Branch 6 → 14 not taken.
✓ Branch 10 → 11 taken 1007 times.
✓ Branch 10 → 13 taken 369 times.
|
4128 | if (Function *dtor = implicitlyCallStructMethod(entry, DTOR_FUNCTION_NAME, {}, node)) |
| 410 |
2/4✓ Branch 11 → 12 taken 1007 times.
✗ Branch 11 → 23 not taken.
✓ Branch 12 → 13 taken 1007 times.
✗ Branch 12 → 23 not taken.
|
1007 | node->resourcesToCleanup.at(manIdx).dtorFunctionsToCall.emplace_back(entry, dtor); |
| 411 | 1376 | } | |
| 412 | |||
| 413 | /** | ||
| 414 | * Prepare the generation of a call to the deallocate function for a heap-allocated variable | ||
| 415 | * | ||
| 416 | * @param node Current AST node for error messages | ||
| 417 | */ | ||
| 418 | 4 | void TypeChecker::implicitlyCallDeallocate(const ASTNode *node) const { | |
| 419 |
1/2✓ Branch 2 → 3 taken 4 times.
✗ Branch 2 → 46 not taken.
|
4 | const SourceFile *memoryRT = sourceFile->requestRuntimeModule(MEMORY_RT); |
| 420 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4 times.
|
4 | assert(memoryRT != nullptr); |
| 421 | 4 | Scope *matchScope = memoryRT->globalScope.get(); | |
| 422 | // Set dealloc function to used | ||
| 423 |
1/2✓ Branch 6 → 7 taken 4 times.
✗ Branch 6 → 46 not taken.
|
4 | const QualType thisType(TY_DYN); |
| 424 |
3/6✓ Branch 7 → 8 taken 4 times.
✗ Branch 7 → 28 not taken.
✓ Branch 8 → 9 taken 4 times.
✗ Branch 8 → 28 not taken.
✓ Branch 9 → 10 taken 4 times.
✗ Branch 9 → 28 not taken.
|
4 | QualType bytePtrRefType = QualType(TY_BYTE).toPtr(node).toRef(node); |
| 425 |
1/2✓ Branch 10 → 11 taken 4 times.
✗ Branch 10 → 46 not taken.
|
4 | bytePtrRefType.makeHeap(); |
| 426 |
1/2✓ Branch 14 → 15 taken 4 times.
✗ Branch 14 → 30 not taken.
|
8 | const ArgList args = {{bytePtrRefType, false /* we always have the field as storage */}}; |
| 427 |
2/4✓ Branch 19 → 20 taken 4 times.
✗ Branch 19 → 37 not taken.
✓ Branch 20 → 21 taken 4 times.
✗ Branch 20 → 35 not taken.
|
12 | Function *deallocFct = FunctionManager::match(matchScope, FCT_NAME_DEALLOC, thisType, args, {}, true, node); |
| 428 |
1/2✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 4 times.
|
4 | assert(deallocFct != nullptr); |
| 429 | 4 | deallocFct->used = true; | |
| 430 | 4 | } | |
| 431 | |||
| 432 | /** | ||
| 433 | * Consider calls to destructors for the given scope | ||
| 434 | * | ||
| 435 | * @param node StmtLstNode for the current scope | ||
| 436 | */ | ||
| 437 | 19354 | void TypeChecker::doScopeCleanup(StmtLstNode *node) { | |
| 438 | // Get all variables, that are approved for de-allocation | ||
| 439 |
1/2✓ Branch 2 → 3 taken 19354 times.
✗ Branch 2 → 61 not taken.
|
19354 | std::vector<SymbolTableEntry *> vars = currentScope->getVarsGoingOutOfScope(); |
| 440 | // Sort by reverse declaration order | ||
| 441 |
2/2✓ Branch 2 → 3 taken 142 times.
✓ Branch 2 → 4 taken 6014 times.
|
12312 | auto comp = [](const SymbolTableEntry *a, const SymbolTableEntry *b) { return a->declNode->codeLoc > b->declNode->codeLoc; }; |
| 442 |
1/2✓ Branch 3 → 4 taken 19354 times.
✗ Branch 3 → 59 not taken.
|
19354 | std::ranges::sort(vars, comp); |
| 443 | // Call the dtor of each variable. We call the dtor in reverse declaration order | ||
| 444 |
2/2✓ Branch 54 → 6 taken 7883 times.
✓ Branch 54 → 55 taken 19354 times.
|
27237 | for (SymbolTableEntry *var : vars) { |
| 445 | // Check if we have a heap-allocated pointer | ||
| 446 |
10/14✓ Branch 7 → 8 taken 7883 times.
✗ Branch 7 → 57 not taken.
✓ Branch 8 → 9 taken 7883 times.
✗ Branch 8 → 57 not taken.
✓ Branch 9 → 10 taken 1090 times.
✓ Branch 9 → 14 taken 6793 times.
✓ Branch 10 → 11 taken 1090 times.
✗ Branch 10 → 57 not taken.
✓ Branch 11 → 12 taken 1090 times.
✗ Branch 11 → 57 not taken.
✓ Branch 12 → 13 taken 1061 times.
✓ Branch 12 → 14 taken 29 times.
✓ Branch 15 → 16 taken 1061 times.
✓ Branch 15 → 35 taken 6822 times.
|
7883 | if (var->getQualType().isHeap() && var->getQualType().isOneOf({TY_PTR, TY_STRING, TY_FUNCTION, TY_PROCEDURE})) { |
| 447 | // The memory runtime is ignored, because it manually allocates to avoid circular dependencies. | ||
| 448 | // Same goes for the string runtime. | ||
| 449 |
8/10✓ Branch 16 → 17 taken 1061 times.
✗ Branch 16 → 58 not taken.
✓ Branch 19 → 20 taken 913 times.
✓ Branch 19 → 24 taken 148 times.
✓ Branch 20 → 21 taken 913 times.
✗ Branch 20 → 58 not taken.
✓ Branch 23 → 24 taken 870 times.
✓ Branch 23 → 25 taken 43 times.
✓ Branch 26 → 27 taken 1018 times.
✓ Branch 26 → 28 taken 43 times.
|
3035 | if (sourceFile->isMemoryRT() || sourceFile->isStringRT()) |
| 450 | 1018 | continue; | |
| 451 | // If the local variable currently does not have the ownership, we must not deallocate its memory | ||
| 452 |
3/4✓ Branch 29 → 30 taken 43 times.
✗ Branch 29 → 58 not taken.
✓ Branch 30 → 31 taken 39 times.
✓ Branch 30 → 32 taken 4 times.
|
43 | if (!var->getLifecycle().isInOwningState()) |
| 453 | 39 | continue; | |
| 454 | |||
| 455 |
1/2✓ Branch 32 → 33 taken 4 times.
✗ Branch 32 → 58 not taken.
|
4 | implicitlyCallDeallocate(node); // Required to request the memory runtime |
| 456 |
2/4✓ Branch 33 → 34 taken 4 times.
✗ Branch 33 → 58 not taken.
✓ Branch 34 → 35 taken 4 times.
✗ Branch 34 → 58 not taken.
|
4 | node->resourcesToCleanup.at(manIdx).heapVarsToFree.push_back(var); |
| 457 | } | ||
| 458 | // Only generate dtor call for structs and if not omitted | ||
| 459 |
8/10✓ Branch 35 → 36 taken 6826 times.
✗ Branch 35 → 58 not taken.
✓ Branch 36 → 37 taken 6826 times.
✗ Branch 36 → 58 not taken.
✓ Branch 37 → 38 taken 1480 times.
✓ Branch 37 → 39 taken 5346 times.
✓ Branch 38 → 39 taken 104 times.
✓ Branch 38 → 40 taken 1376 times.
✓ Branch 41 → 42 taken 5450 times.
✓ Branch 41 → 43 taken 1376 times.
|
6826 | if (!var->getQualType().is(TY_STRUCT) || var->omitDtorCall) |
| 460 | 5450 | continue; | |
| 461 | // Variable must be either initialized or a struct field | ||
| 462 |
3/8✓ Branch 44 → 45 taken 1376 times.
✗ Branch 44 → 58 not taken.
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 48 taken 1376 times.
✗ Branch 46 → 47 not taken.
✗ Branch 46 → 48 not taken.
✗ Branch 49 → 50 not taken.
✓ Branch 49 → 51 taken 1376 times.
|
1376 | if (!var->getLifecycle().isInitialized() && var->scope->type != ScopeType::STRUCT) |
| 463 | ✗ | continue; | |
| 464 | // Call dtor | ||
| 465 |
1/2✓ Branch 51 → 52 taken 1376 times.
✗ Branch 51 → 58 not taken.
|
1376 | implicitlyCallStructDtor(var, node); |
| 466 | } | ||
| 467 | 19354 | } | |
| 468 | |||
| 469 | } // namespace spice::compiler | ||
| 470 |