Line |
Branch |
Exec |
Source |
1 |
|
|
// Copyright (c) 2021-2024 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/TypeSpecifiers.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 |
|
|
enum SuperType : uint8_t; |
25 |
|
|
|
26 |
|
|
// Constants |
27 |
|
|
const char *const STROBJ_NAME = "String"; |
28 |
|
|
const char *const RESULTOBJ_NAME = "Result"; |
29 |
|
|
const char *const ERROBJ_NAME = "Error"; |
30 |
|
|
const char *const TIOBJ_NAME = "TypeInfo"; |
31 |
|
|
const char *const IITERATOR_NAME = "IIterator"; |
32 |
|
|
const char *const ARRAY_ITERATOR_NAME = "ArrayIterator"; |
33 |
|
|
static constexpr uint64_t TYPE_ID_ITERATOR_INTERFACE = 255; |
34 |
|
|
static constexpr uint64_t TYPE_ID_ITERABLE_INTERFACE = 256; |
35 |
|
|
|
36 |
|
|
// Typedefs |
37 |
|
|
using QualTypeList = std::vector<QualType>; |
38 |
|
|
|
39 |
|
|
class QualType { |
40 |
|
|
public: |
41 |
|
|
// Constructors |
42 |
|
54619 |
QualType() = default; |
43 |
|
|
explicit QualType(SuperType superType); |
44 |
|
|
QualType(SuperType superType, const std::string &subType); |
45 |
|
|
QualType(const Type *type, TypeSpecifiers specifiers); |
46 |
|
|
|
47 |
|
|
// Getters and setters on type |
48 |
|
705214 |
[[nodiscard]] const Type *getType() const { return type; } |
49 |
|
|
|
50 |
|
|
// Getters on type parts |
51 |
|
|
[[nodiscard]] SuperType getSuperType() const; |
52 |
|
|
[[nodiscard]] const std::string &getSubType() const; |
53 |
|
|
[[nodiscard]] unsigned int getArraySize() const; |
54 |
|
|
[[nodiscard]] Scope *getBodyScope() const; |
55 |
|
|
[[nodiscard]] const QualType &getFunctionReturnType() const; |
56 |
|
|
[[nodiscard]] QualTypeList getFunctionParamTypes() const; |
57 |
|
|
[[nodiscard]] const QualTypeList &getFunctionParamAndReturnTypes() const; |
58 |
|
|
[[nodiscard]] bool hasLambdaCaptures() const; |
59 |
|
|
[[nodiscard]] const QualTypeList &getTemplateTypes() const; |
60 |
|
|
Struct *getStruct(const ASTNode *node) const; |
61 |
|
|
[[nodiscard]] Interface *getInterface(const ASTNode *node) const; |
62 |
|
|
|
63 |
|
|
// Queries on the type |
64 |
|
|
[[nodiscard]] bool is(SuperType superType) const; |
65 |
|
|
[[nodiscard]] bool isOneOf(const std::initializer_list<SuperType> &superTypes) const; |
66 |
|
|
[[nodiscard]] bool isBase(SuperType superType) const; |
67 |
|
|
[[nodiscard]] bool isPrimitive() const; |
68 |
|
|
[[nodiscard]] bool isPtr() const; |
69 |
|
|
[[nodiscard]] bool isPtrTo(SuperType superType) const; |
70 |
|
|
[[nodiscard]] bool isRef() const; |
71 |
|
|
[[nodiscard]] bool isRefTo(SuperType superType) const; |
72 |
|
|
[[nodiscard]] bool isArray() const; |
73 |
|
|
[[nodiscard]] bool isArrayOf(SuperType superType) const; |
74 |
|
|
[[nodiscard]] bool isConstRef() const; |
75 |
|
|
[[nodiscard]] bool isIterator(const ASTNode *node) const; |
76 |
|
|
[[nodiscard]] bool isIterable(const ASTNode *node) const; |
77 |
|
|
[[nodiscard]] bool isStringObj() const; |
78 |
|
|
[[nodiscard]] bool isErrorObj() const; |
79 |
|
|
[[nodiscard]] bool hasAnyGenericParts() const; |
80 |
|
|
|
81 |
|
|
// Complex queries on the type |
82 |
|
|
[[nodiscard]] bool isTriviallyCopyable(const ASTNode *node) const; |
83 |
|
|
[[nodiscard]] bool doesImplement(const QualType &symbolType, const ASTNode *node) const; |
84 |
|
|
[[nodiscard]] bool canBind(const QualType &inputType, bool isTemporary) const; |
85 |
|
|
[[nodiscard]] bool matches(const QualType &otherType, bool ignoreArraySize, bool ignoreSpecifiers, bool allowConstify) const; |
86 |
|
|
[[nodiscard]] bool matchesInterfaceImplementedByStruct(const QualType &structType) const; |
87 |
|
|
[[nodiscard]] bool isSameContainerTypeAs(const QualType &other) const; |
88 |
|
|
[[nodiscard]] bool isSelfReferencingStructType(const QualType *typeToCompareWith = nullptr) const; |
89 |
|
|
[[nodiscard]] bool isCoveredByGenericTypeList(std::vector<GenericType> &genericTypeList) const; |
90 |
|
|
[[nodiscard]] bool needsDeAllocation() const; |
91 |
|
|
|
92 |
|
|
// Serialization |
93 |
|
|
void getName(std::stringstream &name, bool withSize = false, bool ignorePublic = false) const; |
94 |
|
|
[[nodiscard]] std::string getName(bool withSize = false, bool ignorePublic = false) const; |
95 |
|
|
|
96 |
|
|
// LLVM helpers |
97 |
|
|
[[nodiscard]] llvm::Type *toLLVMType(SourceFile *sourceFile) const; |
98 |
|
|
|
99 |
|
|
// Get new type, based on this one |
100 |
|
|
[[nodiscard]] QualType toPtr(const ASTNode *node) const; |
101 |
|
|
[[nodiscard]] QualType toRef(const ASTNode *node) const; |
102 |
|
|
[[nodiscard]] QualType toConstRef(const ASTNode *node) const; |
103 |
|
|
[[nodiscard]] QualType toArray(const ASTNode *node, size_t size, bool skipDynCheck = false) const; |
104 |
|
|
[[nodiscard]] QualType toNonConst() const; |
105 |
|
|
[[nodiscard]] QualType getContained() const; |
106 |
|
|
[[nodiscard]] QualType getBase() const; |
107 |
|
|
[[nodiscard]] QualType removeReferenceWrapper() const; |
108 |
|
|
[[nodiscard]] QualType replaceBaseType(const QualType &newBaseType) const; |
109 |
|
|
[[nodiscard]] QualType getWithLambdaCaptures(bool enabled = true) const; |
110 |
|
|
[[nodiscard]] QualType getWithBodyScope(Scope *bodyScope) const; |
111 |
|
|
[[nodiscard]] QualType getWithTemplateTypes(const QualTypeList &templateTypes) const; |
112 |
|
|
[[nodiscard]] QualType getWithBaseTemplateTypes(const QualTypeList &templateTypes) const; |
113 |
|
|
[[nodiscard]] QualType getWithFunctionParamAndReturnTypes(const QualTypeList ¶mAndReturnTypes) const; |
114 |
|
|
[[nodiscard]] QualType getWithFunctionParamAndReturnTypes(const QualType &returnType, const QualTypeList ¶mTypes) const; |
115 |
|
|
|
116 |
|
|
// Getters and setters on specifiers |
117 |
|
203993 |
[[nodiscard]] TypeSpecifiers &getSpecifiers() { return specifiers; } |
118 |
|
647350 |
[[nodiscard]] const TypeSpecifiers &getSpecifiers() const { return specifiers; } |
119 |
|
37923 |
void setSpecifiers(TypeSpecifiers newSpecifiers) { specifiers = newSpecifiers; } |
120 |
|
|
|
121 |
|
|
// Getters and setters on specifier parts |
122 |
|
|
[[nodiscard]] bool isConst() const; |
123 |
|
|
[[nodiscard]] bool isSigned() const; |
124 |
|
|
[[nodiscard]] bool isUnsigned() const; |
125 |
|
|
[[nodiscard]] bool isInline() const; |
126 |
|
|
[[nodiscard]] bool isPublic() const; |
127 |
|
|
[[nodiscard]] bool isHeap() const; |
128 |
|
|
[[nodiscard]] bool isComposition() const; |
129 |
|
|
void makeConst(bool isConst = true); |
130 |
|
|
void makeUnsigned(bool isUnsigned = true); |
131 |
|
|
void makePublic(bool isPublic = true); |
132 |
|
|
void makeHeap(bool isHeap = true); |
133 |
|
|
|
134 |
|
|
// Overloaded operators |
135 |
|
|
friend bool operator==(const QualType &lhs, const QualType &rhs); |
136 |
|
|
friend bool operator!=(const QualType &lhs, const QualType &rhs); |
137 |
|
|
|
138 |
|
|
// Public static methods |
139 |
|
|
static void unwrapBoth(QualType &typeA, QualType &typeB); |
140 |
|
|
|
141 |
|
|
private: |
142 |
|
|
// Private members |
143 |
|
|
const Type *type = nullptr; |
144 |
|
|
TypeSpecifiers specifiers; |
145 |
|
|
}; |
146 |
|
|
|
147 |
|
|
// Make sure we have no unexpected increases in memory consumption |
148 |
|
|
static_assert(sizeof(QualType) == 16); |
149 |
|
|
|
150 |
|
|
} // namespace spice::compiler |
151 |
|
|
|