X-Git-Url: http://nitlanguage.org diff --git a/src/model/model_base.nit b/src/model/model_base.nit index b2483d9..0a0d8b0 100644 --- a/src/model/model_base.nit +++ b/src/model/model_base.nit @@ -14,182 +14,117 @@ # See the License for the specific language governing permissions and # limitations under the License. +# The abstract concept of model and related common things module model_base - -import poset import location -private import more_collections # The container class of a Nit object-oriented model. # A model knows modules, classes and properties and can retrieve them. class Model - # All known modules - var mmodules: Array[MModule] = new Array[MModule] - - # Module nesting hierarchy. - # where mainmodule < mainmodule::nestedmodule - var mmodule_nesting_hierarchy: POSet[MModule] = new POSet[MModule] + super MEntity - # Full module importation hierarchy including private or nested links. - var mmodule_importation_hierarchy: POSet[MModule] = new POSet[MModule] + redef fun model do return self - # Collections of modules grouped by their short names - private var mmodules_by_name: MultiHashMap[String, MModule] = new MultiHashMap[String, MModule] - - # Return all module named `name' - # If such a module does not exist, null is returned (instead of an empty array) + # Place-holder object that means no-location # - # Visibility or modules are not considered - fun get_mmodules_by_name(name: String): nullable Array[MModule] - do - if mmodules_by_name.has_key(name) then - return mmodules_by_name[name] - else - return null - end - end + # See `MEntity::location` + var no_location = new Location(null, 0, 0, 0, 0) end -# 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 module_nesting_hierarchy - var in_nesting: POSetElement[MModule] - - # The view of the module in the module_importation_hierarchy - var in_importation: POSetElement[MModule] +# A named and possibly documented entity in the model. +# This class is useful to generalize presentation of entities to the human. +abstract class MEntity + # The short (unqualified) name of this model entity. + # + # The short-name is based from the identifiers used to declare or denote the entity. + # It is usually globally ambiguous but is often enough in a precise local context. + # + # It is suitable to use the short-name in message to the user. + # However, special care must be used in case of potential ambiguities or name conflict. + fun name: String is abstract - # 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 + # A fully-qualified name of this model entity. + # + # The full-name is based on the short name and is usually prefixed by the name of an outer entity. + # Usually the quad (`::`) is used to separate the different names. + # + # The full-name is expected to be unique and unambiguous in lawful Nit models for the same kind of entity. + # + # It is often suitable to use it in message to the user. + # However, some full-name could be long and verbose, + # + # See the specific implementation in subclasses for details. + fun full_name: String is abstract - # 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 + # A fully-qualified C-like identifier of this model entity. + # + # The C-name is a name that respects the rule of identifiers in the C language: + # it is only made of alphanumeric characters and starts with a letter (or a underscore). + # + # The C-name can be seen as a mangled version of the `full_name`. + # Therefore, it is expected to be unique and unambiguous in lawful Nit models for the same kind of entity. + # + # The C-name is used by tools that need some identifiers in generated files to designate the + # entity. + # + # Is is not suitable to use it directly with the user (e.g. in message) and + # indirect use should be restricted (e.g. to name a web-page) + fun c_name: String is abstract - # Register the imported modules (ie "import some_module") - # This function can only invoked once by mmodule. - # The visibility must be set with `se_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 + # The origin of the definition. + # + # Most model entities are defined in a specific place in the source base. + # + # Because most model entities have one, + # it is simpler for the client to have a non-nullable return value. + # For entities that lack a location, mock-up special locations are used instead. + # By default it is `model.no_location`. + fun location: Location do return model.no_location - 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] + # A Model Entity has a direct link to its model + fun model: Model is abstract - # 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 + # The indication that the entity did not pass some semantic verifications. + # + # This simple flag is set by a given analysis to say that the entity is broken and unusable in + # an execution. + # When an entity status is set to broken, it is usually associated with a error message. + # + # If it is safe to do so, clients of the model SHOULD just skip broken entities in their processing. + # Clients that do not care about the executability (e.g. metrics) MAY still process the entity or + # perform specific checks to determinate the validity of the entity. + # + # Note that the broken status is not propagated to enclosing and enclosed entities. + # e.g. a broken method does not make the whole module broken. + var is_broken = false is writable - # 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 + # Is `self` created for internal purpose? + # + # Fictive entities are used internally but they should not be + # exposed to the final user. + var is_fictive: Bool = false is writable - # The first module in the nesting hierarchy to export self as public - # This function is used to determine the canonical name of modules, classes and properties. - # REQUIRE: the visibility of all nesting modules is already set for `m'. - fun public_owner: nullable MModule - do - var res = self.direct_owner - var last = res - while last != null do - if last.visibility_for(self) >= public_visibility then res = last - last = last.direct_owner - end - return res - end + # Is `self` created for unit testing purpose? + # + # See `nitunit`. + var is_test: Bool = false is writable +end - # Return true if a class or a property introduced in `intro_mmodule' with a visibility of 'visibility' is visible in self. - fun is_visible(intro_mmodule: MModule, visibility: MVisibility): Bool - do - var v = visibility_for(intro_mmodule) - if v == intrude_visibility then - return visibility >= private_visibility - else if v == public_visibility then - return visibility > private_visibility - else if v == private_visibility then - return visibility > private_visibility - else if v == none_visibility then - return false - else - abort - end - end +# Something that represents a concern +abstract class MConcern + super MEntity + # The concern that contains `self` or null if `self` is the root of the concern hierarchy + fun parent_concern: nullable MConcern is abstract end # A visibility (for modules, class and properties) # Valid visibility are: # -# * intrude_visibility -# * public_visibility -# * protected_visibility -# * none_visibility +# * `intrude_visibility` +# * `public_visibility` +# * `protected_visibility` +# * `none_visibility` +# * `private_visiblity` # # Note this class is basically an enum. # FIXME: use a real enum once user-defined enums are available @@ -201,11 +136,7 @@ class MVisibility private var level: Int - private init(s: String, level: Int) - do - self.to_s = s - self.level = level - end + # TODO: private init because enumeration. # Is self give less visibility than other # none < private < protected < public < intrude @@ -215,8 +146,23 @@ class MVisibility end end -fun intrude_visibility: MVisibility do return once new MVisibility("intrude", 4) +# A `Comparator` to sort mentities by their names. +class MEntityNameSorter + super Comparator + + redef type COMPARED: MEntity + + # Returns `a.name <=> b.name`. + redef fun compare(a, b) do return a.name <=> b.name +end + +# The visibility level `intrude` +fun intrude_visibility: MVisibility do return once new MVisibility("intrude", 5) +# The visibility level `public` fun public_visibility: MVisibility do return once new MVisibility("public", 4) +# The visibility level `protected` fun protected_visibility: MVisibility do return once new MVisibility("protected", 3) +# The visibility level `private` fun private_visibility: MVisibility do return once new MVisibility("private", 2) -fun none_visibility: MVisibility do return once new MVisibility("none", 2) +# The visibility level `none` (no visibility) +fun none_visibility: MVisibility do return once new MVisibility("none", 1)