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