-# A Nit module is usually associated with a Nit source file.
-# Modules can be nested (see `direct_owner`, `public_owner`, and `in_nesting`)
-class MModule
- # The model considered
- var model: Model
-
- # The direct nesting module, return null if self is not nested (ie. is a top-level module)
- var direct_owner: nullable MModule
-
- # The short name of the module
- var name: String
-
- # The origin of the definition
- var location: Location
-
- # Alias for `name`
- redef fun to_s do return self.name
-
- # The view of the module in the `model.mmodule_nesting_hierarchy`
- var in_nesting: POSetElement[MModule]
-
- # The view of the module in the `model.mmodule_importation_hierarchy`
- var in_importation: POSetElement[MModule]
-
- # The canonical name of the module
- # Example: `"owner::name"`
- fun full_name: String
- do
- var owner = self.public_owner
- if owner == null then
- return self.name
- else
- return "{owner.full_name}::{self.name}"
- end
- end
-
- # Create a new empty module and register it to a model
- # `direct_owner` is the direct owner (null if top-level module)
- init(model: Model, direct_owner: nullable MModule, name: String, location: Location)
- do
- self.model = model
- self.name = name
- self.location = location
- model.mmodules_by_name.add_one(name, self)
- model.mmodules.add(self)
- self.in_nesting = model.mmodule_nesting_hierarchy.add_node(self)
- self.direct_owner = direct_owner
- if direct_owner != null then
- model.mmodule_nesting_hierarchy.add_edge(direct_owner, self)
- end
- self.in_importation = model.mmodule_importation_hierarchy.add_node(self)
- end
-
- # Register the imported modules (ie "import some_module")
- # This function can only invoked once by mmodule.
- # The visibility must be set with `set_visibility_for`.
- fun set_imported_mmodules(imported_mmodules: Array[MModule])
- do
- assert unique_invocation: self.in_importation.direct_greaters.is_empty
- for m in imported_mmodules do
- self.model.mmodule_importation_hierarchy.add_edge(self, m)
- end
- end
-
- private var intrude_mmodules: HashSet[MModule] = new HashSet[MModule]
- private var public_mmodules: HashSet[MModule] = new HashSet[MModule]
- private var private_mmodules: HashSet[MModule] = new HashSet[MModule]
-
- # Return the visibility level of an imported module `m`
- fun visibility_for(m: MModule): MVisibility
- do
- if m == self then return intrude_visibility
- if self.intrude_mmodules.has(m) then return intrude_visibility
- if self.public_mmodules.has(m) then return public_visibility
- if self.private_mmodules.has(m) then return private_visibility
- return none_visibility
- end
-
- # Set the visibility of an imported module
- # REQUIRE: the visibility of the modules imported by `m` are already set for `m`
- fun set_visibility_for(m: MModule, v: MVisibility)
- do
- if v == intrude_visibility then
- self.intrude_mmodules.add(m)
- self.intrude_mmodules.add_all(m.intrude_mmodules)
- self.public_mmodules.add_all(m.public_mmodules)
- self.private_mmodules.add_all(m.private_mmodules)
- else if v == public_visibility then
- self.public_mmodules.add(m)
- self.public_mmodules.add_all(m.intrude_mmodules)
- self.public_mmodules.add_all(m.public_mmodules)
- else if v == private_visibility then
- self.private_mmodules.add(m)
- self.private_mmodules.add_all(m.intrude_mmodules)
- self.private_mmodules.add_all(m.public_mmodules)
- else
- print "{self} visibility for {m} = {v}"
- abort # invalid visibility
- end
- end