src/symboltablebuilder/TypeChain.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <cstdint> | ||
| 6 | #include <string> | ||
| 7 | #include <vector> | ||
| 8 | |||
| 9 | #include <symboltablebuilder/QualType.h> | ||
| 10 | |||
| 11 | namespace spice::compiler { | ||
| 12 | |||
| 13 | // Forward declarations | ||
| 14 | class Scope; | ||
| 15 | |||
| 16 | // Constants | ||
| 17 | static constexpr long ARRAY_SIZE_UNKNOWN = 0; | ||
| 18 | |||
| 19 | // New super types must be appended at the end: TypeChainElement's hash (see CustomHashFunctions.cpp) and other | ||
| 20 | // reference-tested output (e.g. the TySan instrumentation's type hashes) incorporate the raw enum ordinal, so | ||
| 21 | // inserting a value in the middle would shift every later ordinal and silently change those hashes. | ||
| 22 | enum SuperType : uint8_t { | ||
| 23 | TY_INVALID, | ||
| 24 | TY_UNRESOLVED, | ||
| 25 | TY_DOUBLE, | ||
| 26 | TY_INT, | ||
| 27 | TY_SHORT, | ||
| 28 | TY_LONG, | ||
| 29 | TY_BYTE, | ||
| 30 | TY_CHAR, | ||
| 31 | TY_STRING, // Alias for 'const char*' | ||
| 32 | TY_BOOL, | ||
| 33 | TY_STRUCT, | ||
| 34 | TY_INTERFACE, | ||
| 35 | TY_ENUM, | ||
| 36 | TY_GENERIC, | ||
| 37 | TY_ALIAS, | ||
| 38 | TY_DYN, | ||
| 39 | TY_PTR, | ||
| 40 | TY_REF, | ||
| 41 | TY_ARRAY, | ||
| 42 | TY_FUNCTION, | ||
| 43 | TY_PROCEDURE, | ||
| 44 | TY_IMPORT, | ||
| 45 | TY_UNION, | ||
| 46 | }; | ||
| 47 | |||
| 48 | union TypeChainElementData { | ||
| 49 | unsigned int arraySize; // TY_ARRAY | ||
| 50 | Scope *bodyScope = nullptr; // TY_STRUCT, TY_INTERFACE, TY_UNION, TY_ENUM | ||
| 51 | bool hasCaptures; // TY_FUNCTION, TY_PROCEDURE (lambdas) or TY_STRUCT (special Lambda std type only) | ||
| 52 | }; | ||
| 53 | |||
| 54 | // Structs | ||
| 55 | struct TypeChainElement { | ||
| 56 | // Constructors | ||
| 57 | TypeChainElement() = default; | ||
| 58 | 10450378 | explicit TypeChainElement(SuperType superType) : superType(superType), typeId(superType) {} | |
| 59 | 12457 | TypeChainElement(SuperType superType, std::string subType) | |
| 60 | 24914 | : superType(superType), subType(std::move(subType)), typeId(superType) {} | |
| 61 | 1887 | TypeChainElement(SuperType superType, TypeChainElementData data) : superType(superType), typeId(superType), data(data) {} | |
| 62 | 14309 | TypeChainElement(SuperType superType, std::string subType, uint64_t typeId, TypeChainElementData data, | |
| 63 | QualTypeList templateTypes) | ||
| 64 | 42927 | : superType(superType), subType(std::move(subType)), typeId(typeId), data(data), templateTypes(std::move(templateTypes)) {} | |
| 65 | |||
| 66 | // Overloaded operators | ||
| 67 | friend bool operator==(const TypeChainElement &lhs, const TypeChainElement &rhs); | ||
| 68 | friend bool operator!=(const TypeChainElement &lhs, const TypeChainElement &rhs); | ||
| 69 | void getName(std::stringstream &name, bool withSize, bool ignorePublic, bool withAliases) const; | ||
| 70 | [[nodiscard]] std::string getName(bool withSize, bool ignorePublic, bool withAliases) const; | ||
| 71 | |||
| 72 | // Public members | ||
| 73 | SuperType superType = TY_INVALID; | ||
| 74 | std::string subType; | ||
| 75 | uint64_t typeId = TY_INVALID; | ||
| 76 | TypeChainElementData data = {.arraySize = 0}; | ||
| 77 | QualTypeList templateTypes; | ||
| 78 | QualTypeList paramTypes; // First type is the return type | ||
| 79 | }; | ||
| 80 | |||
| 81 | // Make sure we have no unexpected increases in memory consumption | ||
| 82 | #if defined(__clang__) && defined(__apple_build_version__) | ||
| 83 | // std::string for Apple Clang is smaller than std::string for GCC and Clang | ||
| 84 | static_assert(sizeof(TypeChainElement) == 96); | ||
| 85 | #else | ||
| 86 | static_assert(sizeof(TypeChainElement) == 104); | ||
| 87 | #endif | ||
| 88 | |||
| 89 | // Typedefs | ||
| 90 | using TypeChain = std::vector<TypeChainElement>; | ||
| 91 | |||
| 92 | } // namespace spice::compiler | ||
| 93 |