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 m
= npropdef
.mpropdef
45 if m
== null then return
46 var v
= new TransformVisitor(self, m
)
47 v
.enter_visit
(npropdef
)
49 val
= new ASTValidationVisitor
50 val
.enter_visit
(npropdef
)
54 private class TransformVisitor
57 var phase
: TransformPhase
58 var mmodule
: MModule is noinit
59 var mclassdef
: MClassDef is noinit
60 var mpropdef
: MPropDef
61 var builder
: ASTBuilder is noinit
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
73 node
.full_transform_visitor
(self)
76 # Get a primitive method or display a fatal error on `location`.
77 fun get_method
(location
: AExpr, name
: String, recv
: MClass): MMethod
79 return phase
.toolcontext
.modelbuilder
.force_get_primitive_method
(location
, name
, recv
, mmodule
)
84 private fun full_transform_visitor
(v
: TransformVisitor)
87 accept_transform_visitor
(v
)
89 private fun accept_transform_visitor
(v
: TransformVisitor)
95 redef fun full_transform_visitor
(v
: TransformVisitor)
97 var na
= comprehension
99 # We are building a comprehension array `array`
100 # Replace `self` with `array.push(self)`
101 var place
= detach_with_placeholder
102 var recv
= na
.nnew
.make_var_read
103 var nadd
= v
.builder
.make_call
(recv
, na
.push_callsite
.as(not null), [self])
104 place
.replace_with
(nadd
)
110 redef class AVardeclExpr
111 # `var x = y` is replaced with `x = y`
113 # Declarations are only useful for scope rules
114 # Once names are associated with real objects, ther declaration become useless
115 # Therefore, if there is no initial value, then just ignore it
116 # Else, replace it with a simple assignment
117 redef fun accept_transform_visitor
(v
)
120 if nexpr
== null then
122 # note: not detached because the collection is currently under iteration
124 var nvar
= v
.builder
.make_var_assign
(self.variable
.as(not null), nexpr
)
130 redef class AIfexprExpr
131 # is replaced with `AIfExpr`
132 # Expression if and statement-if use two distinct classes for historical reasons
133 # However, on can replace the `AIfexprExpr` with the simpler `AIfExpr`
134 redef fun accept_transform_visitor
(v
)
136 var nif
= v
.builder
.make_if
(n_expr
, self.mtype
)
137 nif
.n_then
.add
(n_then
)
138 nif
.n_else
.add
(n_else
)
145 # `x or y` is replaced with `if x then x else y`
146 redef fun accept_transform_visitor
(v
)
148 var nif
= v
.builder
.make_if
(n_expr
, self.mtype
)
149 nif
.n_then
.add
(n_expr
.make_var_read
)
150 nif
.n_else
.add
(n_expr2
)
156 redef class AImpliesExpr
157 redef fun accept_transform_visitor
(v
)
164 # `x and y` is replaced with `if x then y else x`
165 redef fun accept_transform_visitor
(v
)
167 var nif
= v
.builder
.make_if
(n_expr
, self.mtype
)
168 nif
.n_then
.add
(n_expr2
)
169 nif
.n_else
.add
(n_expr
.make_var_read
)
175 redef class AWhileExpr
176 redef fun accept_transform_visitor
(v
)
178 var nloop
= v
.builder
.make_loop
179 var nif
= v
.builder
.make_if
(n_expr
, null)
183 if nblock
!= null then nif
.n_then
.add nblock
185 var escapemark
= self.break_mark
.as(not null)
186 var nbreak
= v
.builder
.make_break
(escapemark
)
187 nif
.n_else
.add nbreak
189 nloop
.break_mark
= self.break_mark
190 nloop
.continue_mark
= self.continue_mark
197 redef fun accept_transform_visitor
(v
)
199 var escapemark
= self.break_mark
200 assert escapemark
!= null
202 var nblock
= v
.builder
.make_block
206 # Shortcut on explicit range
207 # Avoid the instantiation of the range and the iterator
208 if self.variables
.length
== 1 and nexpr
isa ARangeExpr and not v
.phase
.toolcontext
.opt_no_shortcut_range
.value
then
209 var variable
= variables
.first
210 nblock
.add v
.builder
.make_var_assign
(variable
, nexpr
.n_expr
)
211 var to
= nexpr
.n_expr2
214 var nloop
= v
.builder
.make_loop
215 nloop
.break_mark
= escapemark
218 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
])
220 var nif
= v
.builder
.make_if
(is_ok
, null)
223 var nthen
= nif
.n_then
224 var ndo
= v
.builder
.make_do
225 ndo
.break_mark
= escapemark
.continue_mark
228 ndo
.add
self.n_block
.as(not null)
230 var one
= v
.builder
.make_int
(1)
231 var succ
= v
.builder
.make_call
(v
.builder
.make_var_read
(variable
, variable
.declared_type
.as(not null)), method_successor
.as(not null), [one
])
232 nthen
.add v
.builder
.make_var_assign
(variable
, succ
)
234 var nbreak
= v
.builder
.make_break
(escapemark
)
235 nif
.n_else
.add nbreak
243 var iter
= v
.builder
.make_call
(nexpr
.make_var_read
, method_iterator
.as(not null), null)
246 var nloop
= v
.builder
.make_loop
247 nloop
.break_mark
= escapemark
250 var is_ok
= v
.builder
.make_call
(iter
.make_var_read
, method_is_ok
.as(not null), null)
252 var nif
= v
.builder
.make_if
(is_ok
, null)
255 var nthen
= nif
.n_then
256 var ndo
= v
.builder
.make_do
257 ndo
.break_mark
= escapemark
.continue_mark
260 if self.variables
.length
== 1 then
261 var item
= v
.builder
.make_call
(iter
.make_var_read
, method_item
.as(not null), null)
262 ndo
.add v
.builder
.make_var_assign
(variables
.first
, item
)
263 else if self.variables
.length
== 2 then
264 var key
= v
.builder
.make_call
(iter
.make_var_read
, method_key
.as(not null), null)
265 ndo
.add v
.builder
.make_var_assign
(variables
[0], key
)
266 var item
= v
.builder
.make_call
(iter
.make_var_read
, method_item
.as(not null), null)
267 ndo
.add v
.builder
.make_var_assign
(variables
[1], item
)
272 ndo
.add
self.n_block
.as(not null)
274 nthen
.add v
.builder
.make_call
(iter
.make_var_read
, method_next
.as(not null), null)
276 var nbreak
= v
.builder
.make_break
(escapemark
)
277 nif
.n_else
.add nbreak
279 var method_finish
= self.method_finish
280 if method_finish
!= null then
281 nblock
.add v
.builder
.make_call
(iter
.make_var_read
, method_finish
, null)
288 redef class AArrayExpr
289 # `[x,y]` is replaced with
292 # var t = new Array[X].with_capacity(2)
297 redef fun full_transform_visitor
(v
)
299 var nblock
= v
.builder
.make_block
301 var nnew
= v
.builder
.make_new
(with_capacity_callsite
.as(not null), [v
.builder
.make_int
(n_exprs
.length
)])
308 for nexpr
in self.n_exprs
do
311 var nres
= nnew
.make_var_read
317 private var nnew
: ANewExpr is noinit
320 redef class ACrangeExpr
321 # `[x..y]` is replaced with `new Range[X](x,y)`
322 redef fun accept_transform_visitor
(v
)
324 if parent
isa AForExpr then return # to permit shortcut ranges
325 replace_with
(v
.builder
.make_new
(init_callsite
.as(not null), [n_expr
, n_expr2
]))
329 redef class AOrangeExpr
330 # `[x..y[` is replaced with `new Range[X].without_last(x,y)`
331 redef fun accept_transform_visitor
(v
)
333 if parent
isa AForExpr then return # to permit shortcut ranges
334 replace_with
(v
.builder
.make_new
(init_callsite
.as(not null), [n_expr
, n_expr2
]))
339 # `(x)` is replaced with `x`
340 redef fun accept_transform_visitor
(v
)
346 redef class ASendReassignFormExpr
347 # `x.foo(y)+=z` is replaced with
350 # x.foo(y) = x.foo(y) + z
353 # witch is, in reality:
356 # x."foo="(y, x.foo(y)."+"(z))
358 redef fun accept_transform_visitor
(v
)
360 var nblock
= v
.builder
.make_block
363 var read_args
= new Array[AExpr]
364 var write_args
= new Array[AExpr]
365 for a
in raw_arguments
do
367 read_args
.add
(a
.make_var_read
)
368 write_args
.add
(a
.make_var_read
)
371 var nread
= v
.builder
.make_call
(n_expr
.make_var_read
, callsite
.as(not null), read_args
)
373 var nnewvalue
= v
.builder
.make_call
(nread
, reassign_callsite
.as(not null), [n_value
])
375 write_args
.add
(nnewvalue
)
376 var nwrite
= v
.builder
.make_call
(n_expr
.make_var_read
, write_callsite
.as(not null), write_args
)
383 redef class AVarReassignExpr
384 # `v += z` is replaced with `v = v + z`
385 redef fun accept_transform_visitor
(v
)
387 var variable
= self.variable
.as(not null)
389 var nread
= v
.builder
.make_var_read
(variable
, read_type
.as(not null))
391 var nnewvalue
= v
.builder
.make_call
(nread
, reassign_callsite
.as(not null), [n_value
])
392 var nwrite
= v
.builder
.make_var_assign
(variable
, nnewvalue
)
398 redef class AAttrReassignExpr
399 # `x.a += z` is replaced with `x.a = x.a + z`
400 redef fun accept_transform_visitor
(v
)
402 var nblock
= v
.builder
.make_block
404 var attribute
= self.mproperty
.as(not null)
406 var nread
= v
.builder
.make_attr_read
(n_expr
.make_var_read
, attribute
)
407 var nnewvalue
= v
.builder
.make_call
(nread
, reassign_callsite
.as(not null), [n_value
])
408 var nwrite
= v
.builder
.make_attr_assign
(n_expr
.make_var_read
, attribute
, nnewvalue
)