| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <string> | ||
| 6 | #include <utility> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include <symboltablebuilder/QualType.h> | ||
| 10 | #include <symboltablebuilder/TypeChain.h> | ||
| 11 | |||
| 12 | // LLVM forward declarations | ||
| 13 | namespace llvm { | ||
| 14 | class Type; | ||
| 15 | } // namespace llvm | ||
| 16 | |||
| 17 | namespace spice::compiler { | ||
| 18 | |||
| 19 | // Forward declarations | ||
| 20 | class SymbolTable; | ||
| 21 | class ASTNode; | ||
| 22 | class Scope; | ||
| 23 | class GenericType; | ||
| 24 | class Struct; | ||
| 25 | class Interface; | ||
| 26 | |||
| 27 | class Type { | ||
| 28 | public: | ||
| 29 | // Constructors | ||
| 30 | explicit Type(SuperType superType); | ||
| 31 | Type(SuperType superType, const std::string &subType); | ||
| 32 | Type(SuperType superType, const std::string &subType, uint64_t typeId, const TypeChainElementData &data, | ||
| 33 | const QualTypeList &templateTypes); | ||
| 34 | explicit Type(TypeChain typeChain); | ||
| 35 | |||
| 36 | // Getters and setters on type parts | ||
| 37 | [[nodiscard]] SuperType getSuperType() const; | ||
| 38 | [[nodiscard]] const std::string &getSubType() const; | ||
| 39 | [[nodiscard]] unsigned int getArraySize() const; | ||
| 40 | [[nodiscard]] Scope *getBodyScope() const; | ||
| 41 | [[nodiscard]] const QualType &getFunctionReturnType() const; | ||
| 42 | [[nodiscard]] QualTypeList getFunctionParamTypes() const; | ||
| 43 | [[nodiscard]] const QualTypeList &getFunctionParamAndReturnTypes() const; | ||
| 44 | [[nodiscard]] bool hasLambdaCaptures() const; | ||
| 45 | [[nodiscard]] const QualTypeList &getTemplateTypes() const; | ||
| 46 | |||
| 47 | // Queries on the type | ||
| 48 | [[nodiscard]] bool is(SuperType superType) const; | ||
| 49 | [[nodiscard]] bool isOneOf(const std::initializer_list<SuperType> &superTypes) const; | ||
| 50 | [[nodiscard]] bool isBase(SuperType superType) const; | ||
| 51 | [[nodiscard]] bool isPrimitive() const; | ||
| 52 | [[nodiscard]] bool isExtendedPrimitive() const; | ||
| 53 | [[nodiscard]] bool isPtr() const; | ||
| 54 | [[nodiscard]] bool isRef() const; | ||
| 55 | [[nodiscard]] bool isArray() const; | ||
| 56 | [[nodiscard]] bool hasAnyGenericParts() const; | ||
| 57 | |||
| 58 | // Complex queries on the type | ||
| 59 | [[nodiscard]] bool isSameContainerTypeAs(const Type *other) const; | ||
| 60 | [[nodiscard]] bool matches(const Type *otherType, bool ignoreArraySize) const; | ||
| 61 | |||
| 62 | // Serialization | ||
| 63 | void getName(std::stringstream &name, bool withSize, bool ignorePublic) const; | ||
| 64 | [[nodiscard]] std::string getName(bool withSize, bool ignorePublic) const; | ||
| 65 | |||
| 66 | // Get new type, based on this one | ||
| 67 | [[nodiscard]] const Type *toPtr(const ASTNode *node) const; | ||
| 68 | [[nodiscard]] const Type *toRef(const ASTNode *node) const; | ||
| 69 | [[nodiscard]] const Type *toArr(const ASTNode *node, unsigned int size, bool skipDynCheck) const; | ||
| 70 | [[nodiscard]] const Type *getContained() const; | ||
| 71 | [[nodiscard]] const Type *replaceBase(const Type *newBaseType) const; | ||
| 72 | [[nodiscard]] const Type *removeReferenceWrapper() const; | ||
| 73 | [[nodiscard]] const Type *getBase() const; | ||
| 74 | [[nodiscard]] const Type *getWithLambdaCaptures(bool enabled) const; | ||
| 75 | [[nodiscard]] const Type *getWithBodyScope(Scope *bodyScope) const; | ||
| 76 | [[nodiscard]] const Type *getWithTemplateTypes(const QualTypeList &templateTypes) const; | ||
| 77 | [[nodiscard]] const Type *getWithBaseTemplateTypes(const QualTypeList &templateTypes) const; | ||
| 78 | [[nodiscard]] const Type *getWithFunctionParamAndReturnTypes(const QualTypeList ¶mAndReturnTypes) const; | ||
| 79 | |||
| 80 | // LLVM helpers | ||
| 81 | [[nodiscard]] llvm::Type *toLLVMType(SourceFile *sourceFile) const; | ||
| 82 | |||
| 83 | // Public static methods | ||
| 84 | static void unwrapBoth(const Type *&typeA, const Type *&typeB); | ||
| 85 | static bool hasSameTypeChainDepth(const Type *typeA, const Type *typeB); | ||
| 86 | |||
| 87 | // Public members | ||
| 88 | TypeChain typeChain; | ||
| 89 | }; | ||
| 90 | |||
| 91 | // Make sure we have no unexpected increases in memory consumption | ||
| 92 | static_assert(sizeof(Type) == 24); | ||
| 93 | |||
| 94 | } // namespace spice::compiler | ||
| 95 |