syntax: add PType::is_typed
[nit.git] / src / syntax / typing.nit
index 7730152..fad4baa 100644 (file)
@@ -151,6 +151,8 @@ end
 redef class PClassdef
        redef fun accept_typing(v)
        do
+               v.variable_ctx = new RootVariableContext(v, self)
+               v.base_variable_ctx = v.variable_ctx
                v.self_var = new ParamVariable("self".to_symbol, self)
                v.self_var.stype = local_class.get_type
                super
@@ -160,10 +162,13 @@ end
 redef class AAttrPropdef
        redef fun accept_typing(v)
        do
+               var old_var_ctx = v.variable_ctx
+               v.variable_ctx = old_var_ctx.sub(self)
                super
                if n_expr != null then
                        v.check_conform_expr(n_expr.as(not null), prop.signature.return_type.as(not null))
                end
+               v.variable_ctx = old_var_ctx
        end
 end
 
@@ -172,15 +177,16 @@ redef class AMethPropdef
        var _self_var: nullable ParamVariable
        redef fun accept_typing(v)
        do
-               v.variable_ctx = new RootVariableContext(v, self)
-               v.base_variable_ctx = v.variable_ctx
+               var old_var_ctx = v.variable_ctx
+               v.variable_ctx = old_var_ctx.sub(self)
                _self_var = v.self_var
                super
+               v.variable_ctx = old_var_ctx
        end
 end
 
 redef class AConcreteMethPropdef
-       redef fun accept_typing(v)
+       redef fun after_typing(v)
        do
                super
                if v.variable_ctx.unreash == false and method.signature.return_type != null then
@@ -283,6 +289,7 @@ end
 
 redef class PType
        fun stype: MMType do return _stype.as(not null)
+       fun is_typed: Bool do return _stype != null
        var _stype: nullable MMType
 
        redef fun after_typing(v)
@@ -336,6 +343,7 @@ redef class AVardeclExpr
                if n_expr != null then v.variable_ctx.mark_is_set(va)
 
                if n_type != null then
+                       if not n_type.is_typed then return
                        va.stype = n_type.stype
                        if n_expr != null then
                                v.check_conform_expr(n_expr.as(not null), va.stype)
@@ -901,6 +909,7 @@ redef class ACrangeExpr
        redef fun after_typing(v)
        do
                super
+               if not is_typed then return
                _meth_init = stype.local_class.select_method(once "init".to_symbol)
        end
 end
@@ -908,6 +917,7 @@ redef class AOrangeExpr
        redef fun after_typing(v)
        do
                super
+               if not is_typed then return
                _meth_init = stype.local_class.select_method(once "without_last".to_symbol)
        end
 end
@@ -1240,7 +1250,7 @@ redef class ANewExpr
 special AAbsSendExpr
        redef fun after_typing(v)
        do
-               if n_type._stype == null then return
+               if not n_type.is_typed then return
                var t = n_type.stype
                if t.local_class.global.is_abstract then
                        v.error(self, "Error: try to instantiate abstract class {t.local_class}.")
@@ -1355,7 +1365,7 @@ redef class AEqExpr
        redef fun after_typing(v)
        do
                super
-               if not is_typed then return
+               if not n_expr.is_typed or not n_expr2.is_typed then return
                if n_expr.stype isa MMTypeNone and not n_expr2.stype.is_nullable or
                n_expr2.stype isa MMTypeNone and not n_expr.stype.is_nullable then
                        v.warning(self, "Warning: comparaison between null and a non nullable value.")
@@ -1381,7 +1391,7 @@ redef class ANeExpr
        redef fun after_typing(v)
        do
                super
-               if not is_typed then return
+               if not n_expr.is_typed or not n_expr2.is_typed then return
                if n_expr.stype isa MMTypeNone and not n_expr2.stype.is_nullable or
                n_expr2.stype isa MMTypeNone and not n_expr.stype.is_nullable then
                        v.warning(self, "Warning: comparaison between null and a non nullable value.")
@@ -1625,6 +1635,7 @@ special PExpr
        private fun check_expr_cast(v: TypingVisitor, n_expr: PExpr, n_type: PType)
        do
                if not v.check_expr(n_expr) then return
+               if not n_type.is_typed then return
                var etype = n_expr.stype
                var ttype = n_type.stype
                if etype == ttype then
@@ -1652,6 +1663,7 @@ special ATypeCheckExpr
        redef fun after_typing(v)
        do
                check_expr_cast(v, n_expr, n_type)
+               if not n_type.is_typed then return
                var variable = n_expr.its_variable
                if variable != null then
                        _if_true_variable_ctx = v.variable_ctx.sub_with(self, variable, n_type.stype)
@@ -1666,6 +1678,7 @@ special ATypeCheckExpr
        redef fun after_typing(v)
        do
                check_expr_cast(v, n_expr, n_type)
+               if not n_type.is_typed then return
                _stype = n_type.stype
                _is_typed = _stype != null
        end