src/importcollector/ImportCollector.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // Copyright (c) 2021-2026 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 <driver/Driver.h> | ||
| 9 | #include <exception/SemanticError.h> | ||
| 10 | #include <global/GlobalResourceManager.h> | ||
| 11 | #include <symboltablebuilder/Scope.h> | ||
| 12 | #include <util/SystemUtil.h> | ||
| 13 | |||
| 14 | namespace spice::compiler { | ||
| 15 | |||
| 16 | 1423 | std::any ImportCollector::visitEntry(EntryNode *node) { | |
| 17 | // Visit all module attributes | ||
| 18 |
2/2✓ Branch 17 → 4 taken 451 times.
✓ Branch 17 → 18 taken 1423 times.
|
3297 | for (ModAttrNode *attr : node->modAttrs) |
| 19 |
1/2✓ Branch 6 → 7 taken 451 times.
✗ Branch 6 → 38 not taken.
|
451 | visit(attr); |
| 20 | |||
| 21 | // Visit all import defs | ||
| 22 |
2/2✓ Branch 33 → 20 taken 768 times.
✓ Branch 33 → 34 taken 1418 times.
|
3609 | for (ImportDefNode *importDef : node->importDefs) |
| 23 |
2/2✓ Branch 22 → 23 taken 763 times.
✓ Branch 22 → 40 taken 5 times.
|
768 | visit(importDef); |
| 24 | |||
| 25 |
1/2✓ Branch 34 → 35 taken 1418 times.
✗ Branch 34 → 42 not taken.
|
2836 | return nullptr; |
| 26 | } | ||
| 27 | |||
| 28 | 768 | std::any ImportCollector::visitImportDef(ImportDefNode *node) { | |
| 29 | 768 | const bool isStd = node->importPath.starts_with("std/"); | |
| 30 | 768 | const bool isBootstrap = node->importPath.starts_with("bootstrap/"); | |
| 31 | |||
| 32 | 768 | std::filesystem::path basePath; | |
| 33 |
2/2✓ Branch 5 → 6 taken 710 times.
✓ Branch 5 → 17 taken 58 times.
|
768 | if (isStd) { // Include source file from standard library |
| 34 | // Find std library | ||
| 35 |
1/2✓ Branch 6 → 7 taken 710 times.
✗ Branch 6 → 196 not taken.
|
710 | const std::filesystem::path stdPath = SystemUtil::getStdDir(); |
| 36 | // Format: /dir/to/path/file | ||
| 37 |
3/6✓ Branch 8 → 9 taken 710 times.
✗ Branch 8 → 191 not taken.
✓ Branch 9 → 10 taken 710 times.
✗ Branch 9 → 189 not taken.
✓ Branch 10 → 11 taken 710 times.
✗ Branch 10 → 187 not taken.
|
710 | basePath = stdPath / node->importPath.substr(node->importPath.find("std/") + 4); |
| 38 |
2/2✓ Branch 17 → 18 taken 26 times.
✓ Branch 17 → 29 taken 32 times.
|
768 | } else if (isBootstrap) { // Include source file from bootstrap library |
| 39 | // Find bootstrap library | ||
| 40 |
1/2✓ Branch 18 → 19 taken 26 times.
✗ Branch 18 → 206 not taken.
|
26 | const std::filesystem::path bootstrapPath = SystemUtil::getBootstrapDir(); |
| 41 | // Format: /dir/to/path/file | ||
| 42 |
3/6✓ Branch 20 → 21 taken 26 times.
✗ Branch 20 → 201 not taken.
✓ Branch 21 → 22 taken 26 times.
✗ Branch 21 → 199 not taken.
✓ Branch 22 → 23 taken 26 times.
✗ Branch 22 → 197 not taken.
|
26 | basePath = bootstrapPath / node->importPath.substr(node->importPath.find("bootstrap/") + 10); |
| 43 | 26 | } else { // Include own source file | |
| 44 | // Format: /dir/to/path/file | ||
| 45 |
3/6✓ Branch 29 → 30 taken 32 times.
✗ Branch 29 → 212 not taken.
✓ Branch 30 → 31 taken 32 times.
✗ Branch 30 → 209 not taken.
✓ Branch 31 → 32 taken 32 times.
✗ Branch 31 → 207 not taken.
|
32 | basePath = sourceFile->filePath.parent_path() / node->importPath; |
| 46 | } | ||
| 47 | 768 | basePath.make_preferred(); | |
| 48 | |||
| 49 |
2/4✓ Branch 39 → 40 taken 768 times.
✗ Branch 39 → 214 not taken.
✓ Branch 40 → 41 taken 768 times.
✗ Branch 40 → 214 not taken.
|
768 | const std::string osName = cliOptions.targetTriple.getOSTypeName(cliOptions.targetTriple.getOS()).str(); |
| 50 |
2/4✓ Branch 42 → 43 taken 768 times.
✗ Branch 42 → 215 not taken.
✓ Branch 43 → 44 taken 768 times.
✗ Branch 43 → 215 not taken.
|
768 | const std::string archName = cliOptions.targetTriple.getArchTypeName(cliOptions.targetTriple.getArch()).str(); |
| 51 | |||
| 52 | // Format: /dir/to/path/file.spice | ||
| 53 |
1/2✓ Branch 44 → 45 taken 768 times.
✗ Branch 44 → 336 not taken.
|
768 | std::filesystem::path defaultPath = basePath; |
| 54 |
5/10✓ Branch 45 → 46 taken 768 times.
✗ Branch 45 → 224 not taken.
✓ Branch 46 → 47 taken 768 times.
✗ Branch 46 → 222 not taken.
✓ Branch 47 → 48 taken 768 times.
✗ Branch 47 → 220 not taken.
✓ Branch 48 → 49 taken 768 times.
✗ Branch 48 → 218 not taken.
✓ Branch 49 → 50 taken 768 times.
✗ Branch 49 → 216 not taken.
|
768 | defaultPath.replace_filename(basePath.stem().string() + ".spice"); |
| 55 | // Format: /dir/to/path/file_linux.spice | ||
| 56 |
1/2✓ Branch 54 → 55 taken 768 times.
✗ Branch 54 → 334 not taken.
|
768 | std::filesystem::path osPath = basePath; |
| 57 |
7/14✓ Branch 55 → 56 taken 768 times.
✗ Branch 55 → 240 not taken.
✓ Branch 56 → 57 taken 768 times.
✗ Branch 56 → 238 not taken.
✓ Branch 57 → 58 taken 768 times.
✗ Branch 57 → 236 not taken.
✓ Branch 58 → 59 taken 768 times.
✗ Branch 58 → 234 not taken.
✓ Branch 59 → 60 taken 768 times.
✗ Branch 59 → 232 not taken.
✓ Branch 60 → 61 taken 768 times.
✗ Branch 60 → 230 not taken.
✓ Branch 61 → 62 taken 768 times.
✗ Branch 61 → 228 not taken.
|
768 | osPath.replace_filename(basePath.stem().string() + "_" + osName + ".spice"); |
| 58 | // Format: /dir/to/path/file_linux_x86_64.spice | ||
| 59 |
1/2✓ Branch 68 → 69 taken 768 times.
✗ Branch 68 → 332 not taken.
|
768 | std::filesystem::path osArchPath = basePath; |
| 60 |
9/18✓ Branch 69 → 70 taken 768 times.
✗ Branch 69 → 262 not taken.
✓ Branch 70 → 71 taken 768 times.
✗ Branch 70 → 260 not taken.
✓ Branch 71 → 72 taken 768 times.
✗ Branch 71 → 258 not taken.
✓ Branch 72 → 73 taken 768 times.
✗ Branch 72 → 256 not taken.
✓ Branch 73 → 74 taken 768 times.
✗ Branch 73 → 254 not taken.
✓ Branch 74 → 75 taken 768 times.
✗ Branch 74 → 252 not taken.
✓ Branch 75 → 76 taken 768 times.
✗ Branch 75 → 250 not taken.
✓ Branch 76 → 77 taken 768 times.
✗ Branch 76 → 248 not taken.
✓ Branch 77 → 78 taken 768 times.
✗ Branch 77 → 246 not taken.
|
768 | osArchPath.replace_filename(basePath.stem().string() + "_" + osName + "_" + archName + ".spice"); |
| 61 | |||
| 62 | // Check which source file to import | ||
| 63 | 768 | std::filesystem::path importPath; | |
| 64 |
7/10✓ Branch 87 → 88 taken 768 times.
✗ Branch 87 → 328 not taken.
✓ Branch 88 → 89 taken 1 time.
✓ Branch 88 → 92 taken 767 times.
✓ Branch 89 → 90 taken 1 time.
✗ Branch 89 → 328 not taken.
✓ Branch 90 → 91 taken 1 time.
✗ Branch 90 → 92 not taken.
✓ Branch 93 → 94 taken 1 time.
✓ Branch 93 → 95 taken 767 times.
|
768 | if (exists(osArchPath) && !equivalent(sourceFile->filePath, osArchPath)) { |
| 65 | // file_os_arch.spice is first choice | ||
| 66 |
1/2✓ Branch 94 → 158 taken 1 time.
✗ Branch 94 → 328 not taken.
|
1 | importPath = osArchPath; |
| 67 |
7/10✓ Branch 95 → 96 taken 767 times.
✗ Branch 95 → 328 not taken.
✓ Branch 96 → 97 taken 18 times.
✓ Branch 96 → 100 taken 749 times.
✓ Branch 97 → 98 taken 18 times.
✗ Branch 97 → 328 not taken.
✓ Branch 98 → 99 taken 18 times.
✗ Branch 98 → 100 not taken.
✓ Branch 101 → 102 taken 18 times.
✓ Branch 101 → 103 taken 749 times.
|
767 | } else if (exists(osPath) && !equivalent(sourceFile->filePath, osPath)) { |
| 68 | // file_os.spice is second choice | ||
| 69 |
1/2✓ Branch 102 → 158 taken 18 times.
✗ Branch 102 → 328 not taken.
|
18 | importPath = osPath; |
| 70 |
3/4✓ Branch 103 → 104 taken 749 times.
✗ Branch 103 → 328 not taken.
✓ Branch 104 → 105 taken 747 times.
✓ Branch 104 → 106 taken 2 times.
|
749 | } else if (exists(defaultPath)) { |
| 71 | // file.spice is third choice | ||
| 72 |
1/2✓ Branch 105 → 158 taken 747 times.
✗ Branch 105 → 328 not taken.
|
747 | importPath = defaultPath; |
| 73 | } else { | ||
| 74 | 2 | const bool obfuscate = cliOptions.comparableOutput; | |
| 75 |
6/18✓ Branch 106 → 107 taken 2 times.
✗ Branch 106 → 110 not taken.
✓ Branch 107 → 108 taken 2 times.
✗ Branch 107 → 270 not taken.
✓ Branch 108 → 109 taken 2 times.
✗ Branch 108 → 270 not taken.
✓ Branch 109 → 111 taken 2 times.
✗ Branch 109 → 270 not taken.
✗ Branch 110 → 111 not taken.
✗ Branch 110 → 270 not taken.
✓ Branch 111 → 112 taken 2 times.
✗ Branch 111 → 113 not taken.
✓ Branch 113 → 114 taken 2 times.
✗ Branch 113 → 115 not taken.
✗ Branch 270 → 271 not taken.
✗ Branch 270 → 272 not taken.
✗ Branch 274 → 275 not taken.
✗ Branch 274 → 276 not taken.
|
2 | const std::string osArchPathObfuscated = obfuscate ? osArchPath.stem().string() + ".spice" : osArchPath.generic_string(); |
| 76 |
6/18✓ Branch 115 → 116 taken 2 times.
✗ Branch 115 → 119 not taken.
✓ Branch 116 → 117 taken 2 times.
✗ Branch 116 → 278 not taken.
✓ Branch 117 → 118 taken 2 times.
✗ Branch 117 → 278 not taken.
✓ Branch 118 → 120 taken 2 times.
✗ Branch 118 → 278 not taken.
✗ Branch 119 → 120 not taken.
✗ Branch 119 → 278 not taken.
✓ Branch 120 → 121 taken 2 times.
✗ Branch 120 → 122 not taken.
✓ Branch 122 → 123 taken 2 times.
✗ Branch 122 → 124 not taken.
✗ Branch 278 → 279 not taken.
✗ Branch 278 → 280 not taken.
✗ Branch 282 → 283 not taken.
✗ Branch 282 → 284 not taken.
|
2 | const std::string osPathObfuscated = obfuscate ? osPath.stem().string() + ".spice" : osPath.generic_string(); |
| 77 |
6/18✓ Branch 124 → 125 taken 2 times.
✗ Branch 124 → 128 not taken.
✓ Branch 125 → 126 taken 2 times.
✗ Branch 125 → 286 not taken.
✓ Branch 126 → 127 taken 2 times.
✗ Branch 126 → 286 not taken.
✓ Branch 127 → 129 taken 2 times.
✗ Branch 127 → 286 not taken.
✗ Branch 128 → 129 not taken.
✗ Branch 128 → 286 not taken.
✓ Branch 129 → 130 taken 2 times.
✗ Branch 129 → 131 not taken.
✓ Branch 131 → 132 taken 2 times.
✗ Branch 131 → 133 not taken.
✗ Branch 286 → 287 not taken.
✗ Branch 286 → 288 not taken.
✗ Branch 290 → 291 not taken.
✗ Branch 290 → 292 not taken.
|
2 | const std::string defaultPathObfuscated = obfuscate ? defaultPath.stem().string() + ".spice" : defaultPath.generic_string(); |
| 78 | |||
| 79 |
1/2✓ Branch 133 → 134 taken 2 times.
✗ Branch 133 → 308 not taken.
|
2 | std::stringstream errorMessage; |
| 80 |
7/14✓ Branch 134 → 135 taken 2 times.
✗ Branch 134 → 306 not taken.
✓ Branch 135 → 136 taken 2 times.
✗ Branch 135 → 298 not taken.
✓ Branch 136 → 137 taken 2 times.
✗ Branch 136 → 296 not taken.
✓ Branch 137 → 138 taken 2 times.
✗ Branch 137 → 294 not taken.
✓ Branch 138 → 139 taken 2 times.
✗ Branch 138 → 294 not taken.
✓ Branch 139 → 140 taken 2 times.
✗ Branch 139 → 294 not taken.
✓ Branch 140 → 141 taken 2 times.
✗ Branch 140 → 294 not taken.
|
2 | errorMessage << "The source file '" << basePath.stem().string() << ".spice' was not found" << std::endl << std::endl; |
| 81 |
2/4✓ Branch 143 → 144 taken 2 times.
✗ Branch 143 → 306 not taken.
✓ Branch 144 → 145 taken 2 times.
✗ Branch 144 → 306 not taken.
|
2 | errorMessage << "The following paths were checked with descending priority:" << std::endl; |
| 82 |
3/6✓ Branch 145 → 146 taken 2 times.
✗ Branch 145 → 306 not taken.
✓ Branch 146 → 147 taken 2 times.
✗ Branch 146 → 306 not taken.
✓ Branch 147 → 148 taken 2 times.
✗ Branch 147 → 306 not taken.
|
2 | errorMessage << "- " << osArchPathObfuscated << std::endl; |
| 83 |
3/6✓ Branch 148 → 149 taken 2 times.
✗ Branch 148 → 306 not taken.
✓ Branch 149 → 150 taken 2 times.
✗ Branch 149 → 306 not taken.
✓ Branch 150 → 151 taken 2 times.
✗ Branch 150 → 306 not taken.
|
2 | errorMessage << "- " << osPathObfuscated << std::endl; |
| 84 |
2/4✓ Branch 151 → 152 taken 2 times.
✗ Branch 151 → 306 not taken.
✓ Branch 152 → 153 taken 2 times.
✗ Branch 152 → 306 not taken.
|
2 | errorMessage << "- " << defaultPathObfuscated; |
| 85 |
2/4✓ Branch 154 → 155 taken 2 times.
✗ Branch 154 → 303 not taken.
✓ Branch 155 → 156 taken 2 times.
✗ Branch 155 → 300 not taken.
|
2 | throw SemanticError(node, IMPORTED_FILE_NOT_EXISTING, errorMessage.str()); |
| 86 | 8 | } | |
| 87 | |||
| 88 | // Check if the import already exists | ||
| 89 |
3/4✓ Branch 158 → 159 taken 766 times.
✗ Branch 158 → 328 not taken.
✓ Branch 161 → 162 taken 2 times.
✓ Branch 161 → 169 taken 764 times.
|
1532 | if (rootScope->lookupStrict(node->importName) != nullptr) |
| 90 |
3/6✓ Branch 163 → 164 taken 2 times.
✗ Branch 163 → 320 not taken.
✓ Branch 164 → 165 taken 2 times.
✗ Branch 164 → 318 not taken.
✓ Branch 165 → 166 taken 2 times.
✗ Branch 165 → 315 not taken.
|
2 | throw SemanticError(node, DUPLICATE_IMPORT_NAME, "Duplicate import '" + node->importName + "'"); |
| 91 | |||
| 92 | // Create symbol for import | ||
| 93 |
1/2✓ Branch 169 → 170 taken 764 times.
✗ Branch 169 → 328 not taken.
|
764 | node->entry = rootScope->insert(node->importName, node); |
| 94 | |||
| 95 | // Create the imported source file | ||
| 96 |
1/2✓ Branch 172 → 173 taken 764 times.
✗ Branch 172 → 328 not taken.
|
764 | const auto importedSourceFile = resourceManager.createSourceFile(sourceFile, node->importName, importPath, isStd); |
| 97 | // Register it as a dependency to the current source file | ||
| 98 |
3/4✓ Branch 173 → 174 taken 764 times.
✗ Branch 173 → 326 not taken.
✓ Branch 174 → 175 taken 763 times.
✓ Branch 174 → 324 taken 1 time.
|
765 | sourceFile->addDependency(importedSourceFile, node, node->importName, importPath.generic_string()); |
| 99 | |||
| 100 |
1/2✓ Branch 176 → 177 taken 763 times.
✗ Branch 176 → 327 not taken.
|
1526 | return nullptr; |
| 101 | 798 | } | |
| 102 | |||
| 103 | 451 | std::any ImportCollector::visitModAttr(ModAttrNode *node) { | |
| 104 | // !!! Only bool attributes allowed here, due to missing attribute value checks being executed in a later stage !!! | ||
| 105 | |||
| 106 | // core.compiler.keep-on-name-collision | ||
| 107 |
4/6✓ Branch 4 → 5 taken 451 times.
✗ Branch 4 → 36 not taken.
✓ Branch 5 → 6 taken 451 times.
✗ Branch 5 → 34 not taken.
✓ Branch 8 → 9 taken 433 times.
✓ Branch 8 → 16 taken 18 times.
|
1353 | if (node->attrLst->hasAttr(ATTR_CORE_COMPILER_KEEP_ON_NAME_COLLISION)) { |
| 108 |
2/4✓ Branch 11 → 12 taken 433 times.
✗ Branch 11 → 42 not taken.
✓ Branch 12 → 13 taken 433 times.
✗ Branch 12 → 40 not taken.
|
866 | const bool keepOnCollision = node->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_KEEP_ON_NAME_COLLISION)->boolValue; |
| 109 | 433 | sourceFile->alwaysKeepSymbolsOnNameCollision = keepOnCollision; | |
| 110 | } | ||
| 111 | |||
| 112 | // core.compiler.warnings.ignore | ||
| 113 |
4/6✓ Branch 18 → 19 taken 451 times.
✗ Branch 18 → 48 not taken.
✓ Branch 19 → 20 taken 451 times.
✗ Branch 19 → 46 not taken.
✓ Branch 22 → 23 taken 5 times.
✓ Branch 22 → 30 taken 446 times.
|
1353 | if (node->attrLst->hasAttr(ATTR_CORE_COMPILER_WARNINGS_IGNORE)) { |
| 114 |
2/4✓ Branch 25 → 26 taken 5 times.
✗ Branch 25 → 54 not taken.
✓ Branch 26 → 27 taken 5 times.
✗ Branch 26 → 52 not taken.
|
10 | const bool ignoreWarnings = node->attrLst->getAttrValueByName(ATTR_CORE_COMPILER_WARNINGS_IGNORE)->boolValue; |
| 115 | 5 | sourceFile->ignoreWarnings = ignoreWarnings; | |
| 116 | } | ||
| 117 | |||
| 118 |
1/2✓ Branch 30 → 31 taken 451 times.
✗ Branch 30 → 58 not taken.
|
902 | return nullptr; |
| 119 | } | ||
| 120 | |||
| 121 | } // namespace spice::compiler | ||
| 122 |