syntax: merge control_flow.nit in typing.nit
[nit.git] / src / syntax / typing.nit
index 2bf36f4..ee2eb0a 100644 (file)
@@ -69,6 +69,9 @@ special AbsSyntaxVisitor
                end
        end
 
+       # Number of nested once
+       readable writable attr _once_count: Int = 0
+
        init(tc, module) do super
 
        private meth get_default_constructor_for(n: PNode, c: MMLocalClass, prop: MMSrcMethod): MMMethod
@@ -123,7 +126,7 @@ end
 
 # Associate symbols to variable and variables to type
 # Can be nested
-private class VariableContext
+abstract class VariableContext
        # Look for the variable from its name
        # Return null if nothing found
        meth [](s: Symbol): Variable
@@ -139,8 +142,30 @@ private class VariableContext
        meth add(v: Variable)
        do
                _dico[v.name] = v
+               _all_variables.add(v)
+       end
+
+       meth mark_is_set(v: Variable)
+       do
+               _set_variables.add(v)
        end
 
+       meth check_is_set(n: PNode, v: Variable)
+       do
+               if v.must_be_set and not is_set(v) then
+                       _visitor.error(n, "Error: variable '{v}' is possibly unset.")
+                       var x = self
+                       while true do
+                               print "  {x.node.locate}: {x._set_variables.join(", ")} ; {x._dico.join(", ")}"
+                               var x0 = x
+                               if x0 isa SubVariableContext then
+                                       x = x0.prev
+                               else
+                                       break
+                               end
+                       end
+               end
+       end
 
        # The effective static type of a given variable
        # May be different from the declaration static type
@@ -152,25 +177,91 @@ private class VariableContext
        # Variables by name (in the current context only)
        attr _dico: Map[Symbol, Variable]
 
+       # All variables in all contextes
+       attr _all_variables: Set[Variable]
+
        # Build a new VariableContext
-       meth sub: SubVariableContext
+       meth sub(node: PNode): SubVariableContext
        do
-               return new SubVariableContext.with_prev(self)
+               return new SubVariableContext.with_prev(self, node)
        end
 
        # Build a nested VariableContext with new variable information
-       meth sub_with(v: Variable, t: MMType): SubVariableContext
+       meth sub_with(node: PNode, v: Variable, t: MMType): SubVariableContext
        do
-               return new CastVariableContext.with_prev(self, v, t)
+               return new CastVariableContext.with_prev(self, node, v, t)
        end
 
-       init
-       do 
+       # The visitor of the context (used to display error)
+       attr _visitor: AbsSyntaxVisitor
+
+       # The syntax node that introduced the context
+       readable attr _node: PNode
+
+       init(visitor: AbsSyntaxVisitor, node: PNode)
+       do
+               _visitor = visitor
+               _node = node
                _dico = new HashMap[Symbol, Variable]
        end
+
+       # Is a control flow break met? (return, break, continue)
+       readable writable attr _unreash: Bool = false
+
+       # Is a control flow already broken?
+       # Used to avoid repeating the same error message
+       readable writable attr _already_unreash: Bool = false
+
+       # Set of variable that are set (assigned)
+       readable attr _set_variables: HashSet[Variable] = new HashSet[Variable]
+
+       # Is a variable set?
+       meth is_set(v: Variable): Bool
+       do
+               return _set_variables.has(v)
+       end
+
+       # Merge back one flow context information
+       meth merge(ctx: VariableContext)
+       do
+               if ctx.unreash then
+                       unreash = true
+                       if ctx.already_unreash then already_unreash = true
+                       return
+               end
+               for v in _all_variables do
+                       if not is_set(v) and ctx.is_set(v) then
+                               mark_is_set(v)
+                       end
+               end
+       end
+
+       # Merge back two alternative flow context informations
+       meth merge2(ctx1, ctx2: VariableContext)
+       do
+               if ctx1.unreash then
+                       merge(ctx2)
+               else if ctx2.unreash then
+                       merge(ctx1)
+               end
+               for v in _all_variables do
+                       if not is_set(v) and ctx1.is_set(v) and ctx2.is_set(v) then
+                               mark_is_set(v)
+                       end
+               end
+       end
+end
+
+class RootVariableContext
+special VariableContext
+       init(visitor: AbsSyntaxVisitor, node: PNode)
+       do
+               super(visitor, node)
+               _all_variables = new HashSet[Variable]
+       end
 end
 
-private class SubVariableContext
+class SubVariableContext
 special VariableContext
        readable attr _prev: VariableContext
 
@@ -188,14 +279,20 @@ special VariableContext
                return prev.stype(v)
        end
 
-       init with_prev(p: VariableContext)
+       init with_prev(p: VariableContext, node: PNode)
        do
-               init
+               init(p._visitor, node)
                _prev = p
+               _all_variables = p._all_variables
+       end
+
+       redef meth is_set(v)
+       do
+               return _set_variables.has(v) or _prev.is_set(v)
        end
 end
 
-private class CastVariableContext
+class CastVariableContext
 special SubVariableContext
        attr _variable: Variable
        attr _var_type: MMType
@@ -208,14 +305,24 @@ special SubVariableContext
                return prev.stype(v)
        end
 
-       init with_prev(p: VariableContext, v: Variable, t: MMType)
+       init with_prev(p: VariableContext, node: PNode, v: Variable, t: MMType)
        do
-               super(p)
+               super(p, node)
                _variable = v
                _var_type =t
        end
 end
 
+redef class Variable
+       # Is the variable must be set before being used ?
+       meth must_be_set: Bool do return false
+end
+
+redef class VarVariable
+       redef meth must_be_set do return true
+end
+
+
 ###############################################################################
 
 redef class PNode
@@ -250,12 +357,22 @@ redef class AMethPropdef
        redef readable attr _self_var: ParamVariable
        redef meth accept_typing(v)
        do
-               v.variable_ctx = new VariableContext
+               v.variable_ctx = new RootVariableContext(v, self)
                _self_var = v.self_var
                super
        end
 end
 
+redef class AConcreteMethPropdef
+       redef meth accept_typing(v)
+       do
+               super
+               if v.variable_ctx.unreash == false and method.signature.return_type != null then
+                       v.error(self, "Control error: Reached end of function (a 'return' with a value was expected).")
+               end
+       end
+end
+
 redef class AConcreteInitPropdef
        readable attr _super_init_calls: Array[MMMethod] = new Array[MMMethod]
        readable attr _explicit_super_init_calls: Array[MMMethod] = new Array[MMMethod]
@@ -324,13 +441,24 @@ redef class AClosureDecl
                v.variable_ctx.add(variable)
 
                var old_var_ctx = v.variable_ctx
-               v.variable_ctx = v.variable_ctx.sub
+               v.variable_ctx = v.variable_ctx.sub(self)
 
                _escapable = new EscapableClosure(self, variable.closure, null)
                v.escapable_ctx.push(_escapable)
 
                super
 
+               if n_expr != null then
+                       if v.variable_ctx.unreash == false then
+                               if variable.closure.signature.return_type != null then
+                                       v.error(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
+                               else if variable.closure.is_break then
+                                       v.error(self, "Control error: Reached end of break block (an 'abort' was expected).")
+                               end
+                       end
+               end
+
+               old_var_ctx.merge(v.variable_ctx)
                v.variable_ctx = old_var_ctx
                v.escapable_ctx.pop
        end
@@ -345,8 +473,22 @@ redef class PType
 end
 
 redef class PExpr
-       redef readable attr _stype: MMType
-       
+       redef readable attr _is_typed: Bool = false
+       redef meth is_statement: Bool do return _stype == null
+       redef meth stype
+       do
+               if not is_typed then
+                       print "{locate}: not is_typed"
+                       abort
+               end
+               if is_statement then
+                       print "{locate}: is_statement"
+                       abort
+               end
+               return _stype
+       end
+       attr _stype: MMType
+
        # Is the expression the implicit receiver
        meth is_implicit_self: Bool do return false
 
@@ -366,6 +508,7 @@ redef class AVardeclExpr
                var va = new VarVariable(n_id.to_symbol, self)
                variable = va
                v.variable_ctx.add(va)
+               if n_expr != null then v.variable_ctx.mark_is_set(va)
 
                if n_type != null then
                        va.stype = n_type.stype
@@ -373,9 +516,10 @@ redef class AVardeclExpr
                                v.check_conform_expr(n_expr, va.stype)
                        end
                else
-                       v.check_expr(n_expr)
+                       if not v.check_expr(n_expr) then return
                        va.stype = n_expr.stype
                end
+               _is_typed = true
        end
 end
 
@@ -383,17 +527,26 @@ redef class ABlockExpr
        redef meth accept_typing(v)
        do
                var old_var_ctx = v.variable_ctx
-               v.variable_ctx = v.variable_ctx.sub
+               v.variable_ctx = v.variable_ctx.sub(self)
 
-               super
+               for e in n_expr do
+                       if v.variable_ctx.unreash and not v.variable_ctx.already_unreash then
+                               v.variable_ctx.already_unreash = true
+                               v.warning(e, "Warning: unreachable statement.")
+                       end
+                       v.visit(e)
+               end
 
+               old_var_ctx.merge(v.variable_ctx)
                v.variable_ctx = old_var_ctx
+               _is_typed = true
        end
 end
 
 redef class AReturnExpr
        redef meth after_typing(v)
        do
+               v.variable_ctx.unreash = true
                var t = v.local_property.signature.return_type
                if n_expr == null and t != null then
                        v.error(self, "Error: Return without value in a function.")
@@ -402,12 +555,14 @@ redef class AReturnExpr
                else if n_expr != null and t != null then
                        v.check_conform_expr(n_expr, t)
                end
+               _is_typed = true
        end
 end
 
 redef class AContinueExpr
        redef meth after_typing(v)
        do
+               v.variable_ctx.unreash = true
                var esc = compute_escapable_block(v.escapable_ctx)
                if esc == null then return
 
@@ -424,12 +579,14 @@ redef class AContinueExpr
                else if n_expr != null and t != null then
                        v.check_conform_expr(n_expr, t)
                end
+               _is_typed = true
        end
 end
 
 redef class ABreakExpr
        redef meth after_typing(v)
        do
+               v.variable_ctx.unreash = true
                var esc = compute_escapable_block(v.escapable_ctx)
                if esc == null then return
 
@@ -442,6 +599,14 @@ redef class ABreakExpr
                        # Typing check can only be done later
                        bl.add(n_expr)
                end
+               _is_typed = true
+       end
+end
+
+redef class AAbortExpr
+       redef meth after_typing(v)
+       do
+               v.variable_ctx.unreash = true
        end
 end
 
@@ -453,15 +618,24 @@ redef class AIfExpr
                v.check_conform_expr(n_expr, v.type_bool)
 
                v.use_if_true_variable_ctx(n_expr)
+               v.variable_ctx = v.variable_ctx.sub(n_then)
 
                v.visit(n_then)
-               # Restore variable ctx
-               v.variable_ctx = old_var_ctx
 
-               if n_else != null then
+               if n_else == null then
+                       # Restore variable ctx since the 'then' block is optional
+                       v.variable_ctx = old_var_ctx
+               else
+                       # Remember what appened in the 'then'
+                       var then_var_ctx = v.variable_ctx
+                       # Reset to process the 'else'
+                       v.variable_ctx = old_var_ctx.sub(n_else)
                        v.visit(n_else)
+                       # Merge then and else in the old control_flow
+                       old_var_ctx.merge2(then_var_ctx, v.variable_ctx)
                        v.variable_ctx = old_var_ctx
                end
+               _is_typed = true
        end
 end
 
@@ -473,11 +647,15 @@ redef class AWhileExpr
        do
                _escapable = new EscapableBlock(self)
                v.escapable_ctx.push(_escapable)
+               var old_var_ctx = v.variable_ctx
+               v.variable_ctx = v.variable_ctx.sub(self)
 
                super
 
                v.check_conform_expr(n_expr, v.type_bool)
+               v.variable_ctx = old_var_ctx
                v.escapable_ctx.pop
+               _is_typed = true
        end
 end
 
@@ -494,17 +672,16 @@ redef class AForExpr
                _escapable = new EscapableBlock(self)
                v.escapable_ctx.push(_escapable)
 
-               v.variable_ctx = v.variable_ctx.sub
+               var old_var_ctx = v.variable_ctx
+               v.variable_ctx = v.variable_ctx.sub(self)
                var va = new AutoVariable(n_id.to_symbol, self)
                variable = va
                v.variable_ctx.add(va)
 
                v.visit(n_expr)
 
+               if not v.check_conform_expr(n_expr, v.type_collection) then return
                var expr_type = n_expr.stype
-               if not v.check_conform_expr(n_expr, v.type_collection) then
-                       return
-               end
                _meth_iterator = expr_type.local_class.select_method(once ("iterator".to_symbol))
                if _meth_iterator == null then
                        v.error(self, "Error: Collection MUST have an iterate method")
@@ -533,11 +710,9 @@ redef class AForExpr
                if n_block != null then v.visit(n_block)
 
                # pop context
-               var varctx = v.variable_ctx 
-               assert varctx isa SubVariableContext
-               v.variable_ctx = varctx.prev
-
+               v.variable_ctx = old_var_ctx
                v.escapable_ctx.pop
+               _is_typed = true
        end
 end
 
@@ -546,6 +721,7 @@ redef class AAssertExpr
        do
                v.check_conform_expr(n_expr, v.type_bool)
                v.use_if_true_variable_ctx(n_expr)
+               _is_typed = true
        end
 end
 
@@ -554,15 +730,19 @@ redef class AVarExpr
 
        redef meth after_typing(v)
        do
+               v.variable_ctx.check_is_set(self, variable)
                _stype = v.variable_ctx.stype(variable)
+               _is_typed = _stype != null
        end
 end
 
 redef class AVarAssignExpr
        redef meth after_typing(v)
        do
+               v.variable_ctx.mark_is_set(variable)
                var t = v.variable_ctx.stype(variable)
                v.check_conform_expr(n_value, t)
+               _is_typed = true
        end
 end
 
@@ -583,8 +763,8 @@ redef class AReassignFormExpr
                prop.global.check_visibility(v, self, v.module, false)
                var psig = prop.signature_for(type_lvalue)
                _assign_method = prop
-               v.check_conform_expr(n_value, psig[0].not_for_self)
-               v.check_conform(self, psig.return_type.not_for_self, n_value.stype)
+               if not v.check_conform_expr(n_value, psig[0].not_for_self) then return
+               if not v.check_conform(self, psig.return_type.not_for_self, n_value.stype) then return
        end
 
        # Method used through the reassigment operator (once computed)
@@ -594,8 +774,11 @@ end
 redef class AVarReassignExpr
        redef meth after_typing(v)
        do
+               v.variable_ctx.check_is_set(self, variable)
+               v.variable_ctx.mark_is_set(variable)
                var t = v.variable_ctx.stype(variable)
                do_lvalue_typing(v, t)
+               _is_typed = true
        end
 end
 
@@ -616,6 +799,7 @@ redef class ASelfExpr
        do
                variable = v.self_var
                _stype = v.variable_ctx.stype(variable)
+               _is_typed = true
        end
 
         redef meth is_self do return true
@@ -639,6 +823,7 @@ redef class AIfexprExpr
                v.check_conform_expr(n_expr, v.type_bool)
 
                _stype = v.check_conform_multiexpr(null, [n_then, n_else])
+               _is_typed = _stype != null
        end
 end
 
@@ -646,6 +831,7 @@ redef class ABoolExpr
        redef meth after_typing(v)
        do
                _stype = v.type_bool
+               _is_typed = true
        end
 end
 
@@ -655,6 +841,7 @@ redef class AOrExpr
                v.check_conform_expr(n_expr, v.type_bool)
                v.check_conform_expr(n_expr2, v.type_bool)
                _stype = v.type_bool
+               _is_typed = true
        end
 end
 
@@ -678,6 +865,7 @@ redef class AAndExpr
                v.check_conform_expr(n_expr, v.type_bool)
                v.check_conform_expr(n_expr2, v.type_bool)
                _stype = v.type_bool
+               _is_typed = true
        end
 end
 
@@ -686,6 +874,7 @@ redef class ANotExpr
        do
                v.check_conform_expr(n_expr, v.type_bool)
                _stype = v.type_bool
+               _is_typed = true
        end
 end
 
@@ -693,7 +882,7 @@ redef class AIntExpr
        redef meth after_typing(v)
        do
                _stype = v.type_int
-
+               _is_typed = true
        end
 end
 
@@ -701,6 +890,7 @@ redef class AFloatExpr
        redef meth after_typing(v)
        do
                _stype = v.type_float
+               _is_typed = true
        end
 end
 
@@ -708,6 +898,7 @@ redef class ACharExpr
        redef meth after_typing(v)
        do
                _stype = v.type_char
+               _is_typed = true
        end
 end
 
@@ -716,6 +907,7 @@ redef class AStringFormExpr
        redef meth after_typing(v)
        do
                _stype = v.type_string
+               _is_typed = true
                _meth_with_native = _stype.local_class.select_method(once "with_native".to_symbol)
                if _meth_with_native == null then v.error(self, "{_stype} MUST have a with_native method.")
        end
@@ -736,6 +928,7 @@ redef class ASuperstringExpr
                if _meth_add == null then v.error(self, "{_atype} MUST have an add method.")
                _meth_to_s = v.type_object.local_class.select_method(once "to_s".to_symbol)
                if _meth_to_s == null then v.error(self, "Object MUST have a to_s method.")
+               _is_typed = true
        end
 end
 
@@ -743,6 +936,7 @@ redef class ANullExpr
        redef meth after_typing(v)
        do
                _stype = v.type_none
+               _is_typed = true
        end
 end
 
@@ -765,6 +959,8 @@ redef class AArrayExpr
                if _meth_with_capacity == null then v.error(self, "{_stype} MUST have a with_capacity method.")
                _meth_add = _stype.local_class.select_method(once "add".to_symbol)
                if _meth_add == null then v.error(self, "{_stype} MUST have an add method.")
+
+               _is_typed = true
        end
 end
 
@@ -772,11 +968,9 @@ redef class ARangeExpr
        readable attr _meth_init: MMMethod
        redef meth after_typing(v)
        do
+               if not v.check_expr(n_expr) or not v.check_expr(n_expr2) then return
                var ntype = n_expr.stype
                var ntype2 = n_expr2.stype
-               if ntype == null or ntype == null then
-                       return
-               end
                if ntype < ntype2 then
                        ntype = ntype2
                else if not ntype2 < ntype then
@@ -784,9 +978,9 @@ redef class ARangeExpr
                        return
                end
                var dtype = v.type_discrete
-               v.check_conform_expr(n_expr, dtype)
-               v.check_conform_expr(n_expr2, dtype)
+               if not v.check_conform_expr(n_expr, dtype) or not v.check_conform_expr(n_expr2, dtype) then return
                _stype = v.type_range(ntype)
+               _is_typed = true
        end
 end
 
@@ -863,6 +1057,7 @@ special ASuperInitCall
                var p = v.local_property
                assert p isa MMSrcMethod
                _prop = p
+               _is_typed = true
        end
 end
 
@@ -899,10 +1094,9 @@ redef class AAttrExpr
        redef meth after_typing(v)
        do
                do_typing(v)
-               if prop == null then
-                       return
-               end
+               if prop == null then return
                _stype = attr_type
+               _is_typed = true
        end
 end
 
@@ -910,10 +1104,9 @@ redef class AAttrAssignExpr
        redef meth after_typing(v)
        do
                do_typing(v)
-               if prop == null then
-                       return
-               end
-               v.check_conform_expr(n_value, attr_type)
+               if prop == null then return
+               if not v.check_conform_expr(n_value, attr_type) then return
+               _is_typed = true
        end
 end
 
@@ -921,10 +1114,9 @@ redef class AAttrReassignExpr
        redef meth after_typing(v)
        do
                do_typing(v)
-               if prop == null then
-                       return
-               end
+               if prop == null then return
                do_lvalue_typing(v, attr_type)
+               _is_typed = true
        end
 end
 
@@ -1026,6 +1218,7 @@ special AAbsAbsSendExpr
                var args = process_signature(v, sig, prop.name, raw_args)
                if args == null then return
                var rtype = process_closures(v, sig, prop.name, closure_defs)
+               if rtype == null and sig.return_type != null then return
                _prop = prop
                _prop_signature = sig
                _arguments = args
@@ -1138,8 +1331,10 @@ special AAbsSendExpr
 
                if not prop.global.is_init then
                        v.error(self, "Error: {prop} is not a constructor.")
+                       return
                end
                _stype = t
+               _is_typed = true
        end
 end
 
@@ -1177,6 +1372,7 @@ special ASuperInitCall
                end
 
                _stype = return_type
+               _is_typed = true
        end
 end
 
@@ -1217,6 +1413,7 @@ special AReassignFormExpr
                end
 
                _arguments = old_args # FIXME: What if star parameters do not match betwen the two methods?
+               _is_typed = true
        end
 end
 
@@ -1281,6 +1478,7 @@ redef class ACallFormExpr
                                else
                                        if not n_args.is_empty then
                                                v.error(self, "Error: {name} is variable, not a function.")
+                                               return
                                        end
                                        var vform = variable_create(variable)
                                        vform.variable = variable
@@ -1372,6 +1570,7 @@ special AAbsAbsSendExpr
        redef meth after_typing(v)
        do
                var va = variable
+               if va.closure.is_break then v.variable_ctx.unreash = true
                var sig = va.closure.signature
                var args = process_signature(v, sig, n_id.to_symbol, n_args.to_a)
                if not n_closure_defs.is_empty then
@@ -1381,6 +1580,7 @@ special AAbsAbsSendExpr
                _prop_signature = sig
                _arguments = args
                _stype = sig.return_type
+               _is_typed = true
        end
 end
 
@@ -1411,7 +1611,8 @@ redef class AClosureDef
 
                closure = esc.closure
 
-               v.variable_ctx = v.variable_ctx.sub
+               var old_var_ctx = v.variable_ctx
+               v.variable_ctx = v.variable_ctx.sub(self)
                variables = new Array[AutoVariable]
                for i in [0..n_id.length[ do
                        var va = new AutoVariable(n_id[i].to_symbol, self)
@@ -1422,31 +1623,78 @@ redef class AClosureDef
 
                _accept_typing2 = true
                accept_typing(v)
+
+               if v.variable_ctx.unreash == false then
+                       if closure.signature.return_type != null then
+                               v.error(self, "Control error: Reached end of block (a 'continue' with a value was expected).")
+                       else if closure.is_break then
+                               v.error(self, "Control error: Reached end of break block (a 'break' was expected).")
+                       end
+               end
+               v.variable_ctx = old_var_ctx
+       end
+end
+
+class ATypeCheckExpr
+special PExpr
+       private meth check_expr_cast(v: TypingVisitor, n_expr: PExpr, n_type: PType)
+       do
+               if not v.check_expr(n_expr) then return
+               var etype = n_expr.stype
+               var ttype = n_type.stype
+               if etype == ttype then
+                       v.warning(self, "Warning: Expression is already a {ttype}.")
+               else if etype < ttype then
+                       v.warning(self, "Warning: Expression is already a {ttype} since it is a {etype}.")
+               end
        end
 end
 
 redef class AIsaExpr
+special ATypeCheckExpr
        redef meth after_typing(v)
        do
+               check_expr_cast(v, n_expr, n_type)
                var variable = n_expr.its_variable
                if variable != null then
-                       _if_true_variable_ctx = v.variable_ctx.sub_with(variable, n_type.stype)
+                       _if_true_variable_ctx = v.variable_ctx.sub_with(self, variable, n_type.stype)
                end
                _stype = v.type_bool
+               _is_typed = true
        end
 end
 
 redef class AAsCastExpr
+special ATypeCheckExpr
        redef meth after_typing(v)
        do
-               v.check_expr(n_expr)
+               check_expr_cast(v, n_expr, n_type)
                _stype = n_type.stype
+               _is_typed = _stype != null
        end
 end
 
 redef class AProxyExpr
        redef meth after_typing(v)
        do
+               if not n_expr.is_typed then return
+               _is_typed = true
+               if n_expr.is_statement then return
                _stype = n_expr.stype
        end
 end
+
+redef class AOnceExpr
+       redef meth accept_typing(v)
+       do
+               if v.once_count > 0 then
+                       v.warning(self, "Useless once in a once expression.")
+               end
+               v.once_count = v.once_count + 1
+
+               super
+
+               v.once_count = v.once_count - 1
+       end
+end
+