model: filepath is now just an alias on the location
[nit.git] / src / model / mpackage.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Modelisation of a Nit package
16 module mpackage
17
18 import model_base
19 private import more_collections
20 import poset
21 import mdoc
22
23 # A Nit package, that encompass a product
24 class MPackage
25 super MConcern
26
27 # The name of the package
28 redef var name: String
29
30 redef fun full_name do return name
31
32 redef var c_name = name.to_cmangle is lazy
33
34 # The model of the package
35 redef var model: Model
36
37 redef var location
38
39 # The root of the group tree
40 var root: nullable MGroup = null is writable
41
42 # The group tree, as a POSet
43 var mgroups = new POSet[MGroup]
44
45 redef fun to_s do return name
46
47 init
48 do
49 model.mpackages.add(self)
50 model.mpackage_by_name.add_one(name, self)
51 end
52
53 # MPackage are always roots of the concerns hierarchy
54 redef fun parent_concern do return null
55
56 redef fun mdoc_or_fallback
57 do
58 if mdoc != null then return mdoc
59 return root.mdoc_or_fallback
60 end
61 end
62
63 # A group of modules in a package
64 class MGroup
65 super MConcern
66
67 # The name of the group
68 # empty name for a default group in a single-module package
69 redef var name: String
70
71 redef var location
72
73 # The enclosing package
74 var mpackage: MPackage
75
76 # The parent group if any
77 # see `in_nesting` for more
78 var parent: nullable MGroup
79
80 # Fully qualified name.
81 # It includes each parent group separated by `>`.
82 # The full_name is terminated by `>` to avoid collision with other entities.
83 #
84 # E.g. `core>` and `core>collection>`
85 redef fun full_name
86 do
87 var p = parent
88 if p == null then return "{name}>"
89 return "{p.full_name}{name}>"
90 end
91
92 # The group is the group tree on the package (`mpackage.mgroups`)
93 # nested groups (children) are smaller
94 # nesting group (see `parent`) is bigger
95 var in_nesting: POSetElement[MGroup] is noinit
96
97 # Is `self` the root of its package?
98 fun is_root: Bool do return mpackage.root == self
99
100 # The filepath (usually a directory) of the group, if any
101 #
102 # safe alias to `location.file.filename`
103 fun filepath: nullable String do
104 var res
105 res = self.location.file
106 if res == null then return null
107 return res.filename
108 end
109
110 init
111 do
112 var tree = mpackage.mgroups
113 self.in_nesting = tree.add_node(self)
114 var parent = self.parent
115 if parent != null then
116 tree.add_edge(self, parent)
117 end
118 end
119
120 redef fun model do return mpackage.model
121
122 redef fun parent_concern do
123 if not is_root then return parent
124 return mpackage
125 end
126
127 redef fun to_s do return name
128 end
129
130 redef class Model
131 # packages of the model
132 var mpackages = new Array[MPackage]
133
134 # Collections of package grouped by their names
135 private var mpackage_by_name = new MultiHashMap[String, MPackage]
136
137 # Return all package named `name`
138 # If such a package is not yet loaded, null is returned (instead of an empty array)
139 fun get_mpackages_by_name(name: String): nullable Array[MPackage]
140 do
141 if mpackage_by_name.has_key(name) then
142 return mpackage_by_name[name]
143 else
144 return null
145 end
146 end
147 end