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