annotate trunk/src/cmd/ImportGraph.d @ 370:ae4afb66768f

- Renamed findModule() to findModulePath(). - Added struct Edge. - Fix: don't append fileDir to importPaths when it's empty. - Added function findCyclicEdges(). - Added method getFQN() to class ModuleDeclaration. - Added member moduleFQN to class Module. - Renamed Module.fileName to filePath. - Added method setFQN() to class Module. - Fix in Lexer.scanspecialTokenSequence(): corrected MID.
author aziz
date Mon, 03 Sep 2007 16:29:02 +0000
parents 2adf808343d6
children 0bd21b746a04
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
1 /++
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
2 Author: Aziz Köksal
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
3 License: GPL3
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
4 +/
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
5 module cmd.ImportGraph;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
6 import dil.SyntaxTree;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
7 import dil.Declarations;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
8 import dil.Token;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
9 import dil.Parser, dil.Lexer;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
10 import dil.File;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
11 import dil.Module;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
12 import dil.Settings;
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
13 import std.stdio : writefln;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
14 import std.path : getDirName, dirSep = sep;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
15 import std.file : exists;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
16 import std.string : replace;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
17
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
18 string findModulePath(string moduleFQN, string[] importPaths)
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
19 {
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
20 string modulePath;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
21 foreach (path; importPaths)
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
22 {
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
23 modulePath = path ~ (path[$-1] == dirSep[0] ? "" : dirSep) ~ moduleFQN ~ ".d";
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
24 if (exists(modulePath))
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
25 return modulePath;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
26 }
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
27 return null;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
28 }
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
29
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
30 struct Edge
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
31 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
32 Module outgoing;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
33 Module incoming;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
34 static Edge opCall(Module o, Module i)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
35 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
36 Edge e;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
37 e.outgoing = o;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
38 e.incoming = i;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
39 return e;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
40 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
41 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
42
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
43 void execute(string fileName, string[] importPaths)
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
44 {
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
45 // Add directory of file and global directories to import paths.
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
46 auto fileDir = getDirName(fileName);
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
47 if (fileDir.length)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
48 importPaths ~= fileDir;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
49 importPaths ~= GlobalSettings.importPaths;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
50
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
51 Module[string] loadedModules;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
52 Module[] loadedModulesList; // Ordered list of loaded modules.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
53 Edge edges[];
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
54
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
55 Module loadModule(string moduleFQNPath)
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
56 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
57 auto mod_ = moduleFQNPath in loadedModules;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
58 if (mod_ !is null)
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
59 return *mod_;
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
60 // writefln(moduleFQN);
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
61 auto modulePath = findModulePath(moduleFQNPath, importPaths);
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
62 Module mod;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
63 if (modulePath is null)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
64 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
65 // writefln("Warning: Module %s.d couldn't be found.", moduleFQNPath);
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
66 mod = new Module(null, true);
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
67 mod.setFQN(replace(moduleFQNPath, dirSep, "."));
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
68 loadedModules[moduleFQNPath] = mod;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
69 loadedModulesList ~= mod;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
70 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
71 else
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
72 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
73 mod = new Module(modulePath, true);
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
74 mod.parse();
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
75
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
76 loadedModules[moduleFQNPath] = mod;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
77 loadedModulesList ~= mod;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
78
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
79 auto moduleFQNs = mod.getImports();
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
80
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
81 foreach (moduleFQN_; moduleFQNs)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
82 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
83 auto loaded_mod = loadModule(moduleFQN_);
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
84 edges ~= Edge(mod, loaded_mod);
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
85 mod.modules ~= loaded_mod;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
86 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
87 return mod;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
88 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
89 return mod;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
90 }
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
91
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
92 auto mod = new Module(fileName, true);
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
93 mod.parse();
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
94
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
95 auto moduleFQNs = mod.getImports();
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
96
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
97 loadedModules[mod.getFQNPath()] = mod;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
98 loadedModulesList ~= mod;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
99
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
100 foreach (moduleFQN_; moduleFQNs)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
101 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
102 auto loaded_mod = loadModule(moduleFQN_);
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
103 edges ~= Edge(mod, loaded_mod);
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
104 mod.modules ~= loaded_mod;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
105 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
106
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
107 writefln("digraph module_dependencies\n{");
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
108 foreach (edge; edges)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
109 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
110 writefln(` "%s" -> "%s"`, edge.outgoing.getFQN(), edge.incoming.getFQN());
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
111 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
112 writefln("}");
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
113 }
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
114
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
115 Edge[] findCyclicEdges(Module[] modules, Edge[] edges)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
116 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
117 foreach (module_; modules)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
118 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
119 uint outgoing, incoming;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
120 foreach (edge; edges)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
121 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
122 if (edge.outgoing == module_)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
123 outgoing++;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
124 if (edge.incoming == module_)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
125 incoming++;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
126 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
127
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
128 if (outgoing == 0)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
129 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
130 if (incoming != 0)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
131 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
132 // sink
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
133 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
134 else
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
135 assert(0); // orphaned vertex (module) in graph
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
136 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
137 else if (incoming == 0)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
138 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
139 // source
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
140 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
141 else
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
142 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
143 // source && sink
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
144 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
145 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
146 return null;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
147 }