| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2025 ChilliBits. All rights reserved. | ||
| 2 | |||
| 3 | #include "ImportCollector.h" | ||
| 4 | |||
| 5 | #include <SourceFile.h> | ||
| 6 | #include <ast/ASTNodes.h> | ||
| 7 | #include <ast/Attributes.h> | ||
| 8 | #include <exception/SemanticError.h> | ||
| 9 | #include <global/GlobalResourceManager.h> | ||
| 10 | #include <util/FileUtil.h> | ||
| 11 | |||
| 12 | namespace spice::compiler { | ||
| 13 | |||
| 14 | 1135 | std::any ImportCollector::visitEntry(EntryNode *node) { | |
| 15 | // Visit all module attributes | ||
| 16 |
2/2✓ Branch 9 → 4 taken 335 times.
✓ Branch 9 → 10 taken 1135 times.
|
1470 | for (ModAttrNode *attr : node->modAttrs) |
| 17 |
1/2✓ Branch 5 → 6 taken 335 times.
✗ Branch 5 → 21 not taken.
|
335 | visit(attr); |
| 18 | |||
| 19 | // Visit all import defs | ||
| 20 |
2/2✓ Branch 17 → 12 taken 604 times.
✓ Branch 17 → 18 taken 1130 times.
|
1734 | for (ImportDefNode *importDef : node->importDefs) |
| 21 |
2/2✓ Branch 13 → 14 taken 599 times.
✓ Branch 13 → 23 taken 5 times.
|
604 | visit(importDef); |
| 22 | |||
| 23 |
1/2✓ Branch 18 → 19 taken 1130 times.
✗ Branch 18 → 25 not taken.
|
1130 | return nullptr; |
| 24 | } | ||
| 25 | |||
| 26 | 604 | std::any ImportCollector::visitImportDef(ImportDefNode *node) { | |
| 27 | 604 | const bool isStd = node->importPath.starts_with("std/"); | |
| 28 | 604 | const bool isBootstrap = node->importPath.starts_with("bootstrap/"); | |
| 29 | |||
| 30 | 604 | std::filesystem::path basePath; | |
| 31 |
2/2✓ Branch 5 → 6 taken 555 times.
✓ Branch 5 → 27 taken 49 times.
|
604 | if (isStd) { // Include source file from standard library |
| 32 | // Find std library | ||
| 33 |
1/2✓ Branch 6 → 7 taken 555 times.
✗ Branch 6 → 171 not taken.
|
555 | const std::filesystem::path stdPath = FileUtil::getStdDir(); |
| 34 |
1/2✗ Branch 8 → 9 not taken.
✓ Branch 8 → 17 taken 555 times.
|
555 | if (stdPath.empty()) |
| 35 | ✗ | throw CompilerError(STD_NOT_FOUND, "Standard library could not be found. Check if the env var SPICE_STD_DIR exists"); | |
| 36 | // Format: /dir/to/path/file | ||
| 37 |
3/6✓ Branch 18 → 19 taken 555 times.
✗ Branch 18 → 166 not taken.
✓ Branch 19 → 20 taken 555 times.
✗ Branch 19 → 164 not taken.
✓ Branch 20 → 21 taken 555 times.
✗ Branch 20 → 162 not taken.
|
555 | basePath = stdPath / node->importPath.substr(node->importPath.find("std/") + 4); |
| 38 |
2/2✓ Branch 27 → 28 taken 18 times.
✓ Branch 27 → 49 taken 31 times.
|
604 | } else if (isBootstrap) { // Include source file from bootstrap library |
| 39 | // Find bootstrap library | ||
| 40 |
1/2✓ Branch 28 → 29 taken 18 times.
✗ Branch 28 → 190 not taken.
|
18 | const std::filesystem::path bootstrapPath = FileUtil::getBootstrapDir(); |
| 41 |
1/2✗ Branch 30 → 31 not taken.
✓ Branch 30 → 39 taken 18 times.
|
18 | if (bootstrapPath.empty()) |
| 42 | ✗ | throw CompilerError(BOOTSTRAP_NOT_FOUND, | |
| 43 | ✗ | "Bootstrap compiler could not be found. Check if the env var SPICE_BOOTSTRAP_DIR exists"); | |
| 44 | // Format: /dir/to/path/file | ||
| 45 |
3/6✓ Branch 40 → 41 taken 18 times.
✗ Branch 40 → 185 not taken.
✓ Branch 41 → 42 taken 18 times.
✗ Branch 41 → 183 not taken.
✓ Branch 42 → 43 taken 18 times.
✗ Branch 42 → 181 not taken.
|
18 | basePath = bootstrapPath / node->importPath.substr(node->importPath.find("bootstrap/") + 10); |
| 46 | 18 | } else { // Include own source file | |
| 47 | // Format: /dir/to/path/file | ||
| 48 |
3/6✓ Branch 49 → 50 taken 31 times.
✗ Branch 49 → 196 not taken.
✓ Branch 50 → 51 taken 31 times.
✗ Branch 50 → 193 not taken.
✓ Branch 51 → 52 taken 31 times.
✗ Branch 51 → 191 not taken.
|
31 | basePath = sourceFile->filePath.parent_path() / node->importPath; |
| 49 | } | ||
| 50 | 604 | basePath.make_preferred(); | |
| 51 | |||
| 52 | // Format: /dir/to/path/file.spice | ||
| 53 |
1/2✓ Branch 58 → 59 taken 604 times.
✗ Branch 58 → 282 not taken.
|
604 | std::filesystem::path defaultPath = basePath; |
| 54 |
5/10✓ Branch 59 → 60 taken 604 times.
✗ Branch 59 → 206 not taken.
✓ Branch 60 → 61 taken 604 times.
✗ Branch 60 → 204 not taken.
✓ Branch 61 → 62 taken 604 times.
✗ Branch 61 → 202 not taken.
✓ Branch 62 → 63 taken 604 times.
✗ Branch 62 → 200 not taken.
✓ Branch 63 → 64 taken 604 times.
✗ Branch 63 → 198 not taken.
|
604 | defaultPath.replace_filename(basePath.stem().string() + ".spice"); |
| 55 | // Format: /dir/to/path/file_linux.spice | ||
| 56 |
1/2✓ Branch 68 → 69 taken 604 times.
✗ Branch 68 → 280 not taken.
|
604 | std::filesystem::path osPath = basePath; |
| 57 |
7/14✓ Branch 69 → 70 taken 604 times.
✗ Branch 69 → 222 not taken.
✓ Branch 70 → 71 taken 604 times.
✗ Branch 70 → 220 not taken.
✓ Branch 71 → 72 taken 604 times.
✗ Branch 71 → 218 not taken.
✓ Branch 72 → 73 taken 604 times.
✗ Branch 72 → 216 not taken.
✓ Branch 73 → 74 taken 604 times.
✗ Branch 73 → 214 not taken.
✓ Branch 74 → 75 taken 604 times.
✗ Branch 74 → 212 not taken.
✓ Branch 75 → 76 taken 604 times.
✗ Branch 75 → 210 not taken.
|
604 | osPath.replace_filename(basePath.stem().string() + "_" + cliOptions.targetOs + ".spice"); |
| 58 | // Format: /dir/to/path/file_linux_x86_64.spice | ||
| 59 |
1/2✓ Branch 82 → 83 taken 604 times.
✗ Branch 82 → 278 not taken.
|
604 | std::filesystem::path osArchPath = basePath; |
| 60 |
9/18✓ Branch 83 → 84 taken 604 times.
✗ Branch 83 → 244 not taken.
✓ Branch 84 → 85 taken 604 times.
✗ Branch 84 → 242 not taken.
✓ Branch 85 → 86 taken 604 times.
✗ Branch 85 → 240 not taken.
✓ Branch 86 → 87 taken 604 times.
✗ Branch 86 → 238 not taken.
✓ Branch 87 → 88 taken 604 times.
✗ Branch 87 → 236 not taken.
✓ Branch 88 → 89 taken 604 times.
✗ Branch 88 → 234 not taken.
✓ Branch 89 → 90 taken 604 times.
✗ Branch 89 → 232 not taken.
✓ Branch 90 → 91 taken 604 times.
✗ Branch 90 → 230 not taken.
✓ Branch 91 → 92 taken 604 times.
✗ Branch 91 → 228 not taken.
|
604 | osArchPath.replace_filename(basePath.stem().string() + "_" + cliOptions.targetOs + "_" + cliOptions.targetArch + ".spice"); |
| 61 | |||
| 62 | // Check which source file to import | ||
| 63 | 604 | std::filesystem::path importPath; | |
| 64 |
7/10✓ Branch 101 → 102 taken 604 times.
✗ Branch 101 → 274 not taken.
✓ Branch 102 → 103 taken 1 time.
✓ Branch 102 → 106 taken 603 times.
✓ Branch 103 → 104 taken 1 time.
✗ Branch 103 → 274 not taken.
✓ Branch 104 → 105 taken 1 time.
✗ Branch 104 → 106 not taken.
✓ Branch 107 → 108 taken 1 time.
✓ Branch 107 → 109 taken 603 times.
|
604 | if (exists(osArchPath) && !equivalent(sourceFile->filePath, osArchPath)) // file_os_arch.spice is first choice |
| 65 |
1/2✓ Branch 108 → 127 taken 1 time.
✗ Branch 108 → 274 not taken.
|
1 | importPath = osArchPath; |
| 66 |
7/10✓ Branch 109 → 110 taken 603 times.
✗ Branch 109 → 274 not taken.
✓ Branch 110 → 111 taken 21 times.
✓ Branch 110 → 114 taken 582 times.
✓ Branch 111 → 112 taken 21 times.
✗ Branch 111 → 274 not taken.
✓ Branch 112 → 113 taken 21 times.
✗ Branch 112 → 114 not taken.
✓ Branch 115 → 116 taken 21 times.
✓ Branch 115 → 117 taken 582 times.
|
603 | else if (exists(osPath) && !equivalent(sourceFile->filePath, osPath)) // file_os.spice is second choice |
| 67 |
1/2✓ Branch 116 → 127 taken 21 times.
✗ Branch 116 → 274 not taken.
|
21 | importPath = osPath; |
| 68 |
3/4✓ Branch 117 → 118 taken 582 times.
✗ Branch 117 → 274 not taken.
✓ Branch 118 → 119 taken 580 times.
✓ Branch 118 → 120 taken 2 times.
|
582 | else if (exists(defaultPath)) // file.spice is third choice |
| 69 |
1/2✓ Branch 119 → 127 taken 580 times.
✗ Branch 119 → 274 not taken.
|
580 | importPath = defaultPath; |
| 70 | else | ||
| 71 |
3/6✓ Branch 121 → 122 taken 2 times.
✗ Branch 121 → 257 not taken.
✓ Branch 122 → 123 taken 2 times.
✗ Branch 122 → 255 not taken.
✓ Branch 123 → 124 taken 2 times.
✗ Branch 123 → 252 not taken.
|
2 | throw SemanticError(node, IMPORTED_FILE_NOT_EXISTING, "The source file '" + node->importPath + ".spice' does not exist"); |
| 72 | |||
| 73 | // Check if the import already exists | ||
| 74 |
1/2✓ Branch 127 → 128 taken 602 times.
✗ Branch 127 → 274 not taken.
|
602 | SymbolTableEntry *importEntry = rootScope->lookupStrict(node->importName); |
| 75 |
2/2✓ Branch 130 → 131 taken 2 times.
✓ Branch 130 → 138 taken 600 times.
|
602 | if (importEntry != nullptr) |
| 76 |
3/6✓ Branch 132 → 133 taken 2 times.
✗ Branch 132 → 266 not taken.
✓ Branch 133 → 134 taken 2 times.
✗ Branch 133 → 264 not taken.
✓ Branch 134 → 135 taken 2 times.
✗ Branch 134 → 261 not taken.
|
2 | throw SemanticError(node, DUPLICATE_IMPORT_NAME, "Duplicate import '" + node->importName + "'"); |
| 77 | |||
| 78 | // Create symbol for import | ||
| 79 |
1/2✓ Branch 138 → 139 taken 600 times.
✗ Branch 138 → 274 not taken.
|
600 | node->entry = rootScope->insert(node->importName, node); |
| 80 | |||
| 81 | // Create the imported source file | ||
| 82 |
1/2✓ Branch 141 → 142 taken 600 times.
✗ Branch 141 → 274 not taken.
|
600 | const auto importedSourceFile = resourceManager.createSourceFile(sourceFile, node->importName, importPath, isStd); |
| 83 | // Register it as a dependency to the current source file | ||
| 84 |
3/4✓ Branch 142 → 143 taken 600 times.
✗ Branch 142 → 272 not taken.
✓ Branch 143 → 144 taken 599 times.
✓ Branch 143 → 270 taken 1 time.
|
601 | sourceFile->addDependency(importedSourceFile, node, node->importName, importPath.generic_string()); |
| 85 | |||
| 86 |
1/2✓ Branch 145 → 146 taken 599 times.
✗ Branch 145 → 273 not taken.
|
1198 | return nullptr; |
| 87 | 624 | } | |
| 88 | |||
| 89 | 335 | std::any ImportCollector::visitModAttr(ModAttrNode *node) { | |
| 90 | // !!! Only bool attributes allowed here, due to missing attribute value checks being executed in a later stage !!! | ||
| 91 | |||
| 92 | // core.compiler.keep-on-name-collision | ||
| 93 |
4/6✓ Branch 4 → 5 taken 335 times.
✗ Branch 4 → 35 not taken.
✓ Branch 5 → 6 taken 335 times.
✗ Branch 5 → 33 not taken.
✓ Branch 8 → 9 taken 325 times.
✓ Branch 8 → 16 taken 10 times.
|
1005 | if (node->attrLst->hasAttr(ATTR_CORE_COMPILER_KEEP_ON_NAME_COLLISION)) { |
| 94 |
2/4✓ Branch 11 → 12 taken 325 times.
✗ Branch 11 → 41 not taken.
✓ Branch 12 → 13 taken 325 times.
✗ Branch 12 → 39 not taken.
|
650 | const bool keepOnCollision = node->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_KEEP_ON_NAME_COLLISION)->boolValue; |
| 95 | 325 | sourceFile->alwaysKeepSymbolsOnNameCollision = keepOnCollision; | |
| 96 | } | ||
| 97 | |||
| 98 | // core.compiler.warnings.ignore | ||
| 99 |
4/6✓ Branch 18 → 19 taken 335 times.
✗ Branch 18 → 47 not taken.
✓ Branch 19 → 20 taken 335 times.
✗ Branch 19 → 45 not taken.
✓ Branch 22 → 23 taken 2 times.
✓ Branch 22 → 30 taken 333 times.
|
1005 | if (node->attrLst->hasAttr(ATTR_CORE_COMPILER_WARNINGS_IGNORE)) { |
| 100 |
2/4✓ Branch 25 → 26 taken 2 times.
✗ Branch 25 → 53 not taken.
✓ Branch 26 → 27 taken 2 times.
✗ Branch 26 → 51 not taken.
|
4 | const bool ignoreWarnings = node->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_WARNINGS_IGNORE)->boolValue; |
| 101 | 2 | sourceFile->ignoreWarnings = ignoreWarnings; | |
| 102 | } | ||
| 103 | |||
| 104 |
1/2✓ Branch 30 → 31 taken 335 times.
✗ Branch 30 → 57 not taken.
|
335 | return nullptr; |
| 105 | } | ||
| 106 | |||
| 107 | } // namespace spice::compiler | ||
| 108 |