New class MMClosure
authorJean Privat <jean@pryen.org>
Thu, 22 Jan 2009 19:35:12 +0000 (14:35 -0500)
committerJean Privat <jean@pryen.org>
Thu, 22 Jan 2009 19:35:12 +0000 (14:35 -0500)
Closure in signature are not only signature.
This commit prepare to add more metainfo like 'break' closure or optionality.

src/compiling/compiling_methods.nit
src/metamodel/static_type.nit
src/syntax/control_flow.nit
src/syntax/mmbuilder.nit
src/syntax/syntax_base.nit
src/syntax/typing.nit

index 6c9ce56..9de0463 100644 (file)
@@ -372,7 +372,7 @@ redef class MMSrcMethod
                var first_closure_index = signature.arity + 1 # Wich parameter is the first closure
                for i in [0..signature.closures.length[ do
                        var closcn = closure_cname(i)
-                       var cs = signature.closures[i] # Closure signature
+                       var cs = signature.closures[i].signature # Closure signature
                        var subparams = new Array[String] # Parameters of the closure
                        subparams.add("struct {closcn}*")
                        for j in [0..cs.arity[ do
@@ -1502,7 +1502,7 @@ redef class AClosureDef
                var cname = "OC_{v.nmc.method.cname}_{v.out_contexts.length}"
                _cname = cname
                var args = new Array[String]
-               for i in [0..signature.arity[ do
+               for i in [0..closure.signature.arity[ do
                        args.add(" param{i}")
                end
 
@@ -1555,12 +1555,12 @@ redef class AClosureDef
        do
                var params = new Array[String]
                params.add("struct {closcn}* closctx")
-               for i in [0..signature.arity[ do
+               for i in [0..closure.signature.arity[ do
                        var p = "val_t {args[i]}"
                        params.add(p)
                end
                var ret: String
-               if signature.return_type != null then
+               if closure.signature.return_type != null then
                        ret = "val_t"
                else
                        ret = "void"
@@ -1593,7 +1593,7 @@ redef class AClosureDef
                v.add_instr("{v.nmc.continue_label}: while(false);")
 
                var ret: String = null
-               if signature.return_type != null then ret = v.nmc.continue_value
+               if closure.signature.return_type != null then ret = v.nmc.continue_value
 
                v.nmc.continue_value = old_cv
                v.nmc.continue_label = old_cl
@@ -1614,7 +1614,7 @@ redef class AClosureCallExpr
                end
                var s = "({ivar}->fun({cargs.join(", ")})) /* Invoke closure {variable} */"
                var va: String = null
-               if variable.signature.return_type != null then
+               if variable.closure.signature.return_type != null then
                        va = v.cfc.get_var
                        v.add_assignment(va, s)
                else
index 12d7941..2e439d0 100644 (file)
@@ -83,7 +83,7 @@ class MMSignature
        readable attr _return_type: MMType 
 
        # The closure parameters
-       readable attr _closures: Array[MMSignature] = new Array[MMSignature]
+       readable attr _closures: Array[MMClosure] = new Array[MMClosure]
 
        # Number of parameters
        meth arity: Int
@@ -215,6 +215,28 @@ class MMSignature
        end
 end
 
+# A closure in a signature
+class MMClosure
+       # The signature of the closure
+       readable attr _signature: MMSignature
+
+       # Adapt the signature to a different receiver
+       meth adaptation_to(r: MMType): MMClosure
+       do
+               return new MMClosure(_signature.adaptation_to(r))
+       end
+
+       init(s: MMSignature)
+       do
+               _signature = s
+       end
+
+       meth not_for_self: MMClosure
+       do
+               return new MMClosure(_signature.not_for_self)
+       end
+end
+
 # Inheritance relation between two types
 abstract class MMAncestor
        # The inherited type
index e477fca..f95adf1 100644 (file)
@@ -318,7 +318,7 @@ special AControlableBlock
 
        redef meth check_control_flow(v)
        do
-               if v.control_flow_ctx.unreash == false and signature.return_type != null then
+               if v.control_flow_ctx.unreash == false and closure.signature.return_type != null then
                        v.error(self, "Control error: Reached end of bloc (a 'continue' with a value was expected).")
                end
        end
index 934b21e..85aa9ec 100644 (file)
@@ -790,7 +790,7 @@ redef class PPropdef
                                prop.signature = new MMSignature(new Array[MMType], null, v.local_class.get_type)
                                if v.signature_builder.closure_decls != null then
                                        for clos in v.signature_builder.closure_decls do
-                                               prop.signature.closures.add(clos.signature)
+                                               prop.signature.closures.add(clos.variable.closure)
                                        end
                                end
                        end
@@ -1112,7 +1112,7 @@ redef class ASignature
                                v.signature_builder.signature.vararg_rank = v.signature_builder.vararg_rank
                        end
                        for clos in v.signature_builder.closure_decls do
-                               v.signature_builder.signature.closures.add(clos.signature)
+                               v.signature_builder.signature.closures.add(clos.variable.closure)
                        end
                end
        end
@@ -1168,9 +1168,6 @@ redef class AParam
 end
 
 redef class AClosureDecl
-
-       redef readable attr _signature: MMSignature
-
        redef readable attr _variable: ClosureVariable
 
        redef meth accept_property_verifier(v)
@@ -1182,10 +1179,10 @@ redef class AClosureDecl
                if sig == null then
                        sig = new MMSignature(new Array[MMType], null, v.local_class.get_type)
                end
-               _signature = sig
+               var clos = new MMClosure(sig)
                v.signature_builder = old_signature_builder
                old_signature_builder.closure_decls.add(self)
-               _variable = new ClosureVariable(n_id.to_symbol, self, sig)
+               _variable = new ClosureVariable(n_id.to_symbol, self, clos)
        end
 end
 
index 3da7ef7..9605b87 100644 (file)
@@ -242,12 +242,12 @@ special Variable
        redef meth kind do return once "closure"
 
        # The signature of the closure
-       readable attr _signature: MMSignature
+       readable attr _closure: MMClosure
 
-       init(n: Symbol, d: PNode, s: MMSignature)
+       init(n: Symbol, d: PNode, c: MMClosure)
        do
                super(n, d)
-               _signature = s
+               _closure = c
        end
 end
 
@@ -450,10 +450,7 @@ redef class PParam
 end
 
 redef class PClosureDecl
-       # The signature of the declared closure
-       meth signature: MMSignature is abstract
-
-       # Associated bloc variable
+       # Associated closure variable
        meth variable: ClosureVariable is abstract
 end
 
@@ -621,8 +618,8 @@ redef class AClosureCallExpr
 end
 
 redef class PClosureDef
-       # Associated signature
-       readable writable attr _signature: MMSignature
+       # Associated closure
+       readable writable attr _closure: MMClosure
 
        # Automatic variables
        readable writable attr _variables: Array[AutoVariable]
index a66b29e..e784269 100644 (file)
@@ -1256,7 +1256,7 @@ redef class AClosureCallExpr
        redef meth after_typing(v)
        do
                var va = variable
-               var sig = va.signature 
+               var sig = va.closure.signature 
                var args = process_signature(v, sig, n_id.to_symbol, n_args.to_a)
                if args == null then return
                _prop = null
@@ -1274,18 +1274,19 @@ redef class PClosureDef
                if _accept_typing2 then super
        end
 
-       private meth accept_typing2(v: TypingVisitor, sig: MMSignature) is abstract
+       private meth accept_typing2(v: TypingVisitor, clos: MMClosure) is abstract
 end
 
 redef class AClosureDef
-       redef meth accept_typing2(v, sig)
+       redef meth accept_typing2(v, clos)
        do      
+               var sig = clos.signature
                if sig.arity != n_id.length then
                        v.error(self, "Error: {sig.arity} automatic variable names expected, {n_id.length} found.")
                        return
                end
 
-               signature = sig
+               closure = clos
 
                var old_stype = v.closure_stype
                v.closure_stype = sig.return_type