syntax: enable a generalized for with iterate
[nit.git] / src / syntax / typing.nit
index c1d6399..96c4840 100644 (file)
@@ -37,7 +37,7 @@ end
 # * Resolve call and attribute access
 # * Check type conformance
 private class TypingVisitor
-special AbsSyntaxVisitor
+       super AbsSyntaxVisitor
        redef fun visit(n)
        do
                if n != null then n.accept_typing(self)
@@ -109,7 +109,7 @@ special AbsSyntaxVisitor
        # Number of nested once
        readable writable var _once_count: Int = 0
 
-       init(tc, module) do super
+       init(tc, mod) do super
 
        private fun get_default_constructor_for(n: ANode, c: MMLocalClass, prop: MMSrcMethod): nullable MMMethod
        do
@@ -247,12 +247,12 @@ redef class AConcreteInitPropdef
                        var cur_c: nullable MMLocalClass = null
                        if i < l then
                                cur_m = explicit_super_init_calls[i]
-                               cur_c = cur_m.global.intro.local_class.for_module(v.module)
+                               cur_c = cur_m.global.intro.local_class.for_module(v.mmmodule)
                        end
                        var j = 0
                        while j < v.local_class.cshe.direct_greaters.length do
                                var c = v.local_class.cshe.direct_greaters[j]
-                               if c.global.is_interface or c.global.is_universal or c.global.is_mixin then
+                               if c.global.is_interface or c.global.is_enum or c.global.is_mixin then
                                        j += 1
                                else if cur_c != null and (c.cshe <= cur_c or cur_c.global.is_mixin) then
                                        if c == cur_c then j += 1
@@ -260,7 +260,7 @@ redef class AConcreteInitPropdef
                                        i += 1
                                        if i < l then
                                                cur_m = explicit_super_init_calls[i]
-                                               cur_c = cur_m.global.intro.local_class.for_module(v.module)
+                                               cur_c = cur_m.global.intro.local_class.for_module(v.mmmodule)
                                        else
                                                cur_m = null
                                                cur_c = null
@@ -503,7 +503,7 @@ end
 
 # An abstract control structure with feature escapable block
 class AAbsControl
-special AExpr
+       super AExpr
        # The corresponding escapable block
        readable var _escapable: nullable EscapableBlock
 
@@ -545,7 +545,7 @@ special AExpr
 end
 
 redef class ADoExpr
-special AAbsControl
+       super AAbsControl
        redef fun accept_typing(v)
        do
                process_control(v, new BreakOnlyEscapableBlock(self), n_label, false)
@@ -587,7 +587,7 @@ redef class AIfExpr
 end
 
 redef class AWhileExpr
-special AAbsControl
+       super AAbsControl
        redef fun accept_typing(v)
        do
                process_control(v, new EscapableBlock(self), n_label, true)
@@ -619,7 +619,7 @@ special AAbsControl
 end
 
 redef class ALoopExpr
-special AAbsControl
+       super AAbsControl
        redef fun accept_typing(v)
        do
                process_control(v, new EscapableBlock(self), n_label, true)
@@ -636,9 +636,9 @@ special AAbsControl
 end
 
 redef class AForExpr
-special AAbsControl
-       var _variable: nullable AutoVariable
-       redef fun variable do return _variable.as(not null)
+       super AAbsControl
+       var _variables: nullable Array[AutoVariable]
+       redef fun variables do return _variables.as(not null)
 
        redef fun accept_typing(v)
        do
@@ -650,24 +650,7 @@ special AAbsControl
                v.scope_ctx.push(self)
                var old_flow_ctx = v.flow_ctx
 
-               # Create the automatic variable
-               var va = new AutoVariable(n_id.to_symbol, n_id)
-               _variable = va
-               v.scope_ctx.add_variable(va)
-
-               # Process collection
-               v.enter_visit(n_expr)
-
-               if not v.check_conform_expr(n_expr, v.type_collection) then return
-               var expr_type = n_expr.stype
-
-               # Get iterator
-               var meth_iterator = v.get_method(expr_type, once "iterator".to_symbol)
-               var iter_type = meth_iterator.signature_for(expr_type).return_type.as(not null)
-               var meth_item = v.get_method(iter_type, once ("item".to_symbol))
-               var va_stype = meth_item.signature_for(iter_type).return_type.as(not null)
-               if not n_expr.is_self then va_stype = va_stype.not_for_self
-               va.stype = va_stype
+               do_typing(v)
 
                # Process inside
                v.enter_visit_block(n_block)
@@ -676,6 +659,64 @@ special AAbsControl
                v.flow_ctx = old_flow_ctx
                v.scope_ctx.pop
        end
+
+       private fun do_typing(v: TypingVisitor)
+       do
+               # Create the automatic variables
+               var vas = new Array[AutoVariable]
+               for n_id in n_ids do
+                       var va = new AutoVariable(n_id.to_symbol, n_id)
+                       v.scope_ctx.add_variable(va)
+                       vas.add(va)
+               end
+               _variables = vas
+
+               # Process reciever
+               v.enter_visit(n_expr)
+               if not v.check_expr(n_expr) then return
+               var expr_type = n_expr.stype
+
+               if expr_type.is_nullable then
+                       v.error(n_expr, "Type error: 'for' on a nullable expression.")
+                       return
+               end
+
+               # Get iterate
+               var iterate_name = once "iterate".to_symbol
+               if not expr_type.local_class.has_global_property_by_name(iterate_name) then
+                       v.error(n_expr, "Type error: Expected a type with an 'iterate' method. Found {expr_type}.")
+                       return
+               end
+               var prop = expr_type.local_class.select_method(iterate_name)
+               prop.global.check_visibility(v, self, v.mmmodule, n_expr.is_self)
+               var psig = prop.signature_for(expr_type)
+               if not n_expr.is_self then psig = psig.not_for_self
+               if psig.arity != 0 then
+                       v.error(self, "Error: 'iterate' incompatible with 'for': require no arguments.")
+                       return
+               else if psig.closures.length != 1 then
+                       v.error(self, "Error: 'iterate' incompatible with 'for': require one closure.")
+                       return
+               end
+               psig = psig.closures.first.signature
+               if psig.return_type != null then
+                       v.error(self, "Error: 'iterate' incompatible with 'for': require one procedural closure.")
+                       return
+               end
+               if vas.length != psig.arity then
+                       if psig.arity == 1 then
+                               v.error(self, "Error: Expected {psig.arity} variable {psig}, found {vas.length}.")
+                       else
+                               v.error(self, "Error: Expected {psig.arity} variables {psig}, found {vas.length}.")
+                       end
+                       return
+               end
+
+               # Type the automatic variables
+               for i in [0..vas.length[ do
+                       vas[i].stype = psig[i]
+               end
+       end
 end
 
 redef class AAssertExpr
@@ -752,7 +793,7 @@ redef class AReassignFormExpr
                        return null
                end
                var prop = lc.select_method(name)
-               prop.global.check_visibility(v, self, v.module, false)
+               prop.global.check_visibility(v, self, v.mmmodule, false)
                var psig = prop.signature_for(type_lvalue)
                _assign_method = prop
                if not v.check_conform_expr(n_value, psig[0].not_for_self) then return null
@@ -1100,7 +1141,7 @@ redef class ASuperExpr
                        var stype: nullable MMType = null
                        for prop in precs do
                                assert prop isa MMMethod
-                               var t = prop.signature_for(v.self_var.stype.as(not null)).return_type.for_module(v.module).adapt_to(v.local_property.signature.recv)
+                               var t = prop.signature_for(v.self_var.stype.as(not null)).return_type.for_module(v.mmmodule).adapt_to(v.local_property.signature.recv)
                                stypes.add(t)
                                if stype == null or stype < t then
                                        stype = t
@@ -1141,8 +1182,8 @@ redef class AAttrFormExpr
                        return
                end
                var prop = lc.select_attribute(name)
-               if v.module.visibility_for(prop.global.local_class.module) < 3 then
-                       v.error(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
+               if v.mmmodule.visibility_for(prop.global.local_class.mmmodule) < 3 then
+                       v.error(self, "Error: Attribute {name} from {prop.global.local_class.mmmodule} is invisible in {v.mmmodule}")
                end
                _prop = prop
                var at = prop.signature_for(type_recv).return_type 
@@ -1365,7 +1406,7 @@ redef class AAbsSendExpr
        # Get the signature for a local property and a receiver
        private fun get_signature(v: TypingVisitor, type_recv: MMType, prop: MMMethod, recv_is_self: Bool): MMSignature
        do
-               prop.global.check_visibility(v, self, v.module, recv_is_self)
+               prop.global.check_visibility(v, self, v.mmmodule, recv_is_self)
                var psig = prop.signature_for(type_recv)
                if not recv_is_self then psig = psig.not_for_self
                return psig
@@ -1387,7 +1428,7 @@ redef class ASuperInitCall
                if parent != v.top_block and self != v.top_block then
                        v.error(self, "Error: Constructor invocation {property} must not be in nested block.")
                end
-               var cla = v.module[property.global.intro.local_class.global]
+               var cla = v.mmmodule[property.global.intro.local_class.global]
                var prev_class: nullable MMLocalClass = null
                var esic = v.explicit_super_init_calls.as(not null)
                if not esic.is_empty then
@@ -1822,7 +1863,7 @@ redef class AClosureDef
 end
 
 class ATypeCheckExpr
-special AExpr
+       super AExpr
        private fun check_expr_cast(v: TypingVisitor, n_expr: AExpr, n_type: AType)
        do
                if not v.check_expr(n_expr) then return
@@ -1853,7 +1894,7 @@ special AExpr
 end
 
 redef class AIsaExpr
-special ATypeCheckExpr
+       super ATypeCheckExpr
        redef fun after_typing(v)
        do
                check_expr_cast(v, n_expr, n_type)
@@ -1868,7 +1909,7 @@ special ATypeCheckExpr
 end
 
 redef class AAsCastExpr
-special ATypeCheckExpr
+       super ATypeCheckExpr
        redef fun after_typing(v)
        do
                check_expr_cast(v, n_expr, n_type)