metamodel: rename 'universal' to 'enum'
[nit.git] / src / analysis / inline_methods.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2009 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Detect inlinable methods and inline them
18 package inline_methods
19
20 import icode
21
22 private class InlineMethodVisitor
23 super ICodeVisitor
24 var _pass: Int = 0
25 var _icb: ICodeBuilder
26
27 redef fun visit_icode(ic)
28 do
29 if ic isa ICall then
30 var m = ic.property
31 var ir = m.iroutine
32 if ir != null and ic.is_inlinable then
33 var icb = _icb
34 if icb.iroutine == ir then
35 # We cannot inline ir
36 # So, at least transform the call to a static one
37 current_icode.delete
38 var icall = new IStaticCall(ic.property, ic.exprs)
39 icall.closure_defs = ic.closure_defs
40 icall.result = ic.result
41 current_icode.insert_before(icall)
42 current_icode.delete
43 else
44 var seq = new ISeq
45 var old_seq = icb.seq
46 icb.seq = seq
47 current_icode.insert_before(seq)
48 var e = icb.inline_routine(ir, ic.exprs, ic.closure_defs)
49 var r = ic.result
50 if r != null then
51 assert e != null
52 current_icode.insert_before(new IMove(r, e))
53 end
54 current_icode.delete
55 icb.seq = old_seq
56 visit_icode(seq)
57 end
58 end
59 end
60 super
61 end
62
63 init(m: MMModule, r: IRoutine)
64 do
65 _icb = new ICodeBuilder(m, r)
66 end
67 end
68
69 redef class ICall
70 fun is_inlinable: Bool
71 do
72 var m = property
73 var mn = m.name
74 var cn = m.local_class.name
75 return (m.is_intern and cn != once ("Object".to_symbol)) or
76 (cn == (once ("Int".to_symbol)) and (mn == (once ("enumerate_to".to_symbol)) or mn == (once ("enumerate_before".to_symbol)))) or
77 (cn == (once ("Array".to_symbol)) and (mn == (once ("length".to_symbol)) or mn == (once ("[]".to_symbol)) or mn == (once ("iterate".to_symbol)))) or
78 (cn == (once ("AbstractArrayRead".to_symbol)) and (mn == (once ("length".to_symbol)) or mn == (once ("[]".to_symbol)))) or
79 (m.global.intro.local_class.name == (once ("Inline__".to_symbol)))
80 end
81 end
82
83 redef class IRoutine
84 fun inline_methods(m: MMModule)
85 do
86 var v = new InlineMethodVisitor(m, self)
87 v.visit_iroutine(self)
88 end
89 end