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
22 intrude import modelbuilder_base
23 intrude import modelize_property
25 # General factory to build semantic nodes in the AST of expressions
27 # The module used as reference for the building
28 # It is used to gather types and other stuff
31 # The anchor used for some mechanism relying on types
32 var anchor
: nullable MClassType
34 # Check mmodule to avoid a new instantiation of ASTBuilder
35 fun check_mmodule
(mmodule
: MModule)
37 if self.mmodule
!= mmodule
then self.mmodule
= mmodule
40 # Make a new Int literal
41 fun make_int
(value
: Int): AIntegerExpr
43 return new AIntegerExpr.make
(value
, mmodule
.int_type
)
46 # Make a new instantiation
47 fun make_new
(callsite
: CallSite, args
: nullable Array[AExpr]): ANewExpr
49 return new ANewExpr.make
(callsite
, args
)
52 # Make a new message send
53 fun make_call
(recv
: AExpr, callsite
: CallSite, args
: nullable Array[AExpr]): ACallExpr
55 return new ACallExpr.make
(recv
, callsite
, args
)
58 # Make a new, empty, sequence of statements
59 fun make_block
: ABlockExpr
61 return new ABlockExpr.make
64 # Make a new, empty, loop of statements
65 fun make_loop
: ALoopExpr
67 return new ALoopExpr.make
70 # Make a new variable read
71 fun make_var_read
(variable
: Variable, mtype
: MType): AVarExpr
73 return new AVarExpr.make
(variable
, mtype
)
76 # Make a new variable assignment
77 fun make_var_assign
(variable
: Variable, value
: AExpr): AVarAssignExpr
79 return new AVarAssignExpr.make
(variable
, value
)
82 # Make a new attribute read
83 fun make_attr_read
(recv
: AExpr, attribute
: MAttribute): AAttrExpr
85 var mtype
= attribute
.intro
.static_mtype
.resolve_for
(recv
.mtype
.as(not null), anchor
, mmodule
, true)
86 return new AAttrExpr.make
(recv
, attribute
, mtype
)
89 # Make a new attribute assignment
90 fun make_attr_assign
(recv
: AExpr, attribute
: MAttribute, value
: AExpr): AAttrAssignExpr
92 return new AAttrAssignExpr.make
(recv
, attribute
, value
)
95 # Make a new escapable block
98 return new ADoExpr.make
101 # Make a new break for a given escapemark
102 fun make_break
(escapemark
: EscapeMark): ABreakExpr
104 return new ABreakExpr.make
(escapemark
)
107 # Make a new conditional
108 # `mtype` is the return type of the whole if, in case of a ternary operator.
109 fun make_if
(condition
: AExpr, mtype
: nullable MType): AIfExpr
111 return new AIfExpr.make
(condition
, mtype
)
115 fun make_assert
(n_id
: nullable TId , n_expr
: AExpr , n_else
: nullable AExpr): AAssertExpr
117 return new AAssertExpr.make
(n_id
, n_expr
, n_else
)
121 fun make_method
(n_visibility
: nullable AVisibility,
122 tk_redef
: nullable TKwredef,
123 mmethoddef
: nullable MMethodDef,
124 n_signature
: nullable ASignature,
125 n_annotations
: nullable AAnnotations,
126 n_extern_calls
: nullable AExternCalls,
127 n_extern_code_block
: nullable AExternCodeBlock,
128 n_block
: nullable AExpr): AMethPropdef
130 return new AMethPropdef.make
(n_visibility
, tk_redef
, mmethoddef
, n_signature
, n_annotations
, n_extern_calls
, n_extern_code_block
, n_block
)
133 # Make a new or with two expr
134 fun make_or
(right_expr
: AExpr, left_expr
: AExpr): AOrExpr
136 return new AOrExpr.make
(right_expr
,left_expr
)
139 # Make a new and with two expr
140 fun make_and
(right_expr
: AExpr, left_expr
: AExpr): AAndExpr
142 return new AAndExpr.make
(right_expr
,left_expr
)
145 # Make a new parenthesis expr
146 fun make_parenthesis
(expr
: AExpr, annotations
: nullable AAnnotations): AParExpr
148 return new AParExpr.make
(expr
,annotations
)
151 # Make a new message super
152 fun make_super_call
(args
: nullable Array[AExpr], n_qualified
: nullable AQualified): ASuperExpr
154 return new ASuperExpr.make
(args
,n_qualified
)
158 fun make_return
(expr
: nullable AExpr): AReturnExpr
160 return new AReturnExpr.make
(expr
)
164 fun make_not
(expr
: AExpr): ANotExpr
166 return new ANotExpr.make
(expr
)
169 # Make a new attribute
170 fun make_attribute
(name
: String,
171 n_type
: nullable AType,
172 n_visibility
: nullable AVisibility,
173 initial_value
: nullable AExpr,
174 n_block
: nullable AExpr,
175 m_attributedef
: nullable MAttributeDef,
176 m_setterdef
: nullable MMethodDef,
177 m_getterdef
: nullable MMethodDef): AAttrPropdef
179 return new AAttrPropdef.make
(name
, n_type
, n_visibility
, initial_value
, n_block
, m_attributedef
, m_setterdef
, m_getterdef
)
182 # Make a new class (AStdClassdef)
183 fun make_class
(mclassdef
: nullable MClassDef,
184 n_visibility
: nullable AVisibility,
185 n_formaldefs
: Collection[AFormaldef],
186 n_extern_code_block
: nullable AExternCodeBlock,
187 n_propdefs
: Collection[APropdef],
188 n_classkind
: nullable AClasskind): AStdClassdef
190 return new AStdClassdef.make
(mclassdef
, n_visibility
, n_formaldefs
, n_extern_code_block
, n_propdefs
, n_classkind
)
193 fun make_var
(variable
: Variable, mtype
: MType): AVarExpr
195 return new AVarExpr.make
(variable
, mtype
)
198 # Make a call assignment i.e `a = 10`
199 fun make_call_assign
(recv
: AExpr, callsite
: CallSite, n_args
: nullable Collection[AExpr], n_value
: AExpr): ACallAssignExpr
201 return new ACallAssignExpr.make
(recv
, callsite
, n_args
, n_value
)
204 # Build a callsite to call the `mproperty` in the current method `caller_method`.
205 # `is_self_call` indicate if the method caller is a property of `self`
206 fun create_callsite
(modelbuilder
: ModelBuilder, caller_method
: AMethPropdef, mproperty
: MMethod, is_self_call
: Bool): CallSite
208 # FIXME It's not the better solution to call `TypeVisitor` here to build a model entity, but some make need to have a callsite
209 var type_visitor
= new TypeVisitor(modelbuilder
, caller_method
.mpropdef
.as(not null))
210 var callsite
= type_visitor
.build_callsite_by_property
(caller_method
, mproperty
.intro_mclassdef
.bound_mtype
, mproperty
, is_self_call
)
211 assert callsite
!= null
217 # Return a new variable read that contains the value of the expression
218 # This method take care efficiently of creating and initalising an anonymous local variable
220 # Note: since this method do side-effects (AST replacement), there could be unexpected effects when used as
221 # argument of other methods related to AST transformations.
222 fun make_var_read
: AVarExpr
224 var variable
= self.variable_cache
225 if variable
== null then
226 assert parent
!= null
227 var place
= detach_with_placeholder
228 variable
= new Variable("")
229 variable
.declared_type
= self.mtype
230 var nvar
= new AVarAssignExpr.make
(variable
, self)
231 place
.replace_with
(nvar
)
232 self.variable_cache
= variable
234 return new AVarExpr.make
(variable
, variable
.declared_type
.as(not null))
237 private var variable_cache
: nullable Variable
239 # The `detach` method completely remove the node in the parent.
240 # However, sometime, it is useful to keep the emplacement of the removed child.
242 # The standard use case is the insertion of a node between a parent `p` and a child `p.c`.
243 # To create the new node `n`, we need to attach the child to it.
244 # But, to put `n` where `c` was in `p`, the place has to be remembered.
249 # var h = c.detach_with_placeholder
250 # var n = astbuilder.make_XXX(c)
253 fun detach_with_placeholder
: AExpr
255 var h
= new APlaceholderExpr.make
261 # Add `expr` at the end of the block
263 # REQUIRE: self isa ABlockExpr
265 # Note: this method, aimed to `ABlockExpr` is promoted to `AExpr` because of the limitations of the hierarchies generated by sablecc3
268 print
"add not implemented in {inspect}"
272 redef fun accept_ast_validation
(v
)
275 if mtype
== null and not is_typed
then
276 #debug "TYPING: untyped expression"
281 # A placeholder for a `AExpr` node
282 # Instances are transiantly used to mark some specific emplacements in the AST
283 # during complex transformations.
285 # Their must not appear in a valid AST
287 # @see AExpr::detach_with_placeholder
288 class APlaceholderExpr
294 redef fun accept_ast_validation
(v
)
297 debug
"PARENT: remaining placeholder"
301 redef class ACallAssignExpr
302 private init make
(recv
: AExpr, callsite
: CallSite, args
: nullable Collection[AExpr], n_value
: AExpr)
305 _mtype
= callsite
.recv
307 var n_args
= new AListExprs
309 n_args
.n_exprs
.add_all
(args
)
313 n_qid
.n_id
.text
= callsite
.mproperty
.name
314 init_acallassignexpr
(recv
, n_qid
, n_args
, new TAssign, n_value
)
318 redef class AStdClassdef
319 private init make
(mclassdef
: nullable MClassDef,
320 n_visibility
: nullable AVisibility,
321 n_formaldefs
: Collection[Object],
322 n_extern_code_block
: nullable AExternCodeBlock,
323 n_propdefs
: Collection[Object],
324 n_classkind
: nullable AClasskind)
326 if n_visibility
== null then n_visibility
= new APublicVisibility
327 if n_classkind
== null then n_classkind
= new AConcreteClasskind.init_aconcreteclasskind
(new TKwclass)
328 var n_qid
= new AQclassid.init_aqclassid
(null, new TClassid)
329 init_astdclassdef
(null, null, n_visibility
, n_classkind
, n_qid
, null, n_formaldefs
, null, n_extern_code_block
, n_propdefs
, new TKwend)
330 _mclassdef
= mclassdef
331 _mclass
= mclassdef
.mclass
335 redef class AAttrPropdef
337 # Create a new `AAttrPropdef`
338 # Note: By default if the `AVisibility` is not given the visibility is set to public
339 private init make
(name
: String,
340 n_type
: nullable AType,
341 n_visibility
: nullable AVisibility,
342 initial_value
: nullable AExpr,
343 n_block
: nullable AExpr,
344 m_attributedef
: nullable MAttributeDef,
345 m_setterdef
: nullable MMethodDef,
346 m_getterdef
: nullable MMethodDef)
349 if n_type
!= null then mtype
= n_type
.mtype
350 # Define the visibility default is public
351 if n_visibility
== null then n_visibility
= new APublicVisibility
352 init_aattrpropdef
(null, null, n_visibility
, new TKwvar, new TId, n_type
, null, initial_value
, null, null , n_block
, null)
353 # Set the name of the attribute
355 _mpropdef
= m_attributedef
356 _mreadpropdef
= m_getterdef
357 _mwritepropdef
= m_setterdef
358 if initial_value
!= null or n_block
!= null then has_value
= true
359 if m_attributedef
!= null then self.location
= m_attributedef
.location
364 private init make
(expr
: AExpr)
366 self.init_anotexpr
(new TKwnot, expr
)
370 redef class AReturnExpr
371 private init make
(expr
: nullable AExpr)
373 self.init_areturnexpr
(null, expr
)
377 redef class ASuperExpr
378 private init make
(args
: nullable Array[AExpr], n_qualified
: nullable AQualified, mpropdef
: nullable MMethodDef)
380 var n_args
= new AListExprs
382 n_args
.n_exprs
.add_all
(args
)
385 self.init_asuperexpr
(n_qualified
, new TKwsuper, n_args
)
390 private init make
(right_expr
: AExpr, left_expr
: AExpr)
392 self.init_aorexpr
(right_expr
,new TKwor,left_expr
)
397 private init make
(right_expr
: AExpr, left_expr
: AExpr)
399 self.init_aandexpr
(right_expr
,new TKwand ,left_expr
)
403 redef class AMethPropdef
404 private init make
(n_visibility
: nullable AVisibility,
405 tk_redef
: nullable TKwredef,
406 mmethoddef
: nullable MMethodDef,
407 n_signature
: nullable ASignature,
408 n_annotations
: nullable AAnnotations,
409 n_extern_calls
: nullable AExternCalls,
410 n_extern_code_block
: nullable AExternCodeBlock,
411 n_block
: nullable AExpr)
414 var n_methid
= new AIdMethid.init_aidmethid
(n_tid
)
415 if n_signature
== null then n_signature
= new ASignature
416 if n_visibility
== null then n_visibility
= new APublicVisibility
417 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)
418 self.mpropdef
= mmethoddef
419 if mpropdef
!= null then self.location
= mmethoddef
.location
422 # Execute all method verification scope flow and typing.
423 # It also execute an ast validation to define all parents and all locations
424 fun do_all
(toolcontext
: ToolContext)
427 # FIXME: The `do_` usage it is maybe to much (verification...). Solution: Cut the `do_` methods into simpler parts
428 self.do_scope
(toolcontext
)
429 self.do_flow
(toolcontext
)
430 self.do_typing
(toolcontext
.modelbuilder
)
434 redef class AAssertExpr
435 private init make
(n_id
: nullable TId , n_expr
: nullable AExpr , n_else
: nullable AExpr)
437 n_kwassert
= new TKwassert
439 if n_else
!= null then n_kwelse
= new TKwelse
440 self.init_aassertexpr
(n_kwassert
, n_id
, n_expr
, n_kwelse
, n_else
)
444 redef class ALoopExpr
447 _n_kwloop
= new TKwloop
449 n_block
= new ABlockExpr
450 n_block
.is_typed
= true
464 n_block
= new ABlockExpr
465 n_block
.is_typed
= true
468 # Make a new break expression of the given do
469 fun make_break
: ABreakExpr
471 var escapemark
= self.break_mark
472 if escapemark
== null then
473 escapemark
= new EscapeMark(null)
474 self.break_mark
= escapemark
476 return new ABreakExpr.make
(escapemark
)
485 redef class ABreakExpr
486 private init make
(escapemark
: EscapeMark)
488 _n_kwbreak
= new TKwbreak
489 self.escapemark
= escapemark
490 escapemark
.escapes
.add
self
496 private init make
(condition
: AExpr, mtype
: nullable MType)
500 _n_expr
.parent
= self
501 _n_kwthen
= new TKwthen
502 _n_then
= new ABlockExpr.make
503 _n_kwelse
= new TKwelse
504 _n_else
= new ABlockExpr.make
506 if mtype
!= null then self.is_typed
= true
512 private init make
(t
: nullable MType)
514 var n_id
= new TClassid
515 var n_qid
= new AQclassid
521 redef fun clone
: SELF
523 return new AType.make
(mtype
)
529 redef class AIntegerExpr
531 private init make
(value
: nullable Numeric, t
: nullable MType)
534 if t
!= null then self.is_typed
= true
536 _n_integer
= new TInteger # dummy
539 redef fun clone
: SELF
541 return new AIntegerExpr.make
(value
, mtype
)
545 redef class AFloatExpr
547 private init make
(value
: nullable Float, t
: nullable MType)
550 if t
!= null then self.is_typed
= true
552 _n_float
= new TFloat # dummy
555 redef fun clone
: SELF
557 return new AFloatExpr.make
(value
, mtype
)
561 redef class ATrueExpr
563 private init make
(t
: nullable MType)
565 init_atrueexpr
(new TKwtrue, null)
567 if t
!= null then self.is_typed
= true
570 redef fun clone
: SELF
572 return new ATrueExpr.make
(mtype
)
576 redef class AFalseExpr
578 private init make
(t
: nullable MType)
580 init_afalseexpr
(new TKwfalse, null)
582 if t
!= null then self.is_typed
= true
585 redef fun clone
: SELF
587 return new AFalseExpr.make
(mtype
)
591 redef class ACharExpr
593 # `token_text` represent the real value as it's present in a file not only the char.
594 # `token_text` is needed if you want to use some methods (for exemple: `prefix`, `suffix` or `is_code_point methods`)
595 private init make
(value
: nullable Char, t
: nullable MType, token_text
: nullable String)
600 if token_text
!= null then n_char
.text
= token_text
601 if t
!= null then self.is_typed
= true
604 redef fun clone
: SELF
606 var self_clone
= new ACharExpr.make
(self.value
, mtype
, n_char
.text
)
612 private init make
(callsite
: CallSite, args
: nullable Array[AExpr])
614 _n_kwnew
= new TKwnew
615 _n_type
= new AType.make
616 _n_args
= new AListExprs
618 n_args
.n_exprs
.add_all
(args
)
620 self.callsite
= callsite
621 self.recvtype
= callsite
.recv
.as(MClassType)
622 if callsite
.mproperty
.is_new
then
623 self.mtype
= callsite
.msignature
.return_mtype
625 self.mtype
= callsite
.recv
631 redef class ACallExpr
632 private init make
(recv
: AExpr, callsite
: nullable CallSite, args
: nullable Array[AExpr])
635 _n_args
= new AListExprs
637 _n_qid
.n_id
= new TId
638 _n_qid
.n_id
.text
= callsite
.mproperty
.name
640 self.n_args
.n_exprs
.add_all
(args
)
643 if callsite
!= null then
644 self.callsite
= callsite
645 self.mtype
= callsite
.msignature
.return_mtype
651 redef class AAsNotnullExpr
652 private init make
(n_expr
: AExpr, t
: nullable MType)
654 init_aasnotnullexpr
(n_expr
, new TKwas, null, new TKwnot, new TKwnull, null)
656 if t
!= null then _is_typed
= true
660 redef class ANullExpr
662 private init make
(t
: nullable MType)
664 init_anullexpr
(new TKwnull, null)
666 if t
!= null then self.is_typed
= true
670 redef class ASelfExpr
672 private init make
(v
: nullable Variable, t
: nullable MType)
674 init_aselfexpr
(new TKwself, null)
676 if t
!= null then is_typed
=true
679 redef fun clone
: SELF
681 return new ASelfExpr.make
(self.variable
, self.mtype
)
685 redef class AImplicitSelfExpr
687 redef fun clone
: SELF
689 var self_clone
= new AImplicitSelfExpr.make
(variable
, mtype
)
690 self_clone
.is_sys
= is_sys
696 redef class AAttrExpr
697 private init make
(recv
: AExpr, attribute
: nullable MAttribute, t
: nullable MType)
702 _mproperty
= attribute
704 if t
!= null then _is_typed
= true
708 redef class AAttrAssignExpr
709 private init make
(recv
: AExpr, attribute
: nullable MAttribute, value
: AExpr)
716 _n_assign
= new TAssign
717 _mproperty
= attribute
723 private init make
(v
: nullable Variable, t
: nullable MType)
726 if v
!= null then _n_id
.text
= v
.name
729 if t
!= null then is_typed
= true
733 redef class AVarAssignExpr
734 private init make
(v
: nullable Variable, value
: AExpr)
737 if v
!= null then _n_id
.text
= v
.name
740 _n_assign
= new TAssign
743 if _mtype
!= null then _is_typed
= true
747 redef class ASignature
749 init make_from_msignature
(msignature
: MSignature)
751 var nparams
= new Array[AParam]
752 for mparam
in msignature
.mparameters
do
753 var variable
= new Variable(mparam
.name
)
754 variable
.declared_type
= mparam
.mtype
755 n_params
.add
(new AParam.make
(variable
, new AType.make
(mparam
.mtype
)))
757 var return_type
= null
758 if msignature
.return_mtype
!= null then return_type
= new AType.make
(msignature
.return_mtype
)
759 init_asignature
(null, nparams
, null, return_type
)
762 redef fun clone
: SELF
765 if ntype
!= null then ntype
= n_type
.clone
766 return new ASignature.init_asignature
(null, n_params
.clone
, null, ntype
)
772 private init make
(v
: nullable Variable, t
: nullable AType)
775 if v
!= null then _n_id
.text
= v
.name
780 redef fun clone
: SELF
783 if ntype
!= null then ntype
= n_type
.clone
784 return new AParam.make
(variable
, ntype
)
788 redef class ABlockExpr
789 private init make
(t
: nullable MType)
803 fun add_all
(exprs
: Array[AExpr])
810 redef fun clone
: SELF
812 var clone
= new ABlockExpr.make
(mtype
)
813 for expr
in self.n_expr
do
814 clone
.add
(expr
.clone
)
820 redef class AQclassid
821 redef fun clone
: SELF
823 return new AQclassid.init_aqclassid
(n_qualified
.clone
, n_id
)
827 redef class AQualified
828 redef fun clone
: SELF
830 return new AQualified.init_aqualified
(n_id
.clone
, n_classid
)
835 redef fun clone
: SELF
837 var clone_n_qualified
= n_qualified
838 if n_qualified
!= null then clone_n_qualified
= n_qualified
.clone
839 return new AQid.init_aqid
(clone_n_qualified
, n_id
.clone
)
844 redef fun clone
: SELF
846 return new TId.init_tk
(location
)
851 private init make
(expr
: AExpr, annotations
: nullable AAnnotations)
853 self.init_aparexpr
(new TOpar, expr
, new TCpar, annotations
)
857 # Check the consitency of AST
858 class ASTValidationVisitor
860 redef fun visit
(node
)
862 node
.accept_ast_validation
(self)
864 private var path
= new CircularArray[ANode]
865 private var seen
= new HashSet[ANode]
871 redef fun clone
: SELF
873 var clone_anodes
= new ANodes[E
](self.parent
)
875 clone_anodes
.add
(node
.clone
)
884 redef fun clone
: SELF
886 # By default the clone abort to avoid surprises
887 print
"The clone method is not implemented for the `{self.class_name}` class"
890 # Recursively validate a AST node.
891 # This ensure that location and parenting are defined and coherent.
893 # After complex low-level AST manipulation and construction,
894 # it is recommended to call it.
896 # Note: this just instantiate and run an `ASTValidationVisitor`.
899 (new ASTValidationVisitor).enter_visit
(self)
902 private fun accept_ast_validation
(v
: ASTValidationVisitor)
904 var parent
= self.parent
907 if path
.length
> 0 then
908 var path_parent
= v
.path
.first
909 if parent
== null then
910 self.parent
= path_parent
911 #debug "PARENT: expected parent: {path_parent}"
913 else if parent
!= path_parent
then
914 self.parent
= path_parent
915 if v
.seen
.has
(self) then
916 debug
"DUPLICATE (NOTATREE): already seen node with parent {parent} now with {path_parent}."
919 debug
"PARENT: expected parent: {path_parent}, got {parent}"
924 if not isset _location
then
925 #debug "LOCATION: unlocated node {v.path.join(", ")}"
926 _location
= self.parent
.location
935 redef class AAnnotation
937 redef fun accept_ast_validation
(v
)
939 # Do not enter in annotations
942 private init make
(n_args
: ANodes[AExpr])
944 _n_visibility
= new APublicVisibility