be3b2b0ff0c51216bb15f9911dcc75ce5f673265
[nit.git] / src / typing.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2012 Jean Privat <jean@pryen.org>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Intraprocedural resolution of static types and OO-services
18 # By OO-services we mean message sending, attribute access, instantiation, etc.
19 module typing
20
21 import flow
22 import modelize_property
23 import phase
24 import local_var_init
25
26 redef class ToolContext
27 var typing_phase: Phase = new TypingPhase(self, [flow_phase, modelize_property_phase, local_var_init_phase])
28 end
29
30 private class TypingPhase
31 super Phase
32 redef fun process_npropdef(npropdef) do npropdef.do_typing(toolcontext.modelbuilder)
33 end
34
35 private class TypeVisitor
36 var modelbuilder: ModelBuilder
37 var nclassdef: AClassdef
38 var mpropdef: MPropDef
39
40 var selfvariable: Variable = new Variable("self")
41
42 init(modelbuilder: ModelBuilder, nclassdef: AClassdef, mpropdef: MPropDef)
43 do
44 self.modelbuilder = modelbuilder
45 self.nclassdef = nclassdef
46 self.mpropdef = mpropdef
47
48 var mclass = nclassdef.mclassdef.mclass
49
50 var selfvariable = new Variable("self")
51 self.selfvariable = selfvariable
52 selfvariable.declared_type = mclass.mclass_type
53 end
54
55 fun mmodule: MModule do return self.nclassdef.mclassdef.mmodule
56
57 fun anchor: MClassType do return self.nclassdef.mclassdef.bound_mtype
58
59 fun anchor_to(mtype: MType): MType
60 do
61 var mmodule = self.nclassdef.mclassdef.mmodule
62 var anchor = self.nclassdef.mclassdef.bound_mtype
63 return mtype.anchor_to(mmodule, anchor)
64 end
65
66 fun is_subtype(sub, sup: MType): Bool
67 do
68 var mmodule = self.nclassdef.mclassdef.mmodule
69 var anchor = self.nclassdef.mclassdef.bound_mtype
70 return sub.is_subtype(mmodule, anchor, sup)
71 end
72
73 fun resolve_for(mtype, subtype: MType, for_self: Bool): MType
74 do
75 var mmodule = self.nclassdef.mclassdef.mmodule
76 var anchor = self.nclassdef.mclassdef.bound_mtype
77 #print "resolve_for {mtype} sub={subtype} forself={for_self} mmodule={mmodule} anchor={anchor}"
78 var res = mtype.resolve_for(subtype, anchor, mmodule, not for_self)
79 return res
80 end
81
82 # Retrieve the signature of a `MMethodDef` resolved for a specific call.
83 # This method is an helper to symplify the query on the model.
84 #
85 # Note: `for_self` indicates if the reciever is self or not.
86 # If yes, virtual types are not resolved.
87 fun resolve_signature_for(mmethoddef: MMethodDef, recv: MType, for_self: Bool): MSignature
88 do
89 return self.resolve_for(mmethoddef.msignature.as(not null), recv, for_self).as(MSignature)
90 end
91
92 # Check that `sub` is a subtype of `sup`.
93 # If `sub` is not a valud suptype, then display an error on `node` an return null.
94 # If `sub` is a safe subtype of `sup` then return `sub`.
95 # If `sub` is an insafe subtype (ie an imlicit cast is required), then return `sup`.
96 #
97 # The point of the return type is to determinate the usable type on an expression:
98 # If the suptype is safe, then the return type is the one on the expression typed by `sub`.
99 # Is the subtype is unsafe, then the return type is the one of an implicit cast on `sup`.
100 fun check_subtype(node: ANode, sub, sup: MType): nullable MType
101 do
102 if self.is_subtype(sub, sup) then return sub
103 if self.is_subtype(sub, self.anchor_to(sup)) then
104 # FIXME workarround to the current unsafe typing policy. To remove once fixed virtual types exists.
105 #node.debug("Unsafe typing: expected {sup}, got {sub}")
106 return sup
107 end
108 self.modelbuilder.error(node, "Type error: expected {sup}, got {sub}")
109 return null
110 end
111
112 # Visit an expression and do not care about the return value
113 fun visit_stmt(nexpr: nullable AExpr)
114 do
115 if nexpr == null then return
116 nexpr.accept_typing(self)
117 end
118
119 # Visit an expression and expects that it is not a statement
120 # Return the type of the expression
121 # Display an error and return null if:
122 # * the type cannot be determined or
123 # * `nexpr` is a statement
124 fun visit_expr(nexpr: AExpr): nullable MType
125 do
126 nexpr.accept_typing(self)
127 var mtype = nexpr.mtype
128 if mtype != null then return mtype
129 if not nexpr.is_typed then
130 if not self.modelbuilder.toolcontext.error_count > 0 then # check that there is really an error
131 if self.modelbuilder.toolcontext.verbose_level > 1 then
132 nexpr.debug("No return type but no error.")
133 end
134 end
135 return null # forward error
136 end
137 self.error(nexpr, "Type error: expected expression.")
138 return null
139 end
140
141 # Visit an expression and expect its static type is a least a `sup`
142 # Return the type of the expression or null if
143 # * the type cannot be determined or
144 # * `nexpr` is a statement or
145 # * `nexpr` is not a `sup`
146 fun visit_expr_subtype(nexpr: AExpr, sup: nullable MType): nullable MType
147 do
148 var sub = visit_expr(nexpr)
149 if sub == null then return null # Forward error
150
151 if sup == null then return null # Forward error
152
153 var res = check_subtype(nexpr, sub, sup)
154 if res != sub then
155 nexpr.implicit_cast_to = res
156 end
157 return res
158 end
159
160 # Visit an expression and expect its static type is a `Bool`
161 # Return the type of the expression or null if
162 # * the type cannot be determined or
163 # * `nexpr` is a statement or
164 # * `nexpr` is not a `Bool`
165 fun visit_expr_bool(nexpr: AExpr): nullable MType
166 do
167 return self.visit_expr_subtype(nexpr, self.type_bool(nexpr))
168 end
169
170
171 private fun visit_expr_cast(node: ANode, nexpr: AExpr, ntype: AType): nullable MType
172 do
173 var sub = visit_expr(nexpr)
174 if sub == null then return null # Forward error
175
176 var sup = self.resolve_mtype(ntype)
177 if sup == null then return null # Forward error
178
179 var mmodule = self.nclassdef.mclassdef.mmodule
180 var anchor = self.nclassdef.mclassdef.bound_mtype
181 if sup == sub then
182 self.modelbuilder.warning(node, "Warning: Expression is already a {sup}.")
183 else if self.is_subtype(sub, sup) and not sup.need_anchor then
184 self.modelbuilder.warning(node, "Warning: Expression is already a {sup} since it is a {sub}.")
185 end
186 return sup
187 end
188
189 fun try_get_mproperty_by_name2(anode: ANode, mtype: MType, name: String): nullable MProperty
190 do
191 return self.modelbuilder.try_get_mproperty_by_name2(anode, self.nclassdef.mclassdef.mmodule, mtype, name)
192 end
193
194 fun resolve_mtype(node: AType): nullable MType
195 do
196 return self.modelbuilder.resolve_mtype(self.nclassdef, node)
197 end
198
199 fun try_get_mclass(node: ANode, name: String): nullable MClass
200 do
201 var mmodule = self.nclassdef.mclassdef.mmodule
202 var mclass = modelbuilder.try_get_mclass_by_name(node, mmodule, name)
203 return mclass
204 end
205
206 fun get_mclass(node: ANode, name: String): nullable MClass
207 do
208 var mmodule = self.nclassdef.mclassdef.mmodule
209 var mclass = modelbuilder.try_get_mclass_by_name(node, mmodule, name)
210 if mclass == null then
211 self.modelbuilder.error(node, "Type Error: missing primitive class `{name}'.")
212 end
213 return mclass
214 end
215
216 fun type_bool(node: ANode): nullable MType
217 do
218 var mclass = self.get_mclass(node, "Bool")
219 if mclass == null then return null
220 return mclass.mclass_type
221 end
222
223 fun get_method(node: ANode, recvtype: MType, name: String, recv_is_self: Bool): nullable CallSite
224 do
225 var unsafe_type = self.anchor_to(recvtype)
226
227 #debug("recv: {recvtype} (aka {unsafe_type})")
228 if recvtype isa MNullType then
229 self.error(node, "Error: Method '{name}' call on 'null'.")
230 return null
231 end
232
233 var mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
234 if mproperty == null then
235 #self.modelbuilder.error(node, "Type error: property {name} not found in {unsafe_type} (ie {recvtype})")
236 if recv_is_self then
237 self.modelbuilder.error(node, "Error: Method or variable '{name}' unknown in {recvtype}.")
238 else
239 self.modelbuilder.error(node, "Error: Method '{name}' doesn't exists in {recvtype}.")
240 end
241 return null
242 end
243
244 assert mproperty isa MMethod
245 if mproperty.visibility == protected_visibility and not recv_is_self and self.mmodule.visibility_for(mproperty.intro_mclassdef.mmodule) < intrude_visibility then
246 self.modelbuilder.error(node, "Error: Method '{name}' is protected and can only acceded by self. {mproperty.intro_mclassdef.mmodule.visibility_for(self.mmodule)}")
247 return null
248 end
249
250 var propdefs = mproperty.lookup_definitions(self.mmodule, unsafe_type)
251 var mpropdef
252 if propdefs.length == 0 then
253 self.modelbuilder.error(node, "Type error: no definition found for property {name} in {unsafe_type}")
254 return null
255 else if propdefs.length == 1 then
256 mpropdef = propdefs.first
257 else
258 self.modelbuilder.warning(node, "Warning: confliting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
259 mpropdef = mproperty.intro
260 end
261
262
263 var msignature = self.resolve_signature_for(mpropdef, recvtype, recv_is_self)
264
265 var erasure_cast = false
266 var rettype = mpropdef.msignature.return_mtype
267 if not recv_is_self and rettype != null then
268 if rettype isa MNullableType then rettype = rettype.mtype
269 if rettype isa MParameterType then
270 var erased_rettype = msignature.return_mtype
271 assert erased_rettype != null
272 #node.debug("Erasure cast: Really a {rettype} but unsafely a {erased_rettype}")
273 erasure_cast = true
274 end
275 end
276
277 var callsite = new CallSite(node, recvtype, recv_is_self, mproperty, mpropdef, msignature, erasure_cast)
278 return callsite
279 end
280
281 # Visit the expressions of args and cheik their conformity with the corresponding typi in signature
282 # The point of this method is to handle varargs correctly
283 # Note: The signature must be correctly adapted
284 fun check_signature(node: ANode, args: Array[AExpr], name: String, msignature: MSignature): Bool
285 do
286 var vararg_rank = msignature.vararg_rank
287 if vararg_rank >= 0 then
288 if args.length < msignature.arity then
289 #self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected at least {msignature.arity}. Signature is {msignature}")
290 self.modelbuilder.error(node, "Error: arity mismatch; prototype is '{name}{msignature}'")
291 return false
292 end
293 else if args.length != msignature.arity then
294 self.modelbuilder.error(node, "Error: Incorrect number of parameters. Got {args.length}, expected {msignature.arity}. Signature is {msignature}")
295 return false
296 end
297
298 #debug("CALL {unsafe_type}.{msignature}")
299
300 var vararg_decl = args.length - msignature.arity
301 for i in [0..msignature.arity[ do
302 var j = i
303 if i == vararg_rank then continue # skip the vararg
304 if i > vararg_rank then
305 j = i + vararg_decl
306 end
307 var paramtype = msignature.mparameters[i].mtype
308 self.visit_expr_subtype(args[j], paramtype)
309 end
310 if vararg_rank >= 0 then
311 var varargs = new Array[AExpr]
312 var paramtype = msignature.mparameters[vararg_rank].mtype
313 for j in [vararg_rank..vararg_rank+vararg_decl] do
314 varargs.add(args[j])
315 self.visit_expr_subtype(args[j], paramtype)
316 end
317 end
318 return true
319 end
320
321 fun error(node: ANode, message: String)
322 do
323 self.modelbuilder.toolcontext.error(node.hot_location, message)
324 end
325
326 fun get_variable(node: AExpr, variable: Variable): nullable MType
327 do
328 var flow = node.after_flow_context
329 if flow == null then
330 self.error(node, "No context!")
331 return null
332 end
333
334 if flow.vars.has_key(variable) then
335 return flow.vars[variable]
336 else
337 #node.debug("*** START Collected for {variable}")
338 var mtypes = flow.collect_types(variable)
339 #node.debug("**** END Collected for {variable}")
340 if mtypes == null or mtypes.length == 0 then
341 return variable.declared_type
342 else if mtypes.length == 1 then
343 return mtypes.first
344 else
345 var res = merge_types(node,mtypes)
346 if res == null then res = variable.declared_type
347 return res
348 end
349 end
350 end
351
352 fun set_variable(node: AExpr, variable: Variable, mtype: nullable MType)
353 do
354 var flow = node.after_flow_context
355 assert flow != null
356
357 flow.set_var(variable, mtype)
358 end
359
360 fun merge_types(node: ANode, col: Array[nullable MType]): nullable MType
361 do
362 if col.length == 1 then return col.first
363 var res = new Array[nullable MType]
364 for t1 in col do
365 if t1 == null then continue # return null
366 var found = true
367 for t2 in col do
368 if t2 == null then continue # return null
369 if t2 isa MNullableType or t2 isa MNullType then
370 t1 = t1.as_nullable
371 end
372 if not is_subtype(t2, t1) then found = false
373 end
374 if found then
375 #print "merge {col.join(" ")} -> {t1}"
376 return t1
377 end
378 end
379 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
380 return null
381 end
382 end
383
384 # A specific method call site with its associated informations.
385 class CallSite
386 # The assiciated node for location
387 var node: ANode
388
389 # The statis type of the receiver
390 var recv: MType
391
392 # Is the receiver self?
393 # If "for_self", virtual types of the signature are keeped
394 # If "not_for_self", virtual type are erased
395 var recv_is_self: Bool
396
397 # The designated method
398 var mproperty: MMethod
399
400 # The statically designated method definition
401 # The most specif one, it is.
402 var mpropdef: MMethodDef
403
404 # The resolved signature for the receiver
405 var msignature: MSignature
406
407 # Is a implicit cast required on erasure typing policy?
408 var erasure_cast: Bool
409
410 private fun check_signature(v: TypeVisitor, args: Array[AExpr]): Bool
411 do
412 return v.check_signature(self.node, args, self.mproperty.name, self.msignature)
413 end
414 end
415
416 redef class Variable
417 # The declared type of the variable
418 var declared_type: nullable MType
419 end
420
421 redef class FlowContext
422 # Store changes of types because of type evolution
423 private var vars: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
424 private var cache: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
425
426 # Adapt the variable to a static type
427 # Warning1: do not modify vars directly.
428 # Warning2: sub-flow may have cached a unadapted variabial
429 private fun set_var(variable: Variable, mtype: nullable MType)
430 do
431 self.vars[variable] = mtype
432 self.cache.keys.remove(variable)
433 end
434
435 private fun collect_types(variable: Variable): nullable Array[nullable MType]
436 do
437 if cache.has_key(variable) then
438 return cache[variable]
439 end
440 var res: nullable Array[nullable MType] = null
441 if vars.has_key(variable) then
442 var mtype = vars[variable]
443 res = [mtype]
444 else if self.previous.is_empty then
445 # Root flow
446 res = [variable.declared_type]
447 else
448 for flow in self.previous do
449 if flow.is_unreachable then continue
450 var r2 = flow.collect_types(variable)
451 if r2 == null then continue
452 if res == null then
453 res = r2.to_a
454 else
455 for t in r2 do
456 if not res.has(t) then res.add(t)
457 end
458 end
459 end
460 end
461 cache[variable] = res
462 return res
463 end
464 end
465
466 redef class APropdef
467 # The entry point of the whole typing analysis
468 fun do_typing(modelbuilder: ModelBuilder)
469 do
470 end
471
472 # The variable associated to the reciever (if any)
473 var selfvariable: nullable Variable
474 end
475
476 redef class AConcreteMethPropdef
477 redef fun do_typing(modelbuilder: ModelBuilder)
478 do
479 var nclassdef = self.parent.as(AClassdef)
480 var mpropdef = self.mpropdef.as(not null)
481 var v = new TypeVisitor(modelbuilder, nclassdef, mpropdef)
482 self.selfvariable = v.selfvariable
483
484 var nblock = self.n_block
485 if nblock == null then return
486
487 var mmethoddef = self.mpropdef.as(not null)
488 for i in [0..mmethoddef.msignature.arity[ do
489 var mtype = mmethoddef.msignature.mparameters[i].mtype
490 if mmethoddef.msignature.vararg_rank == i then
491 var arrayclass = v.get_mclass(self.n_signature.n_params[i], "Array")
492 if arrayclass == null then return # Skip error
493 mtype = arrayclass.get_mtype([mtype])
494 end
495 var variable = self.n_signature.n_params[i].variable
496 assert variable != null
497 variable.declared_type = mtype
498 end
499 for i in [0..mmethoddef.msignature.mclosures.length[ do
500 var mclosure = mmethoddef.msignature.mclosures[i]
501 var variable = self.n_signature.n_closure_decls[i].variable
502 assert variable != null
503 variable.declared_type = mclosure.mtype
504 end
505 v.visit_stmt(nblock)
506
507 if not nblock.after_flow_context.is_unreachable and mmethoddef.msignature.return_mtype != null then
508 # We reach the end of the function without having a return, it is bad
509 v.error(self, "Control error: Reached end of function (a 'return' with a value was expected).")
510 end
511 end
512 end
513
514 redef class AAttrPropdef
515 redef fun do_typing(modelbuilder: ModelBuilder)
516 do
517 var nclassdef = self.parent.as(AClassdef)
518 var v = new TypeVisitor(modelbuilder, nclassdef, self.mpropdef.as(not null))
519 self.selfvariable = v.selfvariable
520
521 var nexpr = self.n_expr
522 if nexpr != null then
523 var mtype = self.mpropdef.static_mtype
524 v.visit_expr_subtype(nexpr, mtype)
525 end
526 end
527 end
528
529 ###
530
531 redef class AExpr
532 # The static type of the expression.
533 # null if self is a statement of in case of error
534 var mtype: nullable MType = null
535
536 # Is the statement correctly typed?
537 # Used to distinguish errors and statements when `mtype == null`
538 var is_typed: Bool = false
539
540 # If required, the following implicit cast `.as(XXX)`
541 # Such a cast may by required after evaluating the expression when
542 # a unsafe operation is detected (silently accepted by the Nit language).
543 # The attribute is computed by `check_subtype`
544 var implicit_cast_to: nullable MType = null
545
546 # Return the variable read (if any)
547 # Used to perform adaptive typing
548 fun its_variable: nullable Variable do return null
549
550 private fun accept_typing(v: TypeVisitor)
551 do
552 v.error(self, "no implemented accept_typing for {self.class_name}")
553 end
554 end
555
556 redef class ABlockExpr
557 redef fun accept_typing(v)
558 do
559 for e in self.n_expr do v.visit_stmt(e)
560 self.is_typed = true
561 end
562
563 # The type of a blockexpr is the one of the last expression (or null if empty)
564 redef fun mtype
565 do
566 if self.n_expr.is_empty then return null
567 return self.n_expr.last.mtype
568 end
569 end
570
571 redef class AVardeclExpr
572 redef fun accept_typing(v)
573 do
574 var variable = self.variable
575 if variable == null then return # Skip error
576
577 var ntype = self.n_type
578 var mtype: nullable MType
579 if ntype == null then
580 mtype = null
581 else
582 mtype = v.resolve_mtype(ntype)
583 if mtype == null then return # Skip error
584 end
585
586 var nexpr = self.n_expr
587 if nexpr != null then
588 if mtype != null then
589 v.visit_expr_subtype(nexpr, mtype)
590 else
591 mtype = v.visit_expr(nexpr)
592 if mtype == null then return # Skip error
593 end
594 end
595
596 var decltype = mtype
597 if mtype == null or mtype isa MNullType then
598 decltype = v.get_mclass(self, "Object").mclass_type.as_nullable
599 if mtype == null then mtype = decltype
600 end
601
602 variable.declared_type = decltype
603 v.set_variable(self, variable, mtype)
604
605 #debug("var {variable}: {mtype}")
606
607 self.is_typed = true
608 end
609 end
610
611 redef class AVarExpr
612 redef fun its_variable do return self.variable
613 redef fun accept_typing(v)
614 do
615 var variable = self.variable
616 if variable == null then return # Skip error
617
618 var mtype = v.get_variable(self, variable)
619 if mtype != null then
620 #debug("{variable} is {mtype}")
621 else
622 #debug("{variable} is untyped")
623 end
624
625 self.mtype = mtype
626 end
627 end
628
629 redef class AVarAssignExpr
630 redef fun accept_typing(v)
631 do
632 var variable = self.variable
633 assert variable != null
634
635 var mtype = v.visit_expr_subtype(n_value, variable.declared_type)
636
637 v.set_variable(self, variable, mtype)
638
639 self.is_typed = true
640 end
641 end
642
643 redef class AReassignFormExpr
644 # @depreciated use `reassign_callsite`
645 fun reassign_property: nullable MMethodDef do return self.reassign_callsite.mpropdef
646
647 # The method designed by the reassign operator.
648 var reassign_callsite: nullable CallSite
649
650 var read_type: nullable MType = null
651
652 # Determine the `reassign_property`
653 # `readtype` is the type of the reading of the left value.
654 # `writetype` is the type of the writing of the left value.
655 # (Because of `ACallReassignExpr`, both can be different.
656 # Return the static type of the value to store.
657 private fun resolve_reassignment(v: TypeVisitor, readtype, writetype: MType): nullable MType
658 do
659 var reassign_name: String
660 if self.n_assign_op isa APlusAssignOp then
661 reassign_name = "+"
662 else if self.n_assign_op isa AMinusAssignOp then
663 reassign_name = "-"
664 else
665 abort
666 end
667
668 self.read_type = readtype
669
670 if readtype isa MNullType then
671 v.error(self, "Error: Method '{reassign_name}' call on 'null'.")
672 return null
673 end
674
675 var callsite = v.get_method(self, readtype, reassign_name, false)
676 if callsite == null then return null # Skip error
677 self.reassign_callsite = callsite
678
679 var msignature = callsite.msignature
680 var rettype = msignature.return_mtype
681 assert msignature.arity == 1 and rettype != null
682
683 var value_type = v.visit_expr_subtype(self.n_value, msignature.mparameters.first.mtype)
684 if value_type == null then return null # Skip error
685
686 v.check_subtype(self, rettype, writetype)
687 return rettype
688 end
689 end
690
691 redef class AVarReassignExpr
692 redef fun accept_typing(v)
693 do
694 var variable = self.variable
695 assert variable != null
696
697 var readtype = v.get_variable(self, variable)
698 if readtype == null then return
699
700 var writetype = variable.declared_type
701 if writetype == null then return
702
703 var rettype = self.resolve_reassignment(v, readtype, writetype)
704
705 v.set_variable(self, variable, rettype)
706
707 self.is_typed = true
708 end
709 end
710
711
712 redef class AContinueExpr
713 redef fun accept_typing(v)
714 do
715 var nexpr = self.n_expr
716 if nexpr != null then
717 var mtype = v.visit_expr(nexpr)
718 end
719 self.is_typed = true
720 end
721 end
722
723 redef class ABreakExpr
724 redef fun accept_typing(v)
725 do
726 var nexpr = self.n_expr
727 if nexpr != null then
728 var mtype = v.visit_expr(nexpr)
729 end
730 self.is_typed = true
731 end
732 end
733
734 redef class AReturnExpr
735 redef fun accept_typing(v)
736 do
737 var nexpr = self.n_expr
738 var ret_type = v.mpropdef.as(MMethodDef).msignature.return_mtype
739 if nexpr != null then
740 if ret_type != null then
741 var mtype = v.visit_expr_subtype(nexpr, ret_type)
742 else
743 var mtype = v.visit_expr(nexpr)
744 v.error(self, "Error: Return with value in a procedure.")
745 end
746 else if ret_type != null then
747 v.error(self, "Error: Return without value in a function.")
748 end
749 self.is_typed = true
750 end
751 end
752
753 redef class AAbortExpr
754 redef fun accept_typing(v)
755 do
756 self.is_typed = true
757 end
758 end
759
760 redef class AIfExpr
761 redef fun accept_typing(v)
762 do
763 v.visit_expr_bool(n_expr)
764
765 v.visit_stmt(n_then)
766 v.visit_stmt(n_else)
767 self.is_typed = true
768 end
769 end
770
771 redef class AIfexprExpr
772 redef fun accept_typing(v)
773 do
774 v.visit_expr_bool(n_expr)
775
776 var t1 = v.visit_expr(n_then)
777 var t2 = v.visit_expr(n_else)
778
779 if t1 == null or t2 == null then
780 return # Skip error
781 end
782
783 var t = v.merge_types(self, [t1, t2])
784 if t == null then
785 v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
786 end
787 self.mtype = t
788 end
789 end
790
791 redef class ADoExpr
792 redef fun accept_typing(v)
793 do
794 v.visit_stmt(n_block)
795 self.is_typed = true
796 end
797 end
798
799 redef class AWhileExpr
800 redef fun accept_typing(v)
801 do
802 v.visit_expr_bool(n_expr)
803
804 v.visit_stmt(n_block)
805 self.is_typed = true
806 end
807 end
808
809 redef class ALoopExpr
810 redef fun accept_typing(v)
811 do
812 v.visit_stmt(n_block)
813 self.is_typed = true
814 end
815 end
816
817 redef class AForExpr
818 var coltype: nullable MClassType
819
820 var method_iterator: nullable MMethod
821 var method_is_ok: nullable MMethod
822 var method_item: nullable MMethod
823 var method_next: nullable MMethod
824 var method_key: nullable MMethod
825
826 private fun do_type_iterator(v: TypeVisitor, mtype: MType)
827 do
828 var objcla = v.get_mclass(self, "Object")
829 if objcla == null then return
830
831 var is_col = false
832 var is_map = false
833
834 var colcla = v.try_get_mclass(self, "Collection")
835 if colcla != null and v.is_subtype(mtype, colcla.get_mtype([objcla.mclass_type.as_nullable])) then
836 var coltype = mtype.supertype_to(v.mmodule, v.anchor, colcla)
837 self.coltype = coltype
838 var variables = self.variables
839 if variables.length != 1 then
840 v.error(self, "Type Error: Expected one variable")
841 else
842 variables.first.declared_type = coltype.arguments.first
843 end
844 is_col = true
845 end
846
847 var mapcla = v.try_get_mclass(self, "Map")
848 if mapcla != null and v.is_subtype(mtype, mapcla.get_mtype([objcla.mclass_type.as_nullable, objcla.mclass_type.as_nullable])) then
849 var coltype = mtype.supertype_to(v.mmodule, v.anchor, mapcla)
850 self.coltype = coltype
851 var variables = self.variables
852 if variables.length != 2 then
853 v.error(self, "Type Error: Expected two variables")
854 else
855 variables[0].declared_type = coltype.arguments[0]
856 variables[1].declared_type = coltype.arguments[1]
857 end
858 is_map = true
859 end
860
861 if is_col or is_map then
862 # get iterator method
863 var coltype = self.coltype.as(not null)
864 var itdef = v.get_method(self, coltype, "iterator", true)
865 if itdef == null then
866 v.error(self, "Type Error: Expected method 'iterator' in type {coltype}")
867 return
868 end
869 self.method_iterator = itdef.mproperty
870
871 # get iterator type
872 var ittype = itdef.msignature.return_mtype
873 if ittype == null then
874 v.error(self, "Type Error: Expected method 'iterator' to return an Iterator type")
875 return
876 end
877
878 # get methods is_ok, next, item
879 var ikdef = v.get_method(self, ittype, "is_ok", false)
880 if ikdef == null then
881 v.error(self, "Type Error: Expected method 'is_ok' in Iterator type {ittype}")
882 return
883 end
884 self.method_is_ok = ikdef.mproperty
885
886 var itemdef = v.get_method(self, ittype, "item", false)
887 if itemdef == null then
888 v.error(self, "Type Error: Expected method 'item' in Iterator type {ittype}")
889 return
890 end
891 self.method_item = itemdef.mproperty
892
893 var nextdef = v.get_method(self, ittype, "next", false)
894 if nextdef == null then
895 v.error(self, "Type Error: Expected method 'next' in Iterator type {ittype}")
896 return
897 end
898 self.method_next = nextdef.mproperty
899
900 if is_map then
901 var keydef = v.get_method(self, ittype, "key", false)
902 if keydef == null then
903 v.error(self, "Type Error: Expected method 'key' in Iterator type {ittype}")
904 return
905 end
906 self.method_key = keydef.mproperty
907 end
908 return
909 end
910
911 v.modelbuilder.error(self, "NOT YET IMPLEMENTED: Do 'for' on {mtype}")
912 end
913
914 redef fun accept_typing(v)
915 do
916 var mtype = v.visit_expr(n_expr)
917 if mtype == null then return
918
919 self.do_type_iterator(v, mtype)
920
921 v.visit_stmt(n_block)
922 self.is_typed = true
923 end
924 end
925
926 redef class AAssertExpr
927 redef fun accept_typing(v)
928 do
929 v.visit_expr_bool(n_expr)
930
931 v.visit_stmt(n_else)
932 self.is_typed = true
933 end
934 end
935
936 redef class AOrExpr
937 redef fun accept_typing(v)
938 do
939 v.visit_expr_bool(n_expr)
940 v.visit_expr_bool(n_expr2)
941 self.mtype = v.type_bool(self)
942 end
943 end
944
945 redef class AImpliesExpr
946 redef fun accept_typing(v)
947 do
948 v.visit_expr_bool(n_expr)
949 v.visit_expr_bool(n_expr2)
950 self.mtype = v.type_bool(self)
951 end
952 end
953
954 redef class AAndExpr
955 redef fun accept_typing(v)
956 do
957 v.visit_expr_bool(n_expr)
958 v.visit_expr_bool(n_expr2)
959 self.mtype = v.type_bool(self)
960 end
961 end
962
963
964 redef class ANotExpr
965 redef fun accept_typing(v)
966 do
967 v.visit_expr_bool(n_expr)
968 self.mtype = v.type_bool(self)
969 end
970 end
971
972 redef class AOrElseExpr
973 redef fun accept_typing(v)
974 do
975 var t1 = v.visit_expr(n_expr)
976 var t2 = v.visit_expr(n_expr2)
977
978 if t1 == null or t2 == null then
979 return # Skip error
980 end
981
982 if t1 isa MNullableType then
983 t1 = t1.mtype
984 end
985
986 var t = v.merge_types(self, [t1, t2])
987 if t == null then
988 v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
989 end
990 self.mtype = t
991 end
992 end
993
994 redef class AEeExpr
995 redef fun accept_typing(v)
996 do
997 v.visit_expr(n_expr)
998 v.visit_expr(n_expr2)
999 self.mtype = v.type_bool(self)
1000 end
1001 end
1002
1003 redef class ATrueExpr
1004 redef fun accept_typing(v)
1005 do
1006 self.mtype = v.type_bool(self)
1007 end
1008 end
1009
1010 redef class AFalseExpr
1011 redef fun accept_typing(v)
1012 do
1013 self.mtype = v.type_bool(self)
1014 end
1015 end
1016
1017 redef class AIntExpr
1018 redef fun accept_typing(v)
1019 do
1020 var mclass = v.get_mclass(self, "Int")
1021 if mclass == null then return # Forward error
1022 self.mtype = mclass.mclass_type
1023 end
1024 end
1025
1026 redef class AFloatExpr
1027 redef fun accept_typing(v)
1028 do
1029 var mclass = v.get_mclass(self, "Float")
1030 if mclass == null then return # Forward error
1031 self.mtype = mclass.mclass_type
1032 end
1033 end
1034
1035 redef class ACharExpr
1036 redef fun accept_typing(v)
1037 do
1038 var mclass = v.get_mclass(self, "Char")
1039 if mclass == null then return # Forward error
1040 self.mtype = mclass.mclass_type
1041 end
1042 end
1043
1044 redef class AStringFormExpr
1045 redef fun accept_typing(v)
1046 do
1047 var mclass = v.get_mclass(self, "String")
1048 if mclass == null then return # Forward error
1049 self.mtype = mclass.mclass_type
1050 end
1051 end
1052
1053 redef class ASuperstringExpr
1054 redef fun accept_typing(v)
1055 do
1056 var mclass = v.get_mclass(self, "String")
1057 if mclass == null then return # Forward error
1058 self.mtype = mclass.mclass_type
1059 for nexpr in self.n_exprs do
1060 var t = v.visit_expr(nexpr)
1061 end
1062 end
1063 end
1064
1065 redef class AArrayExpr
1066 redef fun accept_typing(v)
1067 do
1068 var mtypes = new Array[nullable MType]
1069 for e in self.n_exprs.n_exprs do
1070 var t = v.visit_expr(e)
1071 if t == null then
1072 return # Skip error
1073 end
1074 mtypes.add(t)
1075 end
1076 var mtype = v.merge_types(self, mtypes)
1077 if mtype == null then
1078 v.error(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1079 return
1080 end
1081 var mclass = v.get_mclass(self, "Array")
1082 if mclass == null then return # Forward error
1083 self.mtype = mclass.get_mtype([mtype])
1084 end
1085 end
1086
1087 redef class ARangeExpr
1088 redef fun accept_typing(v)
1089 do
1090 var discrete_class = v.get_mclass(self, "Discrete")
1091 if discrete_class == null then return # Forward error
1092 var discrete_type = discrete_class.intro.bound_mtype
1093 var t1 = v.visit_expr_subtype(self.n_expr, discrete_type)
1094 var t2 = v.visit_expr_subtype(self.n_expr2, discrete_type)
1095 if t1 == null or t2 == null then return
1096 var mclass = v.get_mclass(self, "Range")
1097 if mclass == null then return # Forward error
1098 if v.is_subtype(t1, t2) then
1099 self.mtype = mclass.get_mtype([t2])
1100 else if v.is_subtype(t2, t1) then
1101 self.mtype = mclass.get_mtype([t1])
1102 else
1103 v.error(self, "Type Error: Cannot create range: {t1} vs {t2}")
1104 end
1105 end
1106 end
1107
1108 redef class ANullExpr
1109 redef fun accept_typing(v)
1110 do
1111 self.mtype = v.mmodule.model.null_type
1112 end
1113 end
1114
1115 redef class AIsaExpr
1116 # The static type to cast to.
1117 # (different from the static type of the expression that is `Bool`).
1118 var cast_type: nullable MType
1119 redef fun accept_typing(v)
1120 do
1121 var mtype = v.visit_expr_cast(self, self.n_expr, self.n_type)
1122 self.cast_type = mtype
1123
1124 var variable = self.n_expr.its_variable
1125 if variable != null then
1126 var orig = self.n_expr.mtype
1127 var from = if orig != null then orig.to_s else "invalid"
1128 var to = if mtype != null then mtype.to_s else "invalid"
1129 #debug("adapt {variable}: {from} -> {to}")
1130 self.after_flow_context.when_true.set_var(variable, mtype)
1131 end
1132
1133 self.mtype = v.type_bool(self)
1134 end
1135 end
1136
1137 redef class AAsCastExpr
1138 redef fun accept_typing(v)
1139 do
1140 self.mtype = v.visit_expr_cast(self, self.n_expr, self.n_type)
1141 end
1142 end
1143
1144 redef class AAsNotnullExpr
1145 redef fun accept_typing(v)
1146 do
1147 var mtype = v.visit_expr(self.n_expr)
1148 if mtype isa MNullType then
1149 v.error(self, "Type error: as(not null) on null")
1150 return
1151 end
1152 if mtype isa MNullableType then
1153 self.mtype = mtype.mtype
1154 return
1155 end
1156 # TODO: warn on useless as not null
1157 self.mtype = mtype
1158 end
1159 end
1160
1161 redef class AProxyExpr
1162 redef fun accept_typing(v)
1163 do
1164 self.mtype = v.visit_expr(self.n_expr)
1165 end
1166 end
1167
1168 redef class ASelfExpr
1169 redef var its_variable: nullable Variable
1170 redef fun accept_typing(v)
1171 do
1172 var variable = v.selfvariable
1173 self.its_variable = variable
1174 self.mtype = v.get_variable(self, variable)
1175 end
1176 end
1177
1178 ## MESSAGE SENDING AND PROPERTY
1179
1180 redef class ASendExpr
1181 # @depreciated: use `callsite`
1182 fun mproperty: nullable MMethod do return callsite.mproperty
1183
1184 # The property invoked by the send.
1185 var callsite: nullable CallSite
1186
1187 redef fun accept_typing(v)
1188 do
1189 var recvtype = v.visit_expr(self.n_expr)
1190 var name = self.property_name
1191
1192 if recvtype == null then return # Forward error
1193 if recvtype isa MNullType then
1194 v.error(self, "Error: Method '{name}' call on 'null'.")
1195 return
1196 end
1197
1198 var callsite = v.get_method(self, recvtype, name, self.n_expr isa ASelfExpr)
1199 if callsite == null then return
1200 self.callsite = callsite
1201 var msignature = callsite.msignature
1202
1203 var args = compute_raw_arguments
1204 self.raw_arguments = args
1205
1206 callsite.check_signature(v, args)
1207
1208 if callsite.mproperty.is_init then
1209 var vmpropdef = v.mpropdef
1210 if not (vmpropdef isa MMethodDef and vmpropdef.mproperty.is_init) then
1211 v.error(self, "Can call a init only in another init")
1212 end
1213 end
1214
1215 var ret = msignature.return_mtype
1216 if ret != null then
1217 self.mtype = ret
1218 else
1219 self.is_typed = true
1220 end
1221
1222 if self.n_closure_defs.length == msignature.mclosures.length then
1223 for i in [0..self.n_closure_defs.length[ do
1224 self.n_closure_defs[i].accept_typing(v, msignature.mclosures[i])
1225 end
1226 else
1227 debug("closure: got {self.n_closure_defs.length}, want {msignature.mclosures.length}")
1228 end
1229 end
1230
1231 # The name of the property
1232 # Each subclass simply provide the correct name.
1233 private fun property_name: String is abstract
1234
1235 # An array of all arguments (excluding self)
1236 var raw_arguments: nullable Array[AExpr]
1237
1238 private fun compute_raw_arguments: Array[AExpr] is abstract
1239 end
1240
1241 redef class ABinopExpr
1242 redef fun compute_raw_arguments do return [n_expr2]
1243 end
1244 redef class AEqExpr
1245 redef fun property_name do return "=="
1246 redef fun accept_typing(v)
1247 do
1248 super
1249
1250 var variable = self.n_expr.its_variable
1251 if variable == null then return
1252 var mtype = self.n_expr2.mtype
1253 if not mtype isa MNullType then return
1254 var vartype = v.get_variable(self, variable)
1255 if not vartype isa MNullableType then return
1256 self.after_flow_context.when_true.set_var(variable, mtype)
1257 self.after_flow_context.when_false.set_var(variable, vartype.mtype)
1258 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1259 end
1260 end
1261 redef class ANeExpr
1262 redef fun property_name do return "!="
1263 redef fun accept_typing(v)
1264 do
1265 super
1266
1267 var variable = self.n_expr.its_variable
1268 if variable == null then return
1269 var mtype = self.n_expr2.mtype
1270 if not mtype isa MNullType then return
1271 var vartype = v.get_variable(self, variable)
1272 if not vartype isa MNullableType then return
1273 self.after_flow_context.when_false.set_var(variable, mtype)
1274 self.after_flow_context.when_true.set_var(variable, vartype.mtype)
1275 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1276 end
1277 end
1278 redef class ALtExpr
1279 redef fun property_name do return "<"
1280 end
1281 redef class ALeExpr
1282 redef fun property_name do return "<="
1283 end
1284 redef class ALlExpr
1285 redef fun property_name do return "<<"
1286 end
1287 redef class AGtExpr
1288 redef fun property_name do return ">"
1289 end
1290 redef class AGeExpr
1291 redef fun property_name do return ">="
1292 end
1293 redef class AGgExpr
1294 redef fun property_name do return ">>"
1295 end
1296 redef class APlusExpr
1297 redef fun property_name do return "+"
1298 end
1299 redef class AMinusExpr
1300 redef fun property_name do return "-"
1301 end
1302 redef class AStarshipExpr
1303 redef fun property_name do return "<=>"
1304 end
1305 redef class AStarExpr
1306 redef fun property_name do return "*"
1307 end
1308 redef class ASlashExpr
1309 redef fun property_name do return "/"
1310 end
1311 redef class APercentExpr
1312 redef fun property_name do return "%"
1313 end
1314
1315 redef class AUminusExpr
1316 redef fun property_name do return "unary -"
1317 redef fun compute_raw_arguments do return new Array[AExpr]
1318 end
1319
1320
1321 redef class ACallExpr
1322 redef fun property_name do return n_id.text
1323 redef fun compute_raw_arguments do return n_args.to_a
1324 end
1325
1326 redef class ACallAssignExpr
1327 redef fun property_name do return n_id.text + "="
1328 redef fun compute_raw_arguments
1329 do
1330 var res = n_args.to_a
1331 res.add(n_value)
1332 return res
1333 end
1334 end
1335
1336 redef class ABraExpr
1337 redef fun property_name do return "[]"
1338 redef fun compute_raw_arguments do return n_args.to_a
1339 end
1340
1341 redef class ABraAssignExpr
1342 redef fun property_name do return "[]="
1343 redef fun compute_raw_arguments
1344 do
1345 var res = n_args.to_a
1346 res.add(n_value)
1347 return res
1348 end
1349 end
1350
1351 redef class ASendReassignFormExpr
1352 # @depreciated use `write_callsite`
1353 fun write_mproperty: nullable MMethod do return write_callsite.mproperty
1354
1355 # The property invoked for the writing
1356 var write_callsite: nullable CallSite
1357
1358 redef fun accept_typing(v)
1359 do
1360 var recvtype = v.visit_expr(self.n_expr)
1361 var name = self.property_name
1362
1363 if recvtype == null then return # Forward error
1364 if recvtype isa MNullType then
1365 v.error(self, "Error: Method '{name}' call on 'null'.")
1366 return
1367 end
1368
1369 var for_self = self.n_expr isa ASelfExpr
1370 var callsite = v.get_method(self, recvtype, name, for_self)
1371
1372 if callsite == null then return
1373 self.callsite = callsite
1374
1375 var args = compute_raw_arguments
1376 self.raw_arguments = args
1377
1378 callsite.check_signature(v, args)
1379
1380 var readtype = callsite.msignature.return_mtype
1381 if readtype == null then
1382 v.error(self, "Error: {name} is not a function")
1383 return
1384 end
1385
1386 var wcallsite = v.get_method(self, recvtype, name + "=", self.n_expr isa ASelfExpr)
1387 if wcallsite == null then return
1388 self.write_callsite = wcallsite
1389
1390 var wtype = self.resolve_reassignment(v, readtype, wcallsite.msignature.mparameters.last.mtype)
1391 if wtype == null then return
1392
1393 args = args.to_a # duplicate so raw_arguments keeps only the getter args
1394 args.add(self.n_value)
1395 wcallsite.check_signature(v, args)
1396
1397 self.is_typed = true
1398 end
1399 end
1400
1401 redef class ACallReassignExpr
1402 redef fun property_name do return n_id.text
1403 redef fun compute_raw_arguments do return n_args.to_a
1404 end
1405
1406 redef class ABraReassignExpr
1407 redef fun property_name do return "[]"
1408 redef fun compute_raw_arguments do return n_args.to_a
1409 end
1410
1411 redef class AInitExpr
1412 redef fun property_name do return "init"
1413 redef fun compute_raw_arguments do return n_args.to_a
1414 end
1415
1416 redef class AExprs
1417 fun to_a: Array[AExpr] do return self.n_exprs.to_a
1418 end
1419
1420 ###
1421
1422 redef class ASuperExpr
1423 # The method to call if the super is in fact a 'super init call'
1424 # Note: if the super is a normal call-next-method, then this attribute is null
1425 var mproperty: nullable MMethod
1426
1427 redef fun accept_typing(v)
1428 do
1429 var recvtype = v.nclassdef.mclassdef.bound_mtype
1430 var mproperty = v.mpropdef.mproperty
1431 if not mproperty isa MMethod then
1432 v.error(self, "Error: super only usable in a method")
1433 return
1434 end
1435 var superprops = mproperty.lookup_super_definitions(v.mmodule, recvtype)
1436 if superprops.length == 0 then
1437 if mproperty.is_init and v.mpropdef.is_intro then
1438 process_superinit(v)
1439 return
1440 end
1441 v.error(self, "Error: No super method to call for {mproperty}.")
1442 return
1443 end
1444 # FIXME: covariance of return type in linear extension?
1445 var superprop = superprops.first
1446 assert superprop isa MMethodDef
1447
1448 var msignature = v.resolve_signature_for(superprop, recvtype, true)
1449 var args = self.n_args.to_a
1450 if args.length > 0 then
1451 v.check_signature(self, args, mproperty.name, msignature)
1452 end
1453 self.mtype = msignature.return_mtype
1454 self.is_typed = true
1455 end
1456
1457 private fun process_superinit(v: TypeVisitor)
1458 do
1459 var recvtype = v.nclassdef.mclassdef.bound_mtype
1460 var mproperty = v.mpropdef.mproperty
1461 var superprop: nullable MMethodDef = null
1462 for msupertype in v.nclassdef.mclassdef.supertypes do
1463 msupertype = msupertype.anchor_to(v.mmodule, recvtype)
1464 var errcount = v.modelbuilder.toolcontext.error_count
1465 var candidate = v.try_get_mproperty_by_name2(self, msupertype, mproperty.name).as(nullable MMethod)
1466 if candidate == null then
1467 if v.modelbuilder.toolcontext.error_count > errcount then return # Forard error
1468 continue # Try next super-class
1469 end
1470 if superprop != null and superprop.mproperty != candidate then
1471 v.error(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1472 return
1473 end
1474 var candidatedefs = candidate.lookup_definitions(v.mmodule, recvtype)
1475 if superprop != null then
1476 if superprop == candidatedefs.first then continue
1477 candidatedefs.add(superprop)
1478 end
1479 if candidatedefs.length > 1 then
1480 v.error(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1481 return
1482 end
1483 superprop = candidatedefs.first
1484 end
1485 if superprop == null then
1486 v.error(self, "Error: No super method to call for {mproperty}.")
1487 return
1488 end
1489 self.mproperty = superprop.mproperty
1490
1491 var args = self.n_args.to_a
1492 var msignature = v.resolve_signature_for(superprop, recvtype, true)
1493 if args.length > 0 then
1494 v.check_signature(self, args, mproperty.name, msignature)
1495 else
1496 # TODO: Check signature
1497 end
1498
1499 self.is_typed = true
1500 end
1501 end
1502
1503 ####
1504
1505 redef class ANewExpr
1506 # @depreciated use `callsite`
1507 fun mproperty: nullable MMethod do return self.callsite.mproperty
1508
1509 # The constructor invoked by the new.
1510 var callsite: nullable CallSite
1511
1512 redef fun accept_typing(v)
1513 do
1514 var recvtype = v.resolve_mtype(self.n_type)
1515 if recvtype == null then return
1516 self.mtype = recvtype
1517
1518 if not recvtype isa MClassType then
1519 if recvtype isa MNullableType then
1520 v.error(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1521 return
1522 else
1523 v.error(self, "Type error: cannot instantiate the formal type {recvtype}.")
1524 return
1525 end
1526 end
1527
1528 var name: String
1529 var nid = self.n_id
1530 if nid != null then
1531 name = nid.text
1532 else
1533 name = "init"
1534 end
1535 var callsite = v.get_method(self, recvtype, name, false)
1536 if callsite == null then return
1537
1538 self.callsite = callsite
1539
1540 if not callsite.mproperty.is_init_for(recvtype.mclass) then
1541 v.error(self, "Error: {name} is not a constructor.")
1542 return
1543 end
1544
1545 var args = n_args.to_a
1546 callsite.check_signature(v, args)
1547 end
1548 end
1549
1550 ####
1551
1552 redef class AAttrFormExpr
1553 # The attribute acceded.
1554 var mproperty: nullable MAttribute
1555
1556 # The static type of the attribute.
1557 var attr_type: nullable MType
1558
1559 # Resolve the attribute acceded.
1560 private fun resolve_property(v: TypeVisitor)
1561 do
1562 var recvtype = v.visit_expr(self.n_expr)
1563 if recvtype == null then return # Skip error
1564 var name = self.n_id.text
1565 if recvtype isa MNullType then
1566 v.error(self, "Error: Attribute '{name}' access on 'null'.")
1567 return
1568 end
1569
1570 var unsafe_type = v.anchor_to(recvtype)
1571 var mproperty = v.try_get_mproperty_by_name2(self, unsafe_type, name)
1572 if mproperty == null then
1573 v.modelbuilder.error(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1574 return
1575 end
1576 assert mproperty isa MAttribute
1577 self.mproperty = mproperty
1578
1579 var mpropdefs = mproperty.lookup_definitions(v.mmodule, unsafe_type)
1580 assert mpropdefs.length == 1
1581 var mpropdef = mpropdefs.first
1582 var attr_type = mpropdef.static_mtype.as(not null)
1583 attr_type = v.resolve_for(attr_type, recvtype, self.n_expr isa ASelfExpr)
1584 self.attr_type = attr_type
1585 end
1586 end
1587
1588 redef class AAttrExpr
1589 redef fun accept_typing(v)
1590 do
1591 self.resolve_property(v)
1592 self.mtype = self.attr_type
1593 end
1594 end
1595
1596
1597 redef class AAttrAssignExpr
1598 redef fun accept_typing(v)
1599 do
1600 self.resolve_property(v)
1601 var mtype = self.attr_type
1602
1603 v.visit_expr_subtype(self.n_value, mtype)
1604 self.is_typed = true
1605 end
1606 end
1607
1608 redef class AAttrReassignExpr
1609 redef fun accept_typing(v)
1610 do
1611 self.resolve_property(v)
1612 var mtype = self.attr_type
1613 if mtype == null then return # Skip error
1614
1615 self.resolve_reassignment(v, mtype, mtype)
1616
1617 self.is_typed = true
1618 end
1619 end
1620
1621 redef class AIssetAttrExpr
1622 redef fun accept_typing(v)
1623 do
1624 self.resolve_property(v)
1625 var mtype = self.attr_type
1626 if mtype == null then return # Skip error
1627
1628 var recvtype = self.n_expr.mtype.as(not null)
1629 var bound = v.resolve_for(mtype, recvtype, false)
1630 if bound isa MNullableType then
1631 v.error(self, "Error: isset on a nullable attribute.")
1632 end
1633 self.mtype = v.type_bool(self)
1634 end
1635 end
1636
1637 ###
1638
1639 redef class AClosureCallExpr
1640 redef fun accept_typing(v)
1641 do
1642 var variable = self.variable
1643 if variable == null then return # Skip error
1644
1645 var recvtype = v.nclassdef.mclassdef.bound_mtype
1646 var msignature = variable.declared_type.as(not null)
1647 msignature = v.resolve_for(msignature, recvtype, false).as(MSignature)
1648
1649 var args = n_args.to_a
1650 v.check_signature(self, args, variable.name, msignature)
1651
1652 self.is_typed = true
1653 self.mtype = msignature.return_mtype
1654 end
1655 end
1656
1657 redef class AClosureDef
1658 var mclosure: nullable MParameter
1659
1660 private fun accept_typing(v: TypeVisitor, mparameter: MParameter)
1661 do
1662 var variables = self.variables
1663 if variables == null then return
1664
1665 self.mclosure = mparameter
1666 var msignature = mparameter.mtype.as(MSignature)
1667
1668 if msignature.arity != variables.length then
1669 v.error(self, "Type error: closure {mparameter.name} expects {msignature.arity} parameters, {variables.length} given")
1670 return
1671 end
1672
1673 for i in [0..variables.length[ do
1674 variables[i].declared_type = msignature.mparameters[i].mtype
1675 end
1676
1677 v.visit_stmt(self.n_expr)
1678 end
1679 end
1680
1681 ###
1682
1683 redef class ADebugTypeExpr
1684 redef fun accept_typing(v)
1685 do
1686 var expr = v.visit_expr(self.n_expr)
1687 if expr == null then return
1688 var unsafe = v.anchor_to(expr)
1689 var ntype = self.n_type
1690 var mtype = v.resolve_mtype(ntype)
1691 if mtype != null and mtype != expr then
1692 var umtype = v.anchor_to(mtype)
1693 v.modelbuilder.warning(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
1694 end
1695 self.is_typed = true
1696 end
1697 end