GCC Code Coverage Report


Directory: ../
Coverage: low: ≥ 0% medium: ≥ 75.0% high: ≥ 90.0%
Coverage Exec / Excl / Total
Lines: 100.0% 39 / 2 / 41
Functions: 100.0% 5 / 0 / 5
Branches: 55.0% 33 / 4 / 64

src/global/RuntimeModuleManager.cpp
Line Branch Exec Source
1 // Copyright (c) 2021-2026 ChilliBits. All rights reserved.
2
3 #include "RuntimeModuleManager.h"
4
5 #include <SourceFile.h>
6 #include <exception/CompilerError.h>
7 #include <global/GlobalResourceManager.h>
8 #include <symboltablebuilder/Scope.h>
9 #include <util/SystemUtil.h>
10
11 namespace spice::compiler {
12
13 2222 SourceFile *RuntimeModuleManager::requestModule(SourceFile *parentSourceFile, RuntimeModule requestedModule) {
14
2/4
✓ Branch 4 → 5 taken 2222 times.
✗ Branch 4 → 23 not taken.
✓ Branch 5 → 6 taken 2222 times.
✗ Branch 5 → 23 not taken.
4444 const std::string importName = resolveNamePair(requestedModule).importName;
15
16 // Check if the requested module is available already, if not load it
17 const auto rtFile =
18
5/8
✓ Branch 7 → 8 taken 2222 times.
✗ Branch 7 → 26 not taken.
✓ Branch 8 → 9 taken 1571 times.
✓ Branch 8 → 11 taken 651 times.
✓ Branch 9 → 10 taken 1571 times.
✗ Branch 9 → 26 not taken.
✓ Branch 11 → 12 taken 651 times.
✗ Branch 11 → 26 not taken.
2222 isModuleAvailable(requestedModule) ? getModule(requestedModule) : loadModule(parentSourceFile, requestedModule);
19
20 // Add the dependency to the parent source file
21
1/2
✓ Branch 13 → 14 taken 2222 times.
✗ Branch 13 → 26 not taken.
2222 parentSourceFile->addDependency(rtFile, importName);
22
2/4
✓ Branch 14 → 15 taken 2222 times.
✗ Branch 14 → 26 not taken.
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 2222 times.
2222 assert(parentSourceFile->dependencies.contains(importName));
23
1/2
✓ Branch 17 → 18 taken 2222 times.
✗ Branch 17 → 26 not taken.
2222 SourceFile *runtimeFile = parentSourceFile->dependencies.at(importName);
24
1/2
✓ Branch 18 → 19 taken 2222 times.
✗ Branch 18 → 26 not taken.
2222 modules.emplace(requestedModule, runtimeFile);
25
26 // Merge the module name registry with the one of the source file
27
1/2
✓ Branch 19 → 20 taken 2222 times.
✗ Branch 19 → 26 not taken.
2222 parentSourceFile->mergeNameRegistries(*rtFile, importName);
28
29 // Tell the source file, that the requested runtime has been imported
30 2222 parentSourceFile->importedRuntimeModules |= requestedModule;
31
32 2222 return rtFile;
33 2222 }
34
35 11679 SourceFile *RuntimeModuleManager::getModule(RuntimeModule requestedModule) const {
36
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 11679 times.
11679 assert(isModuleAvailable(requestedModule));
37 11679 return modules.at(requestedModule);
38 }
39
40 13901 bool RuntimeModuleManager::isModuleAvailable(RuntimeModule requestedModule) const { return modules.contains(requestedModule); }
41
42 651 SourceFile *RuntimeModuleManager::loadModule(SourceFile *parentSourceFile, RuntimeModule requestedModule) const {
43
1/2
✓ Branch 2 → 3 taken 651 times.
✗ Branch 2 → 62 not taken.
651 const auto [importName, fileName] = resolveNamePair(requestedModule);
44
2/4
✓ Branch 5 → 6 taken 651 times.
✗ Branch 5 → 36 not taken.
✓ Branch 6 → 7 taken 651 times.
✗ Branch 6 → 34 not taken.
1302 const std::string fileNameWithExt = std::string(fileName) + ".spice";
45
5/10
✓ Branch 9 → 10 taken 651 times.
✗ Branch 9 → 51 not taken.
✓ Branch 10 → 11 taken 651 times.
✗ Branch 10 → 47 not taken.
✓ Branch 11 → 12 taken 651 times.
✗ Branch 11 → 44 not taken.
✓ Branch 12 → 13 taken 651 times.
✗ Branch 12 → 42 not taken.
✓ Branch 13 → 14 taken 651 times.
✗ Branch 13 → 40 not taken.
651 const std::filesystem::path filePath = SystemUtil::getStdDir() / "runtime" / fileNameWithExt;
46
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 651 times.
651 assert(filePath != parentSourceFile->filePath);
47
48 // Instruct the global resource manager to create a new source file
49
2/4
✓ Branch 23 → 24 taken 651 times.
✗ Branch 23 → 54 not taken.
✓ Branch 24 → 25 taken 651 times.
✗ Branch 24 → 52 not taken.
1302 SourceFile *moduleSourceFile = resourceManager.createSourceFile(parentSourceFile, importName, filePath, true);
50 651 moduleSourceFile->isMainFile = false;
51
52 // Run frontend and first type checker run for the loaded source file
53
1/2
✓ Branch 27 → 28 taken 651 times.
✗ Branch 27 → 58 not taken.
651 moduleSourceFile->runFrontEnd();
54 // Merge the runtime module's own dependency registries (deferred tail of the front-end, see SourceFile::runMiddleEnd)
55
1/2
✓ Branch 28 → 29 taken 651 times.
✗ Branch 28 → 58 not taken.
651 moduleSourceFile->mergeNameRegistriesRecursive();
56
1/2
✓ Branch 29 → 30 taken 651 times.
✗ Branch 29 → 58 not taken.
651 moduleSourceFile->runTypeCheckerPre();
57
58 651 return moduleSourceFile;
59 651 }
60
61 2873 ModuleNamePair RuntimeModuleManager::resolveNamePair(RuntimeModule runtimeModule) {
62
5/6
✓ Branch 2 → 3 taken 715 times.
✓ Branch 2 → 4 taken 431 times.
✓ Branch 2 → 5 taken 790 times.
✓ Branch 2 → 6 taken 300 times.
✓ Branch 2 → 7 taken 637 times.
✗ Branch 2 → 8 not taken.
2873 switch (runtimeModule) {
63 715 case STRING_RT:
64 715 return {STRING_RT_IMPORT_NAME, "string_rt"};
65 431 case RESULT_RT:
66 431 return {RESULT_RT_IMPORT_NAME, "result_rt"};
67 790 case ERROR_RT:
68 790 return {ERROR_RT_IMPORT_NAME, "error_rt"};
69 300 case MEMORY_RT:
70 300 return {MEMORY_RT_IMPORT_NAME, "memory_rt"};
71 637 case RTTI_RT:
72 637 return {RTTI_RT_IMPORT_NAME, "rtti_rt"};
73 default: // LCOV_EXCL_LINE
74 throw CompilerError(INTERNAL_ERROR, "Requested unknown runtime module"); // LCOV_EXCL_LINE
75 }
76 }
77
78 } // namespace spice::compiler
79