62e1bc077c4fc9f38074d812f64c2c9a9b59f5db
[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 for i in [0..mmethoddef.msignature.mclosures.length[ do
505 var mclosure = mmethoddef.msignature.mclosures[i]
506 var variable = self.n_signature.n_closure_decls[i].variable
507 assert variable != null
508 variable.declared_type = mclosure.mtype
509 end
510 v.visit_stmt(nblock)
511
512 if not nblock.after_flow_context.is_unreachable and mmethoddef.msignature.return_mtype != null then
513 # We reach the end of the function without having a return, it is bad
514 v.error(self, "Control error: Reached end of function (a 'return' with a value was expected).")
515 end
516 end
517 end
518
519 redef class AAttrPropdef
520 redef fun do_typing(modelbuilder: ModelBuilder)
521 do
522 var nclassdef = self.parent.as(AClassdef)
523 var v = new TypeVisitor(modelbuilder, nclassdef, self.mpropdef.as(not null))
524 self.selfvariable = v.selfvariable
525
526 var nexpr = self.n_expr
527 if nexpr != null then
528 var mtype = self.mpropdef.static_mtype
529 v.visit_expr_subtype(nexpr, mtype)
530 end
531 end
532 end
533
534 ###
535
536 redef class AExpr
537 # The static type of the expression.
538 # null if self is a statement of in case of error
539 var mtype: nullable MType = null
540
541 # Is the statement correctly typed?
542 # Used to distinguish errors and statements when `mtype == null`
543 var is_typed: Bool = false
544
545 # If required, the following implicit cast `.as(XXX)`
546 # Such a cast may by required after evaluating the expression when
547 # a unsafe operation is detected (silently accepted by the Nit language).
548 # The attribute is computed by `check_subtype`
549 var implicit_cast_to: nullable MType = null
550
551 # Return the variable read (if any)
552 # Used to perform adaptive typing
553 fun its_variable: nullable Variable do return null
554
555 private fun accept_typing(v: TypeVisitor)
556 do
557 v.error(self, "no implemented accept_typing for {self.class_name}")
558 end
559 end
560
561 redef class ABlockExpr
562 redef fun accept_typing(v)
563 do
564 for e in self.n_expr do v.visit_stmt(e)
565 self.is_typed = true
566 end
567
568 # The type of a blockexpr is the one of the last expression (or null if empty)
569 redef fun mtype
570 do
571 if self.n_expr.is_empty then return null
572 return self.n_expr.last.mtype
573 end
574 end
575
576 redef class AVardeclExpr
577 redef fun accept_typing(v)
578 do
579 var variable = self.variable
580 if variable == null then return # Skip error
581
582 var ntype = self.n_type
583 var mtype: nullable MType
584 if ntype == null then
585 mtype = null
586 else
587 mtype = v.resolve_mtype(ntype)
588 if mtype == null then return # Skip error
589 end
590
591 var nexpr = self.n_expr
592 if nexpr != null then
593 if mtype != null then
594 v.visit_expr_subtype(nexpr, mtype)
595 else
596 mtype = v.visit_expr(nexpr)
597 if mtype == null then return # Skip error
598 end
599 end
600
601 var decltype = mtype
602 if mtype == null or mtype isa MNullType then
603 decltype = v.get_mclass(self, "Object").mclass_type.as_nullable
604 if mtype == null then mtype = decltype
605 end
606
607 variable.declared_type = decltype
608 v.set_variable(self, variable, mtype)
609
610 #debug("var {variable}: {mtype}")
611
612 self.is_typed = true
613 end
614 end
615
616 redef class AVarExpr
617 redef fun its_variable do return self.variable
618 redef fun accept_typing(v)
619 do
620 var variable = self.variable
621 if variable == null then return # Skip error
622
623 var mtype = v.get_variable(self, variable)
624 if mtype != null then
625 #debug("{variable} is {mtype}")
626 else
627 #debug("{variable} is untyped")
628 end
629
630 self.mtype = mtype
631 end
632 end
633
634 redef class AVarAssignExpr
635 redef fun accept_typing(v)
636 do
637 var variable = self.variable
638 assert variable != null
639
640 var mtype = v.visit_expr_subtype(n_value, variable.declared_type)
641
642 v.set_variable(self, variable, mtype)
643
644 self.is_typed = true
645 end
646 end
647
648 redef class AReassignFormExpr
649 # @depreciated use `reassign_callsite`
650 fun reassign_property: nullable MMethodDef do return self.reassign_callsite.mpropdef
651
652 # The method designed by the reassign operator.
653 var reassign_callsite: nullable CallSite
654
655 var read_type: nullable MType = null
656
657 # Determine the `reassign_property`
658 # `readtype` is the type of the reading of the left value.
659 # `writetype` is the type of the writing of the left value.
660 # (Because of `ACallReassignExpr`, both can be different.
661 # Return the static type of the value to store.
662 private fun resolve_reassignment(v: TypeVisitor, readtype, writetype: MType): nullable MType
663 do
664 var reassign_name: String
665 if self.n_assign_op isa APlusAssignOp then
666 reassign_name = "+"
667 else if self.n_assign_op isa AMinusAssignOp then
668 reassign_name = "-"
669 else
670 abort
671 end
672
673 self.read_type = readtype
674
675 if readtype isa MNullType then
676 v.error(self, "Error: Method '{reassign_name}' call on 'null'.")
677 return null
678 end
679
680 var callsite = v.get_method(self, readtype, reassign_name, false)
681 if callsite == null then return null # Skip error
682 self.reassign_callsite = callsite
683
684 var msignature = callsite.msignature
685 var rettype = msignature.return_mtype
686 assert msignature.arity == 1 and rettype != null
687
688 var value_type = v.visit_expr_subtype(self.n_value, msignature.mparameters.first.mtype)
689 if value_type == null then return null # Skip error
690
691 v.check_subtype(self, rettype, writetype)
692 return rettype
693 end
694 end
695
696 redef class AVarReassignExpr
697 redef fun accept_typing(v)
698 do
699 var variable = self.variable
700 assert variable != null
701
702 var readtype = v.get_variable(self, variable)
703 if readtype == null then return
704
705 var writetype = variable.declared_type
706 if writetype == null then return
707
708 var rettype = self.resolve_reassignment(v, readtype, writetype)
709
710 v.set_variable(self, variable, rettype)
711
712 self.is_typed = true
713 end
714 end
715
716
717 redef class AContinueExpr
718 redef fun accept_typing(v)
719 do
720 var nexpr = self.n_expr
721 if nexpr != null then
722 var mtype = v.visit_expr(nexpr)
723 end
724 self.is_typed = true
725 end
726 end
727
728 redef class ABreakExpr
729 redef fun accept_typing(v)
730 do
731 var nexpr = self.n_expr
732 if nexpr != null then
733 var mtype = v.visit_expr(nexpr)
734 end
735 self.is_typed = true
736 end
737 end
738
739 redef class AReturnExpr
740 redef fun accept_typing(v)
741 do
742 var nexpr = self.n_expr
743 var ret_type = v.mpropdef.as(MMethodDef).msignature.return_mtype
744 if nexpr != null then
745 if ret_type != null then
746 var mtype = v.visit_expr_subtype(nexpr, ret_type)
747 else
748 var mtype = v.visit_expr(nexpr)
749 v.error(self, "Error: Return with value in a procedure.")
750 end
751 else if ret_type != null then
752 v.error(self, "Error: Return without value in a function.")
753 end
754 self.is_typed = true
755 end
756 end
757
758 redef class AAbortExpr
759 redef fun accept_typing(v)
760 do
761 self.is_typed = true
762 end
763 end
764
765 redef class AIfExpr
766 redef fun accept_typing(v)
767 do
768 v.visit_expr_bool(n_expr)
769
770 v.visit_stmt(n_then)
771 v.visit_stmt(n_else)
772 self.is_typed = true
773 end
774 end
775
776 redef class AIfexprExpr
777 redef fun accept_typing(v)
778 do
779 v.visit_expr_bool(n_expr)
780
781 var t1 = v.visit_expr(n_then)
782 var t2 = v.visit_expr(n_else)
783
784 if t1 == null or t2 == null then
785 return # Skip error
786 end
787
788 var t = v.merge_types(self, [t1, t2])
789 if t == null then
790 v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
791 end
792 self.mtype = t
793 end
794 end
795
796 redef class ADoExpr
797 redef fun accept_typing(v)
798 do
799 v.visit_stmt(n_block)
800 self.is_typed = true
801 end
802 end
803
804 redef class AWhileExpr
805 redef fun accept_typing(v)
806 do
807 v.visit_expr_bool(n_expr)
808
809 v.visit_stmt(n_block)
810 self.is_typed = true
811 end
812 end
813
814 redef class ALoopExpr
815 redef fun accept_typing(v)
816 do
817 v.visit_stmt(n_block)
818 self.is_typed = true
819 end
820 end
821
822 redef class AForExpr
823 var coltype: nullable MClassType
824
825 var method_iterator: nullable MMethod
826 var method_is_ok: nullable MMethod
827 var method_item: nullable MMethod
828 var method_next: nullable MMethod
829 var method_key: nullable MMethod
830
831 private fun do_type_iterator(v: TypeVisitor, mtype: MType)
832 do
833 if mtype isa MNullType then
834 v.error(self, "Type error: 'for' cannot iterate over 'null'")
835 return
836 end
837
838 # get obj class
839 var objcla = v.get_mclass(self, "Object")
840 if objcla == null then return
841
842 # check iterator method
843 var unsafe_type = v.anchor_to(mtype)
844 if v.try_get_mproperty_by_name2(self, unsafe_type, "iterator") == null then
845 if v.try_get_mproperty_by_name2(self, unsafe_type, "iterate") == null then
846 v.error(self, "Type Error: Expected method 'iterator' in type {mtype}")
847 else
848 v.modelbuilder.error(self, "NOT YET IMPLEMENTED: Do 'for' on {mtype}")
849 end
850 return
851 end
852
853 var itdef = v.get_method(self, mtype, "iterator", true)
854 if itdef == null then
855 v.error(self, "Type Error: Expected method 'iterator' in type {mtype}")
856 return
857 end
858 self.method_iterator = itdef.mproperty
859
860 # check that iterator return something
861 var ittype = itdef.msignature.return_mtype
862 if ittype == null then
863 v.error(self, "Type Error: Expected method 'iterator' to return an Iterator or MapIterator type")
864 return
865 end
866
867 # get iterator type
868 var colit_cla = v.try_get_mclass(self, "Iterator")
869 var mapit_cla = v.try_get_mclass(self, "MapIterator")
870 var is_col = false
871 var is_map = false
872
873 if colit_cla != null and v.is_subtype(ittype, colit_cla.get_mtype([objcla.mclass_type.as_nullable])) then
874 # Iterator
875 var coltype = ittype.supertype_to(v.mmodule, v.anchor, colit_cla)
876 var variables = self.variables
877 if variables.length != 1 then
878 v.error(self, "Type Error: Expected one variable")
879 else
880 variables.first.declared_type = coltype.arguments.first
881 end
882 is_col = true
883 end
884
885 if mapit_cla != null and v.is_subtype(ittype, mapit_cla.get_mtype([objcla.mclass_type, objcla.mclass_type.as_nullable])) then
886 # Map Iterator
887 var coltype = ittype.supertype_to(v.mmodule, v.anchor, mapit_cla)
888 var variables = self.variables
889 if variables.length != 2 then
890 v.error(self, "Type Error: Expected two variables")
891 else
892 variables[0].declared_type = coltype.arguments[0]
893 variables[1].declared_type = coltype.arguments[1]
894 end
895 is_map = true
896 end
897
898 if not is_col and not is_map then
899 v.error(self, "Type Error: Expected method 'iterator' to return an Iterator of MapIterator type")
900 return
901 end
902
903 # anchor formal and virtual types
904 if mtype.need_anchor then mtype = v.anchor_to(mtype)
905
906 if mtype isa MNullableType then mtype = mtype.mtype
907 self.coltype = mtype.as(MClassType)
908
909 # get methods is_ok, next, item
910 var ikdef = v.get_method(self, ittype, "is_ok", false)
911 if ikdef == null then
912 v.error(self, "Type Error: Expected method 'is_ok' in Iterator type {ittype}")
913 return
914 end
915 self.method_is_ok = ikdef.mproperty
916
917 var itemdef = v.get_method(self, ittype, "item", false)
918 if itemdef == null then
919 v.error(self, "Type Error: Expected method 'item' in Iterator type {ittype}")
920 return
921 end
922 self.method_item = itemdef.mproperty
923
924 var nextdef = v.get_method(self, ittype, "next", false)
925 if nextdef == null then
926 v.error(self, "Type Error: Expected method 'next' in Iterator type {ittype}")
927 return
928 end
929 self.method_next = nextdef.mproperty
930
931 if is_map then
932 var keydef = v.get_method(self, ittype, "key", false)
933 if keydef == null then
934 v.error(self, "Type Error: Expected method 'key' in Iterator type {ittype}")
935 return
936 end
937 self.method_key = keydef.mproperty
938 end
939 end
940
941 redef fun accept_typing(v)
942 do
943 var mtype = v.visit_expr(n_expr)
944 if mtype == null then return
945
946 self.do_type_iterator(v, mtype)
947
948 v.visit_stmt(n_block)
949 self.is_typed = true
950 end
951 end
952
953 redef class AAssertExpr
954 redef fun accept_typing(v)
955 do
956 v.visit_expr_bool(n_expr)
957
958 v.visit_stmt(n_else)
959 self.is_typed = true
960 end
961 end
962
963 redef class AOrExpr
964 redef fun accept_typing(v)
965 do
966 v.visit_expr_bool(n_expr)
967 v.visit_expr_bool(n_expr2)
968 self.mtype = v.type_bool(self)
969 end
970 end
971
972 redef class AImpliesExpr
973 redef fun accept_typing(v)
974 do
975 v.visit_expr_bool(n_expr)
976 v.visit_expr_bool(n_expr2)
977 self.mtype = v.type_bool(self)
978 end
979 end
980
981 redef class AAndExpr
982 redef fun accept_typing(v)
983 do
984 v.visit_expr_bool(n_expr)
985 v.visit_expr_bool(n_expr2)
986 self.mtype = v.type_bool(self)
987 end
988 end
989
990
991 redef class ANotExpr
992 redef fun accept_typing(v)
993 do
994 v.visit_expr_bool(n_expr)
995 self.mtype = v.type_bool(self)
996 end
997 end
998
999 redef class AOrElseExpr
1000 redef fun accept_typing(v)
1001 do
1002 var t1 = v.visit_expr(n_expr)
1003 var t2 = v.visit_expr(n_expr2)
1004
1005 if t1 == null or t2 == null then
1006 return # Skip error
1007 end
1008
1009 if t1 isa MNullableType then
1010 t1 = t1.mtype
1011 end
1012
1013 var t = v.merge_types(self, [t1, t2])
1014 if t == null then
1015 t = v.mmodule.object_type
1016 if t2 isa MNullableType then
1017 t = t.as_nullable
1018 end
1019 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1020 end
1021 self.mtype = t
1022 end
1023 end
1024
1025 redef class AEeExpr
1026 redef fun accept_typing(v)
1027 do
1028 v.visit_expr(n_expr)
1029 v.visit_expr(n_expr2)
1030 self.mtype = v.type_bool(self)
1031 end
1032 end
1033
1034 redef class ATrueExpr
1035 redef fun accept_typing(v)
1036 do
1037 self.mtype = v.type_bool(self)
1038 end
1039 end
1040
1041 redef class AFalseExpr
1042 redef fun accept_typing(v)
1043 do
1044 self.mtype = v.type_bool(self)
1045 end
1046 end
1047
1048 redef class AIntExpr
1049 redef fun accept_typing(v)
1050 do
1051 var mclass = v.get_mclass(self, "Int")
1052 if mclass == null then return # Forward error
1053 self.mtype = mclass.mclass_type
1054 end
1055 end
1056
1057 redef class AFloatExpr
1058 redef fun accept_typing(v)
1059 do
1060 var mclass = v.get_mclass(self, "Float")
1061 if mclass == null then return # Forward error
1062 self.mtype = mclass.mclass_type
1063 end
1064 end
1065
1066 redef class ACharExpr
1067 redef fun accept_typing(v)
1068 do
1069 var mclass = v.get_mclass(self, "Char")
1070 if mclass == null then return # Forward error
1071 self.mtype = mclass.mclass_type
1072 end
1073 end
1074
1075 redef class AStringFormExpr
1076 redef fun accept_typing(v)
1077 do
1078 var mclass = v.get_mclass(self, "String")
1079 if mclass == null then return # Forward error
1080 self.mtype = mclass.mclass_type
1081 end
1082 end
1083
1084 redef class ASuperstringExpr
1085 redef fun accept_typing(v)
1086 do
1087 var mclass = v.get_mclass(self, "String")
1088 if mclass == null then return # Forward error
1089 self.mtype = mclass.mclass_type
1090 for nexpr in self.n_exprs do
1091 var t = v.visit_expr(nexpr)
1092 end
1093 end
1094 end
1095
1096 redef class AArrayExpr
1097 redef fun accept_typing(v)
1098 do
1099 var mtypes = new Array[nullable MType]
1100 for e in self.n_exprs.n_exprs do
1101 var t = v.visit_expr(e)
1102 if t == null then
1103 return # Skip error
1104 end
1105 mtypes.add(t)
1106 end
1107 var mtype = v.merge_types(self, mtypes)
1108 if mtype == null then
1109 v.error(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1110 return
1111 end
1112 var mclass = v.get_mclass(self, "Array")
1113 if mclass == null then return # Forward error
1114 self.mtype = mclass.get_mtype([mtype])
1115 end
1116 end
1117
1118 redef class ARangeExpr
1119 redef fun accept_typing(v)
1120 do
1121 var discrete_class = v.get_mclass(self, "Discrete")
1122 if discrete_class == null then return # Forward error
1123 var discrete_type = discrete_class.intro.bound_mtype
1124 var t1 = v.visit_expr_subtype(self.n_expr, discrete_type)
1125 var t2 = v.visit_expr_subtype(self.n_expr2, discrete_type)
1126 if t1 == null or t2 == null then return
1127 var mclass = v.get_mclass(self, "Range")
1128 if mclass == null then return # Forward error
1129 if v.is_subtype(t1, t2) then
1130 self.mtype = mclass.get_mtype([t2])
1131 else if v.is_subtype(t2, t1) then
1132 self.mtype = mclass.get_mtype([t1])
1133 else
1134 v.error(self, "Type Error: Cannot create range: {t1} vs {t2}")
1135 end
1136 end
1137 end
1138
1139 redef class ANullExpr
1140 redef fun accept_typing(v)
1141 do
1142 self.mtype = v.mmodule.model.null_type
1143 end
1144 end
1145
1146 redef class AIsaExpr
1147 # The static type to cast to.
1148 # (different from the static type of the expression that is `Bool`).
1149 var cast_type: nullable MType
1150 redef fun accept_typing(v)
1151 do
1152 var mtype = v.visit_expr_cast(self, self.n_expr, self.n_type)
1153 self.cast_type = mtype
1154
1155 var variable = self.n_expr.its_variable
1156 if variable != null then
1157 var orig = self.n_expr.mtype
1158 var from = if orig != null then orig.to_s else "invalid"
1159 var to = if mtype != null then mtype.to_s else "invalid"
1160 #debug("adapt {variable}: {from} -> {to}")
1161 self.after_flow_context.when_true.set_var(variable, mtype)
1162 end
1163
1164 self.mtype = v.type_bool(self)
1165 end
1166 end
1167
1168 redef class AAsCastExpr
1169 redef fun accept_typing(v)
1170 do
1171 self.mtype = v.visit_expr_cast(self, self.n_expr, self.n_type)
1172 end
1173 end
1174
1175 redef class AAsNotnullExpr
1176 redef fun accept_typing(v)
1177 do
1178 var mtype = v.visit_expr(self.n_expr)
1179 if mtype isa MNullType then
1180 v.error(self, "Type error: as(not null) on null")
1181 return
1182 end
1183 if mtype isa MNullableType then
1184 self.mtype = mtype.mtype
1185 return
1186 end
1187 # TODO: warn on useless as not null
1188 self.mtype = mtype
1189 end
1190 end
1191
1192 redef class AProxyExpr
1193 redef fun accept_typing(v)
1194 do
1195 self.mtype = v.visit_expr(self.n_expr)
1196 end
1197 end
1198
1199 redef class ASelfExpr
1200 redef var its_variable: nullable Variable
1201 redef fun accept_typing(v)
1202 do
1203 var variable = v.selfvariable
1204 self.its_variable = variable
1205 self.mtype = v.get_variable(self, variable)
1206 end
1207 end
1208
1209 ## MESSAGE SENDING AND PROPERTY
1210
1211 redef class ASendExpr
1212 # @depreciated: use `callsite`
1213 fun mproperty: nullable MMethod do return callsite.mproperty
1214
1215 # The property invoked by the send.
1216 var callsite: nullable CallSite
1217
1218 redef fun accept_typing(v)
1219 do
1220 var recvtype = v.visit_expr(self.n_expr)
1221 var name = self.property_name
1222
1223 if recvtype == null then return # Forward error
1224 if recvtype isa MNullType then
1225 v.error(self, "Error: Method '{name}' call on 'null'.")
1226 return
1227 end
1228
1229 var callsite = v.get_method(self, recvtype, name, self.n_expr isa ASelfExpr)
1230 if callsite == null then return
1231 self.callsite = callsite
1232 var msignature = callsite.msignature
1233
1234 var args = compute_raw_arguments
1235 self.raw_arguments = args
1236
1237 callsite.check_signature(v, args)
1238
1239 if callsite.mproperty.is_init then
1240 var vmpropdef = v.mpropdef
1241 if not (vmpropdef isa MMethodDef and vmpropdef.mproperty.is_init) then
1242 v.error(self, "Can call a init only in another init")
1243 end
1244 end
1245
1246 var ret = msignature.return_mtype
1247 if ret != null then
1248 self.mtype = ret
1249 else
1250 self.is_typed = true
1251 end
1252
1253 if self.n_closure_defs.length == msignature.mclosures.length then
1254 for i in [0..self.n_closure_defs.length[ do
1255 self.n_closure_defs[i].accept_typing(v, msignature.mclosures[i])
1256 end
1257 else
1258 debug("closure: got {self.n_closure_defs.length}, want {msignature.mclosures.length}")
1259 end
1260 end
1261
1262 # The name of the property
1263 # Each subclass simply provide the correct name.
1264 private fun property_name: String is abstract
1265
1266 # An array of all arguments (excluding self)
1267 var raw_arguments: nullable Array[AExpr]
1268
1269 private fun compute_raw_arguments: Array[AExpr] is abstract
1270 end
1271
1272 redef class ABinopExpr
1273 redef fun compute_raw_arguments do return [n_expr2]
1274 end
1275 redef class AEqExpr
1276 redef fun property_name do return "=="
1277 redef fun accept_typing(v)
1278 do
1279 super
1280
1281 var variable = self.n_expr.its_variable
1282 if variable == null then return
1283 var mtype = self.n_expr2.mtype
1284 if not mtype isa MNullType then return
1285 var vartype = v.get_variable(self, variable)
1286 if not vartype isa MNullableType then return
1287 self.after_flow_context.when_true.set_var(variable, mtype)
1288 self.after_flow_context.when_false.set_var(variable, vartype.mtype)
1289 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1290 end
1291 end
1292 redef class ANeExpr
1293 redef fun property_name do return "!="
1294 redef fun accept_typing(v)
1295 do
1296 super
1297
1298 var variable = self.n_expr.its_variable
1299 if variable == null then return
1300 var mtype = self.n_expr2.mtype
1301 if not mtype isa MNullType then return
1302 var vartype = v.get_variable(self, variable)
1303 if not vartype isa MNullableType then return
1304 self.after_flow_context.when_false.set_var(variable, mtype)
1305 self.after_flow_context.when_true.set_var(variable, vartype.mtype)
1306 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1307 end
1308 end
1309 redef class ALtExpr
1310 redef fun property_name do return "<"
1311 end
1312 redef class ALeExpr
1313 redef fun property_name do return "<="
1314 end
1315 redef class ALlExpr
1316 redef fun property_name do return "<<"
1317 end
1318 redef class AGtExpr
1319 redef fun property_name do return ">"
1320 end
1321 redef class AGeExpr
1322 redef fun property_name do return ">="
1323 end
1324 redef class AGgExpr
1325 redef fun property_name do return ">>"
1326 end
1327 redef class APlusExpr
1328 redef fun property_name do return "+"
1329 end
1330 redef class AMinusExpr
1331 redef fun property_name do return "-"
1332 end
1333 redef class AStarshipExpr
1334 redef fun property_name do return "<=>"
1335 end
1336 redef class AStarExpr
1337 redef fun property_name do return "*"
1338 end
1339 redef class ASlashExpr
1340 redef fun property_name do return "/"
1341 end
1342 redef class APercentExpr
1343 redef fun property_name do return "%"
1344 end
1345
1346 redef class AUminusExpr
1347 redef fun property_name do return "unary -"
1348 redef fun compute_raw_arguments do return new Array[AExpr]
1349 end
1350
1351
1352 redef class ACallExpr
1353 redef fun property_name do return n_id.text
1354 redef fun compute_raw_arguments do return n_args.to_a
1355 end
1356
1357 redef class ACallAssignExpr
1358 redef fun property_name do return n_id.text + "="
1359 redef fun compute_raw_arguments
1360 do
1361 var res = n_args.to_a
1362 res.add(n_value)
1363 return res
1364 end
1365 end
1366
1367 redef class ABraExpr
1368 redef fun property_name do return "[]"
1369 redef fun compute_raw_arguments do return n_args.to_a
1370 end
1371
1372 redef class ABraAssignExpr
1373 redef fun property_name do return "[]="
1374 redef fun compute_raw_arguments
1375 do
1376 var res = n_args.to_a
1377 res.add(n_value)
1378 return res
1379 end
1380 end
1381
1382 redef class ASendReassignFormExpr
1383 # @depreciated use `write_callsite`
1384 fun write_mproperty: nullable MMethod do return write_callsite.mproperty
1385
1386 # The property invoked for the writing
1387 var write_callsite: nullable CallSite
1388
1389 redef fun accept_typing(v)
1390 do
1391 var recvtype = v.visit_expr(self.n_expr)
1392 var name = self.property_name
1393
1394 if recvtype == null then return # Forward error
1395 if recvtype isa MNullType then
1396 v.error(self, "Error: Method '{name}' call on 'null'.")
1397 return
1398 end
1399
1400 var for_self = self.n_expr isa ASelfExpr
1401 var callsite = v.get_method(self, recvtype, name, for_self)
1402
1403 if callsite == null then return
1404 self.callsite = callsite
1405
1406 var args = compute_raw_arguments
1407 self.raw_arguments = args
1408
1409 callsite.check_signature(v, args)
1410
1411 var readtype = callsite.msignature.return_mtype
1412 if readtype == null then
1413 v.error(self, "Error: {name} is not a function")
1414 return
1415 end
1416
1417 var wcallsite = v.get_method(self, recvtype, name + "=", self.n_expr isa ASelfExpr)
1418 if wcallsite == null then return
1419 self.write_callsite = wcallsite
1420
1421 var wtype = self.resolve_reassignment(v, readtype, wcallsite.msignature.mparameters.last.mtype)
1422 if wtype == null then return
1423
1424 args = args.to_a # duplicate so raw_arguments keeps only the getter args
1425 args.add(self.n_value)
1426 wcallsite.check_signature(v, args)
1427
1428 self.is_typed = true
1429 end
1430 end
1431
1432 redef class ACallReassignExpr
1433 redef fun property_name do return n_id.text
1434 redef fun compute_raw_arguments do return n_args.to_a
1435 end
1436
1437 redef class ABraReassignExpr
1438 redef fun property_name do return "[]"
1439 redef fun compute_raw_arguments do return n_args.to_a
1440 end
1441
1442 redef class AInitExpr
1443 redef fun property_name do return "init"
1444 redef fun compute_raw_arguments do return n_args.to_a
1445 end
1446
1447 redef class AExprs
1448 fun to_a: Array[AExpr] do return self.n_exprs.to_a
1449 end
1450
1451 ###
1452
1453 redef class ASuperExpr
1454 # The method to call if the super is in fact a 'super init call'
1455 # Note: if the super is a normal call-next-method, then this attribute is null
1456 var mproperty: nullable MMethod
1457
1458 redef fun accept_typing(v)
1459 do
1460 var recvtype = v.nclassdef.mclassdef.bound_mtype
1461 var mproperty = v.mpropdef.mproperty
1462 if not mproperty isa MMethod then
1463 v.error(self, "Error: super only usable in a method")
1464 return
1465 end
1466 var superprops = mproperty.lookup_super_definitions(v.mmodule, recvtype)
1467 if superprops.length == 0 then
1468 if mproperty.is_init and v.mpropdef.is_intro then
1469 process_superinit(v)
1470 return
1471 end
1472 v.error(self, "Error: No super method to call for {mproperty}.")
1473 return
1474 end
1475 # FIXME: covariance of return type in linear extension?
1476 var superprop = superprops.first
1477 assert superprop isa MMethodDef
1478
1479 var msignature = v.resolve_signature_for(superprop, recvtype, true)
1480 var args = self.n_args.to_a
1481 if args.length > 0 then
1482 v.check_signature(self, args, mproperty.name, msignature)
1483 end
1484 self.mtype = msignature.return_mtype
1485 self.is_typed = true
1486 v.mpropdef.has_supercall = true
1487 end
1488
1489 private fun process_superinit(v: TypeVisitor)
1490 do
1491 var recvtype = v.nclassdef.mclassdef.bound_mtype
1492 var mproperty = v.mpropdef.mproperty
1493 var superprop: nullable MMethodDef = null
1494 for msupertype in v.nclassdef.mclassdef.supertypes do
1495 msupertype = msupertype.anchor_to(v.mmodule, recvtype)
1496 var errcount = v.modelbuilder.toolcontext.error_count
1497 var candidate = v.try_get_mproperty_by_name2(self, msupertype, mproperty.name).as(nullable MMethod)
1498 if candidate == null then
1499 if v.modelbuilder.toolcontext.error_count > errcount then return # Forard error
1500 continue # Try next super-class
1501 end
1502 if superprop != null and superprop.mproperty != candidate then
1503 v.error(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1504 return
1505 end
1506 var candidatedefs = candidate.lookup_definitions(v.mmodule, recvtype)
1507 if superprop != null then
1508 if superprop == candidatedefs.first then continue
1509 candidatedefs.add(superprop)
1510 end
1511 if candidatedefs.length > 1 then
1512 v.error(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1513 return
1514 end
1515 superprop = candidatedefs.first
1516 end
1517 if superprop == null then
1518 v.error(self, "Error: No super method to call for {mproperty}.")
1519 return
1520 end
1521 self.mproperty = superprop.mproperty
1522
1523 var args = self.n_args.to_a
1524 var msignature = v.resolve_signature_for(superprop, recvtype, true)
1525 if args.length > 0 then
1526 v.check_signature(self, args, mproperty.name, msignature)
1527 else
1528 # TODO: Check signature
1529 end
1530
1531 self.is_typed = true
1532 end
1533 end
1534
1535 ####
1536
1537 redef class ANewExpr
1538 # @depreciated use `callsite`
1539 fun mproperty: nullable MMethod do return self.callsite.mproperty
1540
1541 # The constructor invoked by the new.
1542 var callsite: nullable CallSite
1543
1544 redef fun accept_typing(v)
1545 do
1546 var recvtype = v.resolve_mtype(self.n_type)
1547 if recvtype == null then return
1548 self.mtype = recvtype
1549
1550 if not recvtype isa MClassType then
1551 if recvtype isa MNullableType then
1552 v.error(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1553 return
1554 else
1555 v.error(self, "Type error: cannot instantiate the formal type {recvtype}.")
1556 return
1557 end
1558 end
1559
1560 var name: String
1561 var nid = self.n_id
1562 if nid != null then
1563 name = nid.text
1564 else
1565 name = "init"
1566 end
1567 var callsite = v.get_method(self, recvtype, name, false)
1568 if callsite == null then return
1569
1570 self.callsite = callsite
1571
1572 if not callsite.mproperty.is_init_for(recvtype.mclass) then
1573 v.error(self, "Error: {name} is not a constructor.")
1574 return
1575 end
1576
1577 var args = n_args.to_a
1578 callsite.check_signature(v, args)
1579 end
1580 end
1581
1582 ####
1583
1584 redef class AAttrFormExpr
1585 # The attribute acceded.
1586 var mproperty: nullable MAttribute
1587
1588 # The static type of the attribute.
1589 var attr_type: nullable MType
1590
1591 # Resolve the attribute acceded.
1592 private fun resolve_property(v: TypeVisitor)
1593 do
1594 var recvtype = v.visit_expr(self.n_expr)
1595 if recvtype == null then return # Skip error
1596 var name = self.n_id.text
1597 if recvtype isa MNullType then
1598 v.error(self, "Error: Attribute '{name}' access on 'null'.")
1599 return
1600 end
1601
1602 var unsafe_type = v.anchor_to(recvtype)
1603 var mproperty = v.try_get_mproperty_by_name2(self, unsafe_type, name)
1604 if mproperty == null then
1605 v.modelbuilder.error(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1606 return
1607 end
1608 assert mproperty isa MAttribute
1609 self.mproperty = mproperty
1610
1611 var mpropdefs = mproperty.lookup_definitions(v.mmodule, unsafe_type)
1612 assert mpropdefs.length == 1
1613 var mpropdef = mpropdefs.first
1614 var attr_type = mpropdef.static_mtype.as(not null)
1615 attr_type = v.resolve_for(attr_type, recvtype, self.n_expr isa ASelfExpr)
1616 self.attr_type = attr_type
1617 end
1618 end
1619
1620 redef class AAttrExpr
1621 redef fun accept_typing(v)
1622 do
1623 self.resolve_property(v)
1624 self.mtype = self.attr_type
1625 end
1626 end
1627
1628
1629 redef class AAttrAssignExpr
1630 redef fun accept_typing(v)
1631 do
1632 self.resolve_property(v)
1633 var mtype = self.attr_type
1634
1635 v.visit_expr_subtype(self.n_value, mtype)
1636 self.is_typed = true
1637 end
1638 end
1639
1640 redef class AAttrReassignExpr
1641 redef fun accept_typing(v)
1642 do
1643 self.resolve_property(v)
1644 var mtype = self.attr_type
1645 if mtype == null then return # Skip error
1646
1647 self.resolve_reassignment(v, mtype, mtype)
1648
1649 self.is_typed = true
1650 end
1651 end
1652
1653 redef class AIssetAttrExpr
1654 redef fun accept_typing(v)
1655 do
1656 self.resolve_property(v)
1657 var mtype = self.attr_type
1658 if mtype == null then return # Skip error
1659
1660 var recvtype = self.n_expr.mtype.as(not null)
1661 var bound = v.resolve_for(mtype, recvtype, false)
1662 if bound isa MNullableType then
1663 v.error(self, "Error: isset on a nullable attribute.")
1664 end
1665 self.mtype = v.type_bool(self)
1666 end
1667 end
1668
1669 ###
1670
1671 redef class AClosureCallExpr
1672 redef fun accept_typing(v)
1673 do
1674 var variable = self.variable
1675 if variable == null then return # Skip error
1676
1677 var recvtype = v.nclassdef.mclassdef.bound_mtype
1678 var msignature = variable.declared_type.as(not null)
1679 msignature = v.resolve_for(msignature, recvtype, false).as(MSignature)
1680
1681 var args = n_args.to_a
1682 v.check_signature(self, args, variable.name, msignature)
1683
1684 self.is_typed = true
1685 self.mtype = msignature.return_mtype
1686 end
1687 end
1688
1689 redef class AClosureDef
1690 var mclosure: nullable MParameter
1691
1692 private fun accept_typing(v: TypeVisitor, mparameter: MParameter)
1693 do
1694 var variables = self.variables
1695 if variables == null then return
1696
1697 self.mclosure = mparameter
1698 var msignature = mparameter.mtype.as(MSignature)
1699
1700 if msignature.arity != variables.length then
1701 v.error(self, "Type error: closure {mparameter.name} expects {msignature.arity} parameters, {variables.length} given")
1702 return
1703 end
1704
1705 for i in [0..variables.length[ do
1706 variables[i].declared_type = msignature.mparameters[i].mtype
1707 end
1708
1709 v.visit_stmt(self.n_expr)
1710 end
1711 end
1712
1713 ###
1714
1715 redef class ADebugTypeExpr
1716 redef fun accept_typing(v)
1717 do
1718 var expr = v.visit_expr(self.n_expr)
1719 if expr == null then return
1720 var unsafe = v.anchor_to(expr)
1721 var ntype = self.n_type
1722 var mtype = v.resolve_mtype(ntype)
1723 if mtype != null and mtype != expr then
1724 var umtype = v.anchor_to(mtype)
1725 v.modelbuilder.warning(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
1726 end
1727 self.is_typed = true
1728 end
1729 end