Mercurial > projects > dil
annotate src/dil/ModuleManager.d @ 812:3b567bce56f3
Forgot to really add dil.ModuleManager.
author | Aziz K?ksal <aziz.koeksal@gmail.com> |
---|---|
date | Tue, 11 Mar 2008 17:23:30 +0100 |
parents | |
children | 1abffc396594 |
rev | line source |
---|---|
812
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
1 /++ |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
2 Author: Aziz Köksal |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
3 License: GPL3 |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
4 +/ |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
5 module dil.ModuleManager; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
6 |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
7 import dil.semantic.Module; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
8 import dil.Information; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
9 import common; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
10 |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
11 import tango.io.FilePath; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
12 |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
13 /// Manages loaded modules in a table. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
14 class ModuleManager |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
15 { |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
16 /// Maps FQN paths to modules. E.g.: dil/ast/Node |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
17 Module[string] modulesTable; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
18 Module[] loadedModules; /// Loaded modules in sequential order. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
19 string[] importPaths; /// Where to look for module files. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
20 InfoManager infoMan; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
21 |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
22 /// Constructs a ModuleManager object. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
23 this(string[] importPaths, InfoManager infoMan) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
24 { |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
25 this.importPaths = importPaths; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
26 this.infoMan = infoMan; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
27 } |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
28 |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
29 /// Loads a module given a file path. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
30 Module loadModuleFile(string moduleFilePath) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
31 { |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
32 auto modul = new Module(moduleFilePath, infoMan); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
33 modul.parse(); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
34 auto moduleFQNPath = modul.getFQNPath(); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
35 // Return the module if it's in the table already. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
36 // This can happen if the module file path has been defined |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
37 // more than once on the command line. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
38 if (auto existingModule = moduleFQNPath in modulesTable) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
39 { |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
40 // TODO: avoid parsing the module in the first place, by |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
41 // using another table mapping absolute file paths |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
42 // to modules. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
43 delete modul; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
44 return *existingModule; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
45 } |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
46 // Insert new module. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
47 modulesTable[moduleFQNPath] = modul; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
48 loadedModules ~= modul; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
49 return modul; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
50 } |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
51 |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
52 /// Loads a module given an FQN path. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
53 Module loadModule(string moduleFQNPath) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
54 { |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
55 // Look up in table if the module is already loaded. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
56 Module* pmodul = moduleFQNPath in modulesTable; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
57 if (pmodul) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
58 return *pmodul; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
59 // Locate the module in the file system. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
60 auto moduleFilePath = findModuleFilePath(moduleFQNPath, |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
61 importPaths); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
62 if (moduleFilePath.length) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
63 return loadModuleFile(moduleFilePath); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
64 return null; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
65 } |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
66 |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
67 /// Searches for a module in the file system looking in importPaths. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
68 /// Returns: the file path to the module, or null if it wasn't found. |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
69 static string findModuleFilePath(string moduleFQNPath, string[] importPaths) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
70 { |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
71 auto filePath = new FilePath(); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
72 foreach (importPath; importPaths) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
73 { |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
74 filePath.set(importPath); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
75 filePath.append(moduleFQNPath); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
76 foreach (moduleSuffix; [".d", ".di"/*interface file*/]) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
77 { |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
78 filePath.suffix(moduleSuffix); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
79 if (filePath.exists()) |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
80 return filePath.toString(); |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
81 } |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
82 } |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
83 return null; |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
84 } |
3b567bce56f3
Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff
changeset
|
85 } |