| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #pragma once | ||
| 4 | |||
| 5 | #include <unordered_map> | ||
| 6 | |||
| 7 | #include <symboltablebuilder/QualType.h> | ||
| 8 | |||
| 9 | namespace spice::compiler { | ||
| 10 | |||
| 11 | // Forward declaration | ||
| 12 | class GlobalResourceManager; | ||
| 13 | class SourceFile; | ||
| 14 | class Scope; | ||
| 15 | |||
| 16 | const char *const STRING_RT_IMPORT_NAME = "__rt_string"; | ||
| 17 | const char *const RESULT_RT_IMPORT_NAME = "__rt_result"; | ||
| 18 | const char *const ERROR_RT_IMPORT_NAME = "__rt_error"; | ||
| 19 | const char *const MEMORY_RT_IMPORT_NAME = "__rt_memory"; | ||
| 20 | const char *const RTTI_RT_IMPORT_NAME = "__rt_rtti"; | ||
| 21 | |||
| 22 | enum RuntimeModule : uint8_t { | ||
| 23 | STRING_RT = 1 << 0, | ||
| 24 | RESULT_RT = 1 << 1, | ||
| 25 | ERROR_RT = 1 << 2, | ||
| 26 | MEMORY_RT = 1 << 3, | ||
| 27 | RTTI_RT = 1 << 4, | ||
| 28 | }; | ||
| 29 | |||
| 30 | const std::unordered_map<const char *, RuntimeModule> TYPE_NAME_TO_RT_MODULE_MAPPING = { | ||
| 31 | {STROBJ_NAME, STRING_RT}, | ||
| 32 | {RESULTOBJ_NAME, RESULT_RT}, | ||
| 33 | {ERROBJ_NAME, ERROR_RT}, | ||
| 34 | }; | ||
| 35 | |||
| 36 | const std::unordered_map<const char *, RuntimeModule> FCT_NAME_TO_RT_MODULE_MAPPING = { | ||
| 37 | // Memory RT | ||
| 38 | {"sAlloc", MEMORY_RT}, | ||
| 39 | {"sAllocUnsafe", MEMORY_RT}, | ||
| 40 | {"sRealloc", MEMORY_RT}, | ||
| 41 | {"sReallocUnsafe", MEMORY_RT}, | ||
| 42 | {"sCopy", MEMORY_RT}, | ||
| 43 | {"sCopyUnsafe", MEMORY_RT}, | ||
| 44 | {"sMove", MEMORY_RT}, | ||
| 45 | {"sNew", MEMORY_RT}, | ||
| 46 | {"sPlacementNew", MEMORY_RT}, | ||
| 47 | {"sDestruct", MEMORY_RT}, | ||
| 48 | {"sDealloc", MEMORY_RT}, | ||
| 49 | {"sDelete", MEMORY_RT}, | ||
| 50 | {"sYieldOwnership", MEMORY_RT}, | ||
| 51 | {"sCompare", MEMORY_RT}, | ||
| 52 | // Result RT | ||
| 53 | {"ok", RESULT_RT}, | ||
| 54 | {"err", RESULT_RT}, | ||
| 55 | }; | ||
| 56 | |||
| 57 | // This serves for the compiler to detect if a source file is a specific runtime module | ||
| 58 | const std::unordered_map<RuntimeModule, const char *> IDENTIFYING_TOP_LEVEL_NAMES = { | ||
| 59 | {STRING_RT, STROBJ_NAME}, // String struct | ||
| 60 | {RESULT_RT, RESULTOBJ_NAME}, // Result struct | ||
| 61 | {ERROR_RT, ERROBJ_NAME}, // Error struct | ||
| 62 | {MEMORY_RT, "sAlloc"}, // sAlloc function | ||
| 63 | {RTTI_RT, TIOBJ_NAME}, // TypeInfo struct | ||
| 64 | }; | ||
| 65 | |||
| 66 | struct ModuleNamePair { | ||
| 67 | const char *const importName; | ||
| 68 | const char *const fileName; | ||
| 69 | }; | ||
| 70 | |||
| 71 | class RuntimeModuleManager { | ||
| 72 | public: | ||
| 73 | // Constructors | ||
| 74 | 420 | explicit RuntimeModuleManager(GlobalResourceManager &resourceManager) : resourceManager(resourceManager) {} | |
| 75 | RuntimeModuleManager(const RuntimeModuleManager &) = delete; | ||
| 76 | |||
| 77 | // Public methods | ||
| 78 | SourceFile *requestModule(SourceFile *parentSourceFile, RuntimeModule requestedModule); | ||
| 79 | [[nodiscard]] SourceFile *getModule(RuntimeModule requestedModule) const; | ||
| 80 | [[nodiscard]] bool isModuleAvailable(RuntimeModule requestedModule) const; | ||
| 81 | |||
| 82 | private: | ||
| 83 | // Private methods | ||
| 84 | SourceFile *loadModule(SourceFile *parentSourceFile, RuntimeModule requestedModule) const; | ||
| 85 | static ModuleNamePair resolveNamePair(RuntimeModule runtimeModule); | ||
| 86 | |||
| 87 | // Private members | ||
| 88 | GlobalResourceManager &resourceManager; | ||
| 89 | std::unordered_map<RuntimeModule, SourceFile *> modules; | ||
| 90 | }; | ||
| 91 | |||
| 92 | } // namespace spice::compiler | ||
| 93 |