GCC Code Coverage Report


Directory: ../
File: src/global/RuntimeModuleManager.cpp
Date: 2025-10-09 06:28:01
Coverage Exec Excl Total
Lines: 95.0% 38 0 40
Functions: 100.0% 5 0 5
Branches: 51.6% 33 0 64

Line Branch Exec Source
1 // Copyright (c) 2021-2025 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/FileUtil.h>
10
11 namespace spice::compiler {
12
13 720 SourceFile *RuntimeModuleManager::requestModule(SourceFile *parentSourceFile, RuntimeModule requestedModule) {
14
2/4
✓ Branch 4 → 5 taken 720 times.
✗ Branch 4 → 25 not taken.
✓ Branch 5 → 6 taken 720 times.
✗ Branch 5 → 25 not taken.
720 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 720 times.
✗ Branch 7 → 31 not taken.
✓ Branch 8 → 9 taken 401 times.
✓ Branch 8 → 11 taken 319 times.
✓ Branch 9 → 10 taken 401 times.
✗ Branch 9 → 31 not taken.
✓ Branch 11 → 12 taken 319 times.
✗ Branch 11 → 31 not taken.
720 isModuleAvailable(requestedModule) ? getModule(requestedModule) : loadModule(parentSourceFile, requestedModule);
19
20 // Add the dependency to the parent source file
21
2/4
✓ Branch 13 → 14 taken 720 times.
✗ Branch 13 → 30 not taken.
✓ Branch 14 → 15 taken 720 times.
✗ Branch 14 → 28 not taken.
720 parentSourceFile->addDependency(rtFile, parentSourceFile->ast, importName, rtFile->filePath.string());
22
2/4
✓ Branch 16 → 17 taken 720 times.
✗ Branch 16 → 31 not taken.
✗ Branch 17 → 18 not taken.
✓ Branch 17 → 19 taken 720 times.
720 assert(parentSourceFile->dependencies.contains(importName));
23
1/2
✓ Branch 19 → 20 taken 720 times.
✗ Branch 19 → 31 not taken.
720 SourceFile *runtimeFile = parentSourceFile->dependencies.at(importName);
24
1/2
✓ Branch 20 → 21 taken 720 times.
✗ Branch 20 → 31 not taken.
720 modules.emplace(requestedModule, runtimeFile);
25
26 // Merge the module name registry with the one of the source file
27
1/2
✓ Branch 21 → 22 taken 720 times.
✗ Branch 21 → 31 not taken.
720 parentSourceFile->mergeNameRegistries(*rtFile, importName);
28
29 // Tell the source file, that the requested runtime has been imported
30 720 parentSourceFile->importedRuntimeModules |= requestedModule;
31
32 720 return rtFile;
33 720 }
34
35 2971 SourceFile *RuntimeModuleManager::getModule(RuntimeModule requestedModule) const {
36
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2971 times.
2971 assert(isModuleAvailable(requestedModule));
37 2971 return modules.at(requestedModule);
38 }
39
40 3691 bool RuntimeModuleManager::isModuleAvailable(RuntimeModule requestedModule) const { return modules.contains(requestedModule); }
41
42 319 SourceFile *RuntimeModuleManager::loadModule(SourceFile *parentSourceFile, RuntimeModule requestedModule) const {
43
1/2
✓ Branch 2 → 3 taken 319 times.
✗ Branch 2 → 61 not taken.
319 const auto [importName, fileName] = resolveNamePair(requestedModule);
44
2/4
✓ Branch 5 → 6 taken 319 times.
✗ Branch 5 → 35 not taken.
✓ Branch 6 → 7 taken 319 times.
✗ Branch 6 → 33 not taken.
319 const std::string fileNameWithExt = std::string(fileName) + ".spice";
45
5/10
✓ Branch 9 → 10 taken 319 times.
✗ Branch 9 → 50 not taken.
✓ Branch 10 → 11 taken 319 times.
✗ Branch 10 → 46 not taken.
✓ Branch 11 → 12 taken 319 times.
✗ Branch 11 → 43 not taken.
✓ Branch 12 → 13 taken 319 times.
✗ Branch 12 → 41 not taken.
✓ Branch 13 → 14 taken 319 times.
✗ Branch 13 → 39 not taken.
319 const std::filesystem::path filePath = FileUtil::getStdDir() / "runtime" / fileNameWithExt;
46
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 319 times.
319 assert(filePath != parentSourceFile->filePath);
47
48 // Instruct the global resource manager to create a new source file
49
2/4
✓ Branch 23 → 24 taken 319 times.
✗ Branch 23 → 53 not taken.
✓ Branch 24 → 25 taken 319 times.
✗ Branch 24 → 51 not taken.
638 SourceFile *moduleSourceFile = resourceManager.createSourceFile(parentSourceFile, importName, filePath, true);
50 319 moduleSourceFile->isMainFile = false;
51
52 // Run frontend and first type checker run for the loaded source file
53
1/2
✓ Branch 27 → 28 taken 319 times.
✗ Branch 27 → 57 not taken.
319 moduleSourceFile->runFrontEnd();
54
1/2
✓ Branch 28 → 29 taken 319 times.
✗ Branch 28 → 57 not taken.
319 moduleSourceFile->runTypeCheckerPre();
55
56 319 return moduleSourceFile;
57 319 }
58
59 1039 ModuleNamePair RuntimeModuleManager::resolveNamePair(RuntimeModule runtimeModule) {
60
5/6
✓ Branch 2 → 3 taken 264 times.
✓ Branch 2 → 4 taken 143 times.
✓ Branch 2 → 5 taken 330 times.
✓ Branch 2 → 6 taken 88 times.
✓ Branch 2 → 7 taken 214 times.
✗ Branch 2 → 8 not taken.
1039 switch (runtimeModule) {
61 264 case STRING_RT:
62 264 return {STRING_RT_IMPORT_NAME, "string_rt"};
63 143 case RESULT_RT:
64 143 return {RESULT_RT_IMPORT_NAME, "result_rt"};
65 330 case ERROR_RT:
66 330 return {ERROR_RT_IMPORT_NAME, "error_rt"};
67 88 case MEMORY_RT:
68 88 return {MEMORY_RT_IMPORT_NAME, "memory_rt"};
69 214 case RTTI_RT:
70 214 return {RTTI_RT_IMPORT_NAME, "rtti_rt"};
71 default:
72 throw CompilerError(INTERNAL_ERROR, "Requested unknown runtime module");
73 }
74 }
75
76 } // namespace spice::compiler
77