GCC Code Coverage Report


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