1 /// Author: Aziz Köksal
2 /// License: GPL3
3 /// $(Maturity average)
4 module dil.semantic.Package;
5 
6 import dil.semantic.Symbol,
7        dil.semantic.Symbols,
8        dil.semantic.Module;
9 import dil.lexer.IdTable;
10 import common;
11 
12 /// A package groups modules and other packages.
13 class Package : PackageSymbol
14 {
15   cstring pckgName;    /// The name of the package. E.g.: 'dil'.
16   Package[] packages; /// The sub-packages contained in this package.
17   Module[] modules;   /// The modules contained in this package.
18 
19   /// Constructs a Package object.
20   this(cstring pckgName, IdTable idtable)
21   {
22     auto name = idtable.lookup(pckgName);
23     super(name);
24     this.pckgName = pckgName;
25   }
26 
27   /// Returns true if this is the root package.
28   bool isRoot()
29   {
30     return parent is null;
31   }
32 
33   /// Returns the parent package or null if this is the root.
34   Package parentPackage()
35   {
36     if (isRoot())
37       return null;
38     assert(parent.isPackage);
39     return parent.to!(Package);
40   }
41 
42   /// Adds a module to this package.
43   void add(Module modul)
44   {
45     modul.parent = this;
46     modules ~= modul;
47     insert(modul, modul.name);
48   }
49 
50   /// Adds a package to this package.
51   void add(Package pckg)
52   {
53     pckg.parent = this;
54     packages ~= pckg;
55     insert(pckg, pckg.name);
56   }
57 
58   /// Returns a list of all modules in this package tree.
59   Module[] getModuleList()
60   {
61     auto list = modules.dup;
62     foreach (pckg; packages)
63       list ~= pckg.getModuleList();
64     return list;
65   }
66 }