Merge: CallSite on AFor and ARange
[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 and not modelbuilder.toolcontext.opt_ignore_visibility.value then
246 self.modelbuilder.error(node, "Error: Method '{name}' is protected and can only acceded by self.")
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, mmodule, anchor, 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 static type of the receiver (possibly unresolved)
390 var recv: MType
391
392 # The module where the callsite is present
393 var mmodule: MModule
394
395 # The anchor to use with `recv` or `msignature`
396 var anchor: nullable MClassType
397
398 # Is the receiver self?
399 # If "for_self", virtual types of the signature are keeped
400 # If "not_for_self", virtual type are erased
401 var recv_is_self: Bool
402
403 # The designated method
404 var mproperty: MMethod
405
406 # The statically designated method definition
407 # The most specif one, it is.
408 var mpropdef: MMethodDef
409
410 # The resolved signature for the receiver
411 var msignature: MSignature
412
413 # Is a implicit cast required on erasure typing policy?
414 var erasure_cast: Bool
415
416 private fun check_signature(v: TypeVisitor, args: Array[AExpr]): Bool
417 do
418 return v.check_signature(self.node, args, self.mproperty.name, self.msignature)
419 end
420 end
421
422 redef class Variable
423 # The declared type of the variable
424 var declared_type: nullable MType
425 end
426
427 redef class FlowContext
428 # Store changes of types because of type evolution
429 private var vars: HashMap[Variable, nullable MType] = new HashMap[Variable, nullable MType]
430 private var cache: HashMap[Variable, nullable Array[nullable MType]] = new HashMap[Variable, nullable Array[nullable MType]]
431
432 # Adapt the variable to a static type
433 # Warning1: do not modify vars directly.
434 # Warning2: sub-flow may have cached a unadapted variabial
435 private fun set_var(variable: Variable, mtype: nullable MType)
436 do
437 self.vars[variable] = mtype
438 self.cache.keys.remove(variable)
439 end
440
441 private fun collect_types(variable: Variable): nullable Array[nullable MType]
442 do
443 if cache.has_key(variable) then
444 return cache[variable]
445 end
446 var res: nullable Array[nullable MType] = null
447 if vars.has_key(variable) then
448 var mtype = vars[variable]
449 res = [mtype]
450 else if self.previous.is_empty then
451 # Root flow
452 res = [variable.declared_type]
453 else
454 for flow in self.previous do
455 if flow.is_unreachable then continue
456 var r2 = flow.collect_types(variable)
457 if r2 == null then continue
458 if res == null then
459 res = r2.to_a
460 else
461 for t in r2 do
462 if not res.has(t) then res.add(t)
463 end
464 end
465 end
466 end
467 cache[variable] = res
468 return res
469 end
470 end
471
472 redef class APropdef
473 # The entry point of the whole typing analysis
474 fun do_typing(modelbuilder: ModelBuilder)
475 do
476 end
477
478 # The variable associated to the reciever (if any)
479 var selfvariable: nullable Variable
480 end
481
482 redef class AConcreteMethPropdef
483 redef fun do_typing(modelbuilder: ModelBuilder)
484 do
485 var nclassdef = self.parent.as(AClassdef)
486 var mpropdef = self.mpropdef.as(not null)
487 var v = new TypeVisitor(modelbuilder, nclassdef, mpropdef)
488 self.selfvariable = v.selfvariable
489
490 var nblock = self.n_block
491 if nblock == null then return
492
493 var mmethoddef = self.mpropdef.as(not null)
494 for i in [0..mmethoddef.msignature.arity[ do
495 var mtype = mmethoddef.msignature.mparameters[i].mtype
496 if mmethoddef.msignature.vararg_rank == i then
497 var arrayclass = v.get_mclass(self.n_signature.n_params[i], "Array")
498 if arrayclass == null then return # Skip error
499 mtype = arrayclass.get_mtype([mtype])
500 end
501 var variable = self.n_signature.n_params[i].variable
502 assert variable != null
503 variable.declared_type = 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 or 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 # The method designed by the reassign operator.
645 var reassign_callsite: nullable CallSite
646
647 var read_type: nullable MType = null
648
649 # Determine the `reassign_property`
650 # `readtype` is the type of the reading of the left value.
651 # `writetype` is the type of the writing of the left value.
652 # (Because of `ACallReassignExpr`, both can be different.
653 # Return the static type of the value to store.
654 private fun resolve_reassignment(v: TypeVisitor, readtype, writetype: MType): nullable MType
655 do
656 var reassign_name: String
657 if self.n_assign_op isa APlusAssignOp then
658 reassign_name = "+"
659 else if self.n_assign_op isa AMinusAssignOp then
660 reassign_name = "-"
661 else
662 abort
663 end
664
665 self.read_type = readtype
666
667 if readtype isa MNullType then
668 v.error(self, "Error: Method '{reassign_name}' call on 'null'.")
669 return null
670 end
671
672 var callsite = v.get_method(self, readtype, reassign_name, false)
673 if callsite == null then return null # Skip error
674 self.reassign_callsite = callsite
675
676 var msignature = callsite.msignature
677 var rettype = msignature.return_mtype
678 assert msignature.arity == 1 and rettype != null
679
680 var value_type = v.visit_expr_subtype(self.n_value, msignature.mparameters.first.mtype)
681 if value_type == null then return null # Skip error
682
683 v.check_subtype(self, rettype, writetype)
684 return rettype
685 end
686 end
687
688 redef class AVarReassignExpr
689 redef fun accept_typing(v)
690 do
691 var variable = self.variable
692 assert variable != null
693
694 var readtype = v.get_variable(self, variable)
695 if readtype == null then return
696
697 read_type = readtype
698
699 var writetype = variable.declared_type
700 if writetype == null then return
701
702 var rettype = self.resolve_reassignment(v, readtype, writetype)
703
704 v.set_variable(self, variable, rettype)
705
706 self.is_typed = true
707 end
708 end
709
710
711 redef class AContinueExpr
712 redef fun accept_typing(v)
713 do
714 var nexpr = self.n_expr
715 if nexpr != null then
716 var mtype = v.visit_expr(nexpr)
717 end
718 self.is_typed = true
719 end
720 end
721
722 redef class ABreakExpr
723 redef fun accept_typing(v)
724 do
725 var nexpr = self.n_expr
726 if nexpr != null then
727 var mtype = v.visit_expr(nexpr)
728 end
729 self.is_typed = true
730 end
731 end
732
733 redef class AReturnExpr
734 redef fun accept_typing(v)
735 do
736 var nexpr = self.n_expr
737 var ret_type = v.mpropdef.as(MMethodDef).msignature.return_mtype
738 if nexpr != null then
739 if ret_type != null then
740 var mtype = v.visit_expr_subtype(nexpr, ret_type)
741 else
742 var mtype = v.visit_expr(nexpr)
743 v.error(self, "Error: Return with value in a procedure.")
744 end
745 else if ret_type != null then
746 v.error(self, "Error: Return without value in a function.")
747 end
748 self.is_typed = true
749 end
750 end
751
752 redef class AAbortExpr
753 redef fun accept_typing(v)
754 do
755 self.is_typed = true
756 end
757 end
758
759 redef class AIfExpr
760 redef fun accept_typing(v)
761 do
762 v.visit_expr_bool(n_expr)
763
764 v.visit_stmt(n_then)
765 v.visit_stmt(n_else)
766 self.is_typed = true
767 end
768 end
769
770 redef class AIfexprExpr
771 redef fun accept_typing(v)
772 do
773 v.visit_expr_bool(n_expr)
774
775 var t1 = v.visit_expr(n_then)
776 var t2 = v.visit_expr(n_else)
777
778 if t1 == null or t2 == null then
779 return # Skip error
780 end
781
782 var t = v.merge_types(self, [t1, t2])
783 if t == null then
784 v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
785 end
786 self.mtype = t
787 end
788 end
789
790 redef class ADoExpr
791 redef fun accept_typing(v)
792 do
793 v.visit_stmt(n_block)
794 self.is_typed = true
795 end
796 end
797
798 redef class AWhileExpr
799 redef fun accept_typing(v)
800 do
801 v.visit_expr_bool(n_expr)
802
803 v.visit_stmt(n_block)
804 self.is_typed = true
805 end
806 end
807
808 redef class ALoopExpr
809 redef fun accept_typing(v)
810 do
811 v.visit_stmt(n_block)
812 self.is_typed = true
813 end
814 end
815
816 redef class AForExpr
817 var coltype: nullable MClassType
818
819 var method_iterator: nullable CallSite
820 var method_is_ok: nullable CallSite
821 var method_item: nullable CallSite
822 var method_next: nullable CallSite
823 var method_key: nullable CallSite
824
825 private fun do_type_iterator(v: TypeVisitor, mtype: MType)
826 do
827 if mtype isa MNullType then
828 v.error(self, "Type error: 'for' cannot iterate over 'null'")
829 return
830 end
831
832 # get obj class
833 var objcla = v.get_mclass(self, "Object")
834 if objcla == null then return
835
836 # check iterator method
837 var itdef = v.get_method(self, mtype, "iterator", true)
838 if itdef == null then
839 v.error(self, "Type Error: 'for' expects a type providing 'iterator' method, got '{mtype}'.")
840 return
841 end
842 self.method_iterator = itdef
843
844 # check that iterator return something
845 var ittype = itdef.msignature.return_mtype
846 if ittype == null then
847 v.error(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.")
848 return
849 end
850
851 # get iterator type
852 var colit_cla = v.try_get_mclass(self, "Iterator")
853 var mapit_cla = v.try_get_mclass(self, "MapIterator")
854 var is_col = false
855 var is_map = false
856
857 if colit_cla != null and v.is_subtype(ittype, colit_cla.get_mtype([objcla.mclass_type.as_nullable])) then
858 # Iterator
859 var coltype = ittype.supertype_to(v.mmodule, v.anchor, colit_cla)
860 var variables = self.variables
861 if variables.length != 1 then
862 v.error(self, "Type Error: 'for' expects only one variable when using 'Iterator'.")
863 else
864 variables.first.declared_type = coltype.arguments.first
865 end
866 is_col = true
867 end
868
869 if mapit_cla != null and v.is_subtype(ittype, mapit_cla.get_mtype([objcla.mclass_type, objcla.mclass_type.as_nullable])) then
870 # Map Iterator
871 var coltype = ittype.supertype_to(v.mmodule, v.anchor, mapit_cla)
872 var variables = self.variables
873 if variables.length != 2 then
874 v.error(self, "Type Error: 'for' expects two variables when using 'MapIterator'.")
875 else
876 variables[0].declared_type = coltype.arguments[0]
877 variables[1].declared_type = coltype.arguments[1]
878 end
879 is_map = true
880 end
881
882 if not is_col and not is_map then
883 v.error(self, "Type Error: 'for' expects method 'iterator' to return an 'Iterator' or 'MapIterator' type'.")
884 return
885 end
886
887 # anchor formal and virtual types
888 if mtype.need_anchor then mtype = v.anchor_to(mtype)
889
890 if mtype isa MNullableType then mtype = mtype.mtype
891 self.coltype = mtype.as(MClassType)
892
893 # get methods is_ok, next, item
894 var ikdef = v.get_method(self, ittype, "is_ok", false)
895 if ikdef == null then
896 v.error(self, "Type Error: 'for' expects a method 'is_ok' in 'Iterator' type {ittype}.")
897 return
898 end
899 self.method_is_ok = ikdef
900
901 var itemdef = v.get_method(self, ittype, "item", false)
902 if itemdef == null then
903 v.error(self, "Type Error: 'for' expects a method 'item' in 'Iterator' type {ittype}.")
904 return
905 end
906 self.method_item = itemdef
907
908 var nextdef = v.get_method(self, ittype, "next", false)
909 if nextdef == null then
910 v.error(self, "Type Error: 'for' expects a method 'next' in 'Iterator' type {ittype}.")
911 return
912 end
913 self.method_next = nextdef
914
915 if is_map then
916 var keydef = v.get_method(self, ittype, "key", false)
917 if keydef == null then
918 v.error(self, "Type Error: 'for' expects a method 'key' in 'Iterator' type {ittype}.")
919 return
920 end
921 self.method_key = keydef
922 end
923 end
924
925 redef fun accept_typing(v)
926 do
927 var mtype = v.visit_expr(n_expr)
928 if mtype == null then return
929
930 self.do_type_iterator(v, mtype)
931
932 v.visit_stmt(n_block)
933 self.is_typed = true
934 end
935 end
936
937 redef class AAssertExpr
938 redef fun accept_typing(v)
939 do
940 v.visit_expr_bool(n_expr)
941
942 v.visit_stmt(n_else)
943 self.is_typed = true
944 end
945 end
946
947 redef class AOrExpr
948 redef fun accept_typing(v)
949 do
950 v.visit_expr_bool(n_expr)
951 v.visit_expr_bool(n_expr2)
952 self.mtype = v.type_bool(self)
953 end
954 end
955
956 redef class AImpliesExpr
957 redef fun accept_typing(v)
958 do
959 v.visit_expr_bool(n_expr)
960 v.visit_expr_bool(n_expr2)
961 self.mtype = v.type_bool(self)
962 end
963 end
964
965 redef class AAndExpr
966 redef fun accept_typing(v)
967 do
968 v.visit_expr_bool(n_expr)
969 v.visit_expr_bool(n_expr2)
970 self.mtype = v.type_bool(self)
971 end
972 end
973
974
975 redef class ANotExpr
976 redef fun accept_typing(v)
977 do
978 v.visit_expr_bool(n_expr)
979 self.mtype = v.type_bool(self)
980 end
981 end
982
983 redef class AOrElseExpr
984 redef fun accept_typing(v)
985 do
986 var t1 = v.visit_expr(n_expr)
987 var t2 = v.visit_expr(n_expr2)
988
989 if t1 == null or t2 == null then
990 return # Skip error
991 end
992
993 if t1 isa MNullableType then
994 t1 = t1.mtype
995 end
996
997 var t = v.merge_types(self, [t1, t2])
998 if t == null then
999 t = v.mmodule.object_type
1000 if t2 isa MNullableType then
1001 t = t.as_nullable
1002 end
1003 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1004 end
1005 self.mtype = t
1006 end
1007 end
1008
1009 redef class ATrueExpr
1010 redef fun accept_typing(v)
1011 do
1012 self.mtype = v.type_bool(self)
1013 end
1014 end
1015
1016 redef class AFalseExpr
1017 redef fun accept_typing(v)
1018 do
1019 self.mtype = v.type_bool(self)
1020 end
1021 end
1022
1023 redef class AIntExpr
1024 redef fun accept_typing(v)
1025 do
1026 var mclass = v.get_mclass(self, "Int")
1027 if mclass == null then return # Forward error
1028 self.mtype = mclass.mclass_type
1029 end
1030 end
1031
1032 redef class AFloatExpr
1033 redef fun accept_typing(v)
1034 do
1035 var mclass = v.get_mclass(self, "Float")
1036 if mclass == null then return # Forward error
1037 self.mtype = mclass.mclass_type
1038 end
1039 end
1040
1041 redef class ACharExpr
1042 redef fun accept_typing(v)
1043 do
1044 var mclass = v.get_mclass(self, "Char")
1045 if mclass == null then return # Forward error
1046 self.mtype = mclass.mclass_type
1047 end
1048 end
1049
1050 redef class AStringFormExpr
1051 redef fun accept_typing(v)
1052 do
1053 var mclass = v.get_mclass(self, "String")
1054 if mclass == null then return # Forward error
1055 self.mtype = mclass.mclass_type
1056 end
1057 end
1058
1059 redef class ASuperstringExpr
1060 redef fun accept_typing(v)
1061 do
1062 var mclass = v.get_mclass(self, "String")
1063 if mclass == null then return # Forward error
1064 self.mtype = mclass.mclass_type
1065 for nexpr in self.n_exprs do
1066 var t = v.visit_expr(nexpr)
1067 end
1068 end
1069 end
1070
1071 redef class AArrayExpr
1072 redef fun accept_typing(v)
1073 do
1074 var mtypes = new Array[nullable MType]
1075 for e in self.n_exprs.n_exprs do
1076 var t = v.visit_expr(e)
1077 if t == null then
1078 return # Skip error
1079 end
1080 mtypes.add(t)
1081 end
1082 var mtype = v.merge_types(self, mtypes)
1083 if mtype == null then
1084 v.error(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1085 return
1086 end
1087 var mclass = v.get_mclass(self, "Array")
1088 if mclass == null then return # Forward error
1089 self.mtype = mclass.get_mtype([mtype])
1090 end
1091 end
1092
1093 redef class ARangeExpr
1094 var init_callsite: nullable CallSite
1095
1096 redef fun accept_typing(v)
1097 do
1098 var discrete_class = v.get_mclass(self, "Discrete")
1099 if discrete_class == null then return # Forward error
1100 var discrete_type = discrete_class.intro.bound_mtype
1101 var t1 = v.visit_expr_subtype(self.n_expr, discrete_type)
1102 var t2 = v.visit_expr_subtype(self.n_expr2, discrete_type)
1103 if t1 == null or t2 == null then return
1104 var mclass = v.get_mclass(self, "Range")
1105 if mclass == null then return # Forward error
1106 var mtype
1107 if v.is_subtype(t1, t2) then
1108 mtype = mclass.get_mtype([t2])
1109 else if v.is_subtype(t2, t1) then
1110 mtype = mclass.get_mtype([t1])
1111 else
1112 v.error(self, "Type Error: Cannot create range: {t1} vs {t2}")
1113 return
1114 end
1115
1116 self.mtype = mtype
1117
1118 # get the constructor
1119 var callsite
1120 if self isa ACrangeExpr then
1121 callsite = v.get_method(self, mtype, "init", false)
1122 else if self isa AOrangeExpr then
1123 callsite = v.get_method(self, mtype, "without_last", false)
1124 else
1125 abort
1126 end
1127 init_callsite = callsite
1128 end
1129 end
1130
1131 redef class ANullExpr
1132 redef fun accept_typing(v)
1133 do
1134 self.mtype = v.mmodule.model.null_type
1135 end
1136 end
1137
1138 redef class AIsaExpr
1139 # The static type to cast to.
1140 # (different from the static type of the expression that is `Bool`).
1141 var cast_type: nullable MType
1142 redef fun accept_typing(v)
1143 do
1144 var mtype = v.visit_expr_cast(self, self.n_expr, self.n_type)
1145 self.cast_type = mtype
1146
1147 var variable = self.n_expr.its_variable
1148 if variable != null then
1149 var orig = self.n_expr.mtype
1150 var from = if orig != null then orig.to_s else "invalid"
1151 var to = if mtype != null then mtype.to_s else "invalid"
1152 #debug("adapt {variable}: {from} -> {to}")
1153 self.after_flow_context.when_true.set_var(variable, mtype)
1154 end
1155
1156 self.mtype = v.type_bool(self)
1157 end
1158 end
1159
1160 redef class AAsCastExpr
1161 redef fun accept_typing(v)
1162 do
1163 self.mtype = v.visit_expr_cast(self, self.n_expr, self.n_type)
1164 end
1165 end
1166
1167 redef class AAsNotnullExpr
1168 redef fun accept_typing(v)
1169 do
1170 var mtype = v.visit_expr(self.n_expr)
1171 if mtype isa MNullType then
1172 v.error(self, "Type error: as(not null) on null")
1173 return
1174 end
1175 if mtype isa MNullableType then
1176 self.mtype = mtype.mtype
1177 return
1178 end
1179 # TODO: warn on useless as not null
1180 self.mtype = mtype
1181 end
1182 end
1183
1184 redef class AProxyExpr
1185 redef fun accept_typing(v)
1186 do
1187 self.mtype = v.visit_expr(self.n_expr)
1188 end
1189 end
1190
1191 redef class ASelfExpr
1192 redef var its_variable: nullable Variable
1193 redef fun accept_typing(v)
1194 do
1195 var variable = v.selfvariable
1196 self.its_variable = variable
1197 self.mtype = v.get_variable(self, variable)
1198 end
1199 end
1200
1201 ## MESSAGE SENDING AND PROPERTY
1202
1203 redef class ASendExpr
1204 # The property invoked by the send.
1205 var callsite: nullable CallSite
1206
1207 redef fun accept_typing(v)
1208 do
1209 var recvtype = v.visit_expr(self.n_expr)
1210 var name = self.property_name
1211
1212 if recvtype == null then return # Forward error
1213 if recvtype isa MNullType then
1214 v.error(self, "Error: Method '{name}' call on 'null'.")
1215 return
1216 end
1217
1218 var callsite = v.get_method(self, recvtype, name, self.n_expr isa ASelfExpr)
1219 if callsite == null then return
1220 self.callsite = callsite
1221 var msignature = callsite.msignature
1222
1223 var args = compute_raw_arguments
1224 self.raw_arguments = args
1225
1226 callsite.check_signature(v, args)
1227
1228 if callsite.mproperty.is_init then
1229 var vmpropdef = v.mpropdef
1230 if not (vmpropdef isa MMethodDef and vmpropdef.mproperty.is_init) then
1231 v.error(self, "Can call a init only in another init")
1232 end
1233 end
1234
1235 var ret = msignature.return_mtype
1236 if ret != null then
1237 self.mtype = ret
1238 else
1239 self.is_typed = true
1240 end
1241 end
1242
1243 # The name of the property
1244 # Each subclass simply provide the correct name.
1245 private fun property_name: String is abstract
1246
1247 # An array of all arguments (excluding self)
1248 var raw_arguments: nullable Array[AExpr]
1249
1250 private fun compute_raw_arguments: Array[AExpr] is abstract
1251 end
1252
1253 redef class ABinopExpr
1254 redef fun compute_raw_arguments do return [n_expr2]
1255 end
1256 redef class AEqExpr
1257 redef fun property_name do return "=="
1258 redef fun accept_typing(v)
1259 do
1260 super
1261
1262 var variable = self.n_expr.its_variable
1263 if variable == null then return
1264 var mtype = self.n_expr2.mtype
1265 if not mtype isa MNullType then return
1266 var vartype = v.get_variable(self, variable)
1267 if not vartype isa MNullableType then return
1268 self.after_flow_context.when_true.set_var(variable, mtype)
1269 self.after_flow_context.when_false.set_var(variable, vartype.mtype)
1270 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1271 end
1272 end
1273 redef class ANeExpr
1274 redef fun property_name do return "!="
1275 redef fun accept_typing(v)
1276 do
1277 super
1278
1279 var variable = self.n_expr.its_variable
1280 if variable == null then return
1281 var mtype = self.n_expr2.mtype
1282 if not mtype isa MNullType then return
1283 var vartype = v.get_variable(self, variable)
1284 if not vartype isa MNullableType then return
1285 self.after_flow_context.when_false.set_var(variable, mtype)
1286 self.after_flow_context.when_true.set_var(variable, vartype.mtype)
1287 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1288 end
1289 end
1290 redef class ALtExpr
1291 redef fun property_name do return "<"
1292 end
1293 redef class ALeExpr
1294 redef fun property_name do return "<="
1295 end
1296 redef class ALlExpr
1297 redef fun property_name do return "<<"
1298 end
1299 redef class AGtExpr
1300 redef fun property_name do return ">"
1301 end
1302 redef class AGeExpr
1303 redef fun property_name do return ">="
1304 end
1305 redef class AGgExpr
1306 redef fun property_name do return ">>"
1307 end
1308 redef class APlusExpr
1309 redef fun property_name do return "+"
1310 end
1311 redef class AMinusExpr
1312 redef fun property_name do return "-"
1313 end
1314 redef class AStarshipExpr
1315 redef fun property_name do return "<=>"
1316 end
1317 redef class AStarExpr
1318 redef fun property_name do return "*"
1319 end
1320 redef class ASlashExpr
1321 redef fun property_name do return "/"
1322 end
1323 redef class APercentExpr
1324 redef fun property_name do return "%"
1325 end
1326
1327 redef class AUminusExpr
1328 redef fun property_name do return "unary -"
1329 redef fun compute_raw_arguments do return new Array[AExpr]
1330 end
1331
1332
1333 redef class ACallExpr
1334 redef fun property_name do return n_id.text
1335 redef fun compute_raw_arguments do return n_args.to_a
1336 end
1337
1338 redef class ACallAssignExpr
1339 redef fun property_name do return n_id.text + "="
1340 redef fun compute_raw_arguments
1341 do
1342 var res = n_args.to_a
1343 res.add(n_value)
1344 return res
1345 end
1346 end
1347
1348 redef class ABraExpr
1349 redef fun property_name do return "[]"
1350 redef fun compute_raw_arguments do return n_args.to_a
1351 end
1352
1353 redef class ABraAssignExpr
1354 redef fun property_name do return "[]="
1355 redef fun compute_raw_arguments
1356 do
1357 var res = n_args.to_a
1358 res.add(n_value)
1359 return res
1360 end
1361 end
1362
1363 redef class ASendReassignFormExpr
1364 # The property invoked for the writing
1365 var write_callsite: nullable CallSite
1366
1367 redef fun accept_typing(v)
1368 do
1369 var recvtype = v.visit_expr(self.n_expr)
1370 var name = self.property_name
1371
1372 if recvtype == null then return # Forward error
1373 if recvtype isa MNullType then
1374 v.error(self, "Error: Method '{name}' call on 'null'.")
1375 return
1376 end
1377
1378 var for_self = self.n_expr isa ASelfExpr
1379 var callsite = v.get_method(self, recvtype, name, for_self)
1380
1381 if callsite == null then return
1382 self.callsite = callsite
1383
1384 var args = compute_raw_arguments
1385 self.raw_arguments = args
1386
1387 callsite.check_signature(v, args)
1388
1389 var readtype = callsite.msignature.return_mtype
1390 if readtype == null then
1391 v.error(self, "Error: {name} is not a function")
1392 return
1393 end
1394
1395 var wcallsite = v.get_method(self, recvtype, name + "=", self.n_expr isa ASelfExpr)
1396 if wcallsite == null then return
1397 self.write_callsite = wcallsite
1398
1399 var wtype = self.resolve_reassignment(v, readtype, wcallsite.msignature.mparameters.last.mtype)
1400 if wtype == null then return
1401
1402 args = args.to_a # duplicate so raw_arguments keeps only the getter args
1403 args.add(self.n_value)
1404 wcallsite.check_signature(v, args)
1405
1406 self.is_typed = true
1407 end
1408 end
1409
1410 redef class ACallReassignExpr
1411 redef fun property_name do return n_id.text
1412 redef fun compute_raw_arguments do return n_args.to_a
1413 end
1414
1415 redef class ABraReassignExpr
1416 redef fun property_name do return "[]"
1417 redef fun compute_raw_arguments do return n_args.to_a
1418 end
1419
1420 redef class AInitExpr
1421 redef fun property_name do return "init"
1422 redef fun compute_raw_arguments do return n_args.to_a
1423 end
1424
1425 redef class AExprs
1426 fun to_a: Array[AExpr] do return self.n_exprs.to_a
1427 end
1428
1429 ###
1430
1431 redef class ASuperExpr
1432 # The method to call if the super is in fact a 'super init call'
1433 # Note: if the super is a normal call-next-method, then this attribute is null
1434 var callsite: nullable CallSite
1435
1436 # The method to call is the super is a standard `call-next-method` super-call
1437 # Note: if the super is a special super-init-call, then this attribute is null
1438 var mpropdef: nullable MMethodDef
1439
1440 redef fun accept_typing(v)
1441 do
1442 var recvtype = v.nclassdef.mclassdef.bound_mtype
1443 var mproperty = v.mpropdef.mproperty
1444 if not mproperty isa MMethod then
1445 v.error(self, "Error: super only usable in a method")
1446 return
1447 end
1448 var superprops = mproperty.lookup_super_definitions(v.mmodule, recvtype)
1449 if superprops.length == 0 then
1450 if mproperty.is_init and v.mpropdef.is_intro then
1451 process_superinit(v)
1452 return
1453 end
1454 v.error(self, "Error: No super method to call for {mproperty}.")
1455 return
1456 end
1457 # FIXME: covariance of return type in linear extension?
1458 var superprop = superprops.first
1459
1460 var msignature = v.resolve_signature_for(superprop, recvtype, true)
1461 var args = self.n_args.to_a
1462 if args.length > 0 then
1463 v.check_signature(self, args, mproperty.name, msignature)
1464 end
1465 self.mtype = msignature.return_mtype
1466 self.is_typed = true
1467 v.mpropdef.has_supercall = true
1468 mpropdef = v.mpropdef.as(MMethodDef)
1469 end
1470
1471 private fun process_superinit(v: TypeVisitor)
1472 do
1473 var recvtype = v.nclassdef.mclassdef.bound_mtype
1474 var mpropdef = v.mpropdef
1475 assert mpropdef isa MMethodDef
1476 var mproperty = mpropdef.mproperty
1477 var superprop: nullable MMethodDef = null
1478 for msupertype in v.nclassdef.mclassdef.supertypes do
1479 msupertype = msupertype.anchor_to(v.mmodule, recvtype)
1480 var errcount = v.modelbuilder.toolcontext.error_count
1481 var candidate = v.try_get_mproperty_by_name2(self, msupertype, mproperty.name).as(nullable MMethod)
1482 if candidate == null then
1483 if v.modelbuilder.toolcontext.error_count > errcount then return # Forard error
1484 continue # Try next super-class
1485 end
1486 if superprop != null and superprop.mproperty != candidate then
1487 v.error(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1488 return
1489 end
1490 var candidatedefs = candidate.lookup_definitions(v.mmodule, recvtype)
1491 if superprop != null then
1492 if superprop == candidatedefs.first then continue
1493 candidatedefs.add(superprop)
1494 end
1495 if candidatedefs.length > 1 then
1496 v.error(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1497 return
1498 end
1499 superprop = candidatedefs.first
1500 end
1501 if superprop == null then
1502 v.error(self, "Error: No super method to call for {mproperty}.")
1503 return
1504 end
1505
1506 var msignature = v.resolve_signature_for(superprop, recvtype, true)
1507 var callsite = new CallSite(self, recvtype, v.mmodule, v.anchor, true, superprop.mproperty, superprop, msignature, false)
1508 self.callsite = callsite
1509
1510 var args = self.n_args.to_a
1511 if args.length > 0 then
1512 callsite.check_signature(v, args)
1513 else
1514 # Check there is at least enough parameters
1515 if mpropdef.msignature.arity < msignature.arity then
1516 v.error(self, "Error: Not enough implicit arguments to pass. Got {mpropdef.msignature.arity}, expected at least {msignature.arity}. Signature is {msignature}")
1517 return
1518 end
1519 # Check that each needed parameter is conform
1520 var i = 0
1521 for sp in msignature.mparameters do
1522 var p = mpropdef.msignature.mparameters[i]
1523 if not v.is_subtype(p.mtype, sp.mtype) then
1524 v.error(self, "Type error: expected argument #{i} of type {sp.mtype}, got implicit argument {p.name} of type {p.mtype}. Signature is {msignature}")
1525 return
1526 end
1527 i += 1
1528 end
1529 end
1530
1531 self.is_typed = true
1532 end
1533 end
1534
1535 ####
1536
1537 redef class ANewExpr
1538 # The constructor invoked by the new.
1539 var callsite: nullable CallSite
1540
1541 redef fun accept_typing(v)
1542 do
1543 var recvtype = v.resolve_mtype(self.n_type)
1544 if recvtype == null then return
1545 self.mtype = recvtype
1546
1547 if not recvtype isa MClassType then
1548 if recvtype isa MNullableType then
1549 v.error(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1550 return
1551 else
1552 v.error(self, "Type error: cannot instantiate the formal type {recvtype}.")
1553 return
1554 end
1555 else
1556 if recvtype.mclass.kind == abstract_kind then
1557 v.error(self, "Cannot instantiate abstract class {recvtype}.")
1558 return
1559 else if recvtype.mclass.kind == interface_kind then
1560 v.error(self, "Cannot instantiate interface {recvtype}.")
1561 return
1562 end
1563 end
1564
1565 var name: String
1566 var nid = self.n_id
1567 if nid != null then
1568 name = nid.text
1569 else
1570 name = "init"
1571 end
1572 var callsite = v.get_method(self, recvtype, name, false)
1573 if callsite == null then return
1574
1575 self.callsite = callsite
1576
1577 if not callsite.mproperty.is_init_for(recvtype.mclass) then
1578 v.error(self, "Error: {name} is not a constructor.")
1579 return
1580 end
1581
1582 var args = n_args.to_a
1583 callsite.check_signature(v, args)
1584 end
1585 end
1586
1587 ####
1588
1589 redef class AAttrFormExpr
1590 # The attribute acceded.
1591 var mproperty: nullable MAttribute
1592
1593 # The static type of the attribute.
1594 var attr_type: nullable MType
1595
1596 # Resolve the attribute acceded.
1597 private fun resolve_property(v: TypeVisitor)
1598 do
1599 var recvtype = v.visit_expr(self.n_expr)
1600 if recvtype == null then return # Skip error
1601 var name = self.n_id.text
1602 if recvtype isa MNullType then
1603 v.error(self, "Error: Attribute '{name}' access on 'null'.")
1604 return
1605 end
1606
1607 var unsafe_type = v.anchor_to(recvtype)
1608 var mproperty = v.try_get_mproperty_by_name2(self, unsafe_type, name)
1609 if mproperty == null then
1610 v.modelbuilder.error(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1611 return
1612 end
1613 assert mproperty isa MAttribute
1614 self.mproperty = mproperty
1615
1616 var mpropdefs = mproperty.lookup_definitions(v.mmodule, unsafe_type)
1617 assert mpropdefs.length == 1
1618 var mpropdef = mpropdefs.first
1619 var attr_type = mpropdef.static_mtype.as(not null)
1620 attr_type = v.resolve_for(attr_type, recvtype, self.n_expr isa ASelfExpr)
1621 self.attr_type = attr_type
1622 end
1623 end
1624
1625 redef class AAttrExpr
1626 redef fun accept_typing(v)
1627 do
1628 self.resolve_property(v)
1629 self.mtype = self.attr_type
1630 end
1631 end
1632
1633
1634 redef class AAttrAssignExpr
1635 redef fun accept_typing(v)
1636 do
1637 self.resolve_property(v)
1638 var mtype = self.attr_type
1639
1640 v.visit_expr_subtype(self.n_value, mtype)
1641 self.is_typed = true
1642 end
1643 end
1644
1645 redef class AAttrReassignExpr
1646 redef fun accept_typing(v)
1647 do
1648 self.resolve_property(v)
1649 var mtype = self.attr_type
1650 if mtype == null then return # Skip error
1651
1652 self.resolve_reassignment(v, mtype, mtype)
1653
1654 self.is_typed = true
1655 end
1656 end
1657
1658 redef class AIssetAttrExpr
1659 redef fun accept_typing(v)
1660 do
1661 self.resolve_property(v)
1662 var mtype = self.attr_type
1663 if mtype == null then return # Skip error
1664
1665 var recvtype = self.n_expr.mtype.as(not null)
1666 var bound = v.resolve_for(mtype, recvtype, false)
1667 if bound isa MNullableType then
1668 v.error(self, "Error: isset on a nullable attribute.")
1669 end
1670 self.mtype = v.type_bool(self)
1671 end
1672 end
1673
1674 ###
1675
1676 redef class ADebugTypeExpr
1677 redef fun accept_typing(v)
1678 do
1679 var expr = v.visit_expr(self.n_expr)
1680 if expr == null then return
1681 var unsafe = v.anchor_to(expr)
1682 var ntype = self.n_type
1683 var mtype = v.resolve_mtype(ntype)
1684 if mtype != null and mtype != expr then
1685 var umtype = v.anchor_to(mtype)
1686 v.modelbuilder.warning(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
1687 end
1688 self.is_typed = true
1689 end
1690 end