GCC Code Coverage Report


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