GCC Code Coverage Report


Directory: ../
File: src/global/RuntimeModuleManager.cpp
Date: 2025-02-05 01:09:36
Exec Total Coverage
Lines: 38 40 95.0%
Functions: 5 5 100.0%
Branches: 33 64 51.6%

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