icode: duplicate closure declaration on ICodeN
authorJean Privat <jean@pryen.org>
Wed, 26 Aug 2009 15:48:03 +0000 (11:48 -0400)
committerJean Privat <jean@pryen.org>
Wed, 26 Aug 2009 16:22:26 +0000 (12:22 -0400)
Just aliasing them broke the constraint that each ICode element is unique
in an IRoutine.

Signed-off-by: Jean Privat <jean@pryen.org>

src/icode/icode_tools.nit
tests/base_inline_closure_fork2.nit [new file with mode: 0644]
tests/sav/base_inline_closure_fork2.sav [new file with mode: 0644]
tests/sav/base_inline_closure_fork2_alt1.sav [new file with mode: 0644]
tests/sav/base_inline_closure_fork2_alt2.sav [new file with mode: 0644]
tests/sav/base_inline_closure_fork2_alt3.sav [new file with mode: 0644]
tests/sav/base_inline_closure_fork2_alt4.sav [new file with mode: 0644]

index 33b0010..87d5c16 100644 (file)
@@ -175,7 +175,7 @@ private class ICodeDupContext
        end
 
        # Return a correct bunch of registers
-       fun dup_iregs(regs: Sequence[IRegister]): Sequence[IRegister]
+       fun dup_iregs(regs: Sequence[IRegister]): Array[IRegister]
        do
                var a = new Array[IRegister].with_capacity(regs.length)
                for r in regs do
@@ -224,7 +224,23 @@ redef class ICode
                var c = inner_dup_with(d)
                if self isa ICodeN then
                        assert c isa ICodeN
-                       c.closure_defs = closure_defs
+                       var cdecl = closure_defs
+                       if cdecl != null then
+                               # Duplicate the colsure definitions
+                               var cdecl2 = new Array[nullable IClosureDef].with_capacity(cdecl.length)
+                               for cd in cdecl do
+                                       if cd == null then
+                                               cdecl2.add(null)
+                                       else
+                                               var r = cd.result
+                                               if r != null then r = d.dup_ireg(r)
+                                               var cd2 = new IClosureDef(d.dup_iregs(cd.params), r)
+                                               cdecl2.add(cd2)
+                                               cd.body.dup_seq_to(d, cd2.body)
+                                       end
+                               end
+                               c.closure_defs = cdecl2
+                       end
                end
                var r = result
                if r != null then c.result = d.dup_ireg(r)
diff --git a/tests/base_inline_closure_fork2.nit b/tests/base_inline_closure_fork2.nit
new file mode 100644 (file)
index 0000000..21c109b
--- /dev/null
@@ -0,0 +1,50 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2008 Jean Privat <jean@pryen.org>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+import kernel
+
+class Inline__
+       fun foo
+               !f
+       do
+               f
+               f
+       end
+end
+
+class B
+       var _b: Int
+       redef fun output do _b.output
+end
+
+var a = new Inline__
+a.foo !f do
+       var x = new B(1)
+       var y = 2
+       a.foo !f do
+               x.output
+               y.output
+               x = new B(3)
+               y = 4
+               #alt1#break label l1
+               #alt2#break label l2
+               #alt3#continue label l1
+               #alt4#continue label l2
+       end label l1
+       x.output
+       y.output
+end label l2
+5.output
diff --git a/tests/sav/base_inline_closure_fork2.sav b/tests/sav/base_inline_closure_fork2.sav
new file mode 100644 (file)
index 0000000..76ae684
--- /dev/null
@@ -0,0 +1,13 @@
+1
+2
+3
+4
+3
+4
+1
+2
+3
+4
+3
+4
+5
diff --git a/tests/sav/base_inline_closure_fork2_alt1.sav b/tests/sav/base_inline_closure_fork2_alt1.sav
new file mode 100644 (file)
index 0000000..5bec908
--- /dev/null
@@ -0,0 +1,9 @@
+1
+2
+3
+4
+1
+2
+3
+4
+5
diff --git a/tests/sav/base_inline_closure_fork2_alt2.sav b/tests/sav/base_inline_closure_fork2_alt2.sav
new file mode 100644 (file)
index 0000000..c728202
--- /dev/null
@@ -0,0 +1,3 @@
+1
+2
+5
diff --git a/tests/sav/base_inline_closure_fork2_alt3.sav b/tests/sav/base_inline_closure_fork2_alt3.sav
new file mode 100644 (file)
index 0000000..76ae684
--- /dev/null
@@ -0,0 +1,13 @@
+1
+2
+3
+4
+3
+4
+1
+2
+3
+4
+3
+4
+5
diff --git a/tests/sav/base_inline_closure_fork2_alt4.sav b/tests/sav/base_inline_closure_fork2_alt4.sav
new file mode 100644 (file)
index 0000000..b23b646
--- /dev/null
@@ -0,0 +1,5 @@
+1
+2
+1
+2
+5