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