transform: do not transform rranges in for; this reactivates the short-range optimisation
[nit.git] / src / transform.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Thansformations that simplify the AST of expressions
16 # This module transform complex AST `AExpr` nodes into simplier ones
17 module transform
18
19 import astbuilder
20 import auto_super_init
21 import astvalidation
22
23 redef class ToolContext
24 var transform_phase: Phase = new TransformPhase(self, [typing_phase, auto_super_init_phase])
25 end
26
27 private class TransformPhase
28 super Phase
29
30 redef fun process_npropdef(npropdef: APropdef)
31 do
32 var val
33
34 var v = new TransformVisitor(self, npropdef)
35 v.enter_visit(npropdef)
36
37 val = new ASTValidationVisitor
38 val.enter_visit(npropdef)
39 end
40 end
41
42 private class TransformVisitor
43 super Visitor
44
45 var phase: TransformPhase
46 var mmodule: MModule
47 var mclassdef: MClassDef
48 var mpropdef: MPropDef
49 var builder: ASTBuilder
50
51 init(phase: TransformPhase, npropdef: APropdef)
52 do
53 self.phase = phase
54 self.mpropdef = npropdef.mpropdef.as(not null)
55 self.mclassdef = mpropdef.mclassdef
56 self.mmodule = mclassdef.mmodule
57 self.builder = new ASTBuilder(mmodule, mpropdef.mclassdef.bound_mtype)
58 end
59
60 redef fun visit(node)
61 do
62 if node isa AAnnotations then return
63 node.visit_all(self)
64 node.accept_transform_visitor(self)
65 end
66
67 # Get a primitive class or display a fatal error on `location`.
68 fun get_class(location: AExpr, name: String): MClass
69 do
70 return mmodule.get_primitive_class(name)
71 end
72
73 # Get a primitive method or display a fatal error on `location`.
74 fun get_method(location: AExpr, name: String, recv: MClass): MMethod
75 do
76 return phase.toolcontext.modelbuilder.force_get_primitive_method(location, name, recv, mmodule)
77 end
78 end
79
80 redef class ANode
81 private fun accept_transform_visitor(v: TransformVisitor)
82 do
83 end
84 end
85
86 redef class AVardeclExpr
87 # `var x = y` is replaced with `x = y`
88 #
89 # Declarations are only useful for scope rules
90 # Once names are associated with real objects, ther declaration become useless
91 # Therefore, if there is no initial value, then just ignore it
92 # Else, replace it with a simple assignment
93 redef fun accept_transform_visitor(v)
94 do
95 var nexpr = n_expr
96 if nexpr == null then
97 # do nothing
98 # note: not detached because the collection is currently under iteration
99 else
100 var nvar = v.builder.make_var_assign(self.variable.as(not null), nexpr)
101 replace_with(nvar)
102 end
103 end
104 end
105
106 redef class AIfexprExpr
107 # is replaced with `AIfExpr`
108 # Expression if and statement-if use two distinct classes for historical reasons
109 # However, on can replace the `AIfexprExpr` with the simpler `AIfExpr`
110 redef fun accept_transform_visitor(v)
111 do
112 var nif = v.builder.make_if(n_expr, self.mtype)
113 nif.n_then.add(n_then)
114 nif.n_else.add(n_else)
115
116 replace_with(nif)
117 end
118 end
119
120 redef class AOrExpr
121 # `x or y` is replaced with `if x then x else y`
122 redef fun accept_transform_visitor(v)
123 do
124 var nif = v.builder.make_if(n_expr, self.mtype)
125 nif.n_then.add(n_expr.make_var_read)
126 nif.n_else.add(n_expr2)
127
128 replace_with(nif)
129 end
130 end
131
132 redef class AImpliesExpr
133 redef fun accept_transform_visitor(v)
134 do
135 # TODO
136 end
137 end
138
139 redef class AAndExpr
140 # `x and y` is replaced with `if x then y else x`
141 redef fun accept_transform_visitor(v)
142 do
143 var nif = v.builder.make_if(n_expr, self.mtype)
144 nif.n_then.add(n_expr2)
145 nif.n_else.add(n_expr.make_var_read)
146
147 replace_with(nif)
148 end
149 end
150
151 redef class AWhileExpr
152 redef fun accept_transform_visitor(v)
153 do
154 # TODO
155 end
156 end
157
158 redef class AForExpr
159 redef fun accept_transform_visitor(v)
160 do
161 # TODO
162 end
163 end
164
165 redef class AArrayExpr
166 # `[x,y]` is replaced with
167 #
168 # var t = new Array[X].with_capacity(2)
169 # t.add(x)
170 # t.add(y)
171 # t
172 redef fun accept_transform_visitor(v)
173 do
174 var nblock = v.builder.make_block
175
176 var nnew = v.builder.make_new(with_capacity_callsite.as(not null), [v.builder.make_int(n_exprs.n_exprs.length)])
177 nblock.add nnew
178
179 for nexpr in self.n_exprs.n_exprs do
180 var nadd = v.builder.make_call(nnew.make_var_read, push_callsite.as(not null), [nexpr])
181 nblock.add nadd
182 end
183 var nres = nnew.make_var_read
184 nblock.add nres
185
186 replace_with(nblock)
187 end
188 end
189
190 redef class ACrangeExpr
191 # `[x..y]` is replaced with `new Range[X](x,y)`
192 redef fun accept_transform_visitor(v)
193 do
194 if parent isa AForExpr then return # to permit shortcut ranges
195 replace_with(v.builder.make_new(init_callsite.as(not null), [n_expr, n_expr2]))
196 end
197 end
198
199 redef class AOrangeExpr
200 # `[x..y[` is replaced with `new Range[X].without_last(x,y)`
201 redef fun accept_transform_visitor(v)
202 do
203 if parent isa AForExpr then return # to permit shortcut ranges
204 replace_with(v.builder.make_new(init_callsite.as(not null), [n_expr, n_expr2]))
205 end
206 end
207
208 redef class AParExpr
209 # `(x)` is replaced with `x`
210 redef fun accept_transform_visitor(v)
211 do
212 replace_with(n_expr)
213 end
214 end
215
216 redef class ASendReassignFormExpr
217 # `x.foo(y)+=z` is replaced with
218 #
219 # x.foo(y) = x.foo(y) + z
220 #
221 # witch is, in reality:
222 #
223 # x."foo="(y, x.foo(y)."+"(z))
224 redef fun accept_transform_visitor(v)
225 do
226 var nblock = v.builder.make_block
227 nblock.add(n_expr)
228
229 var read_args = new Array[AExpr]
230 var write_args = new Array[AExpr]
231 for a in raw_arguments do
232 nblock.add(a)
233 read_args.add(a.make_var_read)
234 write_args.add(a.make_var_read)
235 end
236
237 var nread = v.builder.make_call(n_expr.make_var_read, callsite.as(not null), read_args)
238
239 var nnewvalue = v.builder.make_call(nread, reassign_callsite.as(not null), [n_value])
240
241 write_args.add(nnewvalue)
242 var nwrite = v.builder.make_call(n_expr.make_var_read, write_callsite.as(not null), write_args)
243 nblock.add(nwrite)
244
245 replace_with(nblock)
246 end
247 end
248
249 redef class AVarReassignExpr
250 # `v += z` is replaced with `v = v + z`
251 redef fun accept_transform_visitor(v)
252 do
253 var variable = self.variable.as(not null)
254
255 var nread = v.builder.make_var_read(variable, read_type.as(not null))
256
257 var nnewvalue = v.builder.make_call(nread, reassign_callsite.as(not null), [n_value])
258 var nwrite = v.builder.make_var_assign(variable, nnewvalue)
259
260 replace_with(nwrite)
261 end
262 end
263
264 redef class AAttrReassignExpr
265 # `x.a += z` is replaced with `x.a = x.a + z`
266 redef fun accept_transform_visitor(v)
267 do
268 var nblock = v.builder.make_block
269 nblock.add(n_expr)
270 var attribute = self.mproperty.as(not null)
271
272 var nread = v.builder.make_attr_read(n_expr.make_var_read, attribute)
273 var nnewvalue = v.builder.make_call(nread, reassign_callsite.as(not null), [n_value])
274 var nwrite = v.builder.make_attr_assign(n_expr.make_var_read, attribute, nnewvalue)
275 nblock.add(nwrite)
276
277 replace_with(nblock)
278 end
279 end