src: use MFormalType for type checks when it makes sense
[nit.git] / src / nitni / nitni_callbacks.nit
index 6f85029..8542a95 100644 (file)
@@ -33,7 +33,10 @@ class VerifyNitniCallbacksPhase
 
        redef fun process_npropdef(npropdef)
        do
-               if not npropdef isa AExternPropdef then return
+               if not npropdef isa AMethPropdef then return
+               var mpropdef = npropdef.mpropdef
+               if mpropdef == null then return
+               if not mpropdef.is_extern then return
 
                npropdef.verify_nitni_callbacks(toolcontext)
        end
@@ -80,7 +83,7 @@ class ForeignCallbackSet
        end
 end
 
-redef class AExternPropdef
+redef class AMethPropdef
        private var foreign_callbacks_cache: nullable ForeignCallbackSet = null
 
        # All foreign callbacks from this method
@@ -108,7 +111,7 @@ redef class AExternPropdef
                # return type
                var rmt = mpropdef.msignature.return_mtype
                if rmt != null then
-                       if rmt isa MParameterType or rmt isa MVirtualType then
+                       if rmt isa MFormalType then
                                var mclass_type = mpropdef.mclassdef.bound_mtype
                                rmt = rmt.anchor_to(mmodule, mclass_type)
                        end
@@ -119,7 +122,7 @@ redef class AExternPropdef
                # params
                for p in mpropdef.msignature.mparameters do
                        var mtype = p.mtype.resolve_for(recv_type, recv_type, mmodule, true)
-                       if mtype isa MParameterType or mtype isa MVirtualType then
+                       if mtype isa MFormalType then
                                var mclass_type = mpropdef.mclassdef.bound_mtype
                                mtype = mtype.anchor_to(mmodule, mclass_type)
                        end
@@ -139,6 +142,8 @@ redef class AExternPropdef
 
        redef fun accept_rapid_type_visitor(v)
        do
+               if foreign_callbacks_cache == null then return
+
                for cb in foreign_callbacks.callbacks do v.add_send(cb.recv_mtype, cb.mproperty.as(MMethod))
                for cast in foreign_callbacks.casts do v.add_cast_type(cast.to)
                for sup in foreign_callbacks.supers do
@@ -298,13 +303,12 @@ redef class AFullPropExternCall
        do
                var mmodule = npropdef.mpropdef.mclassdef.mmodule
                var mclassdef = npropdef.mpropdef.mclassdef
-               var nclassdef = toolcontext.modelbuilder.mclassdef2nclassdef[mclassdef]
                var mclass_type = mclassdef.bound_mtype
-               var mtype = toolcontext.modelbuilder.resolve_mtype(nclassdef, n_type)
+               var mtype = toolcontext.modelbuilder.resolve_mtype(mmodule, mclassdef, n_type)
 
                if mtype == null then return
 
-               if mtype isa MParameterType or mtype isa MVirtualType then
+               if mtype isa MFormalType then
                        mtype = mtype.anchor_to(mmodule, mclass_type)
                end
 
@@ -333,8 +337,7 @@ redef class AInitPropExternCall
        do
                var mmodule = npropdef.mpropdef.mclassdef.mmodule
                var mclassdef = npropdef.mpropdef.mclassdef
-               var nclassdef = toolcontext.modelbuilder.mclassdef2nclassdef[mclassdef]
-               var mtype = toolcontext.modelbuilder.resolve_mtype(nclassdef, n_type)
+               var mtype = toolcontext.modelbuilder.resolve_mtype(mmodule, mclassdef, n_type)
                if mtype == null then return
 
                if not mtype isa MClassType then
@@ -342,11 +345,17 @@ redef class AInitPropExternCall
                        return
                end
 
-               var meth_name = "init"
+               var meth_name = "new"
                var meth = toolcontext.modelbuilder.try_get_mproperty_by_name2( self,
                        mmodule, mtype, meth_name )
 
                if meth == null then
+                       meth_name = "init"
+                       meth = toolcontext.modelbuilder.try_get_mproperty_by_name2( self,
+                               mmodule, mtype, meth_name )
+               end
+
+               if meth == null then
                        toolcontext.error(location, "Method {meth_name} not found in {n_type.collect_text}." )
                        return
                end
@@ -388,9 +397,10 @@ redef class ACastAsExternCall
 
        redef fun verify_and_collect(npropdef, callback_set, toolcontext)
        do
-               var parent_aclassdef = npropdef.parent.as(AClassdef)
-               toolcontext.modelbuilder.resolve_mtype_unchecked(parent_aclassdef, n_from_type, true)
-               toolcontext.modelbuilder.resolve_mtype_unchecked(parent_aclassdef, n_to_type, true)
+               var mclassdef = npropdef.mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
+               toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_from_type, true)
+               toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_to_type, true)
                super
        end
 end
@@ -401,8 +411,9 @@ redef class AAsNullableExternCall
 
        redef fun verify_and_collect(npropdef, callback_set, toolcontext)
        do
-               var parent_aclassdef = npropdef.parent.as(AClassdef)
-               toolcontext.modelbuilder.resolve_mtype_unchecked(parent_aclassdef, n_type, true)
+               var mclassdef = npropdef.mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
+               toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_type, true)
                super
        end
 end
@@ -411,14 +422,15 @@ redef class AAsNotNullableExternCall
        redef fun from_mtype do return n_type.mtype.as_nullable
        redef fun to_mtype do
                var mtype = n_type.mtype.as(not null)
-               if mtype isa MNullableType then return mtype.mtype
+               mtype = mtype.as_notnullable
                return mtype
        end
 
        redef fun verify_and_collect(npropdef, callback_set, toolcontext)
        do
-               var parent_aclassdef = npropdef.parent.as(AClassdef)
-               toolcontext.modelbuilder.resolve_mtype_unchecked(parent_aclassdef, n_type, true)
+               var mclassdef = npropdef.mpropdef.mclassdef
+               var mmodule = mclassdef.mmodule
+               toolcontext.modelbuilder.resolve_mtype_unchecked(mmodule, mclassdef, n_type, true)
                super
        end
 end