annotate src/cmd/Compile.d @ 820:1d06b4aed7cf

Revised code in the first pass. Added code to handle anonymous unions and structs. Hope the idea will work. Added type to class Aggregate and isAnonymous to some other Symbol classes.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Fri, 14 Mar 2008 15:42:08 +0100
parents 35d238d502cb
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
810
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
1 /++
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
2 Author: Aziz Köksal
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
3 License: GPL3
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
4 +/
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
5 module cmd.Compile;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
6
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
7 import dil.semantic.Module;
816
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
8 import dil.semantic.Package;
810
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
9 import dil.semantic.Pass1;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
10 import dil.semantic.Pass2;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
11 import dil.semantic.Symbols;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
12 import dil.doc.Doc;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
13 import dil.Compilation;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
14 import dil.Information;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
15 import dil.ModuleManager;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
16 import common;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
17
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
18 /// The compile command.
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
19 struct CompileCommand
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
20 {
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
21 string[] filePaths; /// Explicitly specified modules (on the command line.)
816
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
22 bool printSymbolTree; /// Whether to print the symbol tree.
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
23 bool printModuleTree; /// Whether to print the module tree.
810
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
24 ModuleManager moduleMan;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
25 SemanticPass1[] passes1;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
26
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
27 CompilationContext context;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
28 InfoManager infoMan;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
29
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
30 /// Executes the compile command.
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
31 void run()
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
32 {
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
33 moduleMan = new ModuleManager(context.importPaths, infoMan);
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
34 foreach (filePath; filePaths)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
35 {
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
36 auto modul = moduleMan.loadModuleFile(filePath);
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
37 runPass1(modul);
816
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
38 if (printSymbolTree)
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
39 printSymbolTable(modul, "");
810
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
40 }
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
41
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
42 // foreach (modul; moduleMan.loadedModules)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
43 // {
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
44 // auto pass2 = new SemanticPass2(modul);
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
45 // pass2.run();
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
46 // }
816
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
47
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
48 if (printModuleTree)
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
49 printMTree(moduleMan.rootPackage, "");
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
50 }
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
51
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
52 void printMTree(Package pckg, string indent)
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
53 {
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
54 Stdout(indent)(pckg.pckgName)("/").newline;
820
1d06b4aed7cf Revised code in the first pass.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 816
diff changeset
55 foreach (p; pckg.packages) // TODO: sort packages alphabetically by name?
816
35d238d502cb The ModuleManager handles packages as well now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
56 printMTree(p, indent ~ " ");
820
1d06b4aed7cf Revised code in the first pass.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 816
diff changeset
57 foreach (m; pckg.modules) // TODO: sort modules alphabetically by name?
1d06b4aed7cf Revised code in the first pass.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 816
diff changeset
58 Stdout(indent ~ " ")(m.moduleName)(".")(m.fileExtension()).newline;
810
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
59 }
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
60
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
61 /// Runs the first pass on modul.
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
62 void runPass1(Module modul)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
63 {
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
64 if (modul.hasErrors || modul.semanticPass != 0)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
65 return;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
66 auto pass1 = new SemanticPass1(modul, context);
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
67 pass1.importModule = &importModule;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
68 pass1.run();
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
69 passes1 ~= pass1;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
70 }
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
71
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
72 /// Imports a module and runs the first pass on it.
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
73 Module importModule(string moduleFQNPath)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
74 {
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
75 auto modul = moduleMan.loadModule(moduleFQNPath);
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
76 modul && runPass1(modul);
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
77 return modul;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
78 }
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
79
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
80 /// Prints all symbols recursively (for debugging.)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
81 static void printSymbolTable(ScopeSymbol scopeSym, char[] indent)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
82 {
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
83 foreach (member; scopeSym.members)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
84 {
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
85 auto tokens = getDocTokens(member.node);
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
86 char[] docText;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
87 foreach (token; tokens)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
88 docText ~= token.srcText;
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
89 Stdout(indent).formatln("Id:{}, Symbol:{}, DocText:{}",
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
90 member.name.str, member.classinfo.name,
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
91 docText);
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
92 if (auto s = cast(ScopeSymbol)member)
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
93 printSymbolTable(s, indent ~ "→ ");
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
94 }
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
95 }
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
96 }