GCC Code Coverage Report


Directory: ../
File: src/global/RuntimeModuleManager.h
Date: 2025-06-14 23:29:02
Exec Total Coverage
Lines: 1 1 100.0%
Functions: 1 1 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 <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 {"sNew", MEMORY_RT},
45 {"sPlacementNew", MEMORY_RT},
46 {"sDestroy", MEMORY_RT},
47 {"sDealloc", MEMORY_RT},
48 {"sDelete", MEMORY_RT},
49 {"sCompare", MEMORY_RT},
50 // Result RT
51 {"ok", RESULT_RT},
52 {"err", RESULT_RT},
53 };
54
55 // This serves for the compiler to detect if a source file is a specific runtime module
56 const std::unordered_map<RuntimeModule, const char *> IDENTIFYING_TOP_LEVEL_NAMES = {
57 {STRING_RT, STROBJ_NAME}, // String struct
58 {RESULT_RT, RESULTOBJ_NAME}, // Result struct
59 {ERROR_RT, ERROBJ_NAME}, // Error struct
60 {MEMORY_RT, "sAlloc"}, // sAlloc function
61 {RTTI_RT, TIOBJ_NAME}, // TypeInfo struct
62 };
63
64 struct ModuleNamePair {
65 const char *const importName;
66 const char *const fileName;
67 };
68
69 class RuntimeModuleManager {
70 public:
71 // Constructors
72 410 explicit RuntimeModuleManager(GlobalResourceManager &resourceManager) : resourceManager(resourceManager) {}
73 RuntimeModuleManager(const RuntimeModuleManager &) = delete;
74
75 // Public methods
76 SourceFile *requestModule(SourceFile *parentSourceFile, RuntimeModule requestedModule);
77 [[nodiscard]] SourceFile *getModule(RuntimeModule requestedModule) const;
78 [[nodiscard]] bool isModuleAvailable(RuntimeModule requestedModule) const;
79
80 private:
81 // Private methods
82 SourceFile *loadModule(SourceFile *parentSourceFile, RuntimeModule requestedModule) const;
83 static ModuleNamePair resolveNamePair(RuntimeModule runtimeModule);
84
85 // Private members
86 GlobalResourceManager &resourceManager;
87 std::unordered_map<RuntimeModule, SourceFile *> modules;
88 };
89
90 } // namespace spice::compiler
91