2 #include "analysis/organize_packages.h"
3 #include "ast/package.h"
4 #include "ast/context.h"
7 #include "ast/intrinsic.h"
10 namespace fs = std::filesystem;
12 using namespace tanlang;
14 vector<Package *> OrganizePackages::run_impl(vector<Program *> ps) {
16 _package_top_level_ctx.clear();
17 _package_top_level_asts.clear();
24 vector<Package *> ret{};
25 for (
auto [name, asts] : _package_top_level_asts) {
26 auto *
package = new Package(name, asts);
29 vector<Context *> ctx = _package_top_level_ctx[name];
30 auto *package_ctx = package->ctx();
32 for (
auto *d : c->get_decls()) {
33 package_ctx->set_decl(d->get_name(), d);
35 for (
auto *d : c->get_func_decls()) {
36 package_ctx->set_function_decl(d);
40 ret.push_back(package);
48 vector<ASTBase *> asts{};
50 for (
auto *n : p->get_children()) {
51 if (n->get_node_type() == ASTNodeType::PACKAGE_DECL) {
53 if (!package_name.empty()) {
54 Error(ErrorType::SEMANTIC_ERROR, fmt::format(
"Can only have one package stmt in {}", p->src()->get_filename()))
58 package_name = pcast<PackageDecl>(n)->get_name();
65 if (package_name.empty()) {
66 package_name = fs::path(p->src()->get_filename()).filename().replace_extension().string();
68 if (package_name.empty()) {
69 Error(ErrorType::GENERIC_ERROR,
70 fmt::format(
"Cannot deduce default package name for {}", p->src()->get_filename()))
77 auto q = _package_top_level_asts.find(package_name);
78 if (q == _package_top_level_asts.end()) {
79 _package_top_level_asts[package_name] = asts;
81 q->second.insert(q->second.end(), asts.begin(), asts.end());
87 auto q = _package_top_level_ctx.find(package_name);
88 if (q == _package_top_level_ctx.end()) {
89 _package_top_level_ctx[package_name] = vector<Context *>{p->ctx()};
91 q->second.push_back(p->ctx());
98 if (p->get_intrinsic_type() == IntrinsicType::TEST_COMP_ERROR) {
101 auto *sub = p->get_sub();
103 TAN_ASSERT(sub->get_node_type() == ASTNodeType::COMPOUND_STATEMENT);
104 for (
auto *c : sub->get_children())
108 std::cerr << fmt::format(
"Caught expected compile error: {}\nContinue compilation...\n", e.what());
A generic representation of Intrinsic variables/functions.
Organize a list of source files into their corresponding packages according to the code....