refactor: use Hash::has_key instead of getting null
[nit.git] / src / metamodel / abstractmetamodel.nit
index 1a2eb86..4e3dbff 100644 (file)
@@ -322,18 +322,31 @@ class MMGlobalClass
        end
 
        # Is the global class an interface?
-       readable writable attr _is_interface: Bool
+       readable writable attr _is_interface: Bool = false
 
        # Is the global class an abstract class?
-       readable writable attr _is_abstract: Bool
+       readable writable attr _is_abstract: Bool = false
 
        # Is the global class a universal class?
-       readable writable attr _is_universal: Bool
+       readable writable attr _is_universal: Bool = false
 
        # Visibility of the global class
        # 1 -> public
        # 3 -> private
-       readable writable attr _visibility_level: Int
+       readable writable attr _visibility_level: Int = 1 # FIXME: why this should be defined ?
+
+       # Is the global class a mixin class?
+       # A mixin class inherits all constructors from a superclass
+       meth is_mixin: Bool
+       do
+               return _mixin_of != self
+       end
+
+       # Indicate the superclass the class is a mixin of.
+       # If mixin_of == self then the class is not a mixin
+       # Is two classes have the same mixin_of value, then they both belong to the same mixing group
+       readable writable attr _mixin_of: MMGlobalClass = self
+
 end
 
 # Local classes are classes defined, refined or imported in a module
@@ -348,9 +361,6 @@ class MMLocalClass
        # The module of the local class
        readable attr _module: MMModule
 
-       # Is the class abstract
-       readable writable attr _abstract: Bool
-
        # The global class of the local class
        readable attr _global: MMGlobalClass 
 
@@ -409,18 +419,16 @@ class MMLocalClass
        # TODO: Will disapear when qualified names will be available
        meth has_global_property_by_name(n: Symbol): Bool
        do
-               var props = _properties_by_name[n]
-               return props != null
+               return _properties_by_name.has_key(n)
        end
 
        # Get a global property by its name
        # TODO: Will disapear when qualified names will be available
        meth get_property_by_name(n: Symbol): MMGlobalProperty
        do
+               if not has_global_property_by_name(n) then return null
                var props = _properties_by_name[n]
-               if props == null or props.length > 1 then
-                       return null
-               end
+               if props.length > 1 then return null
                return props.first
        end
 
@@ -436,13 +444,36 @@ class MMLocalClass
        meth method(na: Symbol): MMGlobalProperty
        do
                assert _properties_by_name != null
-               var props = _properties_by_name[na]
-               if props != null then
-                       return props.first
+               if _properties_by_name.has_key(na) then
+                       return _properties_by_name[na].first
                end
 
                return null
        end
+
+       # Select a method from its name
+       # TODO: Will disapear when qualified names will be available
+       meth select_method(name: Symbol): MMMethod
+       do
+               assert name != null
+               var gp = method(name)
+               if gp == null then return null
+               var res = self[gp]
+               assert res isa MMMethod
+               return res
+       end
+       
+       # Select an attribute from its name
+       # TODO: Will disapear when qualified names will be available
+       meth select_attribute(name: Symbol): MMAttribute
+       do
+               assert name != null
+               var gp = attribute(name)
+               if gp == null then return null
+               var res = self[gp]
+               assert res isa MMAttribute
+               return res
+       end
        
        # Look in super-classes (by specialization) and return properties with name
        # Beware, global property of results is not intended to be the same
@@ -479,11 +510,10 @@ class MMLocalClass
        do
                var prop = glob.intro
                var name = prop.name
-               var  props = _properties_by_name[name]
-               if props == null then
-                       _properties_by_name[name] = [glob]
-               else
+               if _properties_by_name.has_key(name) then
                        _properties_by_name[name].add(glob)
+               else
+                       _properties_by_name[name] = [glob]
                end
                _global_properties.add(glob)
                register_local_property(prop)
@@ -514,7 +544,7 @@ class MMGlobalProperty
        # The local property for each class that has the global property
 
        # The introducing local property
-       readable attr _intro: MMConcreteProperty
+       readable attr _intro: MMLocalProperty
 
        # The local class that introduces the global property
        meth local_class: MMLocalClass
@@ -522,29 +552,28 @@ class MMGlobalProperty
                return intro.local_class
        end
 
-       # The concrete property redefinition hierarchy
-       readable attr _concrete_property_hierarchy: PartialOrder[MMConcreteProperty] = new PartialOrder[MMConcreteProperty]
+       # The property redefinition hierarchy
+       readable attr _property_hierarchy: PartialOrder[MMLocalProperty] = new PartialOrder[MMLocalProperty]
 
        # Create a new global property introduced by a local one
-       protected init(p: MMConcreteProperty)
+       protected init(p: MMLocalProperty)
        do
                assert p != null
-               assert p.concrete_property != null
 
-               _concrete_property_hierarchy = new PartialOrder[MMConcreteProperty]
+               _property_hierarchy = new PartialOrder[MMLocalProperty]
        
                _intro = p
-               add_concrete_property(p, new Array[MMConcreteProperty])
+               add_local_property(p, new Array[MMLocalProperty])
        end
 
        redef meth to_s do return intro.full_name
 
-       # Register a new concrete property
-       meth add_concrete_property(i: MMConcreteProperty, sup: Array[MMConcreteProperty])
+       # Register a new local property
+       meth add_local_property(i: MMLocalProperty, sup: Array[MMLocalProperty])
        do
                assert i != null
                assert sup != null
-               i._cprhe = _concrete_property_hierarchy.add(i,sup)
+               i._prhe = _property_hierarchy.add(i,sup)
        end
 
        # Is the global property an attribute ?
@@ -556,15 +585,23 @@ class MMGlobalProperty
        # Is the global property a constructor (thus also a method)?
        readable writable attr _is_init: Bool
 
+       # Is the global property a constructor for a given class?
+       meth is_init_for(c: MMLocalClass): Bool
+       do
+               if not is_init then return false
+               var sc = intro.local_class
+               var res = c.che <= sc and c.global.mixin_of == sc.global.mixin_of
+               return res
+       end
+
        # Visibility of the property
        # 1 -> public
        # 2 -> protected
        # 3 -> private
-       readable writable attr _visibility_level: Int
+       readable writable attr _visibility_level: Int = 1 # FIXME: why this should be defined ?
 end
 
-# Local properties are adaptation of concrete local properties
-# They can be adapted for inheritance (or importation) or for type variarion (genericity, etc.)
+# Local properties are properties defined (explicitely or not) in a local class
 class MMLocalProperty
        # The name of the property
        readable attr _name: Symbol
@@ -575,9 +612,8 @@ class MMLocalProperty
        # The global property where belong the local property
        readable attr _global: MMGlobalProperty
 
-       # The concrete property
-       # May be self if self is a concrete property
-       readable attr _concrete_property: MMConcreteProperty 
+       # Redefinition hierarchy of the local property
+       readable attr _prhe: PartialOrderElement[MMLocalProperty]
 
        # The module of the local property
        meth module: MMModule do return _local_class.module
@@ -601,14 +637,24 @@ class MMLocalProperty
                _global = g
                _local_class.register_local_property(self)
        end
+
+       # Introduce a new global property for this local one
+       meth new_global
+       do
+               assert _global == null
+               _global = new MMGlobalProperty(self)
+               _local_class.register_global_property(_global)
+       end
        
        redef meth to_s do return name.to_s
 
-       protected init(n: Symbol, bc: MMLocalClass, i: MMConcreteProperty)
+       # Is the concrete property contain a `super' in the body?
+       readable writable attr _need_super: Bool = false
+
+       protected init(n: Symbol, bc: MMLocalClass)
        do
                _name = n
                _local_class = bc
-               _concrete_property = i
        end
 end
 
@@ -627,21 +673,3 @@ class MMConcreteClass
 special MMLocalClass
 end
 
-# Concrete local properties
-class MMConcreteProperty
-special MMLocalProperty
-       # Redefinition hierarchy of the concrete property
-       readable attr _cprhe: PartialOrderElement[MMConcreteProperty]
-
-       # Is the concrete property contain a `super' in the body?
-       readable writable attr _need_super: Bool
-       # Introduce a new global property for this local one
-       meth new_global
-       do
-               assert _global == null
-               _global = new MMGlobalProperty(self)
-               _local_class.register_global_property(_global)
-       end
-end
-