From: Jean Privat Date: Tue, 22 Jul 2014 02:57:05 +0000 (-0400) Subject: Merge: proposal for model: Generalize access to `model` form `MEntitiy`. X-Git-Tag: v0.6.7~35 X-Git-Url: http://nitlanguage.org?hp=-c Merge: proposal for model: Generalize access to `model` form `MEntitiy`. Since, MEntity are model things, let the user access to the model from them. Signed-off-by: Alexandre Terrasa Pull-Request: #598 Reviewed-by: Jean Privat --- b7608f7c69891c2e16889d81e107a12f620cc9fa diff --combined src/model/model.nit index be29e6f,8d9c915..7568e9b --- a/src/model/model.nit +++ b/src/model/model.nit @@@ -14,12 -14,16 +14,12 @@@ # 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 @@@ -315,20 -319,9 +315,20 @@@ en # # 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 @@@ -387,6 -380,8 +387,8 @@@ end end + redef fun model do return intro_mmodule.model + # All class definitions (introduction and refinements) var mclassdefs: Array[MClassDef] = new Array[MClassDef] @@@ -455,14 -450,7 +457,14 @@@ en # # 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 @@@ -510,6 -498,8 +512,8 @@@ # 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] @@@ -601,8 -591,7 +605,7 @@@ en 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. @@@ -783,7 -772,7 +786,7 @@@ # 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. # @@@ -844,6 -833,8 +847,6 @@@ # # 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. # @@@ -1211,8 -1202,8 +1214,8 @@@ en # 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. # @@@ -1508,8 -1499,10 +1511,10 @@@ en # 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 @@@ -1517,6 -1510,12 +1522,12 @@@ # 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 @@@ -1533,6 -1532,8 +1544,8 @@@ 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. @@@ -1593,6 -1594,8 +1606,8 @@@ abstract class MPropert # 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 @@@ -1847,6 -1850,8 +1862,8 @@@ abstract class MPropDe # 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