GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 5 / 0 / 5
Functions: 100.0% 5 / 0 / 5
Branches: -% 0 / 0 / 0

src/symboltablebuilder/QualType.h
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #pragma once
4
5 #include <string>
6 #include <unordered_map>
7 #include <vector>
8
9 #include <symboltablebuilder/TypeQualifiers.h>
10
11 // Forward declarations
12 namespace llvm {
13 class Type;
14 } // namespace llvm
15
16 namespace spice::compiler {
17
18 // Forward declarations
19 class SourceFile;
20 class Type;
21 class ASTNode;
22 class Scope;
23 class Struct;
24 class Interface;
25 class Union;
26 class GenericType;
27 class QualType;
28 class SymbolTableEntry;
29 enum SuperType : uint8_t;
30
31 // Constants
32 constexpr const char *const STROBJ_NAME = "String";
33 constexpr const char *const RESULTOBJ_NAME = "Result";
34 constexpr const char *const ERROBJ_NAME = "Error";
35 constexpr const char *const TIOBJ_NAME = "TypeInfo";
36 constexpr const char *const IITERATOR_NAME = "IIterator";
37 constexpr const char *const ARRAY_ITERATOR_NAME = "ArrayIterator";
38 static constexpr const char *const RESERVED_TYPE_NAMES[] = {
39 STROBJ_NAME, RESULTOBJ_NAME, ERROBJ_NAME, TIOBJ_NAME, IITERATOR_NAME, ARRAY_ITERATOR_NAME,
40 };
41 static constexpr uint64_t TYPE_ID_ITERATOR_INTERFACE = 255;
42 static constexpr uint64_t TYPE_ID_ITERABLE_INTERFACE = 256;
43
44 // Typedefs
45 using QualTypeList = std::vector<QualType>;
46 using TypeMapping = std::unordered_map</*typeName=*/std::string, /*concreteType=*/QualType>;
47
48 /**
49 * QualType pairs an interned, immutable type (held behind the dependency-free IType interface) with a set of
50 * qualifiers. It is a small value type that is copied around freely; the rawType pointer is owned by the type
51 * registry.
52 */
53 class QualType {
54 public:
55 // Constructors
56 4476884 QualType() = default;
57 explicit QualType(SuperType superType);
58 QualType(SuperType superType, const std::string &subType);
59 QualType(const Type *type, TypeQualifiers qualifiers);
60
61 // Getters and setters on type
62 38980867 [[nodiscard]] const Type *getType() const { return type; }
63
64 // Getters on type parts
65 [[nodiscard]] SuperType getSuperType() const;
66 [[nodiscard]] const std::string &getSubType() const;
67 [[nodiscard]] unsigned int getArraySize() const;
68 [[nodiscard]] Scope *getBodyScope() const;
69 [[nodiscard]] const QualType &getFunctionReturnType() const;
70 [[nodiscard]] QualTypeList getFunctionParamTypes() const;
71 [[nodiscard]] const QualTypeList &getFunctionParamAndReturnTypes() const;
72 [[nodiscard]] bool hasLambdaCaptures() const;
73 [[nodiscard]] const QualTypeList &getTemplateTypes() const;
74 [[nodiscard]] Struct *getStruct(const ASTNode *node, const QualTypeList &templateTypes) const;
75 [[nodiscard]] Struct *getStruct(const ASTNode *node) const;
76 [[nodiscard]] Struct *getStructAndAdjustType(const ASTNode *node, const QualTypeList &templateTypes);
77 [[nodiscard]] Struct *getStructAndAdjustType(const ASTNode *node);
78 [[nodiscard]] Interface *getInterface(const ASTNode *node, const QualTypeList &templateTypes) const;
79 [[nodiscard]] Interface *getInterface(const ASTNode *node) const;
80 [[nodiscard]] Union *getUnion(const ASTNode *node, const QualTypeList &templateTypes) const;
81 [[nodiscard]] Union *getUnion(const ASTNode *node) const;
82 [[nodiscard]] Union *getUnionAndAdjustType(const ASTNode *node, const QualTypeList &templateTypes);
83 [[nodiscard]] Union *getUnionAndAdjustType(const ASTNode *node);
84
85 // Queries on the type
86 [[nodiscard]] bool is(SuperType superType) const;
87 [[nodiscard]] bool isOneOf(const std::initializer_list<SuperType> &superTypes) const;
88 [[nodiscard]] bool isBase(SuperType superType) const;
89 [[nodiscard]] bool isPrimitive() const;
90 [[nodiscard]] bool isExtendedPrimitive() const;
91 [[nodiscard]] bool isPtr() const;
92 [[nodiscard]] bool isPtrTo(SuperType superType) const;
93 [[nodiscard]] bool isRef() const;
94 [[nodiscard]] bool isRefTo(SuperType superType) const;
95 [[nodiscard]] bool isArray() const;
96 [[nodiscard]] bool isArrayOf(SuperType superType) const;
97 [[nodiscard]] bool isDecayedArray() const;
98 [[nodiscard]] bool isConstRef() const;
99 [[nodiscard]] bool isIterator(const ASTNode *node) const;
100 [[nodiscard]] bool isIterable(const ASTNode *node) const;
101 [[nodiscard]] bool isStringObj() const;
102 [[nodiscard]] bool isErrorObj() const;
103 [[nodiscard]] bool isResultObj() const;
104 [[nodiscard]] bool hasAnyGenericParts() const;
105
106 // Complex queries on the type
107 [[nodiscard]] bool isTriviallyConstructible(const ASTNode *node) const;
108 [[nodiscard]] bool isTriviallyCopyable(const ASTNode *node) const;
109 [[nodiscard]] bool isTriviallyDestructible(const ASTNode *node) const;
110 [[nodiscard]] bool doesImplement(const QualType &implementedInterfaceType, const ASTNode *node) const;
111 [[nodiscard]] bool canBind(const QualType &inputType, bool isTemporary) const;
112 [[nodiscard]] bool matches(const QualType &otherType, bool ignoreArraySize, bool ignoreQualifiers, bool allowConstify) const;
113 [[nodiscard]] bool matchesInterfaceImplementedByStruct(const QualType &structType) const;
114 [[nodiscard]] bool matchesComposedBaseOfStruct(const QualType &structType) const;
115 [[nodiscard]] bool isSameContainerTypeAs(const QualType &other) const;
116 [[nodiscard]] bool isSelfReferencingStructType(const QualType *typeToCompareWith = nullptr) const;
117 [[nodiscard]] bool isCoveredByGenericTypeList(std::vector<GenericType> &genericTypeList) const;
118 [[nodiscard]] bool needsDeAllocation() const;
119
120 // Serialization
121 void getName(std::stringstream &name, bool withSize = false, bool ignorePublic = false, bool withAliases = true) const;
122 [[nodiscard]] std::string getName(bool withSize = false, bool ignorePublic = false, bool withAliases = true) const;
123
124 // LLVM helpers
125 [[nodiscard]] llvm::Type *toLLVMType(SourceFile *sourceFile) const;
126 [[nodiscard]] llvm::Type *getParamLLVMType(SourceFile *sourceFile) const;
127
128 // Get new type, based on this one
129 [[nodiscard]] QualType toPtr(const ASTNode *node) const;
130 [[nodiscard]] QualType toRef(const ASTNode *node) const;
131 [[nodiscard]] QualType toConstRef(const ASTNode *node) const;
132 [[nodiscard]] QualType toArr(const ASTNode *node, size_t size, bool skipDynCheck = false) const;
133 [[nodiscard]] QualType toNonConst() const;
134 [[nodiscard]] QualType getContained() const;
135 [[nodiscard]] QualType getBase() const;
136 [[nodiscard]] QualType getAliased(const SymbolTableEntry *aliasEntry) const;
137 [[nodiscard]] QualType removeReferenceWrapper() const;
138 [[nodiscard]] QualType autoDeReference() const;
139 [[nodiscard]] QualType replaceBaseType(const QualType &newBaseType) const;
140 [[nodiscard]] QualType getWithLambdaCaptures(bool enabled = true) const;
141 [[nodiscard]] QualType getWithBodyScope(Scope *bodyScope) const;
142 [[nodiscard]] QualType getWithTemplateTypes(const QualTypeList &templateTypes) const;
143 [[nodiscard]] QualType getWithBaseTemplateTypes(const QualTypeList &templateTypes) const;
144 [[nodiscard]] QualType getWithFunctionParamAndReturnTypes(const QualTypeList &paramAndReturnTypes) const;
145 [[nodiscard]] QualType getWithFunctionParamAndReturnTypes(const QualType &returnType, const QualTypeList &paramTypes) const;
146
147 // Getters and setters on qualifiers
148 884038 [[nodiscard]] TypeQualifiers &getQualifiers() { return qualifiers; }
149 43090561 [[nodiscard]] const TypeQualifiers &getQualifiers() const { return qualifiers; }
150 314564 void setQualifiers(TypeQualifiers newQualifiers) { qualifiers = newQualifiers; }
151
152 // Getters and setters on qualifier parts
153 [[nodiscard]] bool isConst() const;
154 [[nodiscard]] bool isSigned() const;
155 [[nodiscard]] bool isUnsigned() const;
156 [[nodiscard]] bool isInline() const;
157 [[nodiscard]] bool isPublic() const;
158 [[nodiscard]] bool isHeap() const;
159 [[nodiscard]] bool isComposition() const;
160 void makeConst(bool isConst = true);
161 void makeUnsigned(bool isUnsigned = true);
162 void makePublic(bool isPublic = true);
163 void makeHeap(bool isHeap = true);
164
165 // Overloaded operators
166 friend bool operator==(const QualType &lhs, const QualType &rhs);
167 friend bool operator!=(const QualType &lhs, const QualType &rhs);
168
169 // Public static methods
170 static void unwrapBoth(QualType &typeA, QualType &typeB);
171 static void unwrapBothWithRefWrappers(QualType &typeA, QualType &typeB);
172
173 private:
174 // Private members
175 const Type *type = nullptr;
176 TypeQualifiers qualifiers;
177 };
178
179 // Make sure we have no unexpected increases in memory consumption
180 static_assert(sizeof(QualType) == 16);
181
182 } // namespace spice::compiler
183