annotate trunk/src/dil/Module.d @ 589:de365ddcfbd4

Moved dil.Symbol to dil.semantic.Symbol.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Sat, 05 Jan 2008 23:40:54 +0100
parents 05c375fb2d5c
children 641041912670
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 dil.Module;
520
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
6
580
fa6d3c52757d Moved SyntaxTree.d to new package 'ast'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 579
diff changeset
7 import dil.ast.Node;
585
05c375fb2d5c Moved dil.Declarations to dil.ast.Declarations.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 580
diff changeset
8 import dil.ast.Declarations;
578
c769bc239006 Moved Parser.d to new package 'parser'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 576
diff changeset
9 import dil.parser.Parser;
579
90d0cff4107e Moved ImportParser.d to package 'parser'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 578
diff changeset
10 import dil.parser.ImportParser;
576
0df647660e76 Moved Lexer.d to new package 'lexer'.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 570
diff changeset
11 import dil.lexer.Lexer;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
12 import dil.File;
503
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
13 import dil.Scope;
589
de365ddcfbd4 Moved dil.Symbol to dil.semantic.Symbol.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 585
diff changeset
14 import dil.semantic.Symbol;
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
15 import dil.Symbols;
520
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
16 import dil.Information;
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
17 import tango.io.FilePath;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
18 import tango.io.FileConst;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
19 import common;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
20
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
21 alias FileConst.PathSeparatorChar dirSep;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
22
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
23 class Module : ScopeSymbol
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
24 {
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
25 bool isLightweight; /// If true an ImportParser is used instead of a full Parser.
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
26 string filePath; /// Path to the source file.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
27 string moduleFQN; /// Fully qualified name of the module.
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
28 string packageName;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
29 string moduleName;
520
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
30
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
31 Declarations root; /// The root of the AST.
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
32 ImportDeclaration[] imports;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
33 ModuleDeclaration moduleDecl;
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
34 private Parser parser;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
35
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
36 Module[] modules;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
37
532
50e64bab9c7a Renamed InformationManager to InfoManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 520
diff changeset
38 InfoManager infoMan;
520
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
39
492
9c208925a3d4 Added module ImportParser and new stuff from DMD2.008.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
40 this(string filePath, bool isLightweight = false)
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
41 {
560
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
42 this.sid = SYM.Module;
709e223a8eb9 Added code related to symbols.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 532
diff changeset
43
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
44 this.filePath = filePath;
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
45 this.isLightweight = isLightweight;
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
46 }
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
47
532
50e64bab9c7a Renamed InformationManager to InfoManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 520
diff changeset
48 this(string filePath, InfoManager infoMan)
520
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
49 {
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
50 this(filePath, false);
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
51 this.infoMan = infoMan;
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
52 }
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
53
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
54 void parse()
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
55 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
56 auto sourceText = loadFile(filePath);
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
57 if (this.isLightweight)
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
58 this.parser = new ImportParser(sourceText, filePath);
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
59 else
520
f203c5248d0b Added 'compile' command.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 516
diff changeset
60 this.parser = new Parser(sourceText, filePath, infoMan);
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
61
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
62 this.root = parser.start();
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
63
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
64 if (root.children.length)
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
65 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
66 // moduleDecl will be null if first node can't be cast to ModuleDeclaration.
516
433d51c18524 Renamed template Cast to TryCast.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 515
diff changeset
67 this.moduleDecl = TryCast!(ModuleDeclaration)(root.children[0]);
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
68 if (moduleDecl)
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
69 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
70 this.setFQN(moduleDecl.getFQN());
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
71 }
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
72 else
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
73 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
74 // Take base name of file path as module name.
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
75 auto str = (new FilePath(filePath)).name();
510
dd3ce87b3569 Added module dil.Unicode.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 503
diff changeset
76 if (!Lexer.isReservedIdentifier(str))
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
77 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
78 this.moduleFQN = moduleName = str;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
79 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
80 // else
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
81 // TODO: error: file name has invalid identifier characters.
365
ed67acc82268 - Added option includes to config.d.
aziz
parents: 364
diff changeset
82 }
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
83
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
84 this.imports = parser.imports;
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
85 }
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
86 }
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
87
563
c838ed7f2ac9 Added 'override' to some methods.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 560
diff changeset
88 /// Starts the semantic analysis of this module.
503
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
89 void semantic()
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
90 {
570
3ebdc510a7fc Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 564
diff changeset
91 if (this.hasErrors)
3ebdc510a7fc Added semantic() to InterfaceDeclaration.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 564
diff changeset
92 return;
563
c838ed7f2ac9 Added 'override' to some methods.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 560
diff changeset
93 // Create module scope.
503
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
94 auto scop = new Scope();
563
c838ed7f2ac9 Added 'override' to some methods.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 560
diff changeset
95 scop.symbol = this; // Set this module as the scope's symbol.
564
3c867a683258 Fixed VariableDeclaration.semantic().
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 563
diff changeset
96 scop.infoMan = this.infoMan;
503
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
97 this.root.semantic(scop);
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
98 }
fa63ef408790 Added module dil.Package and semantic() to class Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 492
diff changeset
99
563
c838ed7f2ac9 Added 'override' to some methods.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 560
diff changeset
100 /// Returns true if there are errors in the source file.
515
7cb97346bc6f Using class Module in SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 510
diff changeset
101 bool hasErrors()
7cb97346bc6f Using class Module in SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 510
diff changeset
102 {
7cb97346bc6f Using class Module in SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 510
diff changeset
103 return parser.errors.length || parser.lx.errors.length;
7cb97346bc6f Using class Module in SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 510
diff changeset
104 }
7cb97346bc6f Using class Module in SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 510
diff changeset
105
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
106 string[] getImports()
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
107 {
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
108 string[] result;
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
109 foreach (import_; imports)
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
110 result ~= import_.getModuleFQNs(dirSep);
366
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
111 return result;
dcbd3bf9bf74 - Added command importgraph/igraph to main.d.
aziz
parents: 365
diff changeset
112 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
113
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
114 string getFQN()
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
115 {
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
116 return moduleFQN;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
117 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
118
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
119 void setFQN(string moduleFQN)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
120 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
121 uint i = moduleFQN.length;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
122 if (i != 0) // Don't decrement if string has zero length.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
123 i--;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
124 // Find last dot.
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
125 for (; i != 0 && moduleFQN[i] != '.'; i--)
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
126 {}
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
127 this.moduleFQN = moduleFQN;
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
128 this.packageName = moduleFQN[0..i];
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
129 this.moduleName = moduleFQN[(i == 0 ? 0 : i+1) .. $];
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
130 }
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
131
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
132 string getFQNPath()
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
133 {
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
134 if (packageName.length)
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 370
diff changeset
135 return packageName ~ dirSep ~ moduleName;
370
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
136 else
ae4afb66768f - Renamed findModule() to findModulePath().
aziz
parents: 368
diff changeset
137 return moduleName;
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 366
diff changeset
138 }
364
1059295c2727 - Every command module has an execute method now.
aziz
parents:
diff changeset
139 }