1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Modelisation of a Nit package
19 private import more_collections
23 # A Nit package, that encompass a product
27 # The name of the package
28 redef var name
: String
30 redef fun full_name
do return name
32 redef var c_name
= name
.to_cmangle
is lazy
34 # The model of the package
35 redef var model
: Model
37 # The root of the group tree
38 var root
: nullable MGroup = null is writable
40 # The group tree, as a POSet
41 var mgroups
= new POSet[MGroup]
43 redef fun to_s
do return name
47 model
.mpackages
.add
(self)
48 model
.mpackage_by_name
.add_one
(name
, self)
51 # MPackage are always roots of the concerns hierarchy
52 redef fun parent_concern
do return null
54 redef fun mdoc_or_fallback
56 if mdoc
!= null then return mdoc
57 return root
.mdoc_or_fallback
61 # A group of modules in a package
65 # The name of the group
66 # empty name for a default group in a single-module package
67 redef var name
: String
69 # The enclosing package
70 var mpackage
: MPackage
72 # The parent group if any
73 # see `in_nesting` for more
74 var parent
: nullable MGroup
76 # Fully qualified name.
77 # It includes each parent group separated by `/`
81 if p
== null then return name
82 return "{p.full_name}/{name}"
85 # The group is the group tree on the package (`mpackage.mgroups`)
86 # nested groups (children) are smaller
87 # nesting group (see `parent`) is bigger
88 var in_nesting
: POSetElement[MGroup] is noinit
90 # Is `self` the root of its package?
91 fun is_root
: Bool do return mpackage
.root
== self
93 # The filepath (usually a directory) of the group, if any
94 var filepath
: nullable String = null is writable
98 var tree
= mpackage
.mgroups
99 self.in_nesting
= tree
.add_node
(self)
100 var parent
= self.parent
101 if parent
!= null then
102 tree
.add_edge
(self, parent
)
106 redef fun model
do return mpackage
.model
108 redef fun parent_concern
do
109 if not is_root
then return parent
113 redef fun to_s
do return name
117 # packages of the model
118 var mpackages
= new Array[MPackage]
120 # Collections of package grouped by their names
121 private var mpackage_by_name
= new MultiHashMap[String, MPackage]
123 # Return all package named `name`
124 # If such a package is not yet loaded, null is returned (instead of an empty array)
125 fun get_mpackages_by_name
(name
: String): nullable Array[MPackage]
127 if mpackage_by_name
.has_key
(name
) then
128 return mpackage_by_name
[name
]