model: use > in fullnames of groups. An add a trailing `>` to them distinct.
[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 # The root of the group tree
38 var root: nullable MGroup = null is writable
39
40 # The group tree, as a POSet
41 var mgroups = new POSet[MGroup]
42
43 redef fun to_s do return name
44
45 init
46 do
47 model.mpackages.add(self)
48 model.mpackage_by_name.add_one(name, self)
49 end
50
51 # MPackage are always roots of the concerns hierarchy
52 redef fun parent_concern do return null
53
54 redef fun mdoc_or_fallback
55 do
56 if mdoc != null then return mdoc
57 return root.mdoc_or_fallback
58 end
59 end
60
61 # A group of modules in a package
62 class MGroup
63 super MConcern
64
65 # The name of the group
66 # empty name for a default group in a single-module package
67 redef var name: String
68
69 # The enclosing package
70 var mpackage: MPackage
71
72 # The parent group if any
73 # see `in_nesting` for more
74 var parent: nullable MGroup
75
76 # Fully qualified name.
77 # It includes each parent group separated by `>`.
78 # The full_name is terminated by `>` to avoid collision with other entities.
79 #
80 # E.g. `core>` and `core>collection>`
81 redef fun full_name
82 do
83 var p = parent
84 if p == null then return "{name}>"
85 return "{p.full_name}{name}>"
86 end
87
88 # The group is the group tree on the package (`mpackage.mgroups`)
89 # nested groups (children) are smaller
90 # nesting group (see `parent`) is bigger
91 var in_nesting: POSetElement[MGroup] is noinit
92
93 # Is `self` the root of its package?
94 fun is_root: Bool do return mpackage.root == self
95
96 # The filepath (usually a directory) of the group, if any
97 var filepath: nullable String = null is writable
98
99 init
100 do
101 var tree = mpackage.mgroups
102 self.in_nesting = tree.add_node(self)
103 var parent = self.parent
104 if parent != null then
105 tree.add_edge(self, parent)
106 end
107 end
108
109 redef fun model do return mpackage.model
110
111 redef fun parent_concern do
112 if not is_root then return parent
113 return mpackage
114 end
115
116 redef fun to_s do return name
117 end
118
119 redef class Model
120 # packages of the model
121 var mpackages = new Array[MPackage]
122
123 # Collections of package grouped by their names
124 private var mpackage_by_name = new MultiHashMap[String, MPackage]
125
126 # Return all package named `name`
127 # If such a package is not yet loaded, null is returned (instead of an empty array)
128 fun get_mpackages_by_name(name: String): nullable Array[MPackage]
129 do
130 if mpackage_by_name.has_key(name) then
131 return mpackage_by_name[name]
132 else
133 return null
134 end
135 end
136 end