# See the License for the specific language governing permissions and
# limitations under the License.
-# Object model of the Nit language
+# Classes, types and properties
#
-# This module define the entities of the Nit meta-model like modules,
-# classes, types and properties
-#
-# It also provide an API to build and query models.
-#
-# All model classes starts with the M letter (`MModule`, `MClass`, etc.)
-#
-# TODO: better doc
+# All three concepts are defined in this same module because these are strongly connected:
+# * types are based on classes
+# * classes contains properties
+# * some properties are types (virtual types)
#
# TODO: liearization, extern stuff
# FIXME: better handling of the types
#
# This characteristic helps the reasoning about classes in a program since a
# single `MClass` object always denote the same class.
-# However, because a `MClass` is global, it does not really have properties nor
-# belong to a hierarchy since the property and the
-# hierarchy of a class depends of a module.
+#
+# The drawback is that classes (`MClass`) contain almost nothing by themselves.
+# These do not really have properties nor belong to a hierarchy since the property and the
+# hierarchy of a class depends of the refinement in the modules.
+#
+# Most services on classes require the precision of a module, and no one can asks what are
+# the super-classes of a class nor what are properties of a class without precising what is
+# the module considered.
+#
+# For instance, during the typing of a source-file, the module considered is the module of the file.
+# eg. the question *is the method `foo` exists in the class `Bar`?* must be reformulated into
+# *is the method `foo` exists in the class `Bar` in the current module?*
+#
+# During some global analysis, the module considered may be the main module of the program.
class MClass
super MEntity
end
end
+ redef fun model do return intro_mmodule.model
+
# All class definitions (introduction and refinements)
var mclassdefs: Array[MClassDef] = new Array[MClassDef]
#
# A `MClassDef` is associated with an explicit (or almost) definition of a
# class. Unlike `MClass`, a `MClassDef` is a local definition that belong to
-# a specific module
+# a specific class and a specific module, and contains declarations like super-classes
+# or properties.
+#
+# It is the class definitions that are the backbone of most things in the model:
+# ClassDefs are defined with regard with other classdefs.
+# Refinement and specialization are combined to produce a big poset called the `Model::mclassdef_hierarchy`.
+#
+# Moreover, the extension and the intention of types is defined by looking at the MClassDefs.
class MClassDef
super MEntity
# Actually the name of the `mclass`
redef fun name do return mclass.name
+ redef fun model do return mmodule.model
+
# All declared super-types
# FIXME: quite ugly but not better idea yet
var supertypes: Array[MClassType] = new Array[MClassType]
abstract class MType
super MEntity
- # The model of the type
- fun model: Model is abstract
+ redef fun name do return to_s
# Return true if `self` is an subtype of `sup`.
# The typing is done using the standard typing policy of Nit.
# Replace formals generic types in self with resolved values in `mtype`
# If `cleanup_virtual` is true, then virtual types are also replaced
- # with their bounds
+ # with their bounds.
#
# This function returns self if `need_anchor` is false.
#
#
# The resolution can be done because `E` make sense for the class A (see `can_resolve_for`)
#
- # TODO: Explain the cleanup_virtual
- #
# FIXME: the parameter `cleanup_virtual` is just a bad idea, but having
# two function instead of one seems also to be a bad idea.
#
# It's mean that all refinements of a same class "share" the parameter type,
# but that a generic subclass has its on parameter types.
#
-# However, in the sense of the meta-model, the a parameter type of a class is
-# a valid types in a subclass. The "in the sense of the meta-model" is
+# However, in the sense of the meta-model, a parameter type of a class is
+# a valid type in a subclass. The "in the sense of the meta-model" is
# important because, in the Nit language, the programmer cannot refers
# directly to the parameter types of the super-classes.
#
# A parameter in a signature
class MParameter
+ super MEntity
+
# The name of the parameter
- var name: String
+ redef var name: String
# The static type of the parameter
var mtype: MType
# Is the parameter a vararg?
var is_vararg: Bool
+ init(name: String, mtype: MType, is_vararg: Bool) do
+ self.name = name
+ self.mtype = mtype
+ self.is_vararg = is_vararg
+ end
+
redef fun to_s
do
if is_vararg then
var res = new MParameter(self.name, newtype, self.is_vararg)
return res
end
+
+ redef fun model do return mtype.model
end
# A service (global property) that generalize method, attribute, etc.
# associated definition, this method will abort
fun intro: MPROPDEF do return mpropdefs.first
+ redef fun model do return intro.model
+
# Alias for `name`
redef fun to_s do return name
# Actually the name of the `mproperty`
redef fun name do return mproperty.name
+ redef fun model do return mclassdef.model
+
# Internal name combining the module, the class and the property
# Example: "mymodule#MyClass#mymethod"
redef var to_s: String