1 # This file is part of NIT ( http://www.nitlanguage.org ).
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Thansformations that simplify the AST of expressions
16 # This module transform complex AST `AExpr` nodes into simplier ones
22 intrude import semantize
::scope
24 redef class ToolContext
25 var transform_phase
: Phase = new TransformPhase(self, [typing_phase
, auto_super_init_phase
])
28 var opt_no_shortcut_range
: OptionBool = new OptionBool("Always insantiate a range and its iterator on 'for' loops", "--no-shortcut-range")
33 self.option_context
.add_option
(self.opt_no_shortcut_range
)
37 private class TransformPhase
40 redef fun process_npropdef
(npropdef
: APropdef)
44 var v
= new TransformVisitor(self, npropdef
)
45 v
.enter_visit
(npropdef
)
47 val
= new ASTValidationVisitor
48 val
.enter_visit
(npropdef
)
52 private class TransformVisitor
55 var phase
: TransformPhase
57 var mclassdef
: MClassDef
58 var mpropdef
: MPropDef
59 var builder
: ASTBuilder
61 init(phase
: TransformPhase, npropdef
: APropdef)
64 self.mpropdef
= npropdef
.mpropdef
.as(not null)
65 self.mclassdef
= mpropdef
.mclassdef
66 self.mmodule
= mclassdef
.mmodule
67 self.builder
= new ASTBuilder(mmodule
, mpropdef
.mclassdef
.bound_mtype
)
72 if node
isa AAnnotations then return
74 node
.accept_transform_visitor
(self)
77 # Get a primitive class or display a fatal error on `location`.
78 fun get_class
(location
: AExpr, name
: String): MClass
80 return mmodule
.get_primitive_class
(name
)
83 # Get a primitive method or display a fatal error on `location`.
84 fun get_method
(location
: AExpr, name
: String, recv
: MClass): MMethod
86 return phase
.toolcontext
.modelbuilder
.force_get_primitive_method
(location
, name
, recv
, mmodule
)
91 private fun accept_transform_visitor
(v
: TransformVisitor)
96 redef class AVardeclExpr
97 # `var x = y` is replaced with `x = y`
99 # Declarations are only useful for scope rules
100 # Once names are associated with real objects, ther declaration become useless
101 # Therefore, if there is no initial value, then just ignore it
102 # Else, replace it with a simple assignment
103 redef fun accept_transform_visitor
(v
)
106 if nexpr
== null then
108 # note: not detached because the collection is currently under iteration
110 var nvar
= v
.builder
.make_var_assign
(self.variable
.as(not null), nexpr
)
116 redef class AIfexprExpr
117 # is replaced with `AIfExpr`
118 # Expression if and statement-if use two distinct classes for historical reasons
119 # However, on can replace the `AIfexprExpr` with the simpler `AIfExpr`
120 redef fun accept_transform_visitor
(v
)
122 var nif
= v
.builder
.make_if
(n_expr
, self.mtype
)
123 nif
.n_then
.add
(n_then
)
124 nif
.n_else
.add
(n_else
)
131 # `x or y` is replaced with `if x then x else y`
132 redef fun accept_transform_visitor
(v
)
134 var nif
= v
.builder
.make_if
(n_expr
, self.mtype
)
135 nif
.n_then
.add
(n_expr
.make_var_read
)
136 nif
.n_else
.add
(n_expr2
)
142 redef class AImpliesExpr
143 redef fun accept_transform_visitor
(v
)
150 # `x and y` is replaced with `if x then y else x`
151 redef fun accept_transform_visitor
(v
)
153 var nif
= v
.builder
.make_if
(n_expr
, self.mtype
)
154 nif
.n_then
.add
(n_expr2
)
155 nif
.n_else
.add
(n_expr
.make_var_read
)
161 redef class AWhileExpr
162 redef fun accept_transform_visitor
(v
)
164 var nloop
= v
.builder
.make_loop
165 var nif
= v
.builder
.make_if
(n_expr
, null)
169 if nblock
!= null then nif
.n_then
.add nblock
171 var escapemark
= self.break_mark
.as(not null)
172 var nbreak
= v
.builder
.make_break
(escapemark
)
173 nif
.n_else
.add nbreak
175 nloop
.break_mark
= self.break_mark
176 nloop
.continue_mark
= self.continue_mark
183 redef fun accept_transform_visitor
(v
)
185 var escapemark
= self.break_mark
186 assert escapemark
!= null
188 var nblock
= v
.builder
.make_block
192 # Shortcut on explicit range
193 # Avoid the instantiation of the range and the iterator
194 if self.variables
.length
== 1 and nexpr
isa ARangeExpr and not v
.phase
.toolcontext
.opt_no_shortcut_range
.value
then
195 var variable
= variables
.first
196 nblock
.add v
.builder
.make_var_assign
(variable
, nexpr
.n_expr
)
197 var to
= nexpr
.n_expr2
200 var nloop
= v
.builder
.make_loop
201 nloop
.break_mark
= escapemark
204 var is_ok
= v
.builder
.make_call
(v
.builder
.make_var_read
(variable
, variable
.declared_type
.as(not null)), method_lt
.as(not null), [to
.make_var_read
])
206 var nif
= v
.builder
.make_if
(is_ok
, null)
209 var nthen
= nif
.n_then
210 var ndo
= v
.builder
.make_do
211 ndo
.break_mark
= escapemark
.continue_mark
214 ndo
.add
self.n_block
.as(not null)
216 var one
= v
.builder
.make_int
(1)
217 var succ
= v
.builder
.make_call
(v
.builder
.make_var_read
(variable
, variable
.declared_type
.as(not null)), method_successor
.as(not null), [one
])
218 nthen
.add v
.builder
.make_var_assign
(variable
, succ
)
220 var nbreak
= v
.builder
.make_break
(escapemark
)
221 nif
.n_else
.add nbreak
229 var iter
= v
.builder
.make_call
(nexpr
.make_var_read
, method_iterator
.as(not null), null)
232 var nloop
= v
.builder
.make_loop
233 nloop
.break_mark
= escapemark
236 var is_ok
= v
.builder
.make_call
(iter
.make_var_read
, method_is_ok
.as(not null), null)
238 var nif
= v
.builder
.make_if
(is_ok
, null)
241 var nthen
= nif
.n_then
242 var ndo
= v
.builder
.make_do
243 ndo
.break_mark
= escapemark
.continue_mark
246 if self.variables
.length
== 1 then
247 var item
= v
.builder
.make_call
(iter
.make_var_read
, method_item
.as(not null), null)
248 ndo
.add v
.builder
.make_var_assign
(variables
.first
, item
)
249 else if self.variables
.length
== 2 then
250 var key
= v
.builder
.make_call
(iter
.make_var_read
, method_key
.as(not null), null)
251 ndo
.add v
.builder
.make_var_assign
(variables
[0], key
)
252 var item
= v
.builder
.make_call
(iter
.make_var_read
, method_item
.as(not null), null)
253 ndo
.add v
.builder
.make_var_assign
(variables
[1], item
)
258 ndo
.add
self.n_block
.as(not null)
260 nthen
.add v
.builder
.make_call
(iter
.make_var_read
, method_next
.as(not null), null)
262 var nbreak
= v
.builder
.make_break
(escapemark
)
263 nif
.n_else
.add nbreak
265 var method_finish
= self.method_finish
266 if method_finish
!= null then
267 nblock
.add v
.builder
.make_call
(iter
.make_var_read
, method_finish
, null)
274 redef class AArrayExpr
275 # `[x,y]` is replaced with
277 # var t = new Array[X].with_capacity(2)
281 redef fun accept_transform_visitor
(v
)
283 var nblock
= v
.builder
.make_block
285 var nnew
= v
.builder
.make_new
(with_capacity_callsite
.as(not null), [v
.builder
.make_int
(n_exprs
.n_exprs
.length
)])
288 for nexpr
in self.n_exprs
.n_exprs
do
289 var nadd
= v
.builder
.make_call
(nnew
.make_var_read
, push_callsite
.as(not null), [nexpr
])
292 var nres
= nnew
.make_var_read
299 redef class ACrangeExpr
300 # `[x..y]` is replaced with `new Range[X](x,y)`
301 redef fun accept_transform_visitor
(v
)
303 if parent
isa AForExpr then return # to permit shortcut ranges
304 replace_with
(v
.builder
.make_new
(init_callsite
.as(not null), [n_expr
, n_expr2
]))
308 redef class AOrangeExpr
309 # `[x..y[` is replaced with `new Range[X].without_last(x,y)`
310 redef fun accept_transform_visitor
(v
)
312 if parent
isa AForExpr then return # to permit shortcut ranges
313 replace_with
(v
.builder
.make_new
(init_callsite
.as(not null), [n_expr
, n_expr2
]))
318 # `(x)` is replaced with `x`
319 redef fun accept_transform_visitor
(v
)
325 redef class ASendReassignFormExpr
326 # `x.foo(y)+=z` is replaced with
328 # x.foo(y) = x.foo(y) + z
330 # witch is, in reality:
332 # x."foo="(y, x.foo(y)."+"(z))
333 redef fun accept_transform_visitor
(v
)
335 var nblock
= v
.builder
.make_block
338 var read_args
= new Array[AExpr]
339 var write_args
= new Array[AExpr]
340 for a
in raw_arguments
do
342 read_args
.add
(a
.make_var_read
)
343 write_args
.add
(a
.make_var_read
)
346 var nread
= v
.builder
.make_call
(n_expr
.make_var_read
, callsite
.as(not null), read_args
)
348 var nnewvalue
= v
.builder
.make_call
(nread
, reassign_callsite
.as(not null), [n_value
])
350 write_args
.add
(nnewvalue
)
351 var nwrite
= v
.builder
.make_call
(n_expr
.make_var_read
, write_callsite
.as(not null), write_args
)
358 redef class AVarReassignExpr
359 # `v += z` is replaced with `v = v + z`
360 redef fun accept_transform_visitor
(v
)
362 var variable
= self.variable
.as(not null)
364 var nread
= v
.builder
.make_var_read
(variable
, read_type
.as(not null))
366 var nnewvalue
= v
.builder
.make_call
(nread
, reassign_callsite
.as(not null), [n_value
])
367 var nwrite
= v
.builder
.make_var_assign
(variable
, nnewvalue
)
373 redef class AAttrReassignExpr
374 # `x.a += z` is replaced with `x.a = x.a + z`
375 redef fun accept_transform_visitor
(v
)
377 var nblock
= v
.builder
.make_block
379 var attribute
= self.mproperty
.as(not null)
381 var nread
= v
.builder
.make_attr_read
(n_expr
.make_var_read
, attribute
)
382 var nnewvalue
= v
.builder
.make_call
(nread
, reassign_callsite
.as(not null), [n_value
])
383 var nwrite
= v
.builder
.make_attr_assign
(n_expr
.make_var_read
, attribute
, nnewvalue
)