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 3807084 times.
✗ Branch 5 → 11 not taken.
✓ Branch 7 → 8 taken 3807084 times.
✓ Branch 7 → 9 taken 3807084 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.
|
11421252 | Type::Type(SuperType superType) : typeChain({TypeChainElement{superType}}) {} |
| 25 | |||
| 26 |
4/12✓ Branch 4 → 5 taken 5594 times.
✗ Branch 4 → 19 not taken.
✓ Branch 6 → 7 taken 5594 times.
✗ Branch 6 → 13 not taken.
✓ Branch 8 → 9 taken 5594 times.
✓ Branch 8 → 10 taken 5594 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.
|
16782 | Type::Type(SuperType superType, const std::string &subType) : typeChain({TypeChainElement{superType, subType}}) {} |
| 27 | |||
| 28 | 6249 | Type::Type(SuperType superType, const std::string &subType, uint64_t typeId, const TypeChainElementData &data, | |
| 29 | const QualTypeList &templateTypes) | ||
| 30 |
5/14✓ Branch 4 → 5 taken 6249 times.
✗ Branch 4 → 24 not taken.
✓ Branch 5 → 6 taken 6249 times.
✗ Branch 5 → 21 not taken.
✓ Branch 7 → 8 taken 6249 times.
✗ Branch 7 → 15 not taken.
✓ Branch 9 → 10 taken 6249 times.
✓ Branch 9 → 11 taken 6249 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.
|
18747 | : typeChain({TypeChainElement(superType, subType, typeId, data, templateTypes)}) {} |
| 31 | |||
| 32 | 15989594 | 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 | 37901447 | SuperType Type::getSuperType() const { | |
| 40 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 37901447 times.
|
37901447 | assert(!typeChain.empty()); |
| 41 | 37901447 | return typeChain.back().superType; | |
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Get the sub type of the current type | ||
| 46 | * | ||
| 47 | * @return Sub type | ||
| 48 | */ | ||
| 49 | 949976 | const std::string &Type::getSubType() const { | |
| 50 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 949976 times.
|
949976 | assert(!typeChain.empty()); |
| 51 |
2/4✓ Branch 5 → 6 taken 949976 times.
✗ Branch 5 → 11 not taken.
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 949976 times.
|
949976 | assert(isOneOf({TY_STRUCT, TY_INTERFACE, TY_ENUM, TY_GENERIC})); |
| 52 | 949976 | return typeChain.back().subType; | |
| 53 | } | ||
| 54 | |||
| 55 | /** | ||
| 56 | * Get the array size of the current type | ||
| 57 | * | ||
| 58 | * @return Array size | ||
| 59 | */ | ||
| 60 | 1501 | unsigned int Type::getArraySize() const { | |
| 61 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 1501 times.
|
1501 | assert(isArray()); |
| 62 | 1501 | return typeChain.back().data.arraySize; | |
| 63 | } | ||
| 64 | |||
| 65 | /** | ||
| 66 | * Get the body scope of the current type | ||
| 67 | * | ||
| 68 | * @return Body scope | ||
| 69 | */ | ||
| 70 | 3073083 | Scope *Type::getBodyScope() const { | |
| 71 |
2/4✓ Branch 2 → 3 taken 3073083 times.
✗ Branch 2 → 8 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 3073083 times.
|
3073083 | assert(isOneOf({TY_STRUCT, TY_INTERFACE})); |
| 72 | 3073083 | return typeChain.back().data.bodyScope; | |
| 73 | } | ||
| 74 | |||
| 75 | /** | ||
| 76 | * Get the return type of function type | ||
| 77 | * | ||
| 78 | * @return Function return type | ||
| 79 | */ | ||
| 80 | 49 | const QualType &Type::getFunctionReturnType() const { | |
| 81 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 49 times.
|
49 | assert(is(TY_FUNCTION)); |
| 82 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 49 times.
|
49 | assert(!typeChain.front().paramTypes.empty()); |
| 83 | 49 | return typeChain.front().paramTypes.front(); | |
| 84 | } | ||
| 85 | |||
| 86 | /** | ||
| 87 | * Get the param types of a function or procedure type | ||
| 88 | * | ||
| 89 | * @return Function param types | ||
| 90 | */ | ||
| 91 | 350 | QualTypeList Type::getFunctionParamTypes() const { | |
| 92 |
2/4✓ Branch 2 → 3 taken 350 times.
✗ Branch 2 → 23 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 350 times.
|
350 | assert(isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 93 |
1/2✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 350 times.
|
350 | if (typeChain.front().paramTypes.empty()) |
| 94 | ✗ | return {}; | |
| 95 |
1/2✓ Branch 18 → 19 taken 350 times.
✗ Branch 18 → 24 not taken.
|
1400 | return {typeChain.front().paramTypes.begin() + 1, typeChain.front().paramTypes.end()}; |
| 96 | } | ||
| 97 | |||
| 98 | /** | ||
| 99 | * Get the param and return types of a function or procedure base type | ||
| 100 | * | ||
| 101 | * @return Function param and return types (first is return type, rest are param types) | ||
| 102 | */ | ||
| 103 | 3964 | const QualTypeList &Type::getFunctionParamAndReturnTypes() const { | |
| 104 |
2/4✓ Branch 3 → 4 taken 3964 times.
✗ Branch 3 → 9 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 3964 times.
|
3964 | assert(getBase()->isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 105 | 3964 | return typeChain.front().paramTypes; | |
| 106 | } | ||
| 107 | |||
| 108 | /** | ||
| 109 | * Check if a function or procedure type has captures | ||
| 110 | * | ||
| 111 | * @return Has captures | ||
| 112 | */ | ||
| 113 | 303 | bool Type::hasLambdaCaptures() const { | |
| 114 |
2/4✓ Branch 3 → 4 taken 303 times.
✗ Branch 3 → 9 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 303 times.
|
303 | assert(getBase()->isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 115 | 303 | return typeChain.front().data.hasCaptures; | |
| 116 | } | ||
| 117 | |||
| 118 | /** | ||
| 119 | * Retrieve template types of the current type | ||
| 120 | * | ||
| 121 | * @return Vector of template types | ||
| 122 | */ | ||
| 123 | 1633944 | const QualTypeList &Type::getTemplateTypes() const { return typeChain.back().templateTypes; } | |
| 124 | |||
| 125 | /** | ||
| 126 | * Get the type chain depth of the current type | ||
| 127 | * | ||
| 128 | * @return Type chain depth | ||
| 129 | */ | ||
| 130 | ✗ | size_t Type::getTypeChainDepth() const { return typeChain.size(); } | |
| 131 | |||
| 132 | /** | ||
| 133 | * Check if the current type is of a certain super type | ||
| 134 | * | ||
| 135 | * @return Applicable or not | ||
| 136 | */ | ||
| 137 | 29166758 | bool Type::is(SuperType superType) const { return getSuperType() == superType; } | |
| 138 | |||
| 139 | /** | ||
| 140 | * Check if the current type is one of a list of super types | ||
| 141 | * | ||
| 142 | * @return Applicable or not | ||
| 143 | */ | ||
| 144 | 8159709 | bool Type::isOneOf(const std::initializer_list<SuperType> &superTypes) const { | |
| 145 | 22864619 | return std::ranges::any_of(superTypes, [this](SuperType superType) { return is(superType); }); | |
| 146 | } | ||
| 147 | |||
| 148 | /** | ||
| 149 | * Check if the base type of the current type chain is of a certain super type | ||
| 150 | * | ||
| 151 | * @param superType Super type to check for | ||
| 152 | * @return Applicable or not | ||
| 153 | */ | ||
| 154 | 14774534 | bool Type::isBase(SuperType superType) const { | |
| 155 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 14774534 times.
|
14774534 | assert(!typeChain.empty()); |
| 156 | 14774534 | return typeChain.front().superType == superType; | |
| 157 | } | ||
| 158 | |||
| 159 | /** | ||
| 160 | * Check if the current type is a primitive type | ||
| 161 | * | ||
| 162 | * @return Primitive type or not | ||
| 163 | */ | ||
| 164 |
1/2✓ Branch 2 → 3 taken 352116 times.
✗ Branch 2 → 6 not taken.
|
352116 | bool Type::isPrimitive() const { return isOneOf({TY_DOUBLE, TY_INT, TY_SHORT, TY_LONG, TY_BYTE, TY_CHAR, TY_STRING, TY_BOOL}); } |
| 165 | |||
| 166 | /** | ||
| 167 | * Check if the type is an extended primitive type | ||
| 168 | * The definition of extended primitive types contains all primitive types plus the following: | ||
| 169 | * - structs | ||
| 170 | * - interfaces | ||
| 171 | * - functions/procedures | ||
| 172 | * | ||
| 173 | * @return Extended primitive or not | ||
| 174 | */ | ||
| 175 |
6/8✓ Branch 2 → 3 taken 261497 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 113079 times.
✓ Branch 3 → 6 taken 148418 times.
✓ Branch 4 → 5 taken 113079 times.
✗ Branch 4 → 10 not taken.
✓ Branch 5 → 6 taken 86769 times.
✓ Branch 5 → 7 taken 26310 times.
|
261497 | bool Type::isExtendedPrimitive() const { return isPrimitive() || isOneOf({TY_STRUCT, TY_INTERFACE, TY_FUNCTION, TY_PROCEDURE}); } |
| 176 | |||
| 177 | /** | ||
| 178 | * Check if the current type is a pointer type | ||
| 179 | * | ||
| 180 | * @return Pointer type or not | ||
| 181 | */ | ||
| 182 | 1438025 | bool Type::isPtr() const { return getSuperType() == TY_PTR; } | |
| 183 | |||
| 184 | /** | ||
| 185 | * Check if the current type is a reference type | ||
| 186 | * | ||
| 187 | * @return Reference type or not | ||
| 188 | */ | ||
| 189 | 3248644 | bool Type::isRef() const { return getSuperType() == TY_REF; } | |
| 190 | |||
| 191 | /** | ||
| 192 | * Check if the current type is an array type | ||
| 193 | * | ||
| 194 | * @return Array type or not | ||
| 195 | */ | ||
| 196 | 505997 | bool Type::isArray() const { return getSuperType() == TY_ARRAY; } | |
| 197 | |||
| 198 | /** | ||
| 199 | * Checks if the base type is generic itself or has generic parts in its template types | ||
| 200 | * | ||
| 201 | * @return Contains generic parts or not | ||
| 202 | */ | ||
| 203 | 1409966 | bool Type::hasAnyGenericParts() const { // NOLINT(misc-no-recursion) | |
| 204 |
1/2✓ Branch 2 → 3 taken 1409966 times.
✗ Branch 2 → 34 not taken.
|
1409966 | const Type *baseType = getBase(); |
| 205 | |||
| 206 | // Check if the type itself is generic | ||
| 207 |
2/2✓ Branch 4 → 5 taken 240612 times.
✓ Branch 4 → 6 taken 1169354 times.
|
1409966 | if (baseType->is(TY_GENERIC)) |
| 208 | 240612 | return true; | |
| 209 | |||
| 210 | // Check if the type has generic template types | ||
| 211 |
1/2✓ Branch 7 → 8 taken 1169354 times.
✗ Branch 7 → 34 not taken.
|
1169354 | const auto templateTypes = baseType->getTemplateTypes(); |
| 212 |
3/4✓ Branch 8 → 9 taken 1169354 times.
✗ Branch 8 → 32 not taken.
✓ Branch 9 → 10 taken 97918 times.
✓ Branch 9 → 11 taken 1071436 times.
|
1493273 | if (std::ranges::any_of(templateTypes, [](const QualType &t) { return t.hasAnyGenericParts(); })) |
| 213 | 97918 | return true; | |
| 214 | |||
| 215 | // Check param and return types or functions/procedures | ||
| 216 |
3/4✓ Branch 11 → 12 taken 1071436 times.
✗ Branch 11 → 28 not taken.
✓ Branch 12 → 13 taken 3808 times.
✓ Branch 12 → 24 taken 1067628 times.
|
1071436 | if (baseType->isOneOf({TY_FUNCTION, TY_PROCEDURE})) { |
| 217 |
2/4✓ Branch 13 → 14 taken 3808 times.
✗ Branch 13 → 31 not taken.
✓ Branch 14 → 15 taken 3808 times.
✗ Branch 14 → 31 not taken.
|
3808 | const auto paramTypes = baseType->getFunctionParamAndReturnTypes(); |
| 218 |
3/4✓ Branch 15 → 16 taken 3808 times.
✗ Branch 15 → 29 not taken.
✓ Branch 16 → 17 taken 236 times.
✓ Branch 16 → 18 taken 3572 times.
|
10574 | if (std::ranges::any_of(paramTypes, [](const QualType &t) { return t.hasAnyGenericParts(); })) |
| 219 | 236 | return true; | |
| 220 |
2/2✓ Branch 20 → 21 taken 3572 times.
✓ Branch 20 → 23 taken 236 times.
|
3808 | } |
| 221 | |||
| 222 | 1071200 | return false; // Does not have generic parts | |
| 223 | 1169354 | } | |
| 224 | |||
| 225 | /** | ||
| 226 | * Check if the current type is of the same container type like the other type. | ||
| 227 | * Only TY_PTR, TY_REF and TY_ARRAY are considered as container types. | ||
| 228 | * | ||
| 229 | * @param other Other symbol type | ||
| 230 | * @return Same container type or not | ||
| 231 | */ | ||
| 232 | 345092 | bool Type::isSameContainerTypeAs(const Type *other) const { | |
| 233 |
4/4✓ Branch 3 → 4 taken 10011 times.
✓ Branch 3 → 7 taken 335081 times.
✓ Branch 5 → 6 taken 9288 times.
✓ Branch 5 → 7 taken 723 times.
|
345092 | const bool bothPtr = isPtr() && other->isPtr(); |
| 234 |
4/4✓ Branch 9 → 10 taken 21839 times.
✓ Branch 9 → 13 taken 323253 times.
✓ Branch 11 → 12 taken 16074 times.
✓ Branch 11 → 13 taken 5765 times.
|
345092 | const bool bothRef = isRef() && other->isRef(); |
| 235 |
3/4✓ Branch 15 → 16 taken 381 times.
✓ Branch 15 → 19 taken 344711 times.
✓ Branch 17 → 18 taken 381 times.
✗ Branch 17 → 19 not taken.
|
345092 | const bool bothArray = isArray() && other->isArray(); |
| 236 |
6/6✓ Branch 20 → 21 taken 335804 times.
✓ Branch 20 → 23 taken 9288 times.
✓ Branch 21 → 22 taken 319730 times.
✓ Branch 21 → 23 taken 16074 times.
✓ Branch 22 → 23 taken 381 times.
✓ Branch 22 → 24 taken 319349 times.
|
345092 | return bothPtr || bothRef || bothArray; |
| 237 | } | ||
| 238 | |||
| 239 | /** | ||
| 240 | * Check for the matching compatibility of two types. | ||
| 241 | * Useful for struct and function matching as well as assignment type validation and function arg matching. | ||
| 242 | * | ||
| 243 | * @param otherType Type to compare against | ||
| 244 | * @param ignoreArraySize Ignore array sizes | ||
| 245 | * @return Matching or not | ||
| 246 | */ | ||
| 247 | 407162 | bool Type::matches(const Type *otherType, bool ignoreArraySize) const { | |
| 248 | // If the size does not match, it is not equal | ||
| 249 |
2/2✓ Branch 4 → 5 taken 40465 times.
✓ Branch 4 → 6 taken 366697 times.
|
407162 | if (typeChain.size() != otherType->typeChain.size()) |
| 250 | 40465 | return false; | |
| 251 | |||
| 252 | // Compare the elements | ||
| 253 |
2/2✓ Branch 18 → 7 taken 405622 times.
✓ Branch 18 → 19 taken 246622 times.
|
652244 | for (size_t i = 0; i < typeChain.size(); i++) { |
| 254 | 405622 | const TypeChainElement &lhsElement = typeChain.at(i); | |
| 255 | 405622 | const TypeChainElement &rhsElement = otherType->typeChain.at(i); | |
| 256 | |||
| 257 | // Ignore differences in array size | ||
| 258 |
5/6✓ Branch 9 → 10 taken 247060 times.
✓ Branch 9 → 13 taken 158562 times.
✓ Branch 10 → 11 taken 5 times.
✓ Branch 10 → 13 taken 247055 times.
✓ Branch 11 → 12 taken 5 times.
✗ Branch 11 → 13 not taken.
|
405622 | if (ignoreArraySize && lhsElement.superType == TY_ARRAY && rhsElement.superType == TY_ARRAY) |
| 259 | 5 | continue; | |
| 260 | |||
| 261 | // Not both types are arrays -> compare them as usual | ||
| 262 |
2/2✓ Branch 14 → 15 taken 120075 times.
✓ Branch 14 → 16 taken 285542 times.
|
405617 | if (lhsElement != rhsElement) |
| 263 | 120075 | return false; | |
| 264 | } | ||
| 265 | |||
| 266 | 246622 | return true; | |
| 267 | } | ||
| 268 | |||
| 269 | /** | ||
| 270 | * Get the name of the symbol type as a string | ||
| 271 | * | ||
| 272 | * @param name Get name of type | ||
| 273 | * @param withSize Include the array size for sized types | ||
| 274 | * @param ignorePublic Ignore any potential public qualifier | ||
| 275 | * @param withAliases Print aliases as is and not decompose them | ||
| 276 | * @return Symbol type name | ||
| 277 | */ | ||
| 278 | 2266218 | void Type::getName(std::stringstream &name, bool withSize, bool ignorePublic, bool withAliases) const { | |
| 279 | // Loop through all chain elements | ||
| 280 |
2/2✓ Branch 18 → 4 taken 2941839 times.
✓ Branch 18 → 19 taken 2266218 times.
|
7474275 | for (const TypeChainElement &chainElement : typeChain) |
| 281 |
2/4✓ Branch 6 → 7 taken 2941839 times.
✗ Branch 6 → 22 not taken.
✓ Branch 7 → 8 taken 2941839 times.
✗ Branch 7 → 20 not taken.
|
2941839 | name << chainElement.getName(withSize, ignorePublic, withAliases); |
| 282 | 2266218 | } | |
| 283 | |||
| 284 | /** | ||
| 285 | * Get the name of the symbol type as a string | ||
| 286 | * | ||
| 287 | * @param withSize Include the array size for sized types | ||
| 288 | * @param ignorePublic Ignore any potential public qualifier | ||
| 289 | * @param withAliases Print aliases as is and not decompose them | ||
| 290 | * @return Symbol type name | ||
| 291 | */ | ||
| 292 | 49047 | std::string Type::getName(bool withSize, bool ignorePublic, bool withAliases) const { | |
| 293 |
1/2✓ Branch 2 → 3 taken 49047 times.
✗ Branch 2 → 11 not taken.
|
49047 | std::stringstream name; |
| 294 |
1/2✓ Branch 3 → 4 taken 49047 times.
✗ Branch 3 → 9 not taken.
|
49047 | getName(name, withSize, ignorePublic, withAliases); |
| 295 |
1/2✓ Branch 4 → 5 taken 49047 times.
✗ Branch 4 → 9 not taken.
|
98094 | return name.str(); |
| 296 | 49047 | } | |
| 297 | |||
| 298 | /** | ||
| 299 | * Get the pointer type of the current type as a new type | ||
| 300 | * | ||
| 301 | * @param node AST node for error messages | ||
| 302 | * @return Pointer type of the current type | ||
| 303 | */ | ||
| 304 | 99735 | const Type *Type::toPtr(const ASTNode *node) const { | |
| 305 |
2/2✓ Branch 3 → 4 taken 2 times.
✓ Branch 3 → 12 taken 99733 times.
|
99735 | if (is(TY_DYN)) |
| 306 |
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"); |
| 307 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 22 taken 99733 times.
|
99733 | if (isRef()) |
| 308 | ✗ | throw SemanticError(node, REF_POINTERS_ARE_NOT_ALLOWED, "Pointers to references are not allowed. Use pointer instead"); | |
| 309 | |||
| 310 | // Create new type chain | ||
| 311 |
1/2✓ Branch 22 → 23 taken 99733 times.
✗ Branch 22 → 50 not taken.
|
99733 | TypeChain newTypeChain = typeChain; |
| 312 |
1/2✓ Branch 23 → 24 taken 99733 times.
✗ Branch 23 → 47 not taken.
|
99733 | newTypeChain.emplace_back(TY_PTR); |
| 313 | |||
| 314 | // Register new type or return if already registered | ||
| 315 |
1/2✓ Branch 24 → 25 taken 99733 times.
✗ Branch 24 → 48 not taken.
|
199466 | return TypeRegistry::getOrInsert(newTypeChain); |
| 316 | 99733 | } | |
| 317 | |||
| 318 | /** | ||
| 319 | * Get the reference type of the current type as a new type | ||
| 320 | * | ||
| 321 | * @param node AST node for error messages | ||
| 322 | * @return Reference type of the current type | ||
| 323 | */ | ||
| 324 | 44572 | const Type *Type::toRef(const ASTNode *node) const { | |
| 325 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 12 taken 44572 times.
|
44572 | if (is(TY_DYN)) |
| 326 | ✗ | throw SemanticError(node, DYN_REFERENCES_NOT_ALLOWED, "Just use the dyn type without '&' instead"); | |
| 327 |
1/2✗ Branch 13 → 14 not taken.
✓ Branch 13 → 22 taken 44572 times.
|
44572 | if (isRef()) |
| 328 | ✗ | throw SemanticError(node, MULTI_REF_NOT_ALLOWED, "References to references are not allowed"); | |
| 329 | |||
| 330 | // Create new type chain | ||
| 331 |
1/2✓ Branch 22 → 23 taken 44572 times.
✗ Branch 22 → 50 not taken.
|
44572 | TypeChain newTypeChain = typeChain; |
| 332 |
1/2✓ Branch 23 → 24 taken 44572 times.
✗ Branch 23 → 47 not taken.
|
44572 | newTypeChain.emplace_back(TY_REF); |
| 333 | |||
| 334 | // Register new type or return if already registered | ||
| 335 |
1/2✓ Branch 24 → 25 taken 44572 times.
✗ Branch 24 → 48 not taken.
|
89144 | return TypeRegistry::getOrInsert(newTypeChain); |
| 336 | 44572 | } | |
| 337 | |||
| 338 | /** | ||
| 339 | * Get the array type of the current type as a new type | ||
| 340 | * | ||
| 341 | * @param node AST node for error messages | ||
| 342 | * @param size Size of the array | ||
| 343 | * @param skipDynCheck Skip check if array base type is dyn | ||
| 344 | * @return Array type of the current type | ||
| 345 | */ | ||
| 346 | 583 | const Type *Type::toArr(const ASTNode *node, unsigned int size, bool skipDynCheck) const { | |
| 347 |
6/6✓ Branch 2 → 3 taken 265 times.
✓ Branch 2 → 6 taken 318 times.
✓ Branch 4 → 5 taken 1 time.
✓ Branch 4 → 6 taken 264 times.
✓ Branch 7 → 8 taken 1 time.
✓ Branch 7 → 16 taken 582 times.
|
583 | if (!skipDynCheck && typeChain.back().superType == TY_DYN) |
| 348 |
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"); |
| 349 | |||
| 350 | // Create new type chain | ||
| 351 |
1/2✓ Branch 16 → 17 taken 582 times.
✗ Branch 16 → 36 not taken.
|
582 | TypeChain newTypeChain = typeChain; |
| 352 |
1/2✓ Branch 17 → 18 taken 582 times.
✗ Branch 17 → 32 not taken.
|
582 | newTypeChain.emplace_back(TY_ARRAY, TypeChainElementData{.arraySize = size}); |
| 353 | |||
| 354 | // Register new type or return if already registered | ||
| 355 |
1/2✓ Branch 18 → 19 taken 582 times.
✗ Branch 18 → 34 not taken.
|
1164 | return TypeRegistry::getOrInsert(newTypeChain); |
| 356 | 582 | } | |
| 357 | |||
| 358 | /** | ||
| 359 | * Retrieve the base type of an array or a pointer | ||
| 360 | * | ||
| 361 | * @return Base type | ||
| 362 | */ | ||
| 363 | 510453 | const Type *Type::getContained() const { | |
| 364 |
2/2✓ Branch 3 → 4 taken 2636 times.
✓ Branch 3 → 6 taken 507817 times.
|
510453 | if (is(TY_STRING)) |
| 365 |
1/2✓ Branch 4 → 5 taken 2636 times.
✗ Branch 4 → 18 not taken.
|
2636 | return TypeRegistry::getOrInsert(TY_CHAR); |
| 366 | |||
| 367 | // Create new type chain | ||
| 368 |
1/2✓ Branch 6 → 7 taken 507817 times.
✗ Branch 6 → 18 not taken.
|
507817 | TypeChain newTypeChain = typeChain; |
| 369 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 507817 times.
|
507817 | assert(newTypeChain.size() > 1); |
| 370 | 507817 | newTypeChain.pop_back(); | |
| 371 | |||
| 372 | // Register new type or return if already registered | ||
| 373 |
1/2✓ Branch 11 → 12 taken 507817 times.
✗ Branch 11 → 16 not taken.
|
507817 | return TypeRegistry::getOrInsert(newTypeChain); |
| 374 | 507817 | } | |
| 375 | |||
| 376 | /** | ||
| 377 | * Replace the base type with another one | ||
| 378 | * | ||
| 379 | * @param newBaseType New base type | ||
| 380 | * @return The new type | ||
| 381 | */ | ||
| 382 | 72653 | const Type *Type::replaceBase(const Type *newBaseType) const { | |
| 383 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 72653 times.
|
72653 | assert(!typeChain.empty()); |
| 384 | |||
| 385 | // Create new type | ||
| 386 |
1/2✓ Branch 5 → 6 taken 72653 times.
✗ Branch 5 → 27 not taken.
|
72653 | TypeChain newTypeChain = newBaseType->typeChain; |
| 387 |
4/4✓ Branch 7 → 8 taken 4119 times.
✓ Branch 7 → 11 taken 68534 times.
✓ Branch 9 → 10 taken 59 times.
✓ Branch 9 → 11 taken 4060 times.
|
72653 | const bool doubleRef = newTypeChain.back().superType == TY_REF && typeChain.back().superType == TY_REF; |
| 388 |
2/2✓ Branch 19 → 13 taken 9219 times.
✓ Branch 19 → 20 taken 72653 times.
|
81872 | for (size_t i = 1; i < typeChain.size(); i++) |
| 389 |
3/4✓ Branch 13 → 14 taken 59 times.
✓ Branch 13 → 15 taken 9160 times.
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 17 taken 59 times.
|
9219 | if (!doubleRef || i > 1) |
| 390 |
2/4✓ Branch 15 → 16 taken 9160 times.
✗ Branch 15 → 25 not taken.
✓ Branch 16 → 17 taken 9160 times.
✗ Branch 16 → 25 not taken.
|
9160 | newTypeChain.push_back(typeChain.at(i)); |
| 391 | |||
| 392 | // Register new type or return if already registered | ||
| 393 |
1/2✓ Branch 20 → 21 taken 72653 times.
✗ Branch 20 → 25 not taken.
|
145306 | return TypeRegistry::getOrInsert(newTypeChain); |
| 394 | 72653 | } | |
| 395 | |||
| 396 | /** | ||
| 397 | * Remove reference wrapper from the current type | ||
| 398 | * | ||
| 399 | * @return Type without reference wrapper | ||
| 400 | */ | ||
| 401 |
1/2✓ Branch 3 → 4 taken 126483 times.
✗ Branch 3 → 6 not taken.
|
126483 | const Type *Type::removeReferenceWrapper() const { return isRef() ? getContained() : this; } |
| 402 | |||
| 403 | /** | ||
| 404 | * Retrieve the base type of the current type | ||
| 405 | * | ||
| 406 | * @return Base type | ||
| 407 | */ | ||
| 408 | 7082957 | const Type *Type::getBase() const { | |
| 409 |
1/2✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 7082957 times.
|
7082957 | assert(!typeChain.empty()); |
| 410 | |||
| 411 | // Create new type chain | ||
| 412 |
3/6✓ Branch 9 → 10 taken 7082957 times.
✗ Branch 9 → 19 not taken.
✓ Branch 12 → 13 taken 7082957 times.
✓ Branch 12 → 14 taken 7082957 times.
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
|
21248871 | const TypeChain newTypeChain = {typeChain.front()}; |
| 413 | |||
| 414 | // Register new type or return if already registered | ||
| 415 |
1/2✓ Branch 14 → 15 taken 7082957 times.
✗ Branch 14 → 32 not taken.
|
14165914 | return TypeRegistry::getOrInsert(newTypeChain); |
| 416 |
1/6✓ Branch 6 → 7 taken 7082957 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.
|
14165914 | } |
| 417 | |||
| 418 | /** | ||
| 419 | * Retrieve the same type, but with lambda captures | ||
| 420 | * | ||
| 421 | * @return Type with lambda captures | ||
| 422 | */ | ||
| 423 | 96 | const Type *Type::getWithLambdaCaptures(bool enabled) const { | |
| 424 |
3/6✓ Branch 2 → 3 taken 96 times.
✗ Branch 2 → 16 not taken.
✓ Branch 3 → 4 taken 96 times.
✗ Branch 3 → 13 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 96 times.
|
96 | assert(getBase()->isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 425 | |||
| 426 | // Create new type chain | ||
| 427 |
1/2✓ Branch 6 → 7 taken 96 times.
✗ Branch 6 → 16 not taken.
|
96 | TypeChain newTypeChain = typeChain; |
| 428 | 96 | newTypeChain.front().data.hasCaptures = enabled; | |
| 429 | |||
| 430 | // Register new type or return if already registered | ||
| 431 |
1/2✓ Branch 8 → 9 taken 96 times.
✗ Branch 8 → 14 not taken.
|
192 | return TypeRegistry::getOrInsert(newTypeChain); |
| 432 | 96 | } | |
| 433 | |||
| 434 | /** | ||
| 435 | * Retrieve the same type, but with the body scope removed | ||
| 436 | * | ||
| 437 | * @return Type with body scope removed | ||
| 438 | */ | ||
| 439 | 111512 | const Type *Type::getWithBodyScope(Scope *bodyScope) const { | |
| 440 |
3/6✓ Branch 2 → 3 taken 111512 times.
✗ Branch 2 → 16 not taken.
✓ Branch 3 → 4 taken 111512 times.
✗ Branch 3 → 13 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 111512 times.
|
111512 | assert(getBase()->isOneOf({TY_STRUCT, TY_INTERFACE})); |
| 441 | |||
| 442 | // Create new type chain | ||
| 443 |
1/2✓ Branch 6 → 7 taken 111512 times.
✗ Branch 6 → 16 not taken.
|
111512 | TypeChain newTypeChain = typeChain; |
| 444 | 111512 | newTypeChain.front().data.bodyScope = bodyScope; | |
| 445 | |||
| 446 | // Register new type or return if already registered | ||
| 447 |
1/2✓ Branch 8 → 9 taken 111512 times.
✗ Branch 8 → 14 not taken.
|
223024 | return TypeRegistry::getOrInsert(newTypeChain); |
| 448 | 111512 | } | |
| 449 | |||
| 450 | /** | ||
| 451 | * Retrieve the same type, but with the given template types | ||
| 452 | * | ||
| 453 | * @return Type with new template types | ||
| 454 | */ | ||
| 455 | 15287 | const Type *Type::getWithTemplateTypes(const QualTypeList &templateTypes) const { | |
| 456 |
2/4✓ Branch 2 → 3 taken 15287 times.
✗ Branch 2 → 8 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 15287 times.
|
15287 | assert(isOneOf({TY_STRUCT, TY_INTERFACE})); |
| 457 | 15287 | return getWithBaseTemplateTypes(templateTypes); | |
| 458 | } | ||
| 459 | |||
| 460 | /** | ||
| 461 | * Retrieve the same type, but with the given base template types | ||
| 462 | * | ||
| 463 | * @return Type with new base template types | ||
| 464 | */ | ||
| 465 | 37634 | const Type *Type::getWithBaseTemplateTypes(const QualTypeList &templateTypes) const { | |
| 466 |
3/6✓ Branch 2 → 3 taken 37634 times.
✗ Branch 2 → 17 not taken.
✓ Branch 3 → 4 taken 37634 times.
✗ Branch 3 → 14 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 37634 times.
|
37634 | assert(getBase()->isOneOf({TY_STRUCT, TY_INTERFACE})); |
| 467 | |||
| 468 | // Create new type chain | ||
| 469 |
1/2✓ Branch 6 → 7 taken 37634 times.
✗ Branch 6 → 17 not taken.
|
37634 | TypeChain newTypeChain = typeChain; |
| 470 |
1/2✓ Branch 8 → 9 taken 37634 times.
✗ Branch 8 → 15 not taken.
|
37634 | newTypeChain.front().templateTypes = templateTypes; |
| 471 | |||
| 472 | // Register new type or return if already registered | ||
| 473 |
1/2✓ Branch 9 → 10 taken 37634 times.
✗ Branch 9 → 15 not taken.
|
75268 | return TypeRegistry::getOrInsert(newTypeChain); |
| 474 | 37634 | } | |
| 475 | |||
| 476 | /** | ||
| 477 | * Retrieve the same type, but with the param and return types removed | ||
| 478 | * | ||
| 479 | * @return Type with param and return types removed | ||
| 480 | */ | ||
| 481 | 37241 | const Type *Type::getWithFunctionParamAndReturnTypes(const QualTypeList ¶mAndReturnTypes) const { | |
| 482 |
3/6✓ Branch 2 → 3 taken 37241 times.
✗ Branch 2 → 17 not taken.
✓ Branch 3 → 4 taken 37241 times.
✗ Branch 3 → 14 not taken.
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 37241 times.
|
37241 | assert(getBase()->isOneOf({TY_FUNCTION, TY_PROCEDURE})); |
| 483 | |||
| 484 | // Create new type chain | ||
| 485 |
1/2✓ Branch 6 → 7 taken 37241 times.
✗ Branch 6 → 17 not taken.
|
37241 | TypeChain newTypeChain = typeChain; |
| 486 |
1/2✓ Branch 8 → 9 taken 37241 times.
✗ Branch 8 → 15 not taken.
|
37241 | newTypeChain.front().paramTypes = paramAndReturnTypes; |
| 487 | |||
| 488 | // Register new type or return if already registered | ||
| 489 |
1/2✓ Branch 9 → 10 taken 37241 times.
✗ Branch 9 → 15 not taken.
|
74482 | return TypeRegistry::getOrInsert(newTypeChain); |
| 490 | 37241 | } | |
| 491 | |||
| 492 | /** | ||
| 493 | * Return the LLVM type for this symbol type | ||
| 494 | * | ||
| 495 | * @param sourceFile Referenced source file | ||
| 496 | * @return Corresponding LLVM type | ||
| 497 | */ | ||
| 498 | 31960 | llvm::Type *Type::toLLVMType(SourceFile *sourceFile) const { // NOLINT(misc-no-recursion) | |
| 499 |
2/4✓ Branch 3 → 4 taken 31960 times.
✗ Branch 3 → 7 not taken.
✓ Branch 5 → 6 taken 31960 times.
✗ Branch 5 → 7 not taken.
|
31960 | assert(!typeChain.empty() && !is(TY_INVALID)); |
| 500 |
2/2✓ Branch 8 → 9 taken 2 times.
✓ Branch 8 → 10 taken 31958 times.
|
31960 | llvm::LLVMContext &context = sourceFile->cliOptions.useLTO ? sourceFile->resourceManager.ltoContext : sourceFile->context; |
| 501 | |||
| 502 |
10/12✓ Branch 11 → 12 taken 31960 times.
✗ Branch 11 → 166 not taken.
✓ Branch 12 → 13 taken 15843 times.
✓ Branch 12 → 17 taken 16117 times.
✓ Branch 14 → 15 taken 216 times.
✓ Branch 14 → 18 taken 15627 times.
✓ Branch 15 → 16 taken 216 times.
✗ Branch 15 → 166 not taken.
✓ Branch 16 → 17 taken 59 times.
✓ Branch 16 → 18 taken 157 times.
✓ Branch 19 → 20 taken 16176 times.
✓ Branch 19 → 22 taken 15784 times.
|
31960 | if (isOneOf({TY_PTR, TY_REF, TY_STRING}) || (isArray() && getArraySize() == 0)) |
| 503 | 16176 | return llvm::PointerType::get(context, 0); | |
| 504 | |||
| 505 |
2/2✓ Branch 23 → 24 taken 157 times.
✓ Branch 23 → 32 taken 15627 times.
|
15784 | if (isArray()) { |
| 506 |
1/2✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 157 times.
|
157 | assert(getArraySize() > 0); |
| 507 | 157 | llvm::Type *containedType = sourceFile->getLLVMType(getContained()); | |
| 508 | 157 | return llvm::ArrayType::get(containedType, getArraySize()); | |
| 509 | } | ||
| 510 | |||
| 511 |
1/2✗ Branch 33 → 34 not taken.
✓ Branch 33 → 35 taken 15627 times.
|
15627 | assert(!hasAnyGenericParts()); |
| 512 | |||
| 513 |
2/2✓ Branch 36 → 37 taken 186 times.
✓ Branch 36 → 39 taken 15441 times.
|
15627 | if (is(TY_DOUBLE)) |
| 514 | 186 | return llvm::Type::getDoubleTy(context); | |
| 515 | |||
| 516 |
3/4✓ Branch 39 → 40 taken 15441 times.
✗ Branch 39 → 167 not taken.
✓ Branch 40 → 41 taken 1494 times.
✓ Branch 40 → 43 taken 13947 times.
|
15441 | if (isOneOf({TY_INT, TY_ENUM})) |
| 517 | 1494 | return llvm::Type::getInt32Ty(context); | |
| 518 | |||
| 519 |
2/2✓ Branch 44 → 45 taken 137 times.
✓ Branch 44 → 47 taken 13810 times.
|
13947 | if (is(TY_SHORT)) |
| 520 | 137 | return llvm::Type::getInt16Ty(context); | |
| 521 | |||
| 522 |
2/2✓ Branch 48 → 49 taken 1069 times.
✓ Branch 48 → 51 taken 12741 times.
|
13810 | if (is(TY_LONG)) |
| 523 | 1069 | return llvm::Type::getInt64Ty(context); | |
| 524 | |||
| 525 |
3/4✓ Branch 51 → 52 taken 12741 times.
✗ Branch 51 → 168 not taken.
✓ Branch 52 → 53 taken 898 times.
✓ Branch 52 → 55 taken 11843 times.
|
12741 | if (isOneOf({TY_CHAR, TY_BYTE})) |
| 526 | 898 | return llvm::Type::getInt8Ty(context); | |
| 527 | |||
| 528 |
2/2✓ Branch 56 → 57 taken 1158 times.
✓ Branch 56 → 59 taken 10685 times.
|
11843 | if (is(TY_BOOL)) |
| 529 | 1158 | return llvm::Type::getInt1Ty(context); | |
| 530 | |||
| 531 |
3/4✓ Branch 59 → 60 taken 10685 times.
✗ Branch 59 → 169 not taken.
✓ Branch 60 → 61 taken 10433 times.
✓ Branch 60 → 150 taken 252 times.
|
10685 | if (isOneOf({TY_STRUCT, TY_INTERFACE})) { |
| 532 |
1/2✓ Branch 61 → 62 taken 10433 times.
✗ Branch 61 → 198 not taken.
|
10433 | const Scope *structBodyScope = getBodyScope(); |
| 533 |
2/4✓ Branch 63 → 64 taken 10433 times.
✗ Branch 63 → 198 not taken.
✓ Branch 64 → 65 taken 10433 times.
✗ Branch 64 → 198 not taken.
|
10433 | const std::string structSignature = Struct::getSignature(getSubType(), getTemplateTypes()); |
| 534 |
1/2✓ Branch 65 → 66 taken 10433 times.
✗ Branch 65 → 196 not taken.
|
10433 | const SymbolTableEntry *structSymbol = structBodyScope->parent->lookupStrict(structSignature); |
| 535 |
1/2✗ Branch 68 → 69 not taken.
✓ Branch 68 → 70 taken 10433 times.
|
10433 | assert(structSymbol != nullptr); |
| 536 | |||
| 537 | // Collect concrete field types | ||
| 538 | 10433 | std::string mangledName; | |
| 539 | 10433 | std::vector<llvm::Type *> fieldTypes; | |
| 540 | 10433 | bool isPacked = false; | |
| 541 |
2/2✓ Branch 72 → 73 taken 9244 times.
✓ Branch 72 → 132 taken 1189 times.
|
10433 | if (is(TY_STRUCT)) { // Struct |
| 542 |
2/4✓ Branch 73 → 74 taken 9244 times.
✗ Branch 73 → 192 not taken.
✓ Branch 74 → 75 taken 9244 times.
✗ Branch 74 → 192 not taken.
|
9244 | const Struct *spiceStruct = structSymbol->getQualType().getStruct(structSymbol->declNode); |
| 543 |
1/2✗ Branch 75 → 76 not taken.
✓ Branch 75 → 77 taken 9244 times.
|
9244 | assert(spiceStruct != nullptr); |
| 544 |
1/2✓ Branch 77 → 78 taken 9244 times.
✗ Branch 77 → 170 not taken.
|
9244 | mangledName = NameMangling::mangleStruct(*spiceStruct); |
| 545 | |||
| 546 |
1/2✓ Branch 80 → 81 taken 9244 times.
✗ Branch 80 → 192 not taken.
|
9244 | const size_t totalFieldCount = spiceStruct->scope->getFieldCount(); |
| 547 |
1/2✓ Branch 81 → 82 taken 9244 times.
✗ Branch 81 → 192 not taken.
|
9244 | fieldTypes.reserve(totalFieldCount); |
| 548 | |||
| 549 | // If the struct has no interface types, but a vtable was requested, add another ptr field type | ||
| 550 |
2/4✓ Branch 82 → 83 taken 9244 times.
✗ Branch 82 → 192 not taken.
✗ Branch 83 → 84 not taken.
✓ Branch 83 → 85 taken 9244 times.
|
9244 | assert(structSymbol->declNode->isStructDef()); |
| 551 |
1/2✓ Branch 85 → 86 taken 9244 times.
✗ Branch 85 → 87 not taken.
|
9244 | const auto structDeclNode = spice_pointer_cast<StructDefNode *>(structSymbol->declNode); |
| 552 |
4/4✓ Branch 92 → 93 taken 6781 times.
✓ Branch 92 → 97 taken 2463 times.
✓ Branch 93 → 94 taken 91 times.
✓ Branch 93 → 97 taken 6690 times.
|
9244 | if (!structDeclNode->hasInterfaces && structDeclNode->emitVTable) |
| 553 |
2/4✓ Branch 94 → 95 taken 91 times.
✗ Branch 94 → 171 not taken.
✓ Branch 95 → 96 taken 91 times.
✗ Branch 95 → 171 not taken.
|
91 | fieldTypes.push_back(llvm::PointerType::get(context, 0)); |
| 554 | |||
| 555 | // Collect all field types | ||
| 556 |
2/2✓ Branch 110 → 98 taken 23494 times.
✓ Branch 110 → 111 taken 9244 times.
|
32738 | for (size_t i = 0; i < totalFieldCount; i++) { |
| 557 |
1/2✗ Branch 98 → 99 not taken.
✓ Branch 98 → 100 taken 23494 times.
|
23494 | const SymbolTableEntry *fieldSymbol = spiceStruct->scope->lookupField(i); |
| 558 |
1/2✗ Branch 103 → 104 not taken.
✓ Branch 103 → 105 taken 23494 times.
|
23494 | assert(fieldSymbol != nullptr); |
| 559 |
3/6✓ Branch 105 → 106 taken 23494 times.
✗ Branch 105 → 172 not taken.
✓ Branch 107 → 108 taken 23494 times.
✗ Branch 107 → 172 not taken.
✓ Branch 108 → 109 taken 23494 times.
✗ Branch 108 → 172 not taken.
|
23494 | fieldTypes.push_back(sourceFile->getLLVMType(fieldSymbol->getQualType().getType())); |
| 560 | } | ||
| 561 | |||
| 562 | // Check if the struct is declared as packed | ||
| 563 |
12/18✓ Branch 111 → 112 taken 92 times.
✓ Branch 111 → 118 taken 9152 times.
✓ Branch 114 → 115 taken 92 times.
✗ Branch 114 → 173 not taken.
✓ Branch 115 → 116 taken 92 times.
✗ Branch 115 → 173 not taken.
✓ Branch 116 → 117 taken 1 time.
✓ Branch 116 → 118 taken 91 times.
✓ Branch 119 → 120 taken 92 times.
✓ Branch 119 → 121 taken 9152 times.
✓ Branch 121 → 122 taken 92 times.
✓ Branch 121 → 124 taken 9152 times.
✓ Branch 124 → 125 taken 1 time.
✓ Branch 124 → 142 taken 9243 times.
✗ Branch 173 → 174 not taken.
✗ Branch 173 → 175 not taken.
✗ Branch 177 → 178 not taken.
✗ Branch 177 → 180 not taken.
|
9428 | if (structDeclNode->attrs && structDeclNode->attrs->attrLst->hasAttr(ATTR_CORE_COMPILER_PACKED)) |
| 564 |
2/4✓ Branch 127 → 128 taken 1 time.
✗ Branch 127 → 184 not taken.
✓ Branch 128 → 129 taken 1 time.
✗ Branch 128 → 182 not taken.
|
3 | isPacked = structDeclNode->attrs->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_PACKED)->boolValue; |
| 565 | } else { // Interface | ||
| 566 |
2/4✓ Branch 132 → 133 taken 1189 times.
✗ Branch 132 → 192 not taken.
✓ Branch 133 → 134 taken 1189 times.
✗ Branch 133 → 192 not taken.
|
1189 | const Interface *spiceInterface = structSymbol->getQualType().getInterface(structSymbol->declNode); |
| 567 |
1/2✗ Branch 134 → 135 not taken.
✓ Branch 134 → 136 taken 1189 times.
|
1189 | assert(spiceInterface != nullptr); |
| 568 |
1/2✓ Branch 136 → 137 taken 1189 times.
✗ Branch 136 → 188 not taken.
|
1189 | mangledName = NameMangling::mangleInterface(*spiceInterface); |
| 569 | |||
| 570 | // vtable pointer | ||
| 571 |
2/4✓ Branch 139 → 140 taken 1189 times.
✗ Branch 139 → 189 not taken.
✓ Branch 140 → 141 taken 1189 times.
✗ Branch 140 → 189 not taken.
|
1189 | fieldTypes.push_back(llvm::PointerType::get(context, 0)); |
| 572 | } | ||
| 573 | |||
| 574 |
1/2✓ Branch 144 → 145 taken 10433 times.
✗ Branch 144 → 190 not taken.
|
10433 | return llvm::StructType::create(context, fieldTypes, mangledName, isPacked); |
| 575 | 10433 | } | |
| 576 | |||
| 577 |
2/4✓ Branch 150 → 151 taken 252 times.
✗ Branch 150 → 199 not taken.
✓ Branch 151 → 152 taken 252 times.
✗ Branch 151 → 158 not taken.
|
252 | if (isOneOf({TY_FUNCTION, TY_PROCEDURE})) { |
| 578 | // Lambda/function values are represented as a fat pointer with three slots: | ||
| 579 | // { fctPtr, capturePtr, captureSize }. The capture size (in bytes) travels with | ||
| 580 | // the value so that the std Lambda type can take ownership of the captures on the | ||
| 581 | // heap regardless of where the lambda came from. It is 0 if there is no owned | ||
| 582 | // capture struct (no captures, or a single capture stored inline in capturePtr). | ||
| 583 | 252 | llvm::PointerType *ptrTy = llvm::PointerType::get(context, 0); | |
| 584 | 252 | llvm::IntegerType *int64Ty = llvm::Type::getInt64Ty(context); | |
| 585 |
1/2✓ Branch 155 → 156 taken 252 times.
✗ Branch 155 → 200 not taken.
|
252 | return llvm::StructType::get(context, {ptrTy, ptrTy, int64Ty}); |
| 586 | } | ||
| 587 | |||
| 588 | − | throw CompilerError(UNHANDLED_BRANCH, "Cannot determine LLVM type of " + getName(true, true, true)); // GCOVR_EXCL_LINE | |
| 589 | } | ||
| 590 | |||
| 591 | /** | ||
| 592 | * Remove pointers / arrays / references if both types have them as far as possible. | ||
| 593 | * | ||
| 594 | * @param typeA Candidate type | ||
| 595 | * @param typeB Requested type | ||
| 596 | */ | ||
| 597 | 313223 | void Type::unwrapBoth(const Type *&typeA, const Type *&typeB) { | |
| 598 | // Unwrap both types as far as possible | ||
| 599 |
2/2✓ Branch 7 → 3 taken 23715 times.
✓ Branch 7 → 8 taken 313223 times.
|
336938 | while (typeA->isSameContainerTypeAs(typeB)) { |
| 600 | 23715 | typeB = typeB->getContained(); | |
| 601 | 23715 | typeA = typeA->getContained(); | |
| 602 | } | ||
| 603 | 313223 | } | |
| 604 | |||
| 605 | /** | ||
| 606 | * Remove pointers / arrays / references if both types have them as far as possible. | ||
| 607 | * Furthermore, remove reference wrappers if possible. | ||
| 608 | * | ||
| 609 | * @param typeA Candidate type | ||
| 610 | * @param typeB Requested type | ||
| 611 | */ | ||
| 612 | 307290 | void Type::unwrapBothWithRefWrappers(const Type *&typeA, const Type *&typeB) { | |
| 613 | // Remove reference wrapper of front type if required | ||
| 614 |
6/6✓ Branch 3 → 4 taken 133802 times.
✓ Branch 3 → 7 taken 173488 times.
✓ Branch 5 → 6 taken 117753 times.
✓ Branch 5 → 7 taken 16049 times.
✓ Branch 8 → 9 taken 117753 times.
✓ Branch 8 → 11 taken 189537 times.
|
307290 | if (typeA->isRef() && !typeB->isRef()) |
| 615 | 117753 | typeA = typeA->removeReferenceWrapper(); | |
| 616 | |||
| 617 | // Remove reference wrapper of requested type if required | ||
| 618 |
8/8✓ Branch 12 → 13 taken 291241 times.
✓ Branch 12 → 18 taken 16049 times.
✓ Branch 14 → 15 taken 10949 times.
✓ Branch 14 → 18 taken 280292 times.
✓ Branch 16 → 17 taken 8730 times.
✓ Branch 16 → 18 taken 2219 times.
✓ Branch 19 → 20 taken 8730 times.
✓ Branch 19 → 22 taken 298560 times.
|
307290 | if (!typeA->isRef() && typeB->isRef() && !typeA->isBase(TY_GENERIC)) |
| 619 | 8730 | typeB = typeB->removeReferenceWrapper(); | |
| 620 | |||
| 621 | // Unwrap both types as far as possible | ||
| 622 | 307290 | unwrapBoth(typeA, typeB); | |
| 623 | 307290 | } | |
| 624 | |||
| 625 | /** | ||
| 626 | * Check if two types have the same type chain depth | ||
| 627 | * | ||
| 628 | * @param typeA First type | ||
| 629 | * @param typeB Second type | ||
| 630 | * @return Same depth or not | ||
| 631 | */ | ||
| 632 | 47414 | bool Type::hasSameTypeChainDepth(const Type *typeA, const Type *typeB) { | |
| 633 | 47414 | return typeA->typeChain.size() == typeB->typeChain.size(); | |
| 634 | } | ||
| 635 | |||
| 636 | } // namespace spice::compiler | ||
| 637 |