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 # Instantiation and transformation of semantic nodes in the AST of expressions and statements
18 intrude import semantize
::typing
19 intrude import literal
21 intrude import semantize
::scope
23 # General factory to build semantic nodes in the AST of expressions
25 # The module used as reference for the building
26 # It is used to gather types and other stufs
29 # The anchor used for some mechanism relying on types
30 var anchor
: nullable MClassType
32 # Make a new Int literal
33 fun make_int
(value
: Int): AIntegerExpr
35 return new AIntegerExpr.make
(value
, mmodule
.int_type
)
38 # Make a new instatiation
39 fun make_new
(callsite
: CallSite, args
: nullable Array[AExpr]): ANewExpr
41 return new ANewExpr.make
(callsite
, args
)
44 # Make a new message send
45 fun make_call
(recv
: AExpr, callsite
: CallSite, args
: nullable Array[AExpr]): ACallExpr
47 return new ACallExpr.make
(recv
, callsite
, args
)
50 # Make a new, empty, sequence of statements
51 fun make_block
: ABlockExpr
53 return new ABlockExpr.make
56 # Make a new, empty, loop of statements
57 fun make_loop
: ALoopExpr
59 return new ALoopExpr.make
62 # Make a new variable read
63 fun make_var_read
(variable
: Variable, mtype
: MType): AVarExpr
65 return new AVarExpr.make
(variable
, mtype
)
68 # Make a new variable assignment
69 fun make_var_assign
(variable
: Variable, value
: AExpr): AVarAssignExpr
71 return new AVarAssignExpr.make
(variable
, value
)
74 # Make a new attribute read
75 fun make_attr_read
(recv
: AExpr, attribute
: MAttribute): AAttrExpr
77 var mtype
= attribute
.intro
.static_mtype
.resolve_for
(recv
.mtype
.as(not null), anchor
, mmodule
, true)
78 return new AAttrExpr.make
(recv
, attribute
, mtype
)
81 # Make a new attribute assignment
82 fun make_attr_assign
(recv
: AExpr, attribute
: MAttribute, value
: AExpr): AAttrAssignExpr
84 return new AAttrAssignExpr.make
(recv
, attribute
, value
)
87 # Make a new escapable block
90 return new ADoExpr.make
93 # Make a new break for a given escapemark
94 fun make_break
(escapemark
: EscapeMark): ABreakExpr
96 return new ABreakExpr.make
(escapemark
)
99 # Make a new condinionnal
100 # `mtype` is the return type of the whole if, in case of a ternary operator.
101 fun make_if
(condition
: AExpr, mtype
: nullable MType): AIfExpr
103 return new AIfExpr.make
(condition
, mtype
)
108 # Return a new variable read that contains the value of the expression
109 # This method take care efficiently of creating and initalising an anonymous local variable
111 # Note: since this method do side-effects (AST replacement), there could be unexpected effects when used as
112 # argument of other methods related to AST transformations.
113 fun make_var_read
: AVarExpr
115 var variable
= self.variable_cache
116 if variable
== null then
117 assert parent
!= null
118 var place
= detach_with_placeholder
119 variable
= new Variable("")
120 variable
.declared_type
= self.mtype
121 var nvar
= new AVarAssignExpr.make
(variable
, self)
122 place
.replace_with
(nvar
)
123 self.variable_cache
= variable
125 return new AVarExpr.make
(variable
, variable
.declared_type
.as(not null))
128 private var variable_cache
: nullable Variable
130 # The `detach` method completely remove the node in the parent.
131 # Owever, sometime, it is useful to keep the emplacement of the removed child.
133 # The standard usecase is the insertion of a node beetwen a parent `p` and a child `p.c`.
134 # To create the new node `n`, we need to attach the child to it.
135 # But, to put `n` where `c` was in `p`, the place has to be remembered.
140 # var h = c.detach_with_placeholder
141 # var n = astbuilder.make_XXX(c)
144 fun detach_with_placeholder
: AExpr
146 var h
= new APlaceholderExpr.make
152 # Add `expr` at the end of the block
154 # REQUIRE: self isa ABlockExpr
156 # Note: this method, aimed to `ABlockExpr` is promoted to `AExpr` because of the limitations of the hierarchies generated by sablecc3
159 print
"add not implemented in {inspect}"
164 # A placeholder for a `AExpr` node
165 # Instances are transiantly used to mark some specific emplacments in the AST
166 # during complex transformations.
168 # Their must not appear in a valid AST
170 # @see AExpr::detach_with_placeholder
171 class APlaceholderExpr
178 redef class ABlockExpr
184 redef fun add
(expr
: AExpr)
190 redef class ALoopExpr
193 _n_kwloop
= new TKwloop
195 n_block
= new ABlockExpr
196 n_block
.is_typed
= true
199 redef fun add
(expr
: AExpr)
210 n_block
= new ABlockExpr
211 n_block
.is_typed
= true
214 # Make a new break expression of the given do
215 fun make_break
: ABreakExpr
217 var escapemark
= self.break_mark
218 if escapemark
== null then
219 escapemark
= new EscapeMark(null)
220 self.break_mark
= escapemark
222 return new ABreakExpr.make
(escapemark
)
225 redef fun add
(expr
: AExpr)
231 redef class ABreakExpr
232 private init make
(escapemark
: EscapeMark)
234 _n_kwbreak
= new TKwbreak
235 self.escapemark
= escapemark
236 escapemark
.escapes
.add
self
242 private init make
(condition
: AExpr, mtype
: nullable MType)
246 _n_expr
.parent
= self
247 _n_kwthen
= new TKwthen
248 _n_then
= new ABlockExpr.make
249 _n_kwelse
= new TKwelse
250 _n_else
= new ABlockExpr.make
259 var n_id
= new TClassid
260 var n_qid
= new AQclassid
266 redef class AIntegerExpr
267 private init make
(value
: Int, t
: MType)
270 self._n_integer
= new TInteger # dummy
276 private init make
(callsite
: CallSite, args
: nullable Array[AExpr])
278 _n_kwnew
= new TKwnew
279 _n_type
= new AType.make
280 _n_args
= new AListExprs
282 n_args
.n_exprs
.add_all
(args
)
284 self.callsite
= callsite
285 self.recvtype
= callsite
.recv
.as(MClassType)
286 if callsite
.mproperty
.is_new
then
287 self.mtype
= callsite
.msignature
.return_mtype
289 self.mtype
= callsite
.recv
295 redef class ACallExpr
296 private init make
(recv
: AExpr, callsite
: CallSite, args
: nullable Array[AExpr])
299 _n_args
= new AListExprs
301 _n_qid
.n_id
= new TId
303 self.n_args
.n_exprs
.add_all
(args
)
305 self.callsite
= callsite
306 self.mtype
= callsite
.msignature
.return_mtype
311 redef class AAttrExpr
312 private init make
(recv
: AExpr, attribute
: MAttribute, t
: MType)
317 mproperty
= attribute
322 redef class AAttrAssignExpr
323 private init make
(recv
: AExpr, attribute
: MAttribute, value
: AExpr)
330 _n_assign
= new TAssign
331 mproperty
= attribute
337 private init make
(v
: Variable, mtype
: MType)
345 redef class AVarAssignExpr
346 private init make
(v
: Variable, value
: AExpr)
351 _n_assign
= new TAssign