annotate src/dil/ModuleManager.d @ 813:1abffc396594

Revised the ModuleManager class. Fixed Parser.parseModuleDeclaration().
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 12 Mar 2008 00:49:17 +0100
parents 3b567bce56f3
children 615c1386b18d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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;
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
8 import dil.Location;
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
9 import dil.Information;
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
10 import dil.Messages;
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
11 import common;
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 import tango.io.FilePath;
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
14 import tango.io.FileSystem;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
15 import tango.io.FileConst;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
16
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
17 alias FileConst.PathSeparatorChar dirSep;
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
18
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
19 /// Manages loaded modules in a table.
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
20 class ModuleManager
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 /// Maps FQN paths to modules. E.g.: dil/ast/Node
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
23 Module[string] moduleFQNPathTable;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
24 /// Maps absolute file paths to modules. E.g.: /home/user/dil/src/main.d
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
25 Module[string] absFilePathTable;
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
26 Module[] loadedModules; /// Loaded modules in sequential order.
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
27 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
28 InfoManager infoMan;
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
29
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
30 /// Constructs a ModuleManager object.
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
31 this(string[] importPaths, InfoManager infoMan)
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
32 {
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
33 this.importPaths = importPaths;
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
34 this.infoMan = infoMan;
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
35 }
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
36
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
37 /// Loads a module given a file path.
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
38 Module loadModuleFile(string moduleFilePath)
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
39 {
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
40 auto absFilePath = FileSystem.toAbsolute(moduleFilePath);
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
41 if (auto existingModule = absFilePath in absFilePathTable)
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
42 return *existingModule;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
43
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
44 // Create a new module.
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
45 auto newModule = new Module(moduleFilePath, infoMan);
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
46 newModule.parse();
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
47
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
48 auto moduleFQNPath = newModule.getFQNPath();
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
49 if (auto existingModule = moduleFQNPath in moduleFQNPathTable)
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
50 { // Error: two module files have the same f.q. module name.
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
51 auto location = newModule.moduleDecl.begin.getErrorLocation();
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
52 auto msg = Format(MSG.ConflictingModuleFiles, newModule.filePath());
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
53 infoMan ~= new SemanticError(location, msg);
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
54 return *existingModule;
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
55 }
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
56 // Insert new module.
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
57 moduleFQNPathTable[moduleFQNPath] = newModule;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
58 absFilePathTable[absFilePath] = newModule;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
59 loadedModules ~= newModule;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
60 return newModule;
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
61 }
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
62
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
63 /// Loads a module given an FQN path.
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
64 Module loadModule(string moduleFQNPath)
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 // Look up in table if the module is already loaded.
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
67 Module* pmodul = moduleFQNPath in moduleFQNPathTable;
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
68 if (pmodul)
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
69 return *pmodul;
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
70 // Locate the module in the file system.
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
71 auto moduleFilePath = findModuleFilePath(moduleFQNPath, importPaths);
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
72 if (moduleFilePath.length)
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
73 { // Load the found module file.
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
74 auto modul = loadModuleFile(moduleFilePath);
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
75 if (modul.getFQNPath() != moduleFQNPath)
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
76 { // Error: the requested module is not in the correct package.
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
77 auto location = modul.moduleDecl.begin.getErrorLocation();
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
78 auto msg = Format(MSG.ModuleNotInPackage, getPackage(moduleFQNPath));
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
79 infoMan ~= new SemanticError(location, msg);
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
80 }
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
81 return modul;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
82 }
812
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
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
86 /// Returns e.g. 'dil.ast' for 'dil/ast/Node'.
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
87 string getPackage(string moduleFQNPath)
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
88 {
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
89 string pckg = moduleFQNPath.dup;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
90 uint lastDirSep;
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
91 foreach (i, c; pckg)
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
92 if (c == dirSep)
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
93 (pckg[i] = '.'), (lastDirSep = i);
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
94 return pckg[0..lastDirSep];
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
95 }
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
96
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
97 /// 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
98 /// 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
99 static string findModuleFilePath(string moduleFQNPath, string[] importPaths)
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
100 {
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
101 auto filePath = new FilePath();
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
102 foreach (importPath; importPaths)
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
103 {
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
104 filePath.set(importPath); // E.g.: src/
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
105 filePath.append(moduleFQNPath); // E.g.: dil/ast/Node
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
106 foreach (moduleSuffix; [".d", ".di"/*interface file*/])
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
107 {
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
108 filePath.suffix(moduleSuffix);
813
1abffc396594 Revised the ModuleManager class.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 812
diff changeset
109 if (filePath.exists()) // E.g.: src/dil/ast/Node.d
812
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
110 return filePath.toString();
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
111 }
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
112 }
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
113 return null;
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
114 }
3b567bce56f3 Forgot to really add dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
115 }