model/generic-type.spice
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | // Std imports | ||
| 4 | import "std/data/unordered-map"; | ||
| 5 | |||
| 6 | // Own imports | ||
| 7 | import "@/symboltablebuilder/super-type"; | ||
| 8 | import "@/symboltablebuilder/qual-type"; | ||
| 9 | |||
| 10 | // Type aliases | ||
| 11 | public type TypeMapping alias UnorderedMap</*typeName=*/String, /*concreteType=*/QualType>; | ||
| 12 | |||
| 13 | 525700 | public type GenericType struct { | |
| 14 | public compose QualType qualType | ||
| 15 | QualTypeList typeConditions | ||
| 16 | 28751 | public bool used = false | |
| 17 | } | ||
| 18 | |||
| 19 | ✗ | public p GenericType.ctor() {} | |
| 20 | |||
| 21 | 25689 | public p GenericType.ctor(const QualType& qualType) { | |
| 22 | 25689 | this.qualType = qualType; | |
| 23 | 25689 | this.typeConditions.pushBack(QualType(SuperType::TY_DYN)); | |
| 24 | 25689 | } | |
| 25 | |||
| 26 | 3062 | public p GenericType.ctor(const String& name, const QualTypeList& typeConditions) { | |
| 27 | 3062 | this.qualType = QualType(SuperType::TY_GENERIC, name); | |
| 28 | 3062 | this.typeConditions = typeConditions; | |
| 29 | 3062 | } | |
| 30 | |||
| 31 | /** | ||
| 32 | * Checks if the given symbol qualType matches all conditions to get a manifestation of the current generic qualType | ||
| 33 | * | ||
| 34 | * @param requestedType Qualified qualType to be checked | ||
| 35 | * @param substantiation Concrete substantiation of the generic type | ||
| 36 | * @param ignoreArraySize Ignore the array size for qualType comparison | ||
| 37 | * @param ignoreQualifiers Ignore the type qualifiers for qualType comparison | ||
| 38 | * @return True or false | ||
| 39 | */ | ||
| 40 | 27259 | public f<bool> GenericType.checkConditionsOf(const QualType& requestedType, QualType& substantiation, bool ignoreArraySize = false, bool ignoreQualifiers = false) { | |
| 41 | 27259 | return this.checkTypeConditionOf(requestedType, substantiation, ignoreArraySize, ignoreQualifiers); | |
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * Checks if the given qualType matches all qualType conditions to get a manifestation of the current generic qualType | ||
| 46 | * | ||
| 47 | * @param requestedType Qualified type to be checked | ||
| 48 | * @param substantiation Concrete substantiation of the generic type | ||
| 49 | * @param ignoreArraySize Ignore the array size for qualType comparison | ||
| 50 | * @param ignoreQualifiers Ignore the type qualifiers for qualType comparison | ||
| 51 | * @return True or false | ||
| 52 | */ | ||
| 53 | 27259 | f<bool> GenericType.checkTypeConditionOf(const QualType& requestedType, QualType& substantiation, bool ignoreArraySize, bool ignoreQualifiers) { | |
| 54 | 27259 | substantiation = requestedType; | |
| 55 | // Succeed if there are no type conditions | ||
| 56 | 27259 | if this.typeConditions.isEmpty() { | |
| 57 | ✗ | return true; | |
| 58 | } | ||
| 59 | // Succeed if the given qual type is generic and matches the current one | ||
| 60 | 27259 | if requestedType.hasAnyGenericParts() && requestedType == this.qualType { | |
| 61 | 1419 | return true; | |
| 62 | } | ||
| 63 | // Check type conditions | ||
| 64 | 36780 | foreach const QualType& typeCondition : this.typeConditions { | |
| 65 | // If we have a dyn type condition, the conditions are fulfilled immediately | ||
| 66 | 32451 | if typeCondition.is(SuperType::TY_DYN) { | |
| 67 | 19038 | return true; | |
| 68 | } | ||
| 69 | // In situations like this we need to unwrap: requestedType = const int&, typeCondition = int | ||
| 70 | 13413 | QualType typeConditionCopy = typeCondition; | |
| 71 | 13413 | QualType requestedTypeCopy = requestedType; | |
| 72 | 13413 | unwrapBothWithRefWrappers(typeConditionCopy, requestedTypeCopy); | |
| 73 | 13413 | if typeConditionCopy.matches(requestedTypeCopy, ignoreArraySize, ignoreQualifiers, ignoreQualifiers) { | |
| 74 | // Field-wise copy instead of assigning the const reference through the reference | ||
| 75 | 2473 | substantiation.rawType = typeCondition.rawType; | |
| 76 | 2473 | substantiation.qualifiers = typeCondition.qualifiers; | |
| 77 | 2473 | return true; | |
| 78 | } | ||
| 79 | 10940 | } | |
| 80 | 4329 | return false; | |
| 81 | } | ||
| 82 |