ni_nitdoc: simplified github option
[nit.git] / src / metamodel / static_type.nit
index 541e3e5..d8725e5 100644 (file)
@@ -16,7 +16,7 @@
 # limitations under the License.
 
 # Static types and property signatures
-package static_type
+module static_type
 
 intrude import abstractmetamodel
 
@@ -67,13 +67,26 @@ redef class MMLocalProperty
        end
 end
 
+class MMParam
+       var mmtype: MMType
+       var name: Symbol writable
+
+       init ( t  : MMType, n : Symbol )
+       do
+           mmtype = t
+           name = n
+       end
+
+       redef fun to_s do return "{name}: {mmtype}"
+end
+
 # Signature for local properties
 class MMSignature
        # The type of the reveiver
        readable var _recv: MMType
 
        # The parameter types
-       var _params: Array[MMType]
+       readable var _params: Array[MMParam]
 
        # The return type
        readable var _return_type: nullable MMType
@@ -81,6 +94,15 @@ class MMSignature
        # The closure parameters
        readable var _closures: Array[MMClosure] = new Array[MMClosure]
 
+       # Return the closure named 'name'. Null if no such a closure exists.
+       fun closure_named(name: Symbol): nullable MMClosure
+       do
+               for c in _closures do
+                       if c.name == name then return c
+               end
+               return null
+       end
+
        # Number of parameters
        fun arity: Int
        do
@@ -93,7 +115,7 @@ class MMSignature
                if self == s then
                        return true
                end
-               assert _recv.module == s.recv.module
+               assert _recv.mmmodule == s.recv.mmmodule
                var rt = _return_type
                var srt = s.return_type
                if arity != s.arity or (rt == null) != (srt == null) then return false
@@ -118,7 +140,7 @@ class MMSignature
        fun [](i: Int): MMType
        do
                assert _params.length > i
-               return _params[i]
+               return _params[i].mmtype
        end
 
        redef fun to_s
@@ -128,16 +150,13 @@ class MMSignature
                        var tmp: String
                        var a = new Array[String].with_capacity(_params.length)
                        for i in [0.._params.length[ do
-                               #var pn = _params_name[i]
                                var p = _params[i]
-                               #a.add("{pn}: {p}")
                                a.add(p.to_s)
                        end
-                       s.append("({a.join(",")})")
-               end
-               if _return_type != null then
-                       s.append(": {_return_type}")
+                       s.append("({a.join(", ")})")
                end
+               var rt = _return_type
+               if rt != null then s.append(": {rt}")
                return s.to_s
        end
 
@@ -147,10 +166,18 @@ class MMSignature
                if _recv == r then
                        return self
                end
-               var mod = r.module
-               var p = new Array[MMType]
+               var mod = r.mmmodule
+               var p = new Array[MMParam]
                for i in _params do
-                       p.add(i.for_module(mod).adapt_to(r))
+                       var new_type = i.mmtype.for_module(mod).adapt_to(r)
+                       var new_param
+                       if new_type == i.mmtype then
+                               new_param = i
+                       else
+                               new_param = new MMParam( new_type, i.name )
+                       end
+
+                       p.add( new_param )
                end
                var rv = _return_type
                if rv != null then
@@ -172,11 +199,18 @@ class MMSignature
                if _not_for_self_cache != null then return _not_for_self_cache.as(not null)
 
                var need_for_self = false
-               var p = new Array[MMType]
+               var p = new Array[MMParam]
                for i in _params do
-                       var i2 = i.not_for_self
-                       if i != i2 then need_for_self = true
-                       p.add(i2)
+                       var new_type = i.mmtype.not_for_self
+                       var new_param
+                       if i.mmtype == new_type then
+                               new_param = i
+                       else
+                               need_for_self = true
+                               new_param = new MMParam( new_type, i.name )
+                       end
+
+                       p.add( new_param )
                end
 
                var rv = _return_type
@@ -204,7 +238,7 @@ class MMSignature
                return res
        end
 
-       init(params: Array[MMType], return_type: nullable MMType, r: MMType)
+       init(params: Array[MMParam], return_type: nullable MMType, r: MMType)
        do
                _params = params
                _return_type = return_type
@@ -212,8 +246,22 @@ class MMSignature
        end
 end
 
+
+redef class MMExplicitImport
+       var signature : MMSignature
+
+       redef init( local_class : MMLocalClass, meth : MMMethod )
+       do
+               super
+               signature = meth.signature.adaptation_to( local_class.get_type )
+       end
+end
+
 # A closure in a signature
 class MMClosure
+       # The name of the closure (without the !)
+       readable var _name: Symbol
+
        # The signature of the closure
        readable var _signature: MMSignature
 
@@ -228,11 +276,12 @@ class MMClosure
        # Adapt the signature to a different receiver
        fun adaptation_to(r: MMType): MMClosure
        do
-               return new MMClosure(_signature.adaptation_to(r), _is_break, _is_optional)
+               return new MMClosure(_name, _signature.adaptation_to(r), _is_break, _is_optional)
        end
 
-       init(s: MMSignature, is_break: Bool, is_optional: Bool)
+       init(name: Symbol, s: MMSignature, is_break: Bool, is_optional: Bool)
        do
+               _name = name
                _signature = s
                _is_break = is_break
                _is_optional = is_optional
@@ -242,7 +291,7 @@ class MMClosure
        do
                var sig = _signature.not_for_self
                if sig != _signature then
-                       return new MMClosure(sig, _is_break, _is_optional)
+                       return new MMClosure(_name, sig, _is_break, _is_optional)
                else
                        return self
                end
@@ -271,7 +320,7 @@ abstract class MMAncestor
        fun inheriter: MMType do return _inheriter.as(not null)
 
        fun is_reffinement: Bool do
-               return stype.module != stype.module
+               return stype.mmmodule != stype.mmmodule
        end
 
        fun is_specialisation: Bool do
@@ -292,10 +341,10 @@ abstract class MMAncestor
 end
 
 # A static type
-# Note that static type a related to a specific module
+# Note that static type is related to a specific module
 abstract class MMType
        # The module where self makes sence
-       fun module: MMModule is abstract
+       fun mmmodule: MMModule is abstract
 
        # The local class that self direclty or indirectly refers to
        fun local_class: MMLocalClass is abstract
@@ -324,9 +373,9 @@ abstract class MMType
        # 'c' Must be a super-class of self
        # Example:
        #   class A[E]
-       #   class B[F] special A[F]
-       #   class C[G] special B[String]
-       #   class D special C[Float]
+       #   class B[F] super A[F]
+       #   class C[G] super B[String]
+       #   class D super C[Float]
        # 'A[Int]'.upcast_for('A') -> 'A[Int]'
        # 'A[Int]'.upcast_for('B') -> abort
        # 'B[Int]'.upcast_for('B') -> 'B[Int]'
@@ -367,15 +416,15 @@ abstract class MMType
 end
 
 class MMNullableType
-special MMType
-       var _base_type: MMType
+       super MMType
+       readable var _base_type: MMType
        redef fun is_valid do return _base_type.is_valid
        redef fun is_nullable: Bool do return true
        redef fun as_notnull do return _base_type
        redef fun as_nullable do return self
        init(t: MMType) do _base_type = t
 
-       redef fun module do return _base_type.module
+       redef fun mmmodule do return _base_type.mmmodule
 
        redef fun local_class do return _base_type.local_class
 
@@ -415,10 +464,10 @@ special MMType
        end
 end
 
-class MMTypeClass 
-special MMType
+abstract class MMTypeClass
+       super MMType
        redef readable var _local_class: MMLocalClass
-       redef fun module do return _local_class.module end
+       redef fun mmmodule do return _local_class.mmmodule end
        redef fun <(t) do return t.is_supertype(self)
 
        redef fun to_s
@@ -442,7 +491,7 @@ special MMType
 end
 
 class MMTypeSimpleClass
-special MMTypeClass
+       super MMTypeClass
        redef fun is_supertype(t)
        do
                return  t.local_class.cshe <= _local_class
@@ -451,7 +500,7 @@ special MMTypeClass
        redef fun for_module(mod)
        do
                var t: MMType = self
-               if module != mod then
+               if mmmodule != mod then
                        t = _local_class.for_module(mod).get_type
                end
                return t
@@ -467,8 +516,8 @@ end
 
 # The type of null
 class MMTypeNone
-special MMType
-       redef readable var _module: MMModule
+       super MMType
+       redef readable var _mmmodule: MMModule
        redef fun is_nullable: Bool do return true
        redef fun <(t) do return t isa MMTypeNone or t isa MMNullableType
        redef fun to_s do return "null"
@@ -478,7 +527,7 @@ special MMType
        redef fun as_nullable do return self
        redef fun as_notnull do abort
 
-       private init(m: MMModule) do _module = m
+       private init(m: MMModule) do _mmmodule = m
 end
 
 redef class MMModule
@@ -491,3 +540,37 @@ redef class MMModule
                return class_by_name(once ("Bool".to_symbol)).get_type
        end
 end
+
+# Explicitly imported cast
+class MMImportedCast
+       readable var _from : MMType
+       readable var _to : MMType
+
+       fun is_about_nullable_only : Bool
+       do
+           return ( _from.is_nullable and _to.as_nullable == _from ) or
+                  ( _to.is_nullable and _from.as_nullable == _to )
+       end
+
+       fun is_not_null_to_nullable : Bool
+       do
+           return not _from.is_nullable and _to.is_nullable
+       end
+
+       fun is_nullable_to_not_null : Bool
+       do
+           return _from.is_nullable and not _to.is_nullable
+       end
+
+       redef fun == ( o )
+       do
+               return o isa MMImportedCast and
+                       o.from == from and o.to == to
+       end
+end
+
+# Method local properties
+redef class MMMethod
+       # casts explicitely imported to be available from native implementation
+       fun explicit_casts : Set[ MMImportedCast ] is abstract
+end