GCC Code Coverage Report


Directory: ../
File: src/symboltablebuilder/QualType.h
Date: 2025-03-05 01:50:32
Exec Total Coverage
Lines: 5 5 100.0%
Functions: 5 5 100.0%
Branches: 0 0 -%

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