src/symboltablebuilder/Type.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "Type.h" | ||
| 4 | |||
| 5 | #include <utility> | ||
| 6 | |||
| 7 | #include <SourceFile.h> | ||
| 8 | #include <ast/Attributes.h> | ||
| 9 | #include <driver/Driver.h> | ||
| 10 | #include <exception/CompilerError.h> | ||
| 11 | #include <exception/SemanticError.h> | ||
| 12 | #include <global/GlobalResourceManager.h> | ||
| 13 | #include <global/TypeRegistry.h> | ||
| 14 | #include <irgenerator/NameMangling.h> | ||
| 15 | #include <model/Struct.h> | ||
| 16 | #include <symboltablebuilder/Scope.h> | ||
| 17 | #include <symboltablebuilder/SymbolTableEntry.h> | ||
| 18 | |||
| 19 | #include <llvm/IR/Module.h> | ||
| 20 | #include <llvm/IR/Type.h> | ||
| 21 | |||
| 22 | namespace spice::compiler { | ||
| 23 | |||
| 24 |
3/10✓ Branch 5 → 6 taken 1451577 times.
✗ Branch 5 → 11 not taken.
✓ Branch 7 → 8 taken 1451577 times.
✓ Branch 7 → 9 taken 1451577 times.
✗ Branch 12 → 13 not taken.
✗ Branch 12 → 14 not taken.
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 19 not taken.
✗ Branch 17 → 18 not taken.
✗ Branch 17 → 19 not taken.
|
4354731 | Type::Type(SuperType superType) : typeChain({TypeChainElement{superType}}) {} |
| 25 | |||
| 26 |
4/12✓ Branch 4 → 5 taken 1872 times.
✗ Branch 4 → 19 not taken.
✓ Branch 6 → 7 taken 1872 times.
✗ Branch 6 → 13 not taken.
✓ Branch 8 → 9 taken 1872 times.
✓ Branch 8 → 10 taken 1872 times.
✗ Branch 14 → 15 not taken.
✗ Branch 14 → 16 not taken.
✗ Branch 20 → 21 not taken.
✗ Branch 20 → 24 not taken.
✗ Branch 22 → 23 not taken.
✗ Branch 22 → 24 not taken.
|
5616 | Type::Type(SuperType superType, const std::string &subType) : typeChain({TypeChainElement{superType, subType}}) {} |
| 27 | |||
| 28 | 1265 | Type::Type(SuperType superType, const std::string &subType, uint64_t typeId, const TypeChainElementData &data, | |
| 29 | 1265 | const QualTypeList &templateTypes) | |
| 30 |
5/14✓ Branch 4 → 5 taken 1265 times.
✗ Branch 4 → 24 not taken.
✓ Branch 5 → 6 taken 1265 times.
✗ Branch 5 → 21 not taken.
✓ Branch 7 → 8 taken 1265 times.
✗ Branch 7 → 15 not taken.
✓ Branch 9 → 10 taken 1265 times.
✓ Branch 9 → 11 taken 1265 times.
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ Branch 25 → 26 not taken.
✗ Branch 25 → 29 not taken.
✗ Branch 27 → 28 not taken.
✗ Branch 27 → 29 not taken.
|
3795 | : typeChain({TypeChainElement(superType, subType, typeId, data, templateTypes)}) {} |
| 31 | |||
| 32 | 1569009 | Type::Type(TypeChain typeChain) : typeChain(std::move(typeChain)) {} | |
| 33 | |||
| 34 | /** | ||
| 35 | * Get the super type of the current type | ||
| 36 | * | ||
| 37 | * @return Super type | ||
| 38 | */ | ||
| 39 | 10551720 | SuperType Type::getSuperType() const { | |
| 40 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 10551720 times.
|
10551720 | assert(!typeChain.empty()); |
| 41 | 10551720 | return typeChain.back().superType; | |
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Get the sub type of the current type | ||
| 46 | * | ||
| 47 | * @return Sub type | ||
| 48 | */ | ||
| 49 | 324461 | const std::string &Type::getSubType() const { | |
| 50 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 324461 times.
|
324461 | assert(!typeChain.empty()); |
| 51 |
2/4✓ Branch 5 → 6 taken 324461 times.
✗ Branch 5 → 11 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 324461 times.
|
324461 | assert(isOneOf({TY_STRUCT, TY_INTERFACE, TY_ENUM, TY_GENERIC})); |
| 52 | 324461 | return typeChain.back().subType; | |
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Get the array size of the current type | ||
| 57 | * | ||
| 58 | * @return Array size | ||
| 59 | */ | ||
| 60 | 684 | unsigned int Type::getArraySize() const { | |
| 61 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 684 times.
|
684 | assert(isArray()); |
| 62 | 684 | return typeChain.back().data.arraySize; | |
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * Get the body scope of the current type | ||
| 67 | * | ||
| 68 | * @return Body scope | ||
| 69 | */ | ||
| 70 | 141168 | Scope *Type::getBodyScope() const { | |
| 71 |
2/4✓ Branch 2 → 3 taken 141168 times.
✗ Branch 2 → 8 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 141168 times.
|
141168 | assert(isOneOf({TY_STRUCT, TY_INTERFACE})); |
| 72 | 141168 | return typeChain.back().data.bodyScope; | |
| 73 | } | ||
| 74 | |||
| 75 | /** | ||
| 76 | * Get the pointer type of the current type as a new type | ||
| 77 | * | ||
| 78 | * @param node AST node for error messages | ||
| 79 | * @return Pointer type of the current type | ||
| 80 | */ | ||
| 81 | 25778 | const Type *Type::toPtr(const ASTNode *node) const { | |
| 82 | // Do not allow pointers of dyn | ||
| 83 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 12 taken 25776 times.
|
25778 | if (is(TY_DYN)) |
| 84 |
2/4✓ Branch 7 → 8 taken 2 times.
✗ Branch 7 → 32 not taken.
✓ Branch 8 → 9 taken 2 times.
✗ Branch 8 → 29 not taken.
|
6 | throw SemanticError(node, DYN_POINTERS_NOT_ALLOWED, "Just use the dyn type without '*' instead"); |
| 85 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 22 taken 25776 times.
|
25776 | if (isRef()) |
| 86 | ✗ | throw SemanticError(node, REF_POINTERS_ARE_NOT_ALLOWED, "Pointers to references are not allowed. Use pointer instead"); | |
| 87 | |||
| 88 | // Create new type chain | ||
| 89 |
1/2✓ Branch 22 → 23 taken 25776 times.
✗ Branch 22 → 50 not taken.
|
25776 | TypeChain newTypeChain = typeChain; |
| 90 |
1/2✓ Branch 23 → 24 taken 25776 times.
✗ Branch 23 → 47 not taken.
|
25776 | newTypeChain.emplace_back(TY_PTR); |
| 91 | |||
| 92 | // Register new type or return if already registered | ||
| 93 |
1/2✓ Branch 24 → 25 taken 25776 times.
✗ Branch 24 → 48 not taken.
|
51552 | return TypeRegistry::getOrInsert(newTypeChain); |
| 94 | 25776 | } | |
| 95 | |||
| 96 | /** | ||
| 97 | * Get the reference type of the current type as a new type | ||
| 98 | * | ||
| 99 | * @param node AST node for error messages | ||
| 100 | * @return Reference type of the current type | ||
| 101 | */ | ||
| 102 | 13274 | const Type *Type::toRef(const ASTNode *node) const { | |
| 103 | // Do not allow references of dyn | ||
| 104 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 12 taken 13274 times.
|
13274 | if (is(TY_DYN)) |
| 105 | ✗ | throw SemanticError(node, DYN_REFERENCES_NOT_ALLOWED, "Just use the dyn type without '&' instead"); | |
| 106 | // Do not allow references of references | ||
| 107 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 22 taken 13274 times.
|
13274 | if (isRef()) |
| 108 | ✗ | throw SemanticError(node, MULTI_REF_NOT_ALLOWED, "References to references are not allowed"); | |
| 109 | |||
| 110 | // Create new type chain | ||
| 111 |
1/2✓ Branch 22 → 23 taken 13274 times.
✗ Branch 22 → 50 not taken.
|
13274 | TypeChain newTypeChain = typeChain; |
| 112 |
1/2✓ Branch 23 → 24 taken 13274 times.
✗ Branch 23 → 47 not taken.
|
13274 | newTypeChain.emplace_back(TY_REF); |
| 113 | |||
| 114 | // Register new type or return if already registered | ||
| 115 |
1/2✓ Branch 24 → 25 taken 13274 times.
✗ Branch 24 → 48 not taken.
|
26548 | return TypeRegistry::getOrInsert(newTypeChain); |
| 116 | 13274 | } | |
| 117 | |||
| 118 | /** | ||
| 119 | * Get the array type of the current type as a new type | ||
| 120 | * | ||
| 121 | * @param node AST node for error messages | ||
| 122 | * @param size Size of the array | ||
| 123 | * @param skipDynCheck Skip check if array base type is dyn | ||
| 124 | * @return Array type of the current type | ||
| 125 | */ | ||
| 126 | 235 | const Type *Type::toArr(const ASTNode *node, unsigned int size, bool skipDynCheck) const { | |
| 127 | // Do not allow arrays of dyn | ||
| 128 |
6/6✓ Branch 2 → 3 taken 158 times.
✓ Branch 2 → 6 taken 77 times.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 157 times.
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 16 taken 234 times.
|
235 | if (!skipDynCheck && typeChain.back().superType == TY_DYN) |
| 129 |
2/4✓ Branch 11 → 12 taken 1 time.
✗ Branch 11 → 26 not taken.
✓ Branch 12 → 13 taken 1 time.
✗ Branch 12 → 23 not taken.
|
3 | throw SemanticError(node, DYN_ARRAYS_NOT_ALLOWED, "Just use the dyn type without '[]' instead"); |
| 130 | |||
| 131 | // Create new type chain | ||
| 132 |
1/2✓ Branch 16 → 17 taken 234 times.
✗ Branch 16 → 36 not taken.
|
234 | TypeChain newTypeChain = typeChain; |
| 133 |
1/2✓ Branch 17 → 18 taken 234 times.
✗ Branch 17 → 32 not taken.
|
234 | newTypeChain.emplace_back(TY_ARRAY, TypeChainElementData{.arraySize = size}); |
| 134 | |||
| 135 | // Register new type or return if already registered | ||
| 136 |
1/2✓ Branch 18 → 19 taken 234 times.
✗ Branch 18 → 34 not taken.
|
468 | return TypeRegistry::getOrInsert(newTypeChain); |
| 137 | 234 | } | |
| 138 | |||
| 139 | /** | ||
| 140 | * Retrieve the base type of an array or a pointer | ||
| 141 | * | ||
| 142 | * @return Base type | ||
| 143 | */ | ||
| 144 | 143326 | const Type *Type::getContained() const { | |
| 145 |
2/2✓ Branch 3 → 4 taken 1624 times.
✓ Branch 3 → 6 taken 141702 times.
|
143326 | if (is(TY_STRING)) |
| 146 |
1/2✓ Branch 4 → 5 taken 1624 times.
✗ Branch 4 → 18 not taken.
|
1624 | return TypeRegistry::getOrInsert(TY_CHAR); |
| 147 | |||
| 148 | // Create new type chain | ||
| 149 |
1/2✓ Branch 6 → 7 taken 141702 times.
✗ Branch 6 → 18 not taken.
|
141702 | TypeChain newTypeChain = typeChain; |
| 150 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 141702 times.
|
141702 | assert(newTypeChain.size() > 1); |
| 151 | 141702 | newTypeChain.pop_back(); | |
| 152 | |||
| 153 | // Register new type or return if already registered | ||
| 154 |
1/2✓ Branch 11 → 12 taken 141702 times.
✗ Branch 11 → 16 not taken.
|
141702 | return TypeRegistry::getOrInsert(newTypeChain); |
| 155 | 141702 | } | |
| 156 | |||
| 157 | /** | ||
| 158 | * Replace the base type with another one | ||
| 159 | * | ||
| 160 | * @param newBaseType New base type | ||
| 161 | * @return The new type | ||
| 162 | */ | ||
| 163 | 21352 | const Type *Type::replaceBase(const Type *newBaseType) const { | |
| 164 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 21352 times.
|
21352 | assert(!typeChain.empty()); |
| 165 | |||
| 166 | // Create new type | ||
| 167 |
1/2✓ Branch 5 → 6 taken 21352 times.
✗ Branch 5 → 27 not taken.
|
21352 | TypeChain newTypeChain = newBaseType->typeChain; |
| 168 |
4/4✓ Branch 7 → 8 taken 1018 times.
✓ Branch 7 → 11 taken 20334 times.
✓ Branch 9 → 10 taken 42 times.
✓ Branch 9 → 11 taken 976 times.
|
21352 | const bool doubleRef = newTypeChain.back().superType == TY_REF && typeChain.back().superType == TY_REF; |
| 169 |
2/2✓ Branch 19 → 13 taken 1825 times.
✓ Branch 19 → 20 taken 21352 times.
|
23177 | for (size_t i = 1; i < typeChain.size(); i++) |
| 170 |
3/4✓ Branch 13 → 14 taken 42 times.
✓ Branch 13 → 15 taken 1783 times.
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 17 taken 42 times.
|
1825 | if (!doubleRef || i > 1) |
| 171 |
2/4✓ Branch 15 → 16 taken 1783 times.
✗ Branch 15 → 25 not taken.
✓ Branch 16 → 17 taken 1783 times.
✗ Branch 16 → 25 not taken.
|
1783 | newTypeChain.push_back(typeChain.at(i)); |
| 172 | |||
| 173 | // Register new type or return if already registered | ||
| 174 |
1/2✓ Branch 20 → 21 taken 21352 times.
✗ Branch 20 → 25 not taken.
|
42704 | return TypeRegistry::getOrInsert(newTypeChain); |
| 175 | 21352 | } | |
| 176 | |||
| 177 | /** | ||
| 178 | * Remove reference wrapper from the current type | ||
| 179 | * | ||
| 180 | * @return Type without reference wrapper | ||
| 181 | */ | ||
| 182 |
1/2✓ Branch 3 → 4 taken 38822 times.
✗ Branch 3 → 6 not taken.
|
38822 | const Type *Type::removeReferenceWrapper() const { return isRef() ? getContained() : this; } |
| 183 | |||
| 184 | /** | ||
| 185 | * Return the LLVM type for this symbol type | ||
| 186 | * | ||
| 187 | * @param sourceFile Referenced source file | ||
| 188 | * @return Corresponding LLVM type | ||
| 189 | */ | ||
| 190 | 8663 | llvm::Type *Type::toLLVMType(SourceFile *sourceFile) const { // NOLINT(misc-no-recursion) | |
| 191 |
2/4✓ Branch 3 → 4 taken 8663 times.
✗ Branch 3 → 7 not taken.
✓ Branch 5 → 6 taken 8663 times.
✗ Branch 5 → 7 not taken.
|
8663 | assert(!typeChain.empty() && !is(TY_INVALID)); |
| 192 |
2/2✓ Branch 8 → 9 taken 2 times.
✓ Branch 8 → 10 taken 8661 times.
|
8663 | llvm::LLVMContext &context = sourceFile->cliOptions.useLTO ? sourceFile->resourceManager.ltoContext : sourceFile->context; |
| 193 | |||
| 194 |
10/12✓ Branch 11 → 12 taken 8663 times.
✗ Branch 11 → 165 not taken.
✓ Branch 12 → 13 taken 4490 times.
✓ Branch 12 → 17 taken 4173 times.
✓ Branch 14 → 15 taken 62 times.
✓ Branch 14 → 18 taken 4428 times.
✓ Branch 15 → 16 taken 62 times.
✗ Branch 15 → 165 not taken.
✓ Branch 16 → 17 taken 25 times.
✓ Branch 16 → 18 taken 37 times.
✓ Branch 19 → 20 taken 4198 times.
✓ Branch 19 → 22 taken 4465 times.
|
8663 | if (isOneOf({TY_PTR, TY_REF, TY_STRING}) || (isArray() && getArraySize() == 0)) |
| 195 | 4198 | return llvm::PointerType::get(context, 0); | |
| 196 | |||
| 197 |
2/2✓ Branch 23 → 24 taken 37 times.
✓ Branch 23 → 32 taken 4428 times.
|
4465 | if (isArray()) { |
| 198 |
1/2✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 37 times.
|
37 | assert(getArraySize() > 0); |
| 199 | 37 | llvm::Type *containedType = sourceFile->getLLVMType(getContained()); | |
| 200 | 37 | return llvm::ArrayType::get(containedType, getArraySize()); | |
| 201 | } | ||
| 202 | |||
| 203 |
1/2✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 4428 times.
|
4428 | assert(!hasAnyGenericParts()); |
| 204 | |||
| 205 |
2/2✓ Branch 36 → 37 taken 82 times.
✓ Branch 36 → 39 taken 4346 times.
|
4428 | if (is(TY_DOUBLE)) |
| 206 | 82 | return llvm::Type::getDoubleTy(context); | |
| 207 | |||
| 208 |
3/4✓ Branch 39 → 40 taken 4346 times.
✗ Branch 39 → 166 not taken.
✓ Branch 40 → 41 taken 728 times.
✓ Branch 40 → 43 taken 3618 times.
|
4346 | if (isOneOf({TY_INT, TY_ENUM})) |
| 209 | 728 | return llvm::Type::getInt32Ty(context); | |
| 210 | |||
| 211 |
2/2✓ Branch 44 → 45 taken 67 times.
✓ Branch 44 → 47 taken 3551 times.
|
3618 | if (is(TY_SHORT)) |
| 212 | 67 | return llvm::Type::getInt16Ty(context); | |
| 213 | |||
| 214 |
2/2✓ Branch 48 → 49 taken 439 times.
✓ Branch 48 → 51 taken 3112 times.
|
3551 | if (is(TY_LONG)) |
| 215 | 439 | return llvm::Type::getInt64Ty(context); | |
| 216 | |||
| 217 |
3/4✓ Branch 51 → 52 taken 3112 times.
✗ Branch 51 → 167 not taken.
✓ Branch 52 → 53 taken 402 times.
✓ Branch 52 → 55 taken 2710 times.
|
3112 | if (isOneOf({TY_CHAR, TY_BYTE})) |
| 218 | 402 | return llvm::Type::getInt8Ty(context); | |
| 219 | |||
| 220 |
2/2✓ Branch 56 → 57 taken 513 times.
✓ Branch 56 → 59 taken 2197 times.
|
2710 | if (is(TY_BOOL)) |
| 221 | 513 | return llvm::Type::getInt1Ty(context); | |
| 222 | |||
| 223 |
3/4✓ Branch 59 → 60 taken 2197 times.
✗ Branch 59 → 168 not taken.
✓ Branch 60 → 61 taken 2112 times.
✓ Branch 60 → 150 taken 85 times.
|
2197 | if (isOneOf({TY_STRUCT, TY_INTERFACE})) { |
| 224 |
1/2✓ Branch 61 → 62 taken 2112 times.
✗ Branch 61 → 197 not taken.
|
2112 | const Scope *structBodyScope = getBodyScope(); |
| 225 |
2/4✓ Branch 63 → 64 taken 2112 times.
✗ Branch 63 → 197 not taken.
✓ Branch 64 → 65 taken 2112 times.
✗ Branch 64 → 197 not taken.
|
2112 | const std::string structSignature = Struct::getSignature(getSubType(), getTemplateTypes()); |
| 226 |
1/2✓ Branch 65 → 66 taken 2112 times.
✗ Branch 65 → 195 not taken.
|
2112 | const SymbolTableEntry *structSymbol = structBodyScope->parent->lookupStrict(structSignature); |
| 227 |
1/2✗ Branch 68 → 69 not taken.
✓ Branch 68 → 70 taken 2112 times.
|
2112 | assert(structSymbol != nullptr); |
| 228 | |||
| 229 | // Collect concrete field types | ||
| 230 | 2112 | std::string mangledName; | |
| 231 | 2112 | std::vector<llvm::Type *> fieldTypes; | |
| 232 | 2112 | bool isPacked = false; | |
| 233 |
2/2✓ Branch 72 → 73 taken 1855 times.
✓ Branch 72 → 132 taken 257 times.
|
2112 | if (is(TY_STRUCT)) { // Struct |
| 234 |
2/4✓ Branch 73 → 74 taken 1855 times.
✗ Branch 73 → 191 not taken.
✓ Branch 74 → 75 taken 1855 times.
✗ Branch 74 → 191 not taken.
|
1855 | const Struct *spiceStruct = structSymbol->getQualType().getStruct(structSymbol->declNode); |
| 235 |
1/2✗ Branch 75 → 76 not taken.
✓ Branch 75 → 77 taken 1855 times.
|
1855 | assert(spiceStruct != nullptr); |
| 236 |
1/2✓ Branch 77 → 78 taken 1855 times.
✗ Branch 77 → 169 not taken.
|
1855 | mangledName = NameMangling::mangleStruct(*spiceStruct); |
| 237 | |||
| 238 |
1/2✓ Branch 80 → 81 taken 1855 times.
✗ Branch 80 → 191 not taken.
|
1855 | const size_t totalFieldCount = spiceStruct->scope->getFieldCount(); |
| 239 |
1/2✓ Branch 81 → 82 taken 1855 times.
✗ Branch 81 → 191 not taken.
|
1855 | fieldTypes.reserve(totalFieldCount); |
| 240 | |||
| 241 | // If the struct has no interface types, but a vtable was requested, add another ptr field type | ||
| 242 |
2/4✓ Branch 82 → 83 taken 1855 times.
✗ Branch 82 → 191 not taken.
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 85 taken 1855 times.
|
1855 | assert(structSymbol->declNode->isStructDef()); |
| 243 |
1/2✓ Branch 85 → 86 taken 1855 times.
✗ Branch 85 → 87 not taken.
|
1855 | const auto structDeclNode = spice_pointer_cast<StructDefNode *>(structSymbol->declNode); |
| 244 |
4/4✓ Branch 92 → 93 taken 1490 times.
✓ Branch 92 → 97 taken 365 times.
✓ Branch 93 → 94 taken 50 times.
✓ Branch 93 → 97 taken 1440 times.
|
1855 | if (!structDeclNode->hasInterfaces && structDeclNode->emitVTable) |
| 245 |
2/4✓ Branch 94 → 95 taken 50 times.
✗ Branch 94 → 170 not taken.
✓ Branch 95 → 96 taken 50 times.
✗ Branch 95 → 170 not taken.
|
50 | fieldTypes.push_back(llvm::PointerType::get(context, 0)); |
| 246 | |||
| 247 | // Collect all field types | ||
| 248 |
2/2✓ Branch 110 → 98 taken 4697 times.
✓ Branch 110 → 111 taken 1855 times.
|
6552 | for (size_t i = 0; i < totalFieldCount; i++) { |
| 249 |
1/2✗ Branch 98 → 99 not taken.
✓ Branch 98 → 100 taken 4697 times.
|
4697 | const SymbolTableEntry *fieldSymbol = spiceStruct->scope->lookupField(i); |
| 250 |
1/2✗ Branch 103 → 104 not taken.
✓ Branch 103 → 105 taken 4697 times.
|
4697 | assert(fieldSymbol != nullptr); |
| 251 |
3/6✓ Branch 105 → 106 taken 4697 times.
✗ Branch 105 → 171 not taken.
✓ Branch 107 → 108 taken 4697 times.
✗ Branch 107 → 171 not taken.
✓ Branch 108 → 109 taken 4697 times.
✗ Branch 108 → 171 not taken.
|
4697 | fieldTypes.push_back(sourceFile->getLLVMType(fieldSymbol->getQualType().getType())); |
| 252 | } | ||
| 253 | |||
| 254 | // Check if the struct is declared as packed | ||
| 255 |
12/18✓ Branch 111 → 112 taken 51 times.
✓ Branch 111 → 118 taken 1804 times.
✓ Branch 114 → 115 taken 51 times.
✗ Branch 114 → 172 not taken.
✓ Branch 115 → 116 taken 51 times.
✗ Branch 115 → 172 not taken.
✓ Branch 116 → 117 taken 1 time.
✓ Branch 116 → 118 taken 50 times.
✓ Branch 119 → 120 taken 51 times.
✓ Branch 119 → 121 taken 1804 times.
✓ Branch 121 → 122 taken 51 times.
✓ Branch 121 → 124 taken 1804 times.
✓ Branch 124 → 125 taken 1 time.
✓ Branch 124 → 142 taken 1854 times.
✗ Branch 172 → 173 not taken.
✗ Branch 172 → 174 not taken.
✗ Branch 176 → 177 not taken.
✗ Branch 176 → 179 not taken.
|
1957 | if (structDeclNode->attrs && structDeclNode->attrs->attrLst->hasAttr(ATTR_CORE_COMPILER_PACKED)) |
| 256 |
2/4✓ Branch 127 → 128 taken 1 time.
✗ Branch 127 → 183 not taken.
✓ Branch 128 → 129 taken 1 time.
✗ Branch 128 → 181 not taken.
|
3 | isPacked = structDeclNode->attrs->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_PACKED)->boolValue; |
| 257 | } else { // Interface | ||
| 258 |
2/4✓ Branch 132 → 133 taken 257 times.
✗ Branch 132 → 191 not taken.
✓ Branch 133 → 134 taken 257 times.
✗ Branch 133 → 191 not taken.
|
257 | const Interface *spiceInterface = structSymbol->getQualType().getInterface(structSymbol->declNode); |
| 259 |
1/2✗ Branch 134 → 135 not taken.
✓ Branch 134 → 136 taken 257 times.
|
257 | assert(spiceInterface != nullptr); |
| 260 |
1/2✓ Branch 136 → 137 taken 257 times.
✗ Branch 136 → 187 not taken.
|
257 | mangledName = NameMangling::mangleInterface(*spiceInterface); |
| 261 | |||
| 262 | // vtable pointer | ||
| 263 |
2/4✓ Branch 139 → 140 taken 257 times.
✗ Branch 139 → 188 not taken.
✓ Branch 140 → 141 taken 257 times.
✗ Branch 140 → 188 not taken.
|
257 | fieldTypes.push_back(llvm::PointerType::get(context, 0)); |
| 264 | } | ||
| 265 | |||
| 266 |
1/2✓ Branch 144 → 145 taken 2112 times.
✗ Branch 144 → 189 not taken.
|
2112 | return llvm::StructType::create(context, fieldTypes, mangledName, isPacked); |
| 267 | 2112 | } | |
| 268 | |||
| 269 |
2/4✓ Branch 150 → 151 taken 85 times.
✗ Branch 150 → 198 not taken.
✓ Branch 151 → 152 taken 85 times.
✗ Branch 151 → 157 not taken.
|
85 | if (isOneOf({TY_FUNCTION, TY_PROCEDURE})) { |
| 270 | 85 | llvm::PointerType *ptrTy = llvm::PointerType::get(context, 0); | |
| 271 |
1/2✓ Branch 154 → 155 taken 85 times.
✗ Branch 154 → 199 not taken.
|
85 | return llvm::StructType::get(context, {ptrTy, ptrTy}); |
| 272 | } | ||
| 273 | |||
| 274 | − | throw CompilerError(UNHANDLED_BRANCH, "Cannot determine LLVM type of " + getName(true, true, true)); // GCOVR_EXCL_LINE | |
| 275 | } | ||
| 276 | |||
| 277 | /** | ||
| 278 | * Check if the base type of the current type chain is of a certain super type | ||
| 279 | * | ||
| 280 | * @param superType Super type to check for | ||
| 281 | * @return Applicable or not | ||
| 282 | */ | ||
| 283 | 500768 | bool Type::isBase(SuperType superType) const { | |
| 284 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 500768 times.
|
500768 | assert(!typeChain.empty()); |
| 285 | 500768 | return typeChain.front().superType == superType; | |
| 286 | } | ||
| 287 | |||
| 288 | /** | ||
| 289 | * Check if the current type is a primitive type | ||
| 290 | * | ||
| 291 | * @return Primitive type or not | ||
| 292 | */ | ||
| 293 |
1/2✓ Branch 2 → 3 taken 127457 times.
✗ Branch 2 → 6 not taken.
|
127457 | bool Type::isPrimitive() const { return isOneOf({TY_DOUBLE, TY_INT, TY_SHORT, TY_LONG, TY_BYTE, TY_CHAR, TY_STRING, TY_BOOL}); } |
| 294 | |||
| 295 | /** | ||
| 296 | * Check if the type is an extended primitive type | ||
| 297 | * The definition of extended primitive types contains all primitive types plus the following: | ||
| 298 | * - structs | ||
| 299 | * - interfaces | ||
| 300 | * - functions/procedures | ||
| 301 | * | ||
| 302 | * @return Extended primitive or not | ||
| 303 | */ | ||
| 304 |
6/8✓ Branch 2 → 3 taken 101586 times.
✗ Branch 2 → 11 not taken.
✓ Branch 3 → 4 taken 34102 times.
✓ Branch 3 → 6 taken 67484 times.
✓ Branch 4 → 5 taken 34102 times.
✗ Branch 4 → 11 not taken.
✓ Branch 5 → 6 taken 23592 times.
✓ Branch 5 → 7 taken 10510 times.
|
101586 | bool Type::isExtendedPrimitive() const { return isPrimitive() || isOneOf({TY_STRUCT, TY_INTERFACE, TY_FUNCTION, TY_PROCEDURE}); } |
| 305 | |||
| 306 | /** | ||
| 307 | * Check if the current type is a pointer type | ||
| 308 | * | ||
| 309 | * @return Pointer type or not | ||
| 310 | */ | ||
| 311 | 492987 | bool Type::isPtr() const { return getSuperType() == TY_PTR; } | |
| 312 | |||
| 313 | /** | ||
| 314 | * Check if the current type is a reference type | ||
| 315 | * | ||
| 316 | * @return Reference type or not | ||
| 317 | */ | ||
| 318 | 1062375 | bool Type::isRef() const { return getSuperType() == TY_REF; } | |
| 319 | |||
| 320 | /** | ||
| 321 | * Check if the current type is an array type | ||
| 322 | * | ||
| 323 | * @return Array type or not | ||
| 324 | */ | ||
| 325 | 154342 | bool Type::isArray() const { return getSuperType() == TY_ARRAY; } | |
| 326 | |||
| 327 | /** | ||
| 328 | * Check if the current type is of the same container type like the other type. | ||
| 329 | * Only TY_PTR, TY_REF and TY_ARRAY are considered as container types. | ||
| 330 | * | ||
| 331 | * @param other Other symbol type | ||
| 332 | * @return Same container type or not | ||
| 333 | */ | ||
| 334 | 106875 | bool Type::isSameContainerTypeAs(const Type *other) const { | |
| 335 |
4/4✓ Branch 3 → 4 taken 2318 times.
✓ Branch 3 → 7 taken 104557 times.
✓ Branch 5 → 6 taken 2053 times.
✓ Branch 5 → 7 taken 265 times.
|
106875 | const bool bothPtr = isPtr() && other->isPtr(); |
| 336 |
4/4✓ Branch 9 → 10 taken 5273 times.
✓ Branch 9 → 13 taken 101602 times.
✓ Branch 11 → 12 taken 4180 times.
✓ Branch 11 → 13 taken 1093 times.
|
106875 | const bool bothRef = isRef() && other->isRef(); |
| 337 |
3/4✓ Branch 15 → 16 taken 70 times.
✓ Branch 15 → 19 taken 106805 times.
✓ Branch 17 → 18 taken 70 times.
✗ Branch 17 → 19 not taken.
|
106875 | const bool bothArray = isArray() && other->isArray(); |
| 338 |
6/6✓ Branch 20 → 21 taken 104822 times.
✓ Branch 20 → 23 taken 2053 times.
✓ Branch 21 → 22 taken 100642 times.
✓ Branch 21 → 23 taken 4180 times.
✓ Branch 22 → 23 taken 70 times.
✓ Branch 22 → 24 taken 100572 times.
|
106875 | return bothPtr || bothRef || bothArray; |
| 339 | } | ||
| 340 | |||
| 341 | /** | ||
| 342 | * Retrieve the base type of the current type | ||
| 343 | * | ||
| 344 | * @return Base type | ||
| 345 | */ | ||
| 346 | 1317616 | const Type *Type::getBase() const { | |
| 347 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1317616 times.
|
1317616 | assert(!typeChain.empty()); |
| 348 | |||
| 349 | // Create new type chain | ||
| 350 |
3/6✓ Branch 9 → 10 taken 1317616 times.
✗ Branch 9 → 19 not taken.
✓ Branch 12 → 13 taken 1317616 times.
✓ Branch 12 → 14 taken 1317616 times.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
|
3952848 | const TypeChain newTypeChain = {typeChain.front()}; |
| 351 | |||
| 352 | // Register new type or return if already registered | ||
| 353 |
1/2✓ Branch 14 → 15 taken 1317616 times.
✗ Branch 14 → 32 not taken.
|
2635232 | return TypeRegistry::getOrInsert(newTypeChain); |
| 354 |
1/6✓ Branch 6 → 7 taken 1317616 times.
✗ Branch 6 → 26 not taken.
✗ Branch 26 → 27 not taken.
✗ Branch 26 → 30 not taken.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 30 not taken.
|
2635232 | } |
| 355 | |||
| 356 | /** | ||
| 357 | * Retrieve the same type, but with lambda captures | ||
| 358 | * | ||
| 359 | * @return Type with lambda captures | ||
| 360 | */ | ||
| 361 | 40 | const Type *Type::getWithLambdaCaptures(bool enabled) const { | |
| 362 |
3/6✓ Branch 2 → 3 taken 40 times.
✗ Branch 2 → 16 not taken.
✓ Branch 3 → 4 taken 40 times.
✗ Branch 3 → 13 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 40 times.
|
40 | assert(getBase()->isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 363 | |||
| 364 | // Create new type chain | ||
| 365 |
1/2✓ Branch 6 → 7 taken 40 times.
✗ Branch 6 → 16 not taken.
|
40 | TypeChain newTypeChain = typeChain; |
| 366 | 40 | newTypeChain.front().data.hasCaptures = enabled; | |
| 367 | |||
| 368 | // Register new type or return if already registered | ||
| 369 |
1/2✓ Branch 8 → 9 taken 40 times.
✗ Branch 8 → 14 not taken.
|
80 | return TypeRegistry::getOrInsert(newTypeChain); |
| 370 | 40 | } | |
| 371 | |||
| 372 | /** | ||
| 373 | * Retrieve the same type, but with the body scope removed | ||
| 374 | * | ||
| 375 | * @return Type with body scope removed | ||
| 376 | */ | ||
| 377 | 25122 | const Type *Type::getWithBodyScope(Scope *bodyScope) const { | |
| 378 |
3/6✓ Branch 2 → 3 taken 25122 times.
✗ Branch 2 → 16 not taken.
✓ Branch 3 → 4 taken 25122 times.
✗ Branch 3 → 13 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 25122 times.
|
25122 | assert(getBase()->isOneOf({TY_STRUCT, TY_INTERFACE})); |
| 379 | |||
| 380 | // Create new type chain | ||
| 381 |
1/2✓ Branch 6 → 7 taken 25122 times.
✗ Branch 6 → 16 not taken.
|
25122 | TypeChain newTypeChain = typeChain; |
| 382 | 25122 | newTypeChain.front().data.bodyScope = bodyScope; | |
| 383 | |||
| 384 | // Register new type or return if already registered | ||
| 385 |
1/2✓ Branch 8 → 9 taken 25122 times.
✗ Branch 8 → 14 not taken.
|
50244 | return TypeRegistry::getOrInsert(newTypeChain); |
| 386 | 25122 | } | |
| 387 | |||
| 388 | /** | ||
| 389 | * Retrieve the same type, but with the given template types | ||
| 390 | * | ||
| 391 | * @return Type with new template types | ||
| 392 | */ | ||
| 393 | 4092 | const Type *Type::getWithTemplateTypes(const QualTypeList &templateTypes) const { | |
| 394 |
2/4✓ Branch 2 → 3 taken 4092 times.
✗ Branch 2 → 8 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 4092 times.
|
4092 | assert(isOneOf({TY_STRUCT, TY_INTERFACE})); |
| 395 | 4092 | return getWithBaseTemplateTypes(templateTypes); | |
| 396 | } | ||
| 397 | |||
| 398 | /** | ||
| 399 | * Retrieve the same type, but with the given base template types | ||
| 400 | * | ||
| 401 | * @return Type with new base template types | ||
| 402 | */ | ||
| 403 | 9228 | const Type *Type::getWithBaseTemplateTypes(const QualTypeList &templateTypes) const { | |
| 404 |
3/6✓ Branch 2 → 3 taken 9228 times.
✗ Branch 2 → 17 not taken.
✓ Branch 3 → 4 taken 9228 times.
✗ Branch 3 → 14 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 9228 times.
|
9228 | assert(getBase()->isOneOf({TY_STRUCT, TY_INTERFACE})); |
| 405 | |||
| 406 | // Create new type chain | ||
| 407 |
1/2✓ Branch 6 → 7 taken 9228 times.
✗ Branch 6 → 17 not taken.
|
9228 | TypeChain newTypeChain = typeChain; |
| 408 |
1/2✓ Branch 8 → 9 taken 9228 times.
✗ Branch 8 → 15 not taken.
|
9228 | newTypeChain.front().templateTypes = templateTypes; |
| 409 | |||
| 410 | // Register new type or return if already registered | ||
| 411 |
1/2✓ Branch 9 → 10 taken 9228 times.
✗ Branch 9 → 15 not taken.
|
18456 | return TypeRegistry::getOrInsert(newTypeChain); |
| 412 | 9228 | } | |
| 413 | |||
| 414 | /** | ||
| 415 | * Retrieve the same type, but with the param and return types removed | ||
| 416 | * | ||
| 417 | * @return Type with param and return types removed | ||
| 418 | */ | ||
| 419 | 14665 | const Type *Type::getWithFunctionParamAndReturnTypes(const QualTypeList ¶mAndReturnTypes) const { | |
| 420 |
3/6✓ Branch 2 → 3 taken 14665 times.
✗ Branch 2 → 17 not taken.
✓ Branch 3 → 4 taken 14665 times.
✗ Branch 3 → 14 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 14665 times.
|
14665 | assert(getBase()->isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 421 | |||
| 422 | // Create new type chain | ||
| 423 |
1/2✓ Branch 6 → 7 taken 14665 times.
✗ Branch 6 → 17 not taken.
|
14665 | TypeChain newTypeChain = typeChain; |
| 424 |
1/2✓ Branch 8 → 9 taken 14665 times.
✗ Branch 8 → 15 not taken.
|
14665 | newTypeChain.front().paramTypes = paramAndReturnTypes; |
| 425 | |||
| 426 | // Register new type or return if already registered | ||
| 427 |
1/2✓ Branch 9 → 10 taken 14665 times.
✗ Branch 9 → 15 not taken.
|
29330 | return TypeRegistry::getOrInsert(newTypeChain); |
| 428 | 14665 | } | |
| 429 | |||
| 430 | /** | ||
| 431 | * Checks if the base type is generic itself or has generic parts in its template types | ||
| 432 | * | ||
| 433 | * @return Contains generic parts or not | ||
| 434 | */ | ||
| 435 | 382014 | bool Type::hasAnyGenericParts() const { // NOLINT(misc-no-recursion) | |
| 436 |
1/2✓ Branch 2 → 3 taken 382014 times.
✗ Branch 2 → 34 not taken.
|
382014 | const Type *baseType = getBase(); |
| 437 | |||
| 438 | // Check if the type itself is generic | ||
| 439 |
2/2✓ Branch 4 → 5 taken 69622 times.
✓ Branch 4 → 6 taken 312392 times.
|
382014 | if (baseType->is(TY_GENERIC)) |
| 440 | 69622 | return true; | |
| 441 | |||
| 442 | // Check if the type has generic template types | ||
| 443 |
1/2✓ Branch 7 → 8 taken 312392 times.
✗ Branch 7 → 34 not taken.
|
312392 | const auto templateTypes = baseType->getTemplateTypes(); |
| 444 |
3/4✓ Branch 8 → 9 taken 312392 times.
✗ Branch 8 → 32 not taken.
✓ Branch 9 → 10 taken 23656 times.
✓ Branch 9 → 11 taken 288736 times.
|
376963 | if (std::ranges::any_of(templateTypes, [](const QualType &t) { return t.hasAnyGenericParts(); })) |
| 445 | 23656 | return true; | |
| 446 | |||
| 447 | // Check param and return types or functions/procedures | ||
| 448 |
3/4✓ Branch 11 → 12 taken 288736 times.
✗ Branch 11 → 28 not taken.
✓ Branch 12 → 13 taken 378 times.
✓ Branch 12 → 24 taken 288358 times.
|
288736 | if (baseType->isOneOf({TY_FUNCTION, TY_PROCEDURE})) { |
| 449 |
2/4✓ Branch 13 → 14 taken 378 times.
✗ Branch 13 → 31 not taken.
✓ Branch 14 → 15 taken 378 times.
✗ Branch 14 → 31 not taken.
|
378 | const auto paramTypes = baseType->getFunctionParamAndReturnTypes(); |
| 450 |
3/4✓ Branch 15 → 16 taken 378 times.
✗ Branch 15 → 29 not taken.
✓ Branch 16 → 17 taken 55 times.
✓ Branch 16 → 18 taken 323 times.
|
1131 | if (std::ranges::any_of(paramTypes, [](const QualType &t) { return t.hasAnyGenericParts(); })) |
| 451 | 55 | return true; | |
| 452 |
2/2✓ Branch 20 → 21 taken 323 times.
✓ Branch 20 → 23 taken 55 times.
|
378 | } |
| 453 | |||
| 454 | 288681 | return false; // Does not have generic parts | |
| 455 | 312392 | } | |
| 456 | |||
| 457 | /** | ||
| 458 | * Retrieve template types of the current type | ||
| 459 | * | ||
| 460 | * @return Vector of template types | ||
| 461 | */ | ||
| 462 | 410748 | const QualTypeList &Type::getTemplateTypes() const { return typeChain.back().templateTypes; } | |
| 463 | |||
| 464 | /** | ||
| 465 | * Check if the current type is of a certain super type | ||
| 466 | * | ||
| 467 | * @return Applicable or not | ||
| 468 | */ | ||
| 469 | 7788397 | bool Type::is(SuperType superType) const { return getSuperType() == superType; } | |
| 470 | |||
| 471 | /** | ||
| 472 | * Check if the current type is one of a list of super types | ||
| 473 | * | ||
| 474 | * @return Applicable or not | ||
| 475 | */ | ||
| 476 | 1676111 | bool Type::isOneOf(const std::initializer_list<SuperType> &superTypes) const { | |
| 477 | 5597864 | return std::ranges::any_of(superTypes, [this](SuperType superType) { return is(superType); }); | |
| 478 | } | ||
| 479 | |||
| 480 | /** | ||
| 481 | * Get the name of the symbol type as a string | ||
| 482 | * | ||
| 483 | * @param name Get name of type | ||
| 484 | * @param withSize Include the array size for sized types | ||
| 485 | * @param ignorePublic Ignore any potential public qualifier | ||
| 486 | * @param withAliases Print aliases as is and not decompose them | ||
| 487 | * @return Symbol type name | ||
| 488 | */ | ||
| 489 | 521951 | void Type::getName(std::stringstream &name, bool withSize, bool ignorePublic, bool withAliases) const { | |
| 490 | // Loop through all chain elements | ||
| 491 |
2/2✓ Branch 10 → 4 taken 646483 times.
✓ Branch 10 → 11 taken 521951 times.
|
1168434 | for (const TypeChainElement &chainElement : typeChain) |
| 492 |
2/4✓ Branch 5 → 6 taken 646483 times.
✗ Branch 5 → 14 not taken.
✓ Branch 6 → 7 taken 646483 times.
✗ Branch 6 → 12 not taken.
|
646483 | name << chainElement.getName(withSize, ignorePublic, withAliases); |
| 493 | 521951 | } | |
| 494 | |||
| 495 | /** | ||
| 496 | * Get the name of the symbol type as a string | ||
| 497 | * | ||
| 498 | * @param withSize Include the array size for sized types | ||
| 499 | * @param ignorePublic Ignore any potential public qualifier | ||
| 500 | * @param withAliases Print aliases as is and not decompose them | ||
| 501 | * @return Symbol type name | ||
| 502 | */ | ||
| 503 | 16438 | std::string Type::getName(bool withSize, bool ignorePublic, bool withAliases) const { | |
| 504 |
1/2✓ Branch 2 → 3 taken 16438 times.
✗ Branch 2 → 11 not taken.
|
16438 | std::stringstream name; |
| 505 |
1/2✓ Branch 3 → 4 taken 16438 times.
✗ Branch 3 → 9 not taken.
|
16438 | getName(name, withSize, ignorePublic, withAliases); |
| 506 |
1/2✓ Branch 4 → 5 taken 16438 times.
✗ Branch 4 → 9 not taken.
|
32876 | return name.str(); |
| 507 | 16438 | } | |
| 508 | |||
| 509 | /** | ||
| 510 | * Get the return type of function type | ||
| 511 | * | ||
| 512 | * @return Function return type | ||
| 513 | */ | ||
| 514 | 39 | const QualType &Type::getFunctionReturnType() const { | |
| 515 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 39 times.
|
39 | assert(is(TY_FUNCTION)); |
| 516 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 39 times.
|
39 | assert(!typeChain.back().paramTypes.empty()); |
| 517 | 39 | return typeChain.back().paramTypes.front(); | |
| 518 | } | ||
| 519 | |||
| 520 | /** | ||
| 521 | * Get the param types of a function or procedure type | ||
| 522 | * | ||
| 523 | * @return Function param types | ||
| 524 | */ | ||
| 525 | 138 | QualTypeList Type::getFunctionParamTypes() const { | |
| 526 |
2/4✓ Branch 2 → 3 taken 138 times.
✗ Branch 2 → 20 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 138 times.
|
138 | assert(isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 527 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 138 times.
|
138 | if (typeChain.back().paramTypes.empty()) |
| 528 | ✗ | return {}; | |
| 529 |
1/2✓ Branch 16 → 17 taken 138 times.
✗ Branch 16 → 21 not taken.
|
414 | return {typeChain.back().paramTypes.begin() + 1, typeChain.back().paramTypes.end()}; |
| 530 | } | ||
| 531 | |||
| 532 | /** | ||
| 533 | * Check if a function or procedure type has captures | ||
| 534 | * | ||
| 535 | * @return Has captures | ||
| 536 | */ | ||
| 537 | 151 | bool Type::hasLambdaCaptures() const { | |
| 538 |
2/4✓ Branch 3 → 4 taken 151 times.
✗ Branch 3 → 9 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 151 times.
|
151 | assert(getBase()->isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 539 | 151 | return typeChain.front().data.hasCaptures; | |
| 540 | } | ||
| 541 | |||
| 542 | /** | ||
| 543 | * Get the param and return types of a function or procedure base type | ||
| 544 | * | ||
| 545 | * @return Function param and return types (first is return type, rest are param types) | ||
| 546 | */ | ||
| 547 | 440 | const QualTypeList &Type::getFunctionParamAndReturnTypes() const { | |
| 548 |
2/4✓ Branch 3 → 4 taken 440 times.
✗ Branch 3 → 9 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 440 times.
|
440 | assert(getBase()->isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 549 | 440 | return typeChain.front().paramTypes; | |
| 550 | } | ||
| 551 | |||
| 552 | /** | ||
| 553 | * Check for the matching compatibility of two types. | ||
| 554 | * Useful for struct and function matching as well as assignment type validation and function arg matching. | ||
| 555 | * | ||
| 556 | * @param otherType Type to compare against | ||
| 557 | * @param ignoreArraySize Ignore array sizes | ||
| 558 | * @return Matching or not | ||
| 559 | */ | ||
| 560 | 132972 | bool Type::matches(const Type *otherType, bool ignoreArraySize) const { | |
| 561 | // If the size does not match, it is not equal | ||
| 562 |
2/2✓ Branch 4 → 5 taken 15168 times.
✓ Branch 4 → 6 taken 117804 times.
|
132972 | if (typeChain.size() != otherType->typeChain.size()) |
| 563 | 15168 | return false; | |
| 564 | |||
| 565 | // Compare the elements | ||
| 566 |
2/2✓ Branch 18 → 7 taken 126538 times.
✓ Branch 18 → 19 taken 79559 times.
|
206097 | for (size_t i = 0; i < typeChain.size(); i++) { |
| 567 | 126538 | const TypeChainElement &lhsElement = typeChain.at(i); | |
| 568 | 126538 | const TypeChainElement &rhsElement = otherType->typeChain.at(i); | |
| 569 | |||
| 570 | // Ignore differences in array size | ||
| 571 |
5/6✓ Branch 9 → 10 taken 78934 times.
✓ Branch 9 → 13 taken 47604 times.
✓ Branch 10 → 11 taken 2 times.
✓ Branch 10 → 13 taken 78932 times.
✓ Branch 11 → 12 taken 2 times.
✗ Branch 11 → 13 not taken.
|
126538 | if (ignoreArraySize && lhsElement.superType == TY_ARRAY && rhsElement.superType == TY_ARRAY) |
| 572 | 2 | continue; | |
| 573 | |||
| 574 | // Not both types are arrays -> compare them as usual | ||
| 575 |
2/2✓ Branch 14 → 15 taken 38245 times.
✓ Branch 14 → 16 taken 88291 times.
|
126536 | if (lhsElement != rhsElement) |
| 576 | 38245 | return false; | |
| 577 | } | ||
| 578 | |||
| 579 | 79559 | return true; | |
| 580 | } | ||
| 581 | |||
| 582 | /** | ||
| 583 | * Remove pointers / arrays / references if both types have them as far as possible. | ||
| 584 | * | ||
| 585 | * @param typeA Candidate type | ||
| 586 | * @param typeB Requested type | ||
| 587 | */ | ||
| 588 | 97899 | void Type::unwrapBoth(const Type *&typeA, const Type *&typeB) { | |
| 589 | // Unwrap both types as far as possible | ||
| 590 |
2/2✓ Branch 7 → 3 taken 6146 times.
✓ Branch 7 → 8 taken 97899 times.
|
104045 | while (typeA->isSameContainerTypeAs(typeB)) { |
| 591 | 6146 | typeB = typeB->getContained(); | |
| 592 | 6146 | typeA = typeA->getContained(); | |
| 593 | } | ||
| 594 | 97899 | } | |
| 595 | |||
| 596 | /** | ||
| 597 | * Remove pointers / arrays / references if both types have them as far as possible. | ||
| 598 | * Furthermore, remove reference wrappers if possible. | ||
| 599 | * | ||
| 600 | * @param typeA Candidate type | ||
| 601 | * @param typeB Requested type | ||
| 602 | */ | ||
| 603 | 96756 | void Type::unwrapBothWithRefWrappers(const Type *&typeA, const Type *&typeB) { | |
| 604 | // Remove reference wrapper of front type if required | ||
| 605 |
6/6✓ Branch 3 → 4 taken 40097 times.
✓ Branch 3 → 7 taken 56659 times.
✓ Branch 5 → 6 taken 35924 times.
✓ Branch 5 → 7 taken 4173 times.
✓ Branch 8 → 9 taken 35924 times.
✓ Branch 8 → 11 taken 60832 times.
|
96756 | if (typeA->isRef() && !typeB->isRef()) |
| 606 | 35924 | typeA = typeA->removeReferenceWrapper(); | |
| 607 | |||
| 608 | // Remove reference wrapper of requested type if required | ||
| 609 |
8/8✓ Branch 12 → 13 taken 92583 times.
✓ Branch 12 → 19 taken 4173 times.
✓ Branch 14 → 15 taken 3456 times.
✓ Branch 14 → 19 taken 89127 times.
✓ Branch 17 → 18 taken 2898 times.
✓ Branch 17 → 19 taken 558 times.
✓ Branch 20 → 21 taken 2898 times.
✓ Branch 20 → 23 taken 93858 times.
|
96756 | if (!typeA->isRef() && typeB->isRef() && !typeA->getBase()->is(TY_GENERIC)) |
| 610 | 2898 | typeB = typeB->removeReferenceWrapper(); | |
| 611 | |||
| 612 | // Unwrap both types as far as possible | ||
| 613 | 96756 | unwrapBoth(typeA, typeB); | |
| 614 | 96756 | } | |
| 615 | |||
| 616 | /** | ||
| 617 | * Check if two types have the same type chain depth | ||
| 618 | * | ||
| 619 | * @param typeA First type | ||
| 620 | * @param typeB Second type | ||
| 621 | * @return Same depth or not | ||
| 622 | */ | ||
| 623 | 20875 | bool Type::hasSameTypeChainDepth(const Type *typeA, const Type *typeB) { | |
| 624 | 20875 | return typeA->typeChain.size() == typeB->typeChain.size(); | |
| 625 | } | ||
| 626 | |||
| 627 | } // namespace spice::compiler | ||
| 628 |