Merge: Astbuilder improvement
[nit.git] / src / astbuilder.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Instantiation and transformation of semantic nodes in the AST of expressions and statements
16 module astbuilder
17
18 intrude import semantize::typing
19 intrude import literal
20 intrude import parser
21 intrude import semantize::scope
22 intrude import modelbuilder_base
23 intrude import modelize_property
24
25 # General factory to build semantic nodes in the AST of expressions
26 class ASTBuilder
27 # The module used as reference for the building
28 # It is used to gather types and other stuff
29 var mmodule: MModule
30
31 # The anchor used for some mechanism relying on types
32 var anchor: nullable MClassType
33
34 # Check mmodule to avoid a new instantiation of ASTBuilder
35 fun check_mmodule(mmodule: MModule)
36 do
37 if self.mmodule != mmodule then self.mmodule = mmodule
38 end
39
40 # Make a new Int literal
41 fun make_int(value: Int): AIntegerExpr
42 do
43 return new AIntegerExpr.make(value, mmodule.int_type)
44 end
45
46 # Make a new instantiation
47 fun make_new(callsite: CallSite, args: nullable Array[AExpr]): ANewExpr
48 do
49 return new ANewExpr.make(callsite, args)
50 end
51
52 # Make a new message send
53 fun make_call(recv: AExpr, callsite: CallSite, args: nullable Array[AExpr]): ACallExpr
54 do
55 return new ACallExpr.make(recv, callsite, args)
56 end
57
58 # Make a new, empty, sequence of statements
59 fun make_block: ABlockExpr
60 do
61 return new ABlockExpr.make
62 end
63
64 # Make a new, empty, loop of statements
65 fun make_loop: ALoopExpr
66 do
67 return new ALoopExpr.make
68 end
69
70 # Make a new variable read
71 fun make_var_read(variable: Variable, mtype: MType): AVarExpr
72 do
73 return new AVarExpr.make(variable, mtype)
74 end
75
76 # Make a new variable assignment
77 fun make_var_assign(variable: Variable, value: AExpr): AVarAssignExpr
78 do
79 return new AVarAssignExpr.make(variable, value)
80 end
81
82 # Make a new attribute read
83 fun make_attr_read(recv: AExpr, attribute: MAttribute): AAttrExpr
84 do
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)
87 end
88
89 # Make a new attribute assignment
90 fun make_attr_assign(recv: AExpr, attribute: MAttribute, value: AExpr): AAttrAssignExpr
91 do
92 return new AAttrAssignExpr.make(recv, attribute, value)
93 end
94
95 # Make a new escapable block
96 fun make_do: ADoExpr
97 do
98 return new ADoExpr.make
99 end
100
101 # Make a new break for a given escapemark
102 fun make_break(escapemark: EscapeMark): ABreakExpr
103 do
104 return new ABreakExpr.make(escapemark)
105 end
106
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
110 do
111 return new AIfExpr.make(condition, mtype)
112 end
113
114 # Make a new assert
115 fun make_assert(n_id : nullable TId , n_expr : AExpr , n_else : nullable AExpr): AAssertExpr
116 do
117 return new AAssertExpr.make(n_id , n_expr , n_else)
118 end
119
120 # Make a new method
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
129 do
130 return new AMethPropdef.make(n_visibility, tk_redef, mmethoddef, n_signature, n_annotations, n_extern_calls, n_extern_code_block, n_block)
131 end
132
133 # Make a new or with two expr
134 fun make_or(right_expr: AExpr, left_expr: AExpr): AOrExpr
135 do
136 return new AOrExpr.make(right_expr,left_expr)
137 end
138
139 # Make a new and with two expr
140 fun make_and(right_expr: AExpr, left_expr: AExpr): AAndExpr
141 do
142 return new AAndExpr.make(right_expr,left_expr)
143 end
144
145 # Make a new parenthesis expr
146 fun make_parenthesis(expr: AExpr, annotations: nullable AAnnotations): AParExpr
147 do
148 return new AParExpr.make(expr,annotations)
149 end
150
151 # Make a new message super
152 fun make_super_call(args: nullable Array[AExpr], n_qualified: nullable AQualified): ASuperExpr
153 do
154 return new ASuperExpr.make(args,n_qualified)
155 end
156
157 # Make a new return
158 fun make_return(expr: nullable AExpr): AReturnExpr
159 do
160 return new AReturnExpr.make(expr)
161 end
162
163 # Make a new not
164 fun make_not(expr: AExpr): ANotExpr
165 do
166 return new ANotExpr.make(expr)
167 end
168
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
178 do
179 return new AAttrPropdef.make(name, n_type, n_visibility, initial_value, n_block, m_attributedef, m_setterdef, m_getterdef)
180 end
181
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
189 do
190 return new AStdClassdef.make(mclassdef, n_visibility, n_formaldefs, n_extern_code_block, n_propdefs, n_classkind)
191 end
192
193 fun make_var(variable: Variable, mtype: MType): AVarExpr
194 do
195 return new AVarExpr.make(variable, mtype)
196 end
197
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
200 do
201 return new ACallAssignExpr.make(recv, callsite, n_args, n_value)
202 end
203
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_property: APropdef, mproperty: MMethod, is_self_call: Bool): CallSite
207 do
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_property.mpropdef.as(not null))
210 var callsite = type_visitor.build_callsite_by_property(caller_property, mproperty.intro_mclassdef.bound_mtype, mproperty, is_self_call)
211 assert callsite != null
212 return callsite
213 end
214 end
215
216 redef class AExpr
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
219 #
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
223 do
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
233 end
234 return new AVarExpr.make(variable, variable.declared_type.as(not null))
235 end
236
237 private var variable_cache: nullable Variable
238
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.
241 #
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.
245 #
246 # ~~~nitish
247 # var p: AExpr
248 # var c = p.c
249 # var h = c.detach_with_placeholder
250 # var n = astbuilder.make_XXX(c)
251 # h.replace_with(n)
252 # ~~~
253 fun detach_with_placeholder: AExpr
254 do
255 var h = new APlaceholderExpr.make
256 self.replace_with(h)
257 return h
258 end
259
260
261 # Add `expr` at the end of the block
262 #
263 # REQUIRE: self isa ABlockExpr
264 #
265 # Note: this method, aimed to `ABlockExpr` is promoted to `AExpr` because of the limitations of the hierarchies generated by sablecc3
266 fun add(expr: AExpr)
267 do
268 print "add not implemented in {inspect}"
269 abort
270 end
271
272 redef fun accept_ast_validation(v)
273 do
274 super
275 if mtype == null and not is_typed then
276 #debug "TYPING: untyped expression"
277 end
278 end
279 end
280
281 # A placeholder for a `AExpr` node
282 # Instances are transiantly used to mark some specific emplacements in the AST
283 # during complex transformations.
284 #
285 # Their must not appear in a valid AST
286 #
287 # @see AExpr::detach_with_placeholder
288 class APlaceholderExpr
289 super AExpr
290 private init make
291 do
292 end
293
294 redef fun accept_ast_validation(v)
295 do
296 super
297 debug "PARENT: remaining placeholder"
298 end
299 end
300
301 redef class ACallAssignExpr
302 private init make(recv: AExpr, callsite: CallSite, args: nullable Collection[AExpr], n_value: AExpr)
303 do
304 _callsite = callsite
305 _mtype = callsite.recv
306 _is_typed = true
307 var n_args = new AListExprs
308 if args != null then
309 n_args.n_exprs.add_all(args)
310 end
311 var n_qid = new AQid
312 n_qid.n_id = new TId
313 n_qid.n_id.text = callsite.mproperty.name
314 init_acallassignexpr(recv, n_qid, n_args, new TAssign, n_value)
315 end
316 end
317
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)
325 do
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
332 end
333 end
334
335 redef class AAttrPropdef
336
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)
347 do
348 # Set the model type
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
354 _n_id2.text = name
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
360 end
361 end
362
363 redef class ANotExpr
364 private init make(expr: AExpr)
365 do
366 self.init_anotexpr(new TKwnot, expr)
367 end
368 end
369
370 redef class AReturnExpr
371 private init make(expr: nullable AExpr)
372 do
373 self.init_areturnexpr(null, expr)
374 end
375 end
376
377 redef class ASuperExpr
378 private init make(args: nullable Array[AExpr], n_qualified: nullable AQualified, mpropdef: nullable MMethodDef)
379 do
380 var n_args = new AListExprs
381 if args != null then
382 n_args.n_exprs.add_all(args)
383 end
384 _mpropdef = mpropdef
385 self.init_asuperexpr(n_qualified, new TKwsuper, n_args)
386 end
387 end
388
389 redef class AOrExpr
390 private init make(right_expr: AExpr, left_expr: AExpr)
391 do
392 self.init_aorexpr(right_expr,new TKwor,left_expr)
393 end
394 end
395
396 redef class AAndExpr
397 private init make(right_expr: AExpr, left_expr: AExpr)
398 do
399 self.init_aandexpr(right_expr,new TKwand ,left_expr)
400 end
401 end
402
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)
412 do
413 var n_tid = new TId
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
420 end
421
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)
425 do
426 self.validate
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)
431 end
432 end
433
434 redef class AAssertExpr
435 private init make(n_id : nullable TId , n_expr : nullable AExpr , n_else : nullable AExpr)
436 do
437 n_kwassert = new TKwassert
438 n_kwelse = null
439 if n_else != null then n_kwelse = new TKwelse
440 self.init_aassertexpr(n_kwassert, n_id , n_expr , n_kwelse , n_else)
441 end
442 end
443
444 redef class ALoopExpr
445 private init make
446 do
447 _n_kwloop = new TKwloop
448 self.is_typed = true
449 n_block = new ABlockExpr
450 n_block.is_typed = true
451 end
452
453 redef fun add(expr)
454 do
455 n_block.add expr
456 end
457 end
458
459 redef class ADoExpr
460 private init make
461 do
462 _n_kwdo = new TKwdo
463 self.is_typed = true
464 n_block = new ABlockExpr
465 n_block.is_typed = true
466 end
467
468 # Make a new break expression of the given do
469 fun make_break: ABreakExpr
470 do
471 var escapemark = self.break_mark
472 if escapemark == null then
473 escapemark = new EscapeMark(null)
474 self.break_mark = escapemark
475 end
476 return new ABreakExpr.make(escapemark)
477 end
478
479 redef fun add(expr)
480 do
481 n_block.add expr
482 end
483 end
484
485 redef class ABreakExpr
486 private init make(escapemark: EscapeMark)
487 do
488 _n_kwbreak = new TKwbreak
489 self.escapemark = escapemark
490 escapemark.escapes.add self
491 self.is_typed = true
492 end
493 end
494
495 redef class AIfExpr
496 private init make(condition: AExpr, mtype: nullable MType)
497 do
498 _n_kwif = new TKwif
499 _n_expr = condition
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
505 self.mtype = mtype
506 if mtype != null then self.is_typed = true
507 end
508 end
509
510 redef class AType
511
512 private init make(t: nullable MType)
513 do
514 var n_id = new TClassid
515 var n_qid = new AQclassid
516 n_qid.n_id = n_id
517 _n_qid = n_qid
518 _mtype = t
519 end
520
521 redef fun clone: SELF
522 do
523 return new AType.make(mtype)
524 end
525 end
526
527 # Primitive type
528
529 redef class AIntegerExpr
530
531 private init make(value: nullable Numeric, t: nullable MType)
532 do
533 _mtype = t
534 if t != null then self.is_typed = true
535 _value = value
536 _n_integer = new TInteger # dummy
537 end
538
539 redef fun clone: SELF
540 do
541 return new AIntegerExpr.make(value, mtype)
542 end
543 end
544
545 redef class AFloatExpr
546
547 private init make(value: nullable Float, t: nullable MType)
548 do
549 _mtype = t
550 if t != null then self.is_typed = true
551 _value = value
552 _n_float = new TFloat # dummy
553 end
554
555 redef fun clone: SELF
556 do
557 return new AFloatExpr.make(value, mtype)
558 end
559 end
560
561 redef class ATrueExpr
562
563 private init make(t: nullable MType)
564 do
565 init_atrueexpr(new TKwtrue, null)
566 _mtype = t
567 if t != null then self.is_typed = true
568 end
569
570 redef fun clone: SELF
571 do
572 return new ATrueExpr.make(mtype)
573 end
574 end
575
576 redef class AFalseExpr
577
578 private init make(t: nullable MType)
579 do
580 init_afalseexpr(new TKwfalse, null)
581 _mtype = t
582 if t != null then self.is_typed = true
583 end
584
585 redef fun clone: SELF
586 do
587 return new AFalseExpr.make(mtype)
588 end
589 end
590
591 redef class ACharExpr
592
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)
596 do
597 _value = value
598 _mtype = t
599 _n_char = new TChar
600 if token_text != null then n_char.text = token_text
601 if t != null then self.is_typed = true
602 end
603
604 redef fun clone: SELF
605 do
606 var self_clone = new ACharExpr.make(self.value, mtype, n_char.text)
607 return self_clone
608 end
609 end
610
611 redef class ANewExpr
612 private init make(callsite: CallSite, args: nullable Array[AExpr])
613 do
614 _n_kwnew = new TKwnew
615 _n_type = new AType.make
616 _n_args = new AListExprs
617 if args != null then
618 n_args.n_exprs.add_all(args)
619 end
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
624 else
625 self.mtype = callsite.recv
626 end
627 self.is_typed = true
628 end
629 end
630
631 redef class ACallExpr
632 private init make(recv: AExpr, callsite: nullable CallSite, args: nullable Array[AExpr])
633 do
634 self._n_expr = recv
635 _n_args = new AListExprs
636 _n_qid = new AQid
637 _n_qid.n_id = new TId
638 _n_qid.n_id.text = callsite.mproperty.name
639 if args != null then
640 self.n_args.n_exprs.add_all(args)
641 end
642
643 if callsite != null then
644 self.callsite = callsite
645 self.mtype = callsite.msignature.return_mtype
646 self.is_typed = true
647 end
648 end
649 end
650
651 redef class AAsNotnullExpr
652 private init make(n_expr: AExpr, t: nullable MType)
653 do
654 init_aasnotnullexpr(n_expr, new TKwas, null, new TKwnot, new TKwnull, null)
655 _mtype = t
656 if t != null then _is_typed = true
657 end
658 end
659
660 redef class ANullExpr
661
662 private init make(t: nullable MType)
663 do
664 init_anullexpr(new TKwnull, null)
665 _mtype = t
666 if t != null then self.is_typed = true
667 end
668 end
669
670 redef class ASelfExpr
671
672 private init make(v: nullable Variable, t: nullable MType)
673 do
674 init_aselfexpr(new TKwself, null)
675 _mtype = t
676 if t != null then is_typed =true
677 end
678
679 redef fun clone: SELF
680 do
681 return new ASelfExpr.make(self.variable, self.mtype)
682 end
683 end
684
685 redef class AImplicitSelfExpr
686
687 redef fun clone: SELF
688 do
689 var self_clone = new AImplicitSelfExpr.make(variable, mtype)
690 self_clone.is_sys = is_sys
691 return self_clone
692 end
693 end
694
695
696 redef class AAttrExpr
697 private init make(recv: AExpr, attribute: nullable MAttribute, t: nullable MType)
698 do
699 _n_expr = recv
700 recv.parent = self
701 _n_id = new TAttrid
702 _mproperty = attribute
703 _mtype = t
704 if t != null then _is_typed = true
705 end
706 end
707
708 redef class AAttrAssignExpr
709 private init make(recv: AExpr, attribute: nullable MAttribute, value: AExpr)
710 do
711 _n_expr = recv
712 recv.parent = self
713 _n_id = new TAttrid
714 _n_value = value
715 value.parent = self
716 _n_assign = new TAssign
717 _mproperty = attribute
718 _mtype = value.mtype
719 end
720 end
721
722 redef class AVarExpr
723 private init make(v: nullable Variable, t: nullable MType)
724 do
725 _n_id = new TId
726 if v != null then _n_id.text = v.name
727 _variable = v
728 _mtype = t
729 if t != null then is_typed = true
730 end
731 end
732
733 redef class AVarAssignExpr
734 private init make(v: nullable Variable, value: AExpr)
735 do
736 _n_id = new TId
737 if v != null then _n_id.text = v.name
738 _n_value = value
739 value.parent = self
740 _n_assign = new TAssign
741 _variable = v
742 _mtype = value.mtype
743 if _mtype != null then _is_typed = true
744 end
745 end
746
747 redef class ASignature
748
749 init make_from_msignature(msignature: MSignature)
750 do
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)))
756 end
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)
760 end
761
762 redef fun clone: SELF
763 do
764 var ntype = n_type
765 if ntype != null then ntype = n_type.clone
766 return new ASignature.init_asignature(null, n_params.clone, null, ntype)
767 end
768 end
769
770 redef class AParam
771
772 private init make(v: nullable Variable, t: nullable AType)
773 do
774 _n_id = new TId
775 if v != null then _n_id.text = v.name
776 _variable = v
777 _n_type = t
778 end
779
780 redef fun clone: SELF
781 do
782 var ntype = n_type
783 if ntype != null then ntype = n_type.clone
784 return new AParam.make(variable, ntype)
785 end
786 end
787
788 redef class AFormaldef
789
790 private init make(mparameter: MParameterType, t: AType)
791 do
792 _n_id = new TClassid
793 _n_id.text = mparameter.name
794 _n_type = t
795 _mtype = mparameter
796 end
797 end
798
799 redef class ABlockExpr
800 private init make(t: nullable MType)
801 do
802 if t != null then
803 _mtype = t
804 _is_typed = true
805 end
806 end
807
808 redef fun add(expr)
809 do
810 n_expr.add expr
811 expr.parent = self
812 end
813
814 fun add_all(exprs: Array[AExpr])
815 do
816 for expr in exprs do
817 add(expr)
818 end
819 end
820
821 redef fun clone: SELF
822 do
823 var clone = new ABlockExpr.make(mtype)
824 for expr in self.n_expr do
825 clone.add(expr.clone)
826 end
827 return clone
828 end
829 end
830
831 redef class AQclassid
832 redef fun clone: SELF
833 do
834 return new AQclassid.init_aqclassid(n_qualified.clone, n_id)
835 end
836 end
837
838 redef class AQualified
839 redef fun clone: SELF
840 do
841 return new AQualified.init_aqualified(n_id.clone, n_classid)
842 end
843 end
844
845 redef class AQid
846 redef fun clone: SELF
847 do
848 var clone_n_qualified = n_qualified
849 if n_qualified != null then clone_n_qualified = n_qualified.clone
850 return new AQid.init_aqid(clone_n_qualified, n_id.clone)
851 end
852 end
853
854 redef class TId
855 redef fun clone: SELF
856 do
857 return new TId.init_tk(location)
858 end
859 end
860
861 redef class AParExpr
862 private init make(expr: AExpr, annotations: nullable AAnnotations)
863 do
864 self.init_aparexpr(new TOpar, expr, new TCpar, annotations)
865 end
866 end
867
868 # Check the consitency of AST
869 class ASTValidationVisitor
870 super Visitor
871 redef fun visit(node)
872 do
873 node.accept_ast_validation(self)
874 end
875 private var path = new CircularArray[ANode]
876 private var seen = new HashSet[ANode]
877 end
878
879 redef class ANodes
880 super Cloneable
881
882 redef fun clone: SELF
883 do
884 var clone_anodes = new ANodes[E](self.parent)
885 for node in self do
886 clone_anodes.add(node.clone)
887 end
888 return clone_anodes
889 end
890 end
891
892 redef class ANode
893 super Cloneable
894
895 redef fun clone: SELF
896 do
897 # By default the clone abort to avoid surprises
898 print "The clone method is not implemented for the `{self.class_name}` class"
899 abort
900 end
901 # Recursively validate a AST node.
902 # This ensure that location and parenting are defined and coherent.
903 #
904 # After complex low-level AST manipulation and construction,
905 # it is recommended to call it.
906 #
907 # Note: this just instantiate and run an `ASTValidationVisitor`.
908 fun validate
909 do
910 (new ASTValidationVisitor).enter_visit(self)
911 end
912
913 private fun accept_ast_validation(v: ASTValidationVisitor)
914 do
915 var parent = self.parent
916 var path = v.path
917
918 if path.length > 0 then
919 var path_parent = v.path.first
920 if parent == null then
921 self.parent = path_parent
922 #debug "PARENT: expected parent: {path_parent}"
923 v.seen.add(self)
924 else if parent != path_parent then
925 self.parent = path_parent
926 if v.seen.has(self) then
927 debug "DUPLICATE (NOTATREE): already seen node with parent {parent} now with {path_parent}."
928 else
929 v.seen.add(self)
930 debug "PARENT: expected parent: {path_parent}, got {parent}"
931 end
932 end
933 end
934
935 if not isset _location then
936 #debug "LOCATION: unlocated node {v.path.join(", ")}"
937 _location = self.parent.location
938 end
939
940 path.unshift(self)
941 visit_all(v)
942 path.shift
943 end
944 end
945
946 redef class AAnnotation
947
948 redef fun accept_ast_validation(v)
949 do
950 # Do not enter in annotations
951 end
952
953 private init make(n_args : ANodes[AExpr])
954 do
955 _n_visibility = new APublicVisibility
956 _n_args = n_args
957 end
958 end
959
960 redef class MEntity
961 # Build a ANode from `self`
962 #
963 # Allows the creation of an AST node from a model entity.
964 fun create_ast_representation(astbuilder: nullable ASTBuilder): ANode is abstract
965 end
966
967 redef class MPropDef
968 redef fun create_ast_representation(astbuilder: nullable ASTBuilder): APropdef is abstract
969 end
970
971 redef class MClassDef
972 redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AStdClassdef do
973 if astbuilder == null then astbuilder = new ASTBuilder(mmodule)
974 var n_propdefs = new Array[APropdef]
975 for mpropdef in self.mpropdefs do
976 n_propdefs.add(mpropdef.create_ast_representation(astbuilder))
977 end
978 var n_formaldefs = new Array[AFormaldef]
979 for mparameter in self.mclass.mparameters do n_formaldefs.add(mparameter.create_ast_representation(astbuilder))
980
981 return astbuilder.make_class(self, visibility.create_ast_representation(astbuilder), n_formaldefs, null, n_propdefs, null)
982 end
983 end
984
985 redef class MAttributeDef
986 redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AAttrPropdef do
987 if astbuilder == null then astbuilder = new ASTBuilder(mclassdef.mmodule)
988 var ntype = null
989 if self.static_mtype != null then ntype = static_mtype.create_ast_representation(astbuilder)
990 return astbuilder.make_attribute("_" + self.name, ntype, self.visibility.create_ast_representation(astbuilder), null, null, self, null, null)
991 end
992 end
993
994 redef class MMethodDef
995 redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AMethPropdef do
996 if astbuilder == null then astbuilder = new ASTBuilder(mclassdef.mmodule)
997 var tk_redef = null
998 if self.mproperty.intro != self then tk_redef = new TKwredef
999 var n_signature = if self.msignature == null then new ASignature else self.msignature.create_ast_representation(astbuilder)
1000 return astbuilder.make_method(visibility.create_ast_representation(astbuilder), tk_redef, self, n_signature)
1001 end
1002 end
1003
1004 redef class MVisibility
1005 fun create_ast_representation(astbuilder: nullable ASTBuilder): AVisibility do
1006 if self.to_s == "public" then
1007 return new APublicVisibility
1008 else if self.to_s == "private" then
1009 return new APrivateVisibility
1010 else if self.to_s == "protected" then
1011 return new AProtectedVisibility
1012 else
1013 return new AIntrudeVisibility
1014 end
1015 end
1016 end
1017
1018 redef class MSignature
1019 redef fun create_ast_representation(astbuilder: nullable ASTBuilder): ASignature do
1020 var nparams = new Array[AParam]
1021 for mparam in mparameters do nparams.add(mparam.create_ast_representation(astbuilder))
1022 var return_type = null
1023 if self.return_mtype != null then return_type = self.return_mtype.create_ast_representation(astbuilder)
1024 return new ASignature.init_asignature(null, nparams, null, return_type)
1025 end
1026 end
1027
1028 redef class MParameter
1029 redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AParam do
1030 var variable = new Variable(self.name)
1031 variable.declared_type = self.mtype
1032 return new AParam.make(variable, self.mtype.create_ast_representation(astbuilder))
1033 end
1034 end
1035
1036 redef class MParameterType
1037 redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AFormaldef do
1038 var n_type = super
1039 return new AFormaldef.make(self, n_type)
1040 end
1041 end
1042
1043 redef class MType
1044 redef fun create_ast_representation(astbuilder: nullable ASTBuilder): AType do
1045 return new AType.make(self)
1046 end
1047 end
1048
1049 redef class ModelBuilder
1050 # Try to get MMethod property if exist in the given mclassdef. return new `MMethod` if not exist.
1051 private fun get_mmethod(name: String, mclassdef: MClassDef, visibility: nullable MVisibility): MMethod do
1052 visibility = visibility or else public_visibility
1053 var mproperty = try_get_mproperty_by_name(null, mclassdef, name).as(nullable MMethod)
1054 if mproperty == null then mproperty = new MMethod(mclassdef, name, mclassdef.location, visibility)
1055 return mproperty
1056 end
1057
1058 # Creation of a new method (AST and model representation) with the given name.
1059 # See `create_method_from_property` for more information.
1060 fun create_method_from_name(name: String, mclassdef: MClassDef, is_abstract: Bool, msignature: nullable MSignature, visibility: nullable MVisibility): AMethPropdef do
1061 var mproperty = get_mmethod(name, mclassdef, visibility)
1062 return create_method_from_property(mproperty, mclassdef, is_abstract, msignature)
1063 end
1064
1065 # Creation of a new method (AST and model representation) with the given MMethod.
1066 # Take care, if `is_abstract == false` the AMethPropdef returned has an empty body (potential error if the given signature has an return type).
1067 fun create_method_from_property(mproperty: MMethod, mclassdef: MClassDef, is_abstract: Bool, msignature: nullable MSignature): AMethPropdef do
1068 var m_def = new MMethodDef(mclassdef, mproperty, mclassdef.location)
1069
1070 if msignature == null then msignature = new MSignature(new Array[MParameter])
1071
1072 m_def.msignature = msignature
1073 m_def.is_abstract = true
1074 var n_def = m_def.create_ast_representation
1075 # Association new npropdef to mpropdef
1076 unsafe_add_mpropdef2npropdef(m_def,n_def)
1077
1078 if not is_abstract then
1079 n_def.mpropdef.is_abstract = false
1080 n_def.n_block = new ABlockExpr.make
1081 end
1082
1083 return n_def
1084 end
1085
1086 # Creation of a new attribute (AST and model representation) with the given name.
1087 # See `create_attribute_from_property` for more information.
1088 fun create_attribute_from_name(name: String, mclassdef: MClassDef, mtype: MType, visibility: nullable MVisibility): AAttrPropdef do
1089 if visibility == null then visibility = public_visibility
1090 var mattribute = try_get_mproperty_by_name(null, mclassdef, name)
1091 if mattribute == null then mattribute = new MAttribute(mclassdef, name, mclassdef.location, visibility)
1092 return create_attribute_from_property(mattribute.as(MAttribute), mclassdef, mtype)
1093 end
1094
1095 # Creation of a new attribute (AST and model representation) with the given MAttribute.
1096 # See `create_attribute_from_propdef` for more information.
1097 fun create_attribute_from_property(mattribute: MAttribute, mclassdef: MClassDef, mtype: MType): AAttrPropdef do
1098 var attribut_def = new MAttributeDef(mclassdef, mattribute, mclassdef.location)
1099 attribut_def.static_mtype = mtype
1100 return create_attribute_from_propdef(attribut_def)
1101 end
1102
1103 # Creation of a new attribute (AST representation) with the given MAttributeDef.
1104 fun create_attribute_from_propdef(mattribut_def: MAttributeDef): AAttrPropdef
1105 is
1106 expect(mclassdef2node(mattribut_def.mclassdef) != null)
1107 do
1108 var n_attribute = mattribut_def.create_ast_representation
1109
1110 var nclass = mclassdef2node(mattribut_def.mclassdef)
1111
1112 n_attribute.location = mattribut_def.location
1113 n_attribute.validate
1114
1115 nclass.n_propdefs.unsafe_add_all([n_attribute])
1116 nclass.validate
1117
1118 n_attribute.build_read_property(self, mattribut_def.mclassdef)
1119 n_attribute.build_read_signature
1120
1121 mpropdef2npropdef[mattribut_def] = n_attribute
1122 return n_attribute
1123 end
1124
1125 # Creation of a new class (AST and model representation) with the given name.
1126 # `visibility` : Define the visibility of the method. If it's `null` the default is `public_visibility`
1127 # See `create_class_from_mclass` for more information.
1128 fun create_class_from_name(name: String, super_type: Array[MClassType], mmodule: MModule, visibility: nullable MVisibility): AStdClassdef do
1129 if visibility == null then visibility = public_visibility
1130 var mclass = try_get_mclass_by_name(null, mmodule, name)
1131 if mclass == null then mclass = new MClass(mmodule, name, mmodule.location, new Array[String], concrete_kind, visibility)
1132 return create_class_from_mclass(mclass, super_type, mmodule)
1133 end
1134
1135 # Creation of a new class (AST and model representation) with the given MClass.
1136 # This method creates a new concrete class definition `MClassDef`, and adds it to the class hierarchy.
1137 # See `create_class_from_mclassdef` for more information.
1138 fun create_class_from_mclass(mclass: MClass, super_type: Array[MClassType], mmodule: MModule): AStdClassdef do
1139 var mclassdef = new MClassDef(mmodule, mclass.mclass_type, mmodule.location)
1140 mclassdef.set_supertypes(super_type)
1141 mclassdef.add_in_hierarchy
1142
1143 return create_class_from_mclassdef(mclassdef, mmodule)
1144 end
1145
1146 # Creation of a new class (AST representation) with the given MClassDef.
1147 # Note all the properties of our MClassDef will also generate an AST representation.
1148 # Make an error if the attribute already has a representation in the modelbuilder.
1149 # This method also create the default constructor.
1150 fun create_class_from_mclassdef(mclassdef: MClassDef, mmodule: MModule): AStdClassdef do
1151 var n_classdef = mclassdef.create_ast_representation
1152 n_classdef.location = mclassdef.location
1153 n_classdef.validate
1154
1155 for n_propdef in n_classdef.n_propdefs do
1156 var mpropdef = n_propdef.mpropdef
1157 assert mpropdef != null
1158
1159 var p_npropdef = mpropdef2node(mpropdef)
1160 if p_npropdef != null then error(null, "The property `{mpropdef.name}` already has a representation in the AST.")
1161 unsafe_add_mpropdef2npropdef(mpropdef, n_propdef)
1162 end
1163
1164 process_default_constructors(n_classdef)
1165 unsafe_add_mclassdef2nclassdef(mclassdef, n_classdef)
1166
1167 return n_classdef
1168 end
1169 end