syntax: generalize and publicize raw_arguments concept
[nit.git] / src / syntax / typing.nit
index e89d9e2..ed0e044 100644 (file)
@@ -59,7 +59,7 @@ special AbsSyntaxVisitor
        writable var _self_var: nullable ParamVariable
 
        # Block of the current method
-       readable writable var _top_block: nullable PExpr
+       readable writable var _top_block: nullable AExpr
 
        # List of explicit invocation of constructors of super-classes
        readable writable var _explicit_super_init_calls: nullable Array[MMMethod]
@@ -68,14 +68,14 @@ special AbsSyntaxVisitor
        readable writable var _explicit_other_init_call: Bool = false
 
        # Make the if_true_variable_ctx of the expression effective
-       private fun use_if_true_variable_ctx(e: PExpr)
+       private fun use_if_true_variable_ctx(e: AExpr)
        do
                var ctx = e.if_true_variable_ctx
                if ctx != null then variable_ctx = ctx
        end
 
        # Make the if_false_variable_ctx of the expression effective
-       private fun use_if_false_variable_ctx(e: PExpr)
+       private fun use_if_false_variable_ctx(e: AExpr)
        do
                var ctx = e.if_false_variable_ctx
                if ctx != null then variable_ctx = ctx
@@ -86,7 +86,7 @@ special AbsSyntaxVisitor
 
        init(tc, module) do super
 
-       private fun get_default_constructor_for(n: PNode, c: MMLocalClass, prop: MMSrcMethod): nullable MMMethod
+       private fun get_default_constructor_for(n: ANode, c: MMLocalClass, prop: MMSrcMethod): nullable MMMethod
        do
                var v = self
                #var prop = v.local_property
@@ -139,7 +139,7 @@ end
 
 ###############################################################################
 
-redef class PNode
+redef class ANode
        private fun accept_typing(v: TypingVisitor) 
        do
                accept_abs_syntax_visitor(v)
@@ -148,7 +148,7 @@ redef class PNode
        private fun after_typing(v: TypingVisitor) do end
 end
 
-redef class PClassdef
+redef class AClassdef
        redef fun accept_typing(v)
        do
                v.variable_ctx = new RootVariableContext(v, self)
@@ -159,7 +159,7 @@ redef class PClassdef
        end
 end
 
-redef class PPropdef
+redef class APropdef
        redef fun self_var do return _self_var.as(not null)
        var _self_var: nullable ParamVariable
 end
@@ -250,7 +250,7 @@ redef class AConcreteInitPropdef
        end
 end
 
-redef class PParam
+redef class AParam
        redef fun after_typing(v)
        do
                v.variable_ctx.add(variable)
@@ -273,7 +273,7 @@ redef class AClosureDecl
 
                var escapable = new EscapableClosure(self, variable.closure, null)
                _escapable = escapable
-               v.escapable_ctx.push(escapable)
+               v.escapable_ctx.push(escapable, null)
 
                super
 
@@ -294,7 +294,7 @@ redef class AClosureDecl
        end
 end
 
-redef class PType
+redef class AType
        redef fun stype: MMType do return _stype.as(not null)
        redef fun is_typed: Bool do return _stype != null
        var _stype: nullable MMType
@@ -305,7 +305,7 @@ redef class PType
        end
 end
 
-redef class PExpr
+redef class AExpr
        redef readable var _is_typed: Bool = false
        redef fun is_statement: Bool do return _stype == null
        redef fun stype
@@ -414,7 +414,7 @@ redef class AContinueExpr
                if esc == null then return
 
                if esc.is_break_block then
-                       v.error(self, "Error: 'continue' forbiden in break blocks.")
+                       v.error(self, "Error: cannot 'continue', only 'break'.")
                        return
                end
 
@@ -458,6 +458,23 @@ redef class AAbortExpr
        end
 end
 
+redef class ADoExpr
+       # The corresponding escapable block
+       readable var _escapable: nullable EscapableBlock
+
+       redef fun accept_typing(v)
+       do
+               var escapable = new BreakOnlyEscapableBlock(self)
+               _escapable = escapable
+               v.escapable_ctx.push(escapable, n_label)
+
+               super
+
+               v.escapable_ctx.pop
+               _is_typed = true
+       end
+end
+
 redef class AIfExpr
        redef fun accept_typing(v)
        do
@@ -502,7 +519,7 @@ redef class AWhileExpr
        do
                var escapable = new EscapableBlock(self)
                _escapable = escapable
-               v.escapable_ctx.push(escapable)
+               v.escapable_ctx.push(escapable, n_label)
                var old_var_ctx = v.variable_ctx
                var old_base_var_ctx = v.base_variable_ctx
                v.base_variable_ctx = v.variable_ctx
@@ -539,7 +556,7 @@ redef class AForExpr
        do
                var escapable = new EscapableBlock(self)
                _escapable = escapable
-               v.escapable_ctx.push(escapable)
+               v.escapable_ctx.push(escapable, n_label)
 
                var old_var_ctx = v.variable_ctx
                var old_base_var_ctx = v.base_variable_ctx
@@ -663,7 +680,7 @@ redef class AVarReassignExpr
        end
 end
 
-redef class PAssignOp
+redef class AAssignOp
        fun method_name: Symbol is abstract
 end
 redef class APlusAssignOp
@@ -892,6 +909,7 @@ end
 
 redef class ASuperExpr
        redef readable var _init_in_superclass: nullable MMMethod
+       redef fun compute_raw_arguments do return n_args.to_a
        redef fun after_typing(v)
        do
                var precs: Array[MMLocalProperty] = v.local_property.prhe.direct_greaters
@@ -919,7 +937,7 @@ redef class ASuperExpr
                        register_super_init_call(v, p)
                        if n_args.length > 0 then
                                var signature = get_signature(v, v.self_var.stype.as(not null), p, true)
-                               _arguments = process_signature(v, signature, p.name, n_args.to_a)
+                               process_signature(v, signature, p.name, compute_raw_arguments)
                        end
                else
                        v.error(self, "Error: No super method to call for {v.local_property}.")
@@ -1028,12 +1046,33 @@ redef class AAbsAbsSendExpr
        redef fun prop_signature do return _prop_signature.as(not null)
        var _prop_signature: nullable MMSignature
 
+       # Raw arguments used (without vararg transformation)
+       redef fun raw_arguments: Array[AExpr]
+       do
+               var res = _raw_arguments_cache
+               if res != null then
+                       return res
+               else
+                       res = compute_raw_arguments
+                       if res == null then res = new Array[AExpr]
+                       _raw_arguments_cache = res
+                       return res
+               end
+       end
+
+       var _raw_arguments_cache: nullable Array[AExpr] = null
+
+       fun compute_raw_arguments: nullable Array[AExpr]
+       do
+               print "{location} no compute_raw_arguments"
+               return null
+       end
        # The real arguments used (after star transformation) (once computed)
        redef fun arguments do return _arguments.as(not null)
-       var _arguments: nullable Array[PExpr]
+       var _arguments: nullable Array[AExpr]
 
        # Check the conformity of a set of arguments `raw_args' to a signature.
-       private fun process_signature(v: TypingVisitor, psig: MMSignature, name: Symbol, raw_args: nullable Array[PExpr]): nullable Array[PExpr]
+       private fun process_signature(v: TypingVisitor, psig: MMSignature, name: Symbol, raw_args: nullable Array[AExpr]): nullable Array[AExpr]
        do
                var par_vararg = psig.vararg_rank
                var par_arity = psig.arity
@@ -1044,12 +1083,12 @@ redef class AAbsAbsSendExpr
                        return null
                end
                var arg_idx = 0
-               var args = new Array[PExpr]
+               var args = new Array[AExpr]
                for par_idx in [0..par_arity[ do
-                       var a: PExpr
+                       var a: AExpr
                        var par_type = psig[par_idx]
                        if par_idx == par_vararg then
-                               var star = new Array[PExpr]
+                               var star = new Array[AExpr]
                                for i in [0..(raw_arity-par_arity)] do
                                        a = raw_args[arg_idx]
                                        v.check_conform_expr(a, par_type)
@@ -1070,7 +1109,7 @@ redef class AAbsAbsSendExpr
        end
 
        # Check the conformity of a set of defined closures
-       private fun process_closures(v: TypingVisitor, psig: MMSignature, name: Symbol, cd: nullable Array[PClosureDef]): nullable MMType
+       private fun process_closures(v: TypingVisitor, psig: MMSignature, name: Symbol, cd: nullable Array[AClosureDef]): nullable MMType
        do
                var t = psig.return_type
                var cs = psig.closures # Declared closures
@@ -1090,12 +1129,15 @@ redef class AAbsAbsSendExpr
                                var break_list: nullable Array[ABreakExpr] = null
                                if t != null then break_list = new Array[ABreakExpr]
 
+                               # The n_label, is any in only set on the last decl
+                               var n_label = if arity > 0 then cd[arity-1].n_label else null
+
                                # Process each closure definition
                                for i in [0..arity[ do
                                        var csi = cs[i]
                                        var cdi = cd[i]
                                        var esc = new EscapableClosure(cdi, csi, break_list)
-                                       v.escapable_ctx.push(esc)
+                                       v.escapable_ctx.push(esc, n_label)
                                        cdi.accept_typing2(v, esc)
                                        v.escapable_ctx.pop
                                end
@@ -1114,7 +1156,7 @@ end
 
 redef class AAbsSendExpr
        # Compute the called global property
-       private fun do_typing(v: TypingVisitor, type_recv: MMType, is_implicit_self: Bool, recv_is_self: Bool, name: Symbol, raw_args: nullable Array[PExpr], closure_defs: nullable Array[PClosureDef])
+       private fun do_typing(v: TypingVisitor, type_recv: MMType, is_implicit_self: Bool, recv_is_self: Bool, name: Symbol, raw_args: nullable Array[AExpr], closure_defs: nullable Array[AClosureDef])
        do
                var prop = get_property(v, type_recv, is_implicit_self, name)
                if prop == null then return
@@ -1214,6 +1256,7 @@ redef class ASuperInitCall
 end
 
 redef class ANewExpr
+       redef fun compute_raw_arguments do return n_args.to_a
        redef fun after_typing(v)
        do
                if not n_type.is_typed then return
@@ -1229,7 +1272,7 @@ redef class ANewExpr
                        name = n_id.to_symbol
                end
 
-               do_typing(v, t, false, false, name, n_args.to_a, null)
+               do_typing(v, t, false, false, name, raw_arguments, null)
                if _prop == null then return
 
                if not prop.global.is_init then
@@ -1246,11 +1289,8 @@ redef class ASendExpr
        # Name of the invoked property
        fun name: Symbol is abstract 
 
-       # Raw arguments used (withour star transformation)
-       fun raw_arguments: nullable Array[PExpr] is abstract
-
        # Closure definitions
-       redef fun closure_defs: nullable Array[PClosureDef] do return null
+       redef fun closure_defs: nullable Array[AClosureDef] do return null
 
        redef fun after_typing(v)
        do
@@ -1304,6 +1344,7 @@ redef class ASendReassignExpr
                v.check_conform(self, t2, n_value.stype)
 
                _read_prop = prop
+               raw_args = raw_args.to_a
                var old_args = arguments
                raw_args.add(n_value)
 
@@ -1322,7 +1363,7 @@ redef class ASendReassignExpr
 end
 
 redef class ABinopExpr
-       redef fun raw_arguments do return [n_expr2]
+       redef fun compute_raw_arguments do return [n_expr2]
 end
 redef class AEqExpr
        redef fun name do return once "==".to_symbol
@@ -1342,7 +1383,7 @@ redef class AEqExpr
                end
        end
 
-       private fun try_to_isa(v: TypingVisitor, n: PExpr)
+       private fun try_to_isa(v: TypingVisitor, n: AExpr)
        do
                var variable = n.its_variable
                if variable != null then
@@ -1368,7 +1409,7 @@ redef class ANeExpr
                end
        end
 
-       private fun try_to_isa(v: TypingVisitor, n: PExpr)
+       private fun try_to_isa(v: TypingVisitor, n: AExpr)
        do
                var variable = n.its_variable
                if variable != null then
@@ -1409,7 +1450,7 @@ end
 
 redef class AUminusExpr
        redef fun name do return once "unary -".to_symbol
-       redef fun raw_arguments do return null
+       redef fun compute_raw_arguments do return null
 end
 
 redef class ACallFormExpr
@@ -1419,7 +1460,7 @@ redef class ACallFormExpr
                        var name = n_id.to_symbol
                        var variable = v.variable_ctx[name]
                        if variable != null then
-                               var n: PExpr
+                               var n: AExpr
                                if variable isa ClosureVariable then
                                        n = new AClosureCallExpr.init_aclosurecallexpr(n_id, n_args, n_closure_defs)
                                        n._variable = variable
@@ -1460,7 +1501,7 @@ redef class ACallExpr
        end
 
        redef fun name do return n_id.to_symbol
-       redef fun raw_arguments do return n_args.to_a
+       redef fun compute_raw_arguments do return n_args.to_a
 end
 
 redef class ACallAssignExpr
@@ -1470,7 +1511,7 @@ redef class ACallAssignExpr
        end
 
        redef fun name do return (n_id.text + "=").to_symbol
-       redef fun raw_arguments do
+       redef fun compute_raw_arguments do
                var res = n_args.to_a
                res.add(n_value)
                return res
@@ -1484,17 +1525,17 @@ redef class ACallReassignExpr
        end
 
        redef fun name do return n_id.to_symbol
-       redef fun raw_arguments do return n_args.to_a
+       redef fun compute_raw_arguments do return n_args.to_a
 end
 
 redef class ABraExpr
        redef fun name do return once "[]".to_symbol
-       redef fun raw_arguments do return n_args.to_a
+       redef fun compute_raw_arguments do return n_args.to_a
 end
 
 redef class ABraAssignExpr
        redef fun name do return once "[]=".to_symbol
-       redef fun raw_arguments do
+       redef fun compute_raw_arguments do
                var res = n_args.to_a
                res.add(n_value)
                return res
@@ -1503,17 +1544,18 @@ end
 
 redef class ABraReassignExpr
        redef fun name do return once "[]".to_symbol
-       redef fun raw_arguments do return n_args.to_a
+       redef fun compute_raw_arguments do return n_args.to_a
 end
 
 redef class AInitExpr
        redef fun name do return once "init".to_symbol
-       redef fun raw_arguments do return n_args.to_a
+       redef fun compute_raw_arguments do return n_args.to_a
 end
 
 redef class AClosureCallExpr
        var _variable: nullable ClosureVariable
        redef fun variable do return _variable.as(not null)
+       redef fun compute_raw_arguments do return n_args.to_a
 
        redef fun after_typing(v)
        do
@@ -1532,7 +1574,7 @@ redef class AClosureCallExpr
        end
 end
 
-redef class PClosureDef
+redef class AClosureDef
        var _closure: nullable MMClosure
        redef fun closure do return _closure.as(not null)
 
@@ -1546,11 +1588,7 @@ redef class PClosureDef
                if _accept_typing2 then super
        end
 
-       private fun accept_typing2(v: TypingVisitor, esc: EscapableClosure) is abstract
-end
-
-redef class AClosureDef
-       redef fun accept_typing2(v, esc)
+       private fun accept_typing2(v: TypingVisitor, esc: EscapableClosure)
        do
                _escapable = esc
 
@@ -1590,8 +1628,8 @@ redef class AClosureDef
 end
 
 class ATypeCheckExpr
-special PExpr
-       private fun check_expr_cast(v: TypingVisitor, n_expr: PExpr, n_type: PType)
+special AExpr
+       private fun check_expr_cast(v: TypingVisitor, n_expr: AExpr, n_type: AType)
        do
                if not v.check_expr(n_expr) then return
                if not n_type.is_typed then return