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