@@ -11,24 +11,62 @@ RewriteImportRules solve_rewriting_imports(
11
11
const parser::SymbolExports &symbol_exports) {
12
12
std::map<Identifier, Identifier> graph;
13
13
std::set<std::string> legal_exports;
14
+
14
15
#ifdef ZION_DEBUG
15
16
for (auto &module_pair : symbol_imports) {
16
17
for (auto &pair_ids : module_pair.second ) {
17
- debug_above (3 ,
18
- log (" import: %s: %s -> %s" , module_pair.first .c_str (),
19
- pair_ids.first .c_str (), join (pair_ids.second ).c_str ()));
18
+ debug_above (3 , log (" import: " c_module (" %s" ) " imports from " c_module (
19
+ " %s" ) " {%s}" ,
20
+ module_pair.first .c_str (), pair_ids.first .c_str (),
21
+ join (pair_ids.second ).c_str ()));
20
22
}
21
23
}
22
24
#endif
23
25
26
+ if (getenv (" DOT_DEPS" ) != nullptr ) {
27
+ std::string filename = " zion.dot" ;
28
+ FILE *fp = fopen (filename.c_str (), " wt" );
29
+ if (fp == nullptr ) {
30
+ throw zion::user_error (INTERNAL_LOC (),
31
+ " unable to open %s for writing DOT_DEPS" ,
32
+ filename.c_str ());
33
+ }
34
+ fprintf (fp, " digraph G {\n " );
35
+
36
+ std::unordered_set<std::string> deps;
37
+
38
+ for (auto &module_pair : symbol_imports) {
39
+ for (auto &pair_ids : module_pair.second ) {
40
+ for (auto symbol : pair_ids.second ) {
41
+
42
+ deps.insert (string_format (" \" %s\" -> \" %s\" ;" ,
43
+ tld::tld (module_pair.first ).c_str (),
44
+ tld::mktld (module_pair.first , symbol.name ).c_str ()));
45
+ deps.insert (string_format (" \" %s\" -> \" %s\" ;" ,
46
+ tld::tld (module_pair.first ).c_str (),
47
+ tld::tld (pair_ids.first ).c_str ()));
48
+ deps.insert (string_format (" \" %s\" -> \" %s\" ;" ,
49
+ tld::mktld (module_pair.first , symbol.name ).c_str (),
50
+ tld::mktld (pair_ids.first , symbol.name ).c_str ()));
51
+ }
52
+ }
53
+ }
54
+
55
+ for (auto dep : deps) {
56
+ fprintf (fp, " %s\n " , dep.c_str ());
57
+ }
58
+ fprintf (fp, " }\n " );
59
+ fclose (fp);
60
+ }
61
+
24
62
for (auto &module_pair : symbol_exports) {
25
63
for (auto &id_pair : module_pair.second ) {
26
64
debug_above (2 , log (" export: %s: %s -> %s" , module_pair.first .c_str (),
27
65
id_pair.first .str ().c_str (),
28
66
id_pair.second .str ().c_str ()));
29
67
if (id_pair.second .name != id_pair.first .name ) {
30
68
/* this export actually leads back to something else */
31
- if (graph.count (id_pair.first ) == 1 ) {
69
+ if (graph.count (id_pair.first ) != 0 ) {
32
70
throw user_error (id_pair.first .location , " ambiguous export %s vs. %s" ,
33
71
id_pair.first .str ().c_str (),
34
72
graph.at (id_pair.first ).str ().c_str ());
@@ -53,7 +91,7 @@ RewriteImportRules solve_rewriting_imports(
53
91
Identifier resolved_id = pair.second ;
54
92
std::set<Identifier> visited;
55
93
std::list<Identifier> visited_list;
56
- while (graph.count (resolved_id) == 1 ) {
94
+ while (graph.count (resolved_id) != 0 ) {
57
95
visited.insert (resolved_id);
58
96
visited_list.push_back (resolved_id);
59
97
@@ -69,6 +107,7 @@ RewriteImportRules solve_rewriting_imports(
69
107
throw error;
70
108
}
71
109
}
110
+
72
111
/* rewrite the graph as we go to avoid wasting time for future traversals */
73
112
for (auto &id : visited_list) {
74
113
graph[id] = resolved_id;
@@ -105,8 +144,10 @@ RewriteImportRules solve_rewriting_imports(
105
144
debug_above (3 , log (" couldn't find %s in legal_exports. found %s" ,
106
145
fqn.c_str (), join (legal_exports).c_str ()));
107
146
illegal_imports.push_back (
108
- {Identifier{tld::mktld (source_module, symbol.name ), symbol.location },
109
- Identifier{tld::mktld (dest_module, symbol.name ), symbol.location }});
147
+ {Identifier{tld::mktld (source_module, symbol.name ),
148
+ symbol.location },
149
+ Identifier{tld::mktld (dest_module, symbol.name ),
150
+ symbol.location }});
110
151
}
111
152
}
112
153
}
0 commit comments