GCC Code Coverage Report


Directory: ../
File: src/symboltablebuilder/QualType.h
Date: 2025-02-09 04:23:07
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 93355 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 610525 [[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 Struct *getStruct(const ASTNode *node) const;
62 [[nodiscard]] Interface *getInterface(const ASTNode *node) const;
63
64 // Queries on the type
65 [[nodiscard]] bool is(SuperType superType) const;
66 [[nodiscard]] bool isOneOf(const std::initializer_list<SuperType> &superTypes) const;
67 [[nodiscard]] bool isBase(SuperType superType) const;
68 [[nodiscard]] bool isPrimitive() const;
69 [[nodiscard]] bool isExtendedPrimitive() const;
70 [[nodiscard]] bool isPtr() const;
71 [[nodiscard]] bool isPtrTo(SuperType superType) const;
72 [[nodiscard]] bool isRef() const;
73 [[nodiscard]] bool isRefTo(SuperType superType) const;
74 [[nodiscard]] bool isArray() const;
75 [[nodiscard]] bool isArrayOf(SuperType superType) const;
76 [[nodiscard]] bool isConstRef() const;
77 [[nodiscard]] bool isIterator(const ASTNode *node) const;
78 [[nodiscard]] bool isIterable(const ASTNode *node) const;
79 [[nodiscard]] bool isStringObj() const;
80 [[nodiscard]] bool isErrorObj() const;
81 [[nodiscard]] bool hasAnyGenericParts() const;
82
83 // Complex queries on the type
84 [[nodiscard]] bool isTriviallyCopyable(const ASTNode *node) const;
85 [[nodiscard]] bool doesImplement(const QualType &implementedInterfaceType, const ASTNode *node) const;
86 [[nodiscard]] bool canBind(const QualType &inputType, bool isTemporary) const;
87 [[nodiscard]] bool matches(const QualType &otherType, bool ignoreArraySize, bool ignoreQualifiers, bool allowConstify) const;
88 [[nodiscard]] bool matchesInterfaceImplementedByStruct(const QualType &structType) const;
89 [[nodiscard]] bool isSameContainerTypeAs(const QualType &other) const;
90 [[nodiscard]] bool isSelfReferencingStructType(const QualType *typeToCompareWith = nullptr) const;
91 [[nodiscard]] bool isCoveredByGenericTypeList(std::vector<GenericType> &genericTypeList) const;
92 [[nodiscard]] bool needsDeAllocation() const;
93
94 // Serialization
95 void getName(std::stringstream &name, bool withSize = false, bool ignorePublic = false) const;
96 [[nodiscard]] std::string getName(bool withSize = false, bool ignorePublic = false) const;
97
98 // LLVM helpers
99 [[nodiscard]] llvm::Type *toLLVMType(SourceFile *sourceFile) const;
100
101 // Get new type, based on this one
102 [[nodiscard]] QualType toPtr(const ASTNode *node) const;
103 [[nodiscard]] QualType toRef(const ASTNode *node) const;
104 [[nodiscard]] QualType toConstRef(const ASTNode *node) const;
105 [[nodiscard]] QualType toArr(const ASTNode *node, size_t size, bool skipDynCheck = false) const;
106 [[nodiscard]] QualType toNonConst() const;
107 [[nodiscard]] QualType getContained() const;
108 [[nodiscard]] QualType getBase() const;
109 [[nodiscard]] QualType getAliased(const SymbolTableEntry *aliasEntry) const;
110 [[nodiscard]] QualType removeReferenceWrapper() const;
111 [[nodiscard]] QualType autoDeReference() const;
112 [[nodiscard]] QualType replaceBaseType(const QualType &newBaseType) const;
113 [[nodiscard]] QualType getWithLambdaCaptures(bool enabled = true) const;
114 [[nodiscard]] QualType getWithBodyScope(Scope *bodyScope) const;
115 [[nodiscard]] QualType getWithTemplateTypes(const QualTypeList &templateTypes) const;
116 [[nodiscard]] QualType getWithBaseTemplateTypes(const QualTypeList &templateTypes) const;
117 [[nodiscard]] QualType getWithFunctionParamAndReturnTypes(const QualTypeList &paramAndReturnTypes) const;
118 [[nodiscard]] QualType getWithFunctionParamAndReturnTypes(const QualType &returnType, const QualTypeList &paramTypes) const;
119
120 // Getters and setters on qualifiers
121 257588 [[nodiscard]] TypeQualifiers &getQualifiers() { return qualifiers; }
122 538588 [[nodiscard]] const TypeQualifiers &getQualifiers() const { return qualifiers; }
123 44106 void setQualifiers(TypeQualifiers newQualifiers) { qualifiers = newQualifiers; }
124
125 // Getters and setters on qualifier parts
126 [[nodiscard]] bool isConst() const;
127 [[nodiscard]] bool isSigned() const;
128 [[nodiscard]] bool isUnsigned() const;
129 [[nodiscard]] bool isInline() const;
130 [[nodiscard]] bool isPublic() const;
131 [[nodiscard]] bool isHeap() const;
132 [[nodiscard]] bool isComposition() const;
133 void makeConst(bool isConst = true);
134 void makeUnsigned(bool isUnsigned = true);
135 void makePublic(bool isPublic = true);
136 void makeHeap(bool isHeap = true);
137
138 // Overloaded operators
139 friend bool operator==(const QualType &lhs, const QualType &rhs);
140 friend bool operator!=(const QualType &lhs, const QualType &rhs);
141
142 // Public static methods
143 static void unwrapBoth(QualType &typeA, QualType &typeB);
144
145 private:
146 // Private members
147 const Type *type = nullptr;
148 TypeQualifiers qualifiers;
149 };
150
151 // Make sure we have no unexpected increases in memory consumption
152 static_assert(sizeof(QualType) == 16);
153
154 } // namespace spice::compiler
155