icode: do no do recursive inline
[nit.git] / src / analysis / inline_methods.nit
index 8e174d4..1b06b0d 100644 (file)
@@ -22,27 +22,47 @@ import icode
 private class InlineMethodVisitor
 special ICodeVisitor
        var _pass: Int = 0
+       var _icb: ICodeBuilder
+
+       var _current_inlining: Array[IRoutine]
 
        redef fun visit_icode(ic)
        do
                if ic isa ICall then
                        var m = ic.property
-                       if m.iroutine != null and ic.is_inlinable then
-                               var ir = m.iroutine.as(not null)
-                               var seq = new ISeq
-                               current_icode.insert_before(seq)
-                               var e = ir.inline_in_seq(seq, ic.exprs)
-                               var r = ic.result
-                               if r != null then
-                                       assert e != null
-                                       current_icode.insert_before(new IMove(r, e))
+                       var ir = m.iroutine
+                       if ir != null and ic.is_inlinable then
+                               if _current_inlining.has(ir) then
+                                       # We cannot inline ir
+                                       # FIXME: what we want is a static call
+                               else
+                                       var icb = _icb
+                                       _current_inlining.push(ir)
+                                       var seq = new ISeq
+                                       var old_seq = icb.seq
+                                       icb.seq = seq
+                                       current_icode.insert_before(seq)
+                                       var e = icb.inline_routine(ir, ic.exprs, ic.closure_defs)
+                                       var r = ic.result
+                                       if r != null then
+                                               assert e != null
+                                               current_icode.insert_before(new IMove(r, e))
+                                       end
+                                       current_icode.delete
+                                       icb.seq = old_seq
+                                       visit_icode(seq)
+                                       _current_inlining.pop
                                end
-                               current_icode.delete
-                               visit_icode(seq)
                        end
                end
                super
        end
+
+       init(m: MMModule, r: IRoutine)
+       do
+               _current_inlining = [r]
+               _icb = new ICodeBuilder(m, r)
+       end
 end
 
 redef class ICall
@@ -53,14 +73,15 @@ redef class ICall
                var cn = m.local_class.name
                return (m.is_intern and cn != once ("Object".to_symbol)) or
                        (cn == (once ("Array".to_symbol)) and (mn == (once ("length".to_symbol)) or mn == (once ("[]".to_symbol)))) or
-                       (cn == (once ("AbstractArrayRead".to_symbol)) and (mn == (once ("length".to_symbol)) or mn == (once ("[]".to_symbol))))
+                       (cn == (once ("AbstractArrayRead".to_symbol)) and (mn == (once ("length".to_symbol)) or mn == (once ("[]".to_symbol)))) or
+                       (m.global.intro.local_class.name == (once ("Inline__".to_symbol)))
        end
 end
 
 redef class IRoutine
        fun inline_methods(m: MMModule)
        do
-               var v = new InlineMethodVisitor
+               var v = new InlineMethodVisitor(m, self)
                v.visit_iroutine(self)
        end
 end