GCC Code Coverage Report


Directory: ../
File: src/global/RuntimeModuleManager.h
Date: 2024-12-24 01:17:15
Exec Total Coverage
Lines: 1 1 100.0%
Functions: 1 1 100.0%
Branches: 0 0 -%

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