annotate src/dil/ModuleManager.d @ 815:615c1386b18d

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