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 stuff
29 # The anchor used for some mechanism relying on types
30 var anchor
: nullable MClassType
32 # Check mmodule to avoid a new instantiation of ASTBuilder
33 fun check_mmodule
(mmodule
: MModule)
35 if self.mmodule
!= mmodule
then self.mmodule
= mmodule
38 # Make a new Int literal
39 fun make_int
(value
: Int): AIntegerExpr
41 return new AIntegerExpr.make
(value
, mmodule
.int_type
)
44 # Make a new instantiation
45 fun make_new
(callsite
: CallSite, args
: nullable Array[AExpr]): ANewExpr
47 return new ANewExpr.make
(callsite
, args
)
50 # Make a new message send
51 fun make_call
(recv
: AExpr, callsite
: CallSite, args
: nullable Array[AExpr]): ACallExpr
53 return new ACallExpr.make
(recv
, callsite
, args
)
56 # Make a new, empty, sequence of statements
57 fun make_block
: ABlockExpr
59 return new ABlockExpr.make
62 # Make a new, empty, loop of statements
63 fun make_loop
: ALoopExpr
65 return new ALoopExpr.make
68 # Make a new variable read
69 fun make_var_read
(variable
: Variable, mtype
: MType): AVarExpr
71 return new AVarExpr.make
(variable
, mtype
)
74 # Make a new variable assignment
75 fun make_var_assign
(variable
: Variable, value
: AExpr): AVarAssignExpr
77 return new AVarAssignExpr.make
(variable
, value
)
80 # Make a new attribute read
81 fun make_attr_read
(recv
: AExpr, attribute
: MAttribute): AAttrExpr
83 var mtype
= attribute
.intro
.static_mtype
.resolve_for
(recv
.mtype
.as(not null), anchor
, mmodule
, true)
84 return new AAttrExpr.make
(recv
, attribute
, mtype
)
87 # Make a new attribute assignment
88 fun make_attr_assign
(recv
: AExpr, attribute
: MAttribute, value
: AExpr): AAttrAssignExpr
90 return new AAttrAssignExpr.make
(recv
, attribute
, value
)
93 # Make a new escapable block
96 return new ADoExpr.make
99 # Make a new break for a given escapemark
100 fun make_break
(escapemark
: EscapeMark): ABreakExpr
102 return new ABreakExpr.make
(escapemark
)
105 # Make a new conditional
106 # `mtype` is the return type of the whole if, in case of a ternary operator.
107 fun make_if
(condition
: AExpr, mtype
: nullable MType): AIfExpr
109 return new AIfExpr.make
(condition
, mtype
)
113 fun make_assert
(n_id
: nullable TId , n_expr
: AExpr , n_else
: nullable AExpr): AAssertExpr
115 return new AAssertExpr.make
(n_id
, n_expr
, n_else
)
119 fun make_method
(n_visibility
: nullable APublicVisibility,
120 tk_redef
: nullable TKwredef,
121 mmethoddef
: MMethodDef,
122 n_signature
: nullable ASignature,
123 n_annotations
: nullable AAnnotations,
124 n_extern_calls
: nullable AExternCalls,
125 n_extern_code_block
: nullable AExternCodeBlock,
126 n_block
: AExpr): AMethPropdef
128 return new AMethPropdef.make
(n_visibility
, tk_redef
, mmethoddef
, n_signature
, n_annotations
, n_extern_calls
, n_extern_code_block
, n_block
)
131 # Make a new or with two expr
132 fun make_or
(right_expr
: AExpr, left_expr
: AExpr): AOrExpr
134 return new AOrExpr.make
(right_expr
,left_expr
)
137 # Make a new and with two expr
138 fun make_and
(right_expr
: AExpr, left_expr
: AExpr): AAndExpr
140 return new AAndExpr.make
(right_expr
,left_expr
)
143 # Make a new parenthesis expr
144 fun make_parenthesis
(expr
: AExpr, annotations
: nullable AAnnotations): AParExpr
146 return new AParExpr.make
(expr
,annotations
)
149 # Make a new message super
150 fun make_super_call
(args
: nullable Array[AExpr], n_qualified
: nullable AQualified): ASuperExpr
152 return new ASuperExpr.make
(args
,n_qualified
)
156 fun make_return
(expr
: nullable AExpr): AReturnExpr
158 return new AReturnExpr.make
(expr
)
163 # Return a new variable read that contains the value of the expression
164 # This method take care efficiently of creating and initalising an anonymous local variable
166 # Note: since this method do side-effects (AST replacement), there could be unexpected effects when used as
167 # argument of other methods related to AST transformations.
168 fun make_var_read
: AVarExpr
170 var variable
= self.variable_cache
171 if variable
== null then
172 assert parent
!= null
173 var place
= detach_with_placeholder
174 variable
= new Variable("")
175 variable
.declared_type
= self.mtype
176 var nvar
= new AVarAssignExpr.make
(variable
, self)
177 place
.replace_with
(nvar
)
178 self.variable_cache
= variable
180 return new AVarExpr.make
(variable
, variable
.declared_type
.as(not null))
183 private var variable_cache
: nullable Variable
185 # The `detach` method completely remove the node in the parent.
186 # However, sometime, it is useful to keep the emplacement of the removed child.
188 # The standard use case is the insertion of a node between a parent `p` and a child `p.c`.
189 # To create the new node `n`, we need to attach the child to it.
190 # But, to put `n` where `c` was in `p`, the place has to be remembered.
195 # var h = c.detach_with_placeholder
196 # var n = astbuilder.make_XXX(c)
199 fun detach_with_placeholder
: AExpr
201 var h
= new APlaceholderExpr.make
207 # Add `expr` at the end of the block
209 # REQUIRE: self isa ABlockExpr
211 # Note: this method, aimed to `ABlockExpr` is promoted to `AExpr` because of the limitations of the hierarchies generated by sablecc3
214 print
"add not implemented in {inspect}"
218 redef fun accept_ast_validation
(v
)
221 if mtype
== null and not is_typed
then
222 #debug "TYPING: untyped expression"
227 # A placeholder for a `AExpr` node
228 # Instances are transiantly used to mark some specific emplacements in the AST
229 # during complex transformations.
231 # Their must not appear in a valid AST
233 # @see AExpr::detach_with_placeholder
234 class APlaceholderExpr
240 redef fun accept_ast_validation
(v
)
243 debug
"PARENT: remaining placeholder"
247 redef class AReturnExpr
248 private init make
(expr
: nullable AExpr)
250 self.init_areturnexpr
(null, expr
)
254 redef class ASuperExpr
255 private init make
(args
: nullable Array[AExpr], n_qualified
: nullable AQualified)
257 var n_args
= new AListExprs
259 n_args
.n_exprs
.add_all
(args
)
261 self.init_asuperexpr
(n_qualified
, new TKwsuper, n_args
)
266 private init make
(expr
: AExpr, annotations
: nullable AAnnotations)
268 self.location
= expr
.location
269 self.init_aparexpr
(new TOpar, expr
, new TCpar, annotations
)
274 private init make
(right_expr
: AExpr, left_expr
: AExpr)
276 self.init_aorexpr
(right_expr
,new TKwor,left_expr
)
281 private init make
(right_expr
: AExpr, left_expr
: AExpr)
283 self.init_aandexpr
(right_expr
,new TKwand ,left_expr
)
287 redef class AMethPropdef
288 private init make
(n_visibility
: nullable APublicVisibility,
289 tk_redef
: nullable TKwredef,
290 mmethoddef
: MMethodDef,
291 n_signature
: nullable ASignature,
292 n_annotations
: nullable AAnnotations,
293 n_extern_calls
: nullable AExternCalls,
294 n_extern_code_block
: nullable AExternCodeBlock,
295 n_block
: nullable AExpr)
298 n_tid
.text
= mmethoddef
.name
299 var n_methid
= new AIdMethid.init_aidmethid
(n_tid
)
300 if n_signature
== null then n_signature
= new ASignature
301 if n_visibility
== null then n_visibility
= new APublicVisibility
302 self.init_amethpropdef
(null,tk_redef
,n_visibility
,new TKwmeth,null,null,null,n_methid
,n_signature
,n_annotations
,n_extern_calls
,n_extern_code_block
,new TKwdo,n_block
,new TKwend)
303 self.mpropdef
= mmethoddef
307 redef class AAssertExpr
308 private init make
(n_id
: nullable TId , n_expr
: nullable AExpr , n_else
: nullable AExpr)
310 n_kwassert
= new TKwassert
312 if n_else
!= null then n_kwelse
= new TKwelse
313 self.init_aassertexpr
(n_kwassert
, n_id
, n_expr
, n_kwelse
, n_else
)
317 redef class ABlockExpr
329 redef class ALoopExpr
332 _n_kwloop
= new TKwloop
334 n_block
= new ABlockExpr
335 n_block
.is_typed
= true
349 n_block
= new ABlockExpr
350 n_block
.is_typed
= true
353 # Make a new break expression of the given do
354 fun make_break
: ABreakExpr
356 var escapemark
= self.break_mark
357 if escapemark
== null then
358 escapemark
= new EscapeMark(null)
359 self.break_mark
= escapemark
361 return new ABreakExpr.make
(escapemark
)
370 redef class ABreakExpr
371 private init make
(escapemark
: EscapeMark)
373 _n_kwbreak
= new TKwbreak
374 self.escapemark
= escapemark
375 escapemark
.escapes
.add
self
381 private init make
(condition
: AExpr, mtype
: nullable MType)
385 _n_expr
.parent
= self
386 _n_kwthen
= new TKwthen
387 _n_then
= new ABlockExpr.make
388 _n_kwelse
= new TKwelse
389 _n_else
= new ABlockExpr.make
398 var n_id
= new TClassid
399 var n_qid
= new AQclassid
405 redef class AIntegerExpr
406 private init make
(value
: Int, t
: MType)
409 self._n_integer
= new TInteger # dummy
415 private init make
(callsite
: CallSite, args
: nullable Array[AExpr])
417 _n_kwnew
= new TKwnew
418 _n_type
= new AType.make
419 _n_args
= new AListExprs
421 n_args
.n_exprs
.add_all
(args
)
423 self.callsite
= callsite
424 self.recvtype
= callsite
.recv
.as(MClassType)
425 if callsite
.mproperty
.is_new
then
426 self.mtype
= callsite
.msignature
.return_mtype
428 self.mtype
= callsite
.recv
434 redef class ACallExpr
435 private init make
(recv
: AExpr, callsite
: CallSite, args
: nullable Array[AExpr])
438 _n_args
= new AListExprs
440 _n_qid
.n_id
= new TId
441 _n_qid
.n_id
.text
= callsite
.mproperty
.name
443 self.n_args
.n_exprs
.add_all
(args
)
445 self.callsite
= callsite
446 self.mtype
= callsite
.msignature
.return_mtype
451 redef class AAttrExpr
452 private init make
(recv
: AExpr, attribute
: MAttribute, t
: MType)
457 mproperty
= attribute
462 redef class AAttrAssignExpr
463 private init make
(recv
: AExpr, attribute
: MAttribute, value
: AExpr)
470 _n_assign
= new TAssign
471 mproperty
= attribute
477 private init make
(v
: Variable, mtype
: MType)
485 redef class AVarAssignExpr
486 private init make
(v
: Variable, value
: AExpr)
491 _n_assign
= new TAssign
497 # Check the consitency of AST
498 class ASTValidationVisitor
500 redef fun visit
(node
)
502 node
.accept_ast_validation
(self)
504 private var path
= new CircularArray[ANode]
505 private var seen
= new HashSet[ANode]
509 # Recursively validate a AST node.
510 # This ensure that location and parenting are defined and coherent.
512 # After complex low-level AST manipulation and construction,
513 # it is recommended to call it.
515 # Note: this just instantiate and run an `ASTValidationVisitor`.
518 (new ASTValidationVisitor).enter_visit
(self)
521 private fun accept_ast_validation
(v
: ASTValidationVisitor)
523 var parent
= self.parent
526 if path
.length
> 0 then
527 var path_parent
= v
.path
.first
528 if parent
== null then
529 self.parent
= path_parent
530 #debug "PARENT: expected parent: {path_parent}"
532 else if parent
!= path_parent
then
533 self.parent
= path_parent
534 if v
.seen
.has
(self) then
535 debug
"DUPLICATE (NOTATREE): already seen node with parent {parent} now with {path_parent}."
538 debug
"PARENT: expected parent: {path_parent}, got {parent}"
543 if not isset _location
then
544 #debug "LOCATION: unlocated node {v.path.join(", ")}"
545 _location
= self.parent
.location
554 redef class AAnnotation
555 redef fun accept_ast_validation
(v
)
557 # Do not enter in annotations