tan  0.0.1
scan_imports.cpp
1 #include "ast/package.h"
2 #include "ast/stmt.h"
3 #include "analysis/scan_imports.h"
4 #include "driver/driver.h"
5 
6 #include <filesystem>
7 namespace fs = std::filesystem;
8 
9 using namespace tanlang;
10 
11 ScanImportsOutputType ScanImports::run_impl(Package *package) {
12  ScanImportsOutputType ret{};
13 
14  CompilerDriver *driver = CompilerDriver::instance();
15  for (auto *node : package->get_children()) {
16  if (node->get_node_type() == ASTNodeType::IMPORT) {
17  auto *p = pcast<Import>(node);
18  str name = p->get_name();
19 
20  // Find source files of the package
21  auto res = driver->resolve_package_import(node->src()->get_filename(), name);
22  if (res.empty())
23  error(ErrorType::IMPORT_ERROR, node, "Cannot find package: " + name);
24 
25  if (fs::is_directory(res[0])) { // files in the folder
26  for (const auto &entry : fs::directory_iterator(res[0])) {
27  auto path = entry.path();
28  if (!fs::is_directory(path) && path.has_extension() && path.extension() == ".tan") {
29  ret[name].insert(path.string());
30  }
31  }
32  } else { // a tan source file with the package name
33  TAN_ASSERT(fs::exists(res[0]));
34  ret[name].insert(res[0]);
35  }
36  }
37  }
38 
39  return ret;
40 }
Compile a list of C++ and/or tan source files, and perform linking.
Definition: driver.h:22
static vector< str > resolve_package_import(const str &callee_path, const str &import_name)
Get a list of possible files that corresponds to an import. Check PACKAGES.md.
Definition: driver.cpp:342
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: package.cpp:8