syntax: 'meth' -> 'fun', 'attr' -> 'var'
[nit.git] / src / metamodel / virtualtype.nit
index 84ff3b1..fa2b2aa 100644 (file)
@@ -22,43 +22,33 @@ import type_formal
 
 redef class MMGlobalProperty
        # Is self a virtual type
-       meth is_virtual_type: Bool do return intro isa MMTypeProperty
+       fun is_virtual_type: Bool do return intro isa MMTypeProperty
 end
 
 # Virtual type properties
 class MMTypeProperty
 special MMLocalProperty
-       redef meth inherit_to(t)
+       # The virtual static type associated
+       fun stype_for(recv: MMType): nullable MMVirtualType
        do
-               return new MMImplicitType(self, t)
+               var prop = recv.local_class[global]
+               assert prop isa MMTypeProperty
+               return prop.real_stype_for(recv)
        end
 
-       # Cached result of stype
-       attr _stype_cache: MMVirtualType
+       # Cached results of stype
+       var _stypes_cache: HashMap[MMType, MMVirtualType] = new HashMap[MMType, MMVirtualType]
 
-       # The virtual static type associated
-       meth stype: MMVirtualType
+       private fun real_stype_for(recv: MMType): nullable MMVirtualType
        do
                # If the signature is not build: Circular definition
                if signature == null then return null
 
-               var r = _stype_cache
-               if r == null then
-                       r = new MMVirtualType(self)
-                       _stype_cache = r
-               end
-               return r
-       end
-end
+               if _stypes_cache.has_key(recv) then return _stypes_cache[recv]
+
+               var res = new MMVirtualType(self, recv)
+               _stypes_cache[recv] = res
 
-redef class MMType
-       # Select a virtual type property by its name
-       meth select_virtual_type(name: Symbol): MMTypeProperty
-       do
-               assert local_class != null
-               assert name != null
-               var res = select_property(local_class.virtual_type(name))
-               assert res isa MMTypeProperty
                return res
        end
 end
@@ -66,47 +56,53 @@ end
 class MMVirtualType
 special MMTypeFormal
        # The property associed
-       readable attr _property: MMTypeProperty
+       readable var _property: MMTypeProperty
 
-       protected init(p: MMTypeProperty)
+       # The receiver type
+       readable var _recv: MMType
+
+       protected init(p: MMTypeProperty, recv: MMType)
        do
-               super(p.name, p.signature.return_type)
+               super(p.name, p.signature_for(recv).return_type)
                _property = p
+               _recv = recv
        end
 
-       redef meth for_module(mod)
+       redef fun module do return _recv.module
+
+       redef fun for_module(mod)
        do
-               var recv = _property.signature.recv.for_module(mod)
-               return adapt_to(recv)
+               if mod == module then return self
+               return adapt_to(recv.for_module(mod))
        end
 
-       redef meth not_for_self
+       redef fun not_for_self
        do
                return bound.not_for_self
        end
 
-       redef meth adapt_to(recv)
+       redef fun adapt_to(recv)
        do
-               # print "adapt {self} from {_property.signature.recv.module}::{_property.signature.recv} to {recv.module}::{recv}"
-               var prop = recv.select_property(_property.global)
-               assert prop isa MMTypeProperty
-               return prop.stype
+               return property.stype_for(recv).as(not null)
        end
 end
 
 redef class MMLocalClass
-       meth virtual_type(s: Symbol): MMGlobalProperty
+       fun virtual_type(s: Symbol): MMGlobalProperty
        do
                var prop = get_property_by_name(s)
                if prop.is_virtual_type then
                        return prop
                end
-               return null
+               abort
        end
-end
 
-class MMImplicitType
-special MMTypeProperty
-special MMImplicitProperty
-       init(p, t) do super
+       # Select a virtual type property by its name
+       fun select_virtual_type(name: Symbol): MMTypeProperty
+       do
+               var gp = virtual_type(name)
+               var res = self[gp]
+               assert res isa MMTypeProperty
+               return res
+       end
 end