# 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
+# 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)
#
-# It also provide an API to build and query models.
-#
-# All model classes starts with the M letter (`MModule`, `MClass`, etc.)
-#
-# TODO: better doc
-#
-# TODO: liearization, closures, extern stuff
+# TODO: liearization, extern stuff
# FIXME: better handling of the types
module model
import poset
import location
-import model_base
+import mmodule
+import mdoc
+import ordered_tree
private import more_collections
redef class Model
# The only null type
var null_type: MNullType = new MNullType(self)
+
+ # Build an ordered tree with from `concerns`
+ fun concerns_tree(mconcerns: Collection[MConcern]): ConcernsTree do
+ var seen = new HashSet[MConcern]
+ var res = new ConcernsTree
+
+ var todo = new Array[MConcern]
+ todo.add_all mconcerns
+
+ while not todo.is_empty do
+ var c = todo.pop
+ if seen.has(c) then continue
+ var pc = c.parent_concern
+ if pc == null then
+ res.add(null, c)
+ else
+ res.add(pc, c)
+ todo.add(pc)
+ end
+ seen.add(c)
+ end
+
+ return res
+ end
+end
+
+# An OrderedTree that can be easily refined for display purposes
+class ConcernsTree
+ super OrderedTree[MConcern]
end
redef class MModule
print("Fatal Error: no primitive class {name}")
exit(1)
end
- assert cla.length == 1 else print cla.join(", ")
+ if cla.length != 1 then
+ var msg = "Fatal Error: more than one primitive class {name}:"
+ for c in cla do msg += " {c.full_name}"
+ print msg
+ exit(1)
+ end
return cla.first
end
#
# 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
+
# The module that introduce the class
# While classes are not bound to a specific module,
# the introducing module is used for naming an visibility
# The short name of the class
# In Nit, the name of a class cannot evolve in refinements
- var name: String
+ redef var name: String
# The canonical name of the class
# Example: `"owner::module::MyClass"`
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
+
# The module where the definition is
var mmodule: MModule
self.to_s = "{mmodule}#{mclass}"
end
+ # 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]
# * foo(anchor, mmodule, othertype)
# * foo(othertype, mmodule, anchor)
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.
#
return res
end
+ # Return the not nullable version of the type
+ # Is the type is already not nullable, then self is returned.
+ #
+ # Note: this just remove the `nullable` notation, but the result can still contains null.
+ # For instance if `self isa MNullType` or self is a a formal type bounded by a nullable type.
+ fun as_notnullable: MType
+ do
+ return self
+ end
+
private var as_nullable_cache: nullable MType = null
abort
end
+ # Is the virtual type fixed for a given resolved_receiver?
+ fun is_fixed(mmodule: MModule, resolved_receiver: MType): Bool
+ do
+ assert not resolved_receiver.need_anchor
+ var props = self.mproperty.lookup_definitions(mmodule, resolved_receiver)
+ if props.is_empty then
+ abort
+ end
+ for p in props do
+ if p.as(MVirtualTypeDef).is_fixed then return true
+ end
+ return false
+ end
+
redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
do
assert can_resolve_for(mtype, anchor, mmodule)
if resolved_reciever.as(MClassType).mclass.kind == enum_kind then return res
# If the resolved type isa MVirtualType, it means that self was bound to it, and cannot be unbound. self is just fixed. so return the resolution.
if res isa MVirtualType then return res
+ # If we are final, just return the resolution
+ if is_fixed(mmodule, resolved_reciever) then return res
# It the resolved type isa intern class, then there is no possible valid redefinition is any potentiel subclass. self is just fixed. so simply return the resolution
if res isa MClassType and res.mclass.kind == enum_kind then return res
# TODO: Add 'fixed' virtual type in the specification.
# 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.
#
#print "{class_name}: {self}/{mtype}/{anchor}?"
if mtype isa MGenericType and mtype.mclass == self.mclass then
- return mtype.arguments[self.rank]
+ var res = mtype.arguments[self.rank]
+ if anchor != null and res.need_anchor then
+ # Maybe the result can be resolved more if are bound to a final class
+ var r2 = res.anchor_to(mmodule, anchor)
+ if r2 isa MClassType and r2.mclass.kind == enum_kind then return r2
+ end
+ return res
end
# self is a parameter type of mtype (or of a super-class of mtype)
redef fun need_anchor do return mtype.need_anchor
redef fun as_nullable do return self
+ redef fun as_notnullable do return mtype
redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
do
var res = self.mtype.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
redef fun collect_mtypes(mmodule) do return new HashSet[MClassType]
end
-# A signature of a method (or a closure)
+# A signature of a method
class MSignature
super MType
# The each parameter (in order)
var mparameters: Array[MParameter]
- var mclosures = new Array[MParameter]
-
# The return type (null for a procedure)
var return_mtype: nullable MType
var d = p.mtype.depth
if d > dmax then dmax = d
end
- for p in mclosures do
- var d = p.mtype.depth
- if d > dmax then dmax = d
- end
return dmax + 1
end
for p in mparameters do
res += p.mtype.length
end
- for p in mclosures do
- res += p.mtype.length
- end
return res
end
redef fun to_s
do
- var b = new Buffer
+ var b = new FlatBuffer
if not mparameters.is_empty then
b.append("(")
for i in [0..mparameters.length[ do
ret = ret.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
end
var res = new MSignature(params, ret)
- for p in self.mclosures do
- res.mclosures.add(p.resolve_for(mtype, anchor, mmodule, cleanup_virtual))
- end
return res
end
end
# 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
+ return "{name}: {mtype}..."
+ else
+ return "{name}: {mtype}"
+ end
+ end
+
fun resolve_for(mtype: MType, anchor: nullable MClassType, mmodule: MModule, cleanup_virtual: Bool): MParameter
do
if not self.mtype.need_anchor then return self
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.
# of any dynamic type).
# For instance, a call site "x.foo" is associated to a `MProperty`.
abstract class MProperty
+ super MEntity
+
# The associated MPropDef subclass.
# The two specialization hierarchy are symmetric.
type MPROPDEF: MPropDef
var intro_mclassdef: MClassDef
# The (short) name of the property
- var name: String
+ redef var name: String
# The canonical name of the property
# Example: "owner::my_module::MyClass::my_method"
# 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
fun lookup_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
do
assert not mtype.need_anchor
- if mtype isa MNullableType then mtype = mtype.mtype
+ mtype = mtype.as_notnullable
var cache = self.lookup_definitions_cache[mmodule, mtype]
if cache != null then return cache
end
# Second, filter the most specific ones
- var res = new Array[MPROPDEF]
- for pd1 in candidates do
- var cd1 = pd1.mclassdef
- var c1 = cd1.mclass
- var keep = true
- for pd2 in candidates do
- if pd2 == pd1 then continue # do not compare with self!
- var cd2 = pd2.mclassdef
- var c2 = cd2.mclass
- if c2.mclass_type == c1.mclass_type then
- if cd2.mmodule.in_importation <= cd1.mmodule then
- # cd2 refines cd1; therefore we skip pd1
- keep = false
- break
- end
- else if cd2.bound_mtype.is_subtype(mmodule, null, cd1.bound_mtype) then
- # cd2 < cd1; therefore we skip pd1
- keep = false
- break
- end
- end
- if keep then
- res.add(pd1)
- end
- end
- if res.is_empty then
- print "All lost! {candidates.join(", ")}"
- # FIXME: should be abort!
- end
- self.lookup_definitions_cache[mmodule, mtype] = res
- return res
+ return select_most_specific(mmodule, candidates)
end
private var lookup_definitions_cache: HashMap2[MModule, MType, Array[MPROPDEF]] = new HashMap2[MModule, MType, Array[MPROPDEF]]
# If you want the really most specific property, then look at `lookup_next_definition`
#
# FIXME: Move to `MPropDef`?
- fun lookup_super_definitions(mmodule: MModule, mtype: MType): Array[MPropDef]
+ fun lookup_super_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
do
assert not mtype.need_anchor
- if mtype isa MNullableType then mtype = mtype.mtype
+ mtype = mtype.as_notnullable
# First, select all candidates
- var candidates = new Array[MPropDef]
+ var candidates = new Array[MPROPDEF]
for mpropdef in self.mpropdefs do
# If the definition is not imported by the module, then skip
if not mmodule.in_importation <= mpropdef.mclassdef.mmodule then continue
if candidates.length <= 1 then return candidates
# Second, filter the most specific ones
- var res = new Array[MPropDef]
+ return select_most_specific(mmodule, candidates)
+ end
+
+ # Return an array containing olny the most specific property definitions
+ # This is an helper function for `lookup_definitions` and `lookup_super_definitions`
+ private fun select_most_specific(mmodule: MModule, candidates: Array[MPROPDEF]): Array[MPROPDEF]
+ do
+ var res = new Array[MPROPDEF]
for pd1 in candidates do
var cd1 = pd1.mclassdef
var c1 = cd1.mclass
var cd2 = pd2.mclassdef
var c2 = cd2.mclass
if c2.mclass_type == c1.mclass_type then
- if cd2.mmodule.in_importation <= cd1.mmodule then
+ if cd2.mmodule.in_importation < cd1.mmodule then
# cd2 refines cd1; therefore we skip pd1
keep = false
break
end
- else if cd2.bound_mtype.is_subtype(mmodule, null, cd1.bound_mtype) then
+ else if cd2.bound_mtype.is_subtype(mmodule, null, cd1.bound_mtype) and cd2.bound_mtype != cd1.bound_mtype then
# cd2 < cd1; therefore we skip pd1
keep = false
break
fun lookup_all_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
do
assert not mtype.need_anchor
- if mtype isa MNullableType then mtype = mtype.mtype
+ mtype = mtype.as_notnullable
var cache = self.lookup_all_definitions_cache[mmodule, mtype]
if cache != null then return cache
super
end
+ # Is the property defined at the top_level of the module?
+ # Currently such a property are stored in `Object`
+ var is_toplevel: Bool writable = false
+
# Is the property a constructor?
# Warning, this property can be inherited by subclasses with or without being a constructor
# therefore, you should use `is_init_for` the verify if the property is a legal constructor for a given class
# Unlike `MProperty`, a `MPropDef` is a local definition that belong to a
# specific class definition (which belong to a specific module)
abstract class MPropDef
+ super MEntity
# The associated `MProperty` subclass.
# the two specialization hierarchy are symmetric
self.to_s = "{mclassdef}#{mproperty}"
end
+ # 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
# The signature attached to the property definition
var msignature: nullable MSignature writable = null
- # The the method definition abstract?
+ # Is the method definition abstract?
var is_abstract: Bool writable = false
+
+ # Is the method definition intern?
+ var is_intern writable = false
+
+ # Is the method definition extern?
+ var is_extern writable = false
end
# A local definition of an attribute
# The bound of the virtual type
var bound: nullable MType writable = null
+
+ # Is the bound fixed?
+ var is_fixed writable = false
end
# A kind of class.
self.to_s = s
self.need_init = need_init
end
+
+ # Can a class of kind `self` specializes a class of kine `other`?
+ fun can_specialize(other: MClassKind): Bool
+ do
+ if other == interface_kind then return true # everybody can specialize interfaces
+ if self == interface_kind or self == enum_kind then
+ # no other case for interfaces
+ return false
+ else if self == extern_kind then
+ # only compatible with themselve
+ return self == other
+ else if other == enum_kind or other == extern_kind then
+ # abstract_kind and concrete_kind are incompatible
+ return false
+ end
+ # remain only abstract_kind and concrete_kind
+ return true
+ end
end
fun abstract_kind: MClassKind do return once new MClassKind("abstract class", true)
fun concrete_kind: MClassKind do return once new MClassKind("class", true)
fun interface_kind: MClassKind do return once new MClassKind("interface", false)
fun enum_kind: MClassKind do return once new MClassKind("enum", false)
-fun extern_kind: MClassKind do return once new MClassKind("extern", false)
+fun extern_kind: MClassKind do return once new MClassKind("extern class", false)