model: new class MErrorType as using MBottomType was abuse
[nit.git] / src / semantize / 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 modelize
22 import local_var_init
23 import literal
24
25 redef class ToolContext
26 var typing_phase: Phase = new TypingPhase(self, [flow_phase, modelize_property_phase, local_var_init_phase])
27 end
28
29 private class TypingPhase
30 super Phase
31 redef fun process_npropdef(npropdef) do npropdef.do_typing(toolcontext.modelbuilder)
32 end
33
34 private class TypeVisitor
35 var modelbuilder: ModelBuilder
36
37 # The module of the analysis
38 # Used to correctly query the model
39 var mmodule: MModule
40
41 # The static type of the receiver
42 # Mainly used for type tests and type resolutions
43 var anchor: nullable MClassType = null
44
45 # The analyzed mclassdef
46 var mclassdef: nullable MClassDef = null
47
48 # The analyzed property
49 var mpropdef: nullable MPropDef
50
51 var selfvariable = new Variable("self")
52
53 # Is `self` use restricted?
54 # * no explicit `self`
55 # * method called on the implicit self must be top-level
56 # Currently only used for `new` factory since there is no valid receiver inside
57 var is_toplevel_context = false
58
59 init
60 do
61 var mpropdef = self.mpropdef
62
63 if mpropdef != null then
64 self.mpropdef = mpropdef
65 var mclassdef = mpropdef.mclassdef
66 self.mclassdef = mclassdef
67 self.anchor = mclassdef.bound_mtype
68
69 var mclass = mclassdef.mclass
70
71 var selfvariable = new Variable("self")
72 self.selfvariable = selfvariable
73 selfvariable.declared_type = mclass.mclass_type
74
75 var mprop = mpropdef.mproperty
76 if mprop isa MMethod and mprop.is_new then
77 is_toplevel_context = true
78 end
79 end
80 end
81
82 fun anchor_to(mtype: MType): MType
83 do
84 var anchor = anchor
85 if anchor == null then
86 assert not mtype.need_anchor
87 return mtype
88 end
89 return mtype.anchor_to(mmodule, anchor)
90 end
91
92 fun is_subtype(sub, sup: MType): Bool
93 do
94 return sub.is_subtype(mmodule, anchor, sup)
95 end
96
97 fun resolve_for(mtype, subtype: MType, for_self: Bool): MType
98 do
99 #print "resolve_for {mtype} sub={subtype} forself={for_self} mmodule={mmodule} anchor={anchor}"
100 var res = mtype.resolve_for(subtype, anchor, mmodule, not for_self)
101 return res
102 end
103
104 # Check that `sub` is a subtype of `sup`.
105 # If `sub` is not a valid suptype, then display an error on `node` an return null.
106 # If `sub` is a safe subtype of `sup` then return `sub`.
107 # If `sub` is an unsafe subtype (ie an implicit cast is required), then return `sup`.
108 #
109 # The point of the return type is to determinate the usable type on an expression when `autocast` is true:
110 # If the suptype is safe, then the return type is the one on the expression typed by `sub`.
111 # Is the subtype is unsafe, then the return type is the one of an implicit cast on `sup`.
112 fun check_subtype(node: ANode, sub, sup: MType, autocast: Bool): nullable MType
113 do
114 if self.is_subtype(sub, sup) then return sub
115 if autocast and self.is_subtype(sub, self.anchor_to(sup)) then
116 # FIXME workaround to the current unsafe typing policy. To remove once fixed virtual types exists.
117 #node.debug("Unsafe typing: expected {sup}, got {sub}")
118 return sup
119 end
120 if sup isa MErrorType then return null # Skip error
121 if sub.need_anchor then
122 var u = anchor_to(sub)
123 self.modelbuilder.error(node, "Type Error: expected `{sup}`, got `{sub}: {u}`.")
124 else
125 self.modelbuilder.error(node, "Type Error: expected `{sup}`, got `{sub}`.")
126 end
127 return null
128 end
129
130 # Visit an expression and do not care about the return value
131 fun visit_stmt(nexpr: nullable AExpr)
132 do
133 if nexpr == null then return
134 nexpr.accept_typing(self)
135 end
136
137 # Visit an expression and expects that it is not a statement
138 # Return the type of the expression
139 # Display an error and return null if:
140 # * the type cannot be determined or
141 # * `nexpr` is a statement
142 fun visit_expr(nexpr: AExpr): nullable MType
143 do
144 nexpr.accept_typing(self)
145 var mtype = nexpr.mtype
146 if mtype != null then return mtype
147 if not nexpr.is_typed then
148 if not self.modelbuilder.toolcontext.error_count > 0 then # check that there is really an error
149 if self.modelbuilder.toolcontext.verbose_level > 1 then
150 nexpr.debug("No return type but no error.")
151 end
152 end
153 return null # forward error
154 end
155 var more_message = null
156 var p = nexpr.parent
157 if p != null then more_message = p.bad_expr_message(nexpr)
158 if more_message == null then more_message = "" else more_message = " " + more_message
159 self.error(nexpr, "Error: expected an expression{more_message}.")
160 return null
161 end
162
163 # Visit an expression and expect its static type is a least a `sup`
164 # Return the type of the expression or null if
165 # * the type cannot be determined or
166 # * `nexpr` is a statement or
167 # * `nexpr` is not a `sup`
168 fun visit_expr_subtype(nexpr: AExpr, sup: nullable MType): nullable MType
169 do
170 var sub = visit_expr(nexpr)
171 if sub == null then return null # Forward error
172
173 if sup == null then return null # Forward error
174
175 var res = check_subtype(nexpr, sub, sup, true)
176 if res != sub then
177 nexpr.implicit_cast_to = res
178 end
179 return res
180 end
181
182 # Visit an expression and expect its static type is a `Bool`
183 # Return the type of the expression or null if
184 # * the type cannot be determined or
185 # * `nexpr` is a statement or
186 # * `nexpr` is not a `Bool`
187 fun visit_expr_bool(nexpr: AExpr): nullable MType
188 do
189 return self.visit_expr_subtype(nexpr, self.type_bool(nexpr))
190 end
191
192
193 fun check_expr_cast(node: ANode, nexpr: AExpr, ntype: AType): nullable MType
194 do
195 var sub = nexpr.mtype
196 if sub == null then return null # Forward error
197
198 var sup = ntype.mtype
199 if sup == null then return null # Forward error
200
201 if sup == sub then
202 self.modelbuilder.warning(node, "useless-type-test", "Warning: expression is already a `{sup}`.")
203 else if self.is_subtype(sub, sup) then
204 self.modelbuilder.warning(node, "useless-type-test", "Warning: expression is already a `{sup}` since it is a `{sub}`.")
205 end
206 return sup
207 end
208
209 # Can `mtype` be null (up to the current knowledge)?
210 fun can_be_null(mtype: MType): Bool
211 do
212 if mtype isa MNullableType or mtype isa MNullType then return true
213 if mtype isa MFormalType then
214 var x = anchor_to(mtype)
215 if x isa MNullableType or x isa MNullType then return true
216 end
217 return false
218 end
219
220 # Check that `mtype` can be null (up to the current knowledge).
221 #
222 # If not then display a `useless-null-test` warning on node and return false.
223 # Else return true.
224 fun check_can_be_null(anode: ANode, mtype: MType): Bool
225 do
226 if mtype isa MNullType then
227 modelbuilder.warning(anode, "useless-null-test", "Warning: expression is always `null`.")
228 return true
229 end
230 if can_be_null(mtype) then return true
231
232 if mtype isa MFormalType then
233 var res = anchor_to(mtype)
234 modelbuilder.warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}: {res}`.")
235 else
236 modelbuilder.warning(anode, "useless-null-test", "Warning: expression is not null, since it is a `{mtype}`.")
237 end
238 return false
239 end
240
241 # Special verification on != and == for null
242 # Return true
243 fun null_test(anode: ABinopExpr)
244 do
245 var mtype = anode.n_expr.mtype
246 var mtype2 = anode.n_expr2.mtype
247
248 if mtype == null or mtype2 == null then return
249
250 if not mtype2 isa MNullType then return
251
252 # Check of useless null
253 if not can_be_null(mtype) then return
254
255 if mtype isa MNullType then
256 # Because of type adaptation, we cannot just stop here
257 # so return use `null` as a bottom type that will be merged easily (cf) `merge_types`
258 mtype = null
259 else
260 mtype = mtype.as_notnull
261 end
262
263 # Check for type adaptation
264 var variable = anode.n_expr.its_variable
265 if variable == null then return
266
267 # One is null (mtype2 see above) the other is not null
268 if anode isa AEqExpr then
269 anode.after_flow_context.when_true.set_var(self, variable, mtype2)
270 anode.after_flow_context.when_false.set_var(self, variable, mtype)
271 else if anode isa ANeExpr then
272 anode.after_flow_context.when_false.set_var(self, variable, mtype2)
273 anode.after_flow_context.when_true.set_var(self, variable, mtype)
274 else
275 abort
276 end
277 end
278
279 fun try_get_mproperty_by_name2(anode: ANode, mtype: MType, name: String): nullable MProperty
280 do
281 return self.modelbuilder.try_get_mproperty_by_name2(anode, mmodule, mtype, name)
282 end
283
284 fun resolve_mtype(node: AType): nullable MType
285 do
286 return self.modelbuilder.resolve_mtype(mmodule, mclassdef, node)
287 end
288
289 fun try_get_mclass(node: ANode, name: String): nullable MClass
290 do
291 var mclass = modelbuilder.try_get_mclass_by_name(node, mmodule, name)
292 return mclass
293 end
294
295 fun get_mclass(node: ANode, name: String): nullable MClass
296 do
297 var mclass = modelbuilder.get_mclass_by_name(node, mmodule, name)
298 return mclass
299 end
300
301 fun type_bool(node: ANode): nullable MType
302 do
303 var mclass = self.get_mclass(node, "Bool")
304 if mclass == null then return null
305 return mclass.mclass_type
306 end
307
308 fun get_method(node: ANode, recvtype: MType, name: String, recv_is_self: Bool): nullable CallSite
309 do
310 var unsafe_type = self.anchor_to(recvtype)
311
312 #debug("recv: {recvtype} (aka {unsafe_type})")
313 if recvtype isa MNullType then
314 var objclass = get_mclass(node, "Object")
315 if objclass == null then return null # Forward error
316 unsafe_type = objclass.mclass_type
317 end
318
319 var mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
320 if name == "new" and mproperty == null then
321 name = "init"
322 mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
323 end
324
325 if mproperty == null then
326 if recv_is_self then
327 self.modelbuilder.error(node, "Error: method or variable `{name}` unknown in `{recvtype}`.")
328 else if recvtype.need_anchor then
329 self.modelbuilder.error(node, "Error: method `{name}` does not exists in `{recvtype}: {unsafe_type}`.")
330 else
331 self.modelbuilder.error(node, "Error: method `{name}` does not exists in `{recvtype}`.")
332 end
333 return null
334 end
335
336 assert mproperty isa MMethod
337
338 # `null` only accepts some methods of object.
339 if recvtype isa MNullType and not mproperty.is_null_safe then
340 self.error(node, "Error: method `{name}` called on `null`.")
341 return null
342 else if unsafe_type isa MNullableType and not mproperty.is_null_safe then
343 modelbuilder.advice(node, "call-on-nullable", "Warning: method call on a nullable receiver `{recvtype}`.")
344 end
345
346 if is_toplevel_context and recv_is_self and not mproperty.is_toplevel then
347 error(node, "Error: `{name}` is not a top-level method, thus need a receiver.")
348 end
349 if not recv_is_self and mproperty.is_toplevel then
350 error(node, "Error: cannot call `{name}`, a top-level method, with a receiver.")
351 end
352
353 if mproperty.visibility == protected_visibility and not recv_is_self and self.mmodule.visibility_for(mproperty.intro_mclassdef.mmodule) < intrude_visibility and not modelbuilder.toolcontext.opt_ignore_visibility.value then
354 self.modelbuilder.error(node, "Error: method `{name}` is protected and can only accessed by `self`.")
355 return null
356 end
357
358 var info = mproperty.deprecation
359 if info != null and self.mpropdef.mproperty.deprecation == null then
360 var mdoc = info.mdoc
361 if mdoc != null then
362 self.modelbuilder.warning(node, "deprecated-method", "Deprecation Warning: method `{name}` is deprecated: {mdoc.content.first}")
363 else
364 self.modelbuilder.warning(node, "deprecated-method", "Deprecation Warning: method `{name}` is deprecated.")
365 end
366 end
367
368 var propdefs = mproperty.lookup_definitions(self.mmodule, unsafe_type)
369 var mpropdef
370 if propdefs.length == 0 then
371 self.modelbuilder.error(node, "Type Error: no definition found for property `{name}` in `{unsafe_type}`.")
372 return null
373 else if propdefs.length == 1 then
374 mpropdef = propdefs.first
375 else
376 self.modelbuilder.warning(node, "property-conflict", "Warning: conflicting property definitions for property `{name}` in `{unsafe_type}`: {propdefs.join(" ")}")
377 mpropdef = mproperty.intro
378 end
379
380
381 var msignature = mpropdef.new_msignature or else mpropdef.msignature
382 if msignature == null then return null # skip error
383 msignature = resolve_for(msignature, recvtype, recv_is_self).as(MSignature)
384
385 var erasure_cast = false
386 var rettype = mpropdef.msignature.return_mtype
387 if not recv_is_self and rettype != null then
388 rettype = rettype.undecorate
389 if rettype isa MParameterType then
390 var erased_rettype = msignature.return_mtype
391 assert erased_rettype != null
392 #node.debug("Erasure cast: Really a {rettype} but unsafely a {erased_rettype}")
393 erasure_cast = true
394 end
395 end
396
397 var callsite = new CallSite(node.hot_location, recvtype, mmodule, anchor, recv_is_self, mproperty, mpropdef, msignature, erasure_cast)
398 return callsite
399 end
400
401 fun try_get_method(node: ANode, recvtype: MType, name: String, recv_is_self: Bool): nullable CallSite
402 do
403 var unsafe_type = self.anchor_to(recvtype)
404 var mproperty = self.try_get_mproperty_by_name2(node, unsafe_type, name)
405 if mproperty == null then return null
406 return get_method(node, recvtype, name, recv_is_self)
407 end
408
409
410 # Visit the expressions of args and check their conformity with the corresponding type in signature
411 # The point of this method is to handle varargs correctly
412 # Note: The signature must be correctly adapted
413 fun check_signature(node: ANode, args: Array[AExpr], mproperty: MProperty, msignature: MSignature): nullable SignatureMap
414 do
415 var vararg_rank = msignature.vararg_rank
416 if vararg_rank >= 0 then
417 if args.length < msignature.arity then
418 modelbuilder.error(node, "Error: expected at least {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
419 return null
420 end
421 else if args.length != msignature.arity then
422 # Too much argument
423 if args.length > msignature.arity then
424 modelbuilder.error(node, "Error: expected {msignature.arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
425 return null
426 end
427 # Other cases are managed later
428 end
429
430
431 #debug("CALL {unsafe_type}.{msignature}")
432
433 # Associate each parameter to a position in the arguments
434 var map = new SignatureMap
435
436 # Special case for the isolated last argument
437 # TODO: reify this method characteristics (where? the param, the signature, the method?)
438 var last_is_padded = mproperty.name.chars.last == '='
439 var nbargs = args.length
440 if last_is_padded then
441 nbargs -= 1
442 assert not args.last isa ANamedargExpr
443 map.map[msignature.arity - 1] = args.length - 1
444 self.visit_expr_subtype(args.last, msignature.mparameters.last.mtype)
445 end
446
447 # First, handle named arguments
448 for i in [0..args.length[ do
449 var e = args[i]
450 if not e isa ANamedargExpr then continue
451 var name = e.n_id.text
452 var param = msignature.mparameter_by_name(name)
453 if param == null then
454 modelbuilder.error(e.n_id, "Error: no parameter `{name}` for `{mproperty}{msignature}`.")
455 return null
456 end
457 var idx = msignature.mparameters.index_of(param)
458 var prev = map.map.get_or_null(idx)
459 if prev != null then
460 modelbuilder.error(e, "Error: parameter `{name}` already associated with argument #{prev} for `{mproperty}{msignature}`.")
461 return null
462 end
463 map.map[idx] = i
464 e.mtype = self.visit_expr_subtype(e.n_expr, param.mtype)
465 end
466
467 # Number of minimum mandatory remaining parameters
468 var min_arity = 0
469
470 # Second, associate remaining parameters
471 var vararg_decl = args.length - msignature.arity
472 var j = 0
473 for i in [0..msignature.arity[ do
474 # Skip parameters associated by name
475 if map.map.has_key(i) then continue
476
477 var param = msignature.mparameters[i]
478
479 # Search the next free argument: skip named arguments since they are already associated
480 while j < nbargs and args[j] isa ANamedargExpr do j += 1
481 if j >= nbargs then
482 if not param.mtype isa MNullableType then
483 min_arity = j + 1
484 end
485 j += 1
486 continue
487 end
488 var arg = args[j]
489 map.map[i] = j
490 j += 1
491
492 if i == vararg_rank then
493 j += vararg_decl
494 continue # skip the vararg
495 end
496
497 if not param.is_vararg then
498 var paramtype = param.mtype
499 self.visit_expr_subtype(arg, paramtype)
500 else
501 check_one_vararg(arg, param)
502 end
503 end
504
505 if min_arity > 0 then
506 if last_is_padded then min_arity += 1
507 if min_arity < msignature.arity then
508 modelbuilder.error(node, "Error: expected at least {min_arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
509 else
510 modelbuilder.error(node, "Error: expected {min_arity} argument(s) for `{mproperty}{msignature}`; got {args.length}. See introduction at `{mproperty.full_name}`.")
511 end
512 return null
513 end
514
515 # Third, check varargs
516 if vararg_rank >= 0 then
517 var paramtype = msignature.mparameters[vararg_rank].mtype
518 var first = args[vararg_rank]
519 if vararg_decl == 0 then
520 if not check_one_vararg(first, msignature.mparameters[vararg_rank]) then return null
521 else
522 first.vararg_decl = vararg_decl + 1
523 for i in [vararg_rank..vararg_rank+vararg_decl] do
524 self.visit_expr_subtype(args[i], paramtype)
525 end
526 end
527 end
528
529 return map
530 end
531
532 # Check an expression as a single vararg.
533 # The main point of the method if to handle the case of reversed vararg (see `AVarargExpr`)
534 fun check_one_vararg(arg: AExpr, param: MParameter): Bool
535 do
536 var paramtype = param.mtype
537 var mclass = get_mclass(arg, "Array")
538 if mclass == null then return false # Forward error
539 var array_mtype = mclass.get_mtype([paramtype])
540 if arg isa AVarargExpr then
541 self.visit_expr_subtype(arg.n_expr, array_mtype)
542 arg.mtype = arg.n_expr.mtype
543 else
544 # only one vararg, maybe `...` was forgot, so be gentle!
545 var t = visit_expr(arg)
546 if t == null then return false # Forward error
547 if not is_subtype(t, paramtype) and is_subtype(t, array_mtype) then
548 # Not acceptable but could be a `...`
549 error(arg, "Type Error: expected `{paramtype}`, got `{t}`. Is an ellipsis `...` missing on the argument?")
550 return false
551 end
552 # Standard valid vararg, finish the job
553 arg.vararg_decl = 1
554 self.visit_expr_subtype(arg, paramtype)
555 end
556 return true
557 end
558
559 fun error(node: ANode, message: String)
560 do
561 self.modelbuilder.error(node, message)
562 end
563
564 fun get_variable(node: AExpr, variable: Variable): nullable MType
565 do
566 if not variable.is_adapted then return variable.declared_type
567
568 var flow = node.after_flow_context
569 if flow == null then return null # skip error
570
571 if flow.vars.has_key(variable) then
572 return flow.vars[variable]
573 else
574 #node.debug("*** START Collected for {variable}")
575 var mtypes = flow.collect_types(variable)
576 #node.debug("**** END Collected for {variable}")
577 if mtypes.length == 0 then
578 return variable.declared_type
579 else if mtypes.length == 1 then
580 return mtypes.first
581 else
582 var res = merge_types(node,mtypes)
583 if res == null then res = variable.declared_type
584 return res
585 end
586 end
587 end
588
589 # Some variables where type-adapted during the visit
590 var dirty = false
591
592 # Some loops had been visited during the visit
593 var has_loop = false
594
595 fun set_variable(node: AExpr, variable: Variable, mtype: nullable MType)
596 do
597 var flow = node.after_flow_context
598 assert flow != null
599
600 flow.set_var(self, variable, mtype)
601 end
602
603 fun merge_types(node: ANode, col: Array[nullable MType]): nullable MType
604 do
605 if col.length == 1 then return col.first
606 for t1 in col do
607 if t1 == null then continue # return null
608 var found = true
609 for t2 in col do
610 if t2 == null then continue # return null
611 if can_be_null(t2) and not can_be_null(t1) then
612 t1 = t1.as_nullable
613 end
614 if not is_subtype(t2, t1) then found = false
615 end
616 if found then
617 #print "merge {col.join(" ")} -> {t1}"
618 return t1
619 end
620 end
621 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
622 return null
623 end
624 end
625
626 # Mapping between parameters and arguments in a call.
627 #
628 # Parameters and arguments are not stored in the class but referenced by their position (starting from 0)
629 #
630 # The point of this class is to help engine and other things to map arguments in the AST to parameters of the model.
631 class SignatureMap
632 # Associate a parameter to an argument
633 var map = new ArrayMap[Int, Int]
634 end
635
636 # A specific method call site with its associated informations.
637 class CallSite
638 super MEntity
639
640 redef var location: Location
641
642 # The static type of the receiver (possibly unresolved)
643 var recv: MType
644
645 # The module where the callsite is present
646 var mmodule: MModule
647
648 # The anchor to use with `recv` or `msignature`
649 var anchor: nullable MClassType
650
651 # Is the receiver self?
652 # If "for_self", virtual types of the signature are kept
653 # If "not_for_self", virtual type are erased
654 var recv_is_self: Bool
655
656 # The designated method
657 var mproperty: MMethod
658
659 # The statically designated method definition
660 # The most specif one, it is.
661 var mpropdef: MMethodDef
662
663 # The resolved signature for the receiver
664 var msignature: MSignature
665
666 # Is a implicit cast required on erasure typing policy?
667 var erasure_cast: Bool
668
669 # The mapping used on the call to associate arguments to parameters
670 # If null then no specific association is required.
671 var signaturemap: nullable SignatureMap = null
672
673 private fun check_signature(v: TypeVisitor, node: ANode, args: Array[AExpr]): Bool
674 do
675 var map = v.check_signature(node, args, self.mproperty, self.msignature)
676 signaturemap = map
677 if map == null then is_broken = true
678 return map == null
679 end
680 end
681
682 redef class Variable
683 # The declared type of the variable
684 var declared_type: nullable MType is writable
685
686 # Was the variable type-adapted?
687 # This is used to speedup type retrieval while it remains `false`
688 private var is_adapted = false
689 end
690
691 redef class FlowContext
692 # Store changes of types because of type evolution
693 private var vars = new HashMap[Variable, nullable MType]
694
695 # Adapt the variable to a static type
696 # Warning1: do not modify vars directly.
697 # Warning2: sub-flow may have cached a unadapted variable
698 private fun set_var(v: TypeVisitor, variable: Variable, mtype: nullable MType)
699 do
700 if variable.declared_type == mtype and not variable.is_adapted then return
701 if vars.has_key(variable) and vars[variable] == mtype then return
702 self.vars[variable] = mtype
703 v.dirty = true
704 variable.is_adapted = true
705 #node.debug "set {variable} to {mtype or else "X"}"
706 end
707
708 # Look in the flow and previous flow and collect all first reachable type adaptation of a local variable
709 private fun collect_types(variable: Variable): Array[nullable MType]
710 do
711 #node.debug "flow for {variable}"
712 var res = new Array[nullable MType]
713
714 var todo = [self]
715 var seen = new HashSet[FlowContext]
716 while not todo.is_empty do
717 var f = todo.pop
718 if f.is_unreachable then continue
719 if seen.has(f) then continue
720 seen.add f
721
722 if f.vars.has_key(variable) then
723 # Found something. Collect it and do not process further on this path
724 res.add f.vars[variable]
725 #f.node.debug "process {variable}: got {f.vars[variable] or else "X"}"
726 else
727 todo.add_all f.previous
728 todo.add_all f.loops
729 if f.previous.is_empty then
730 # Root flowcontext mean a parameter or something related
731 res.add variable.declared_type
732 #f.node.debug "root process {variable}: got {variable.declared_type or else "X"}"
733 end
734 end
735 end
736 #self.node.debug "##### end flow for {variable}: {res.join(" ")}"
737 return res
738 end
739 end
740
741 redef class APropdef
742 # The entry point of the whole typing analysis
743 fun do_typing(modelbuilder: ModelBuilder)
744 do
745 end
746
747 # The variable associated to the receiver (if any)
748 var selfvariable: nullable Variable
749 end
750
751 redef class AMethPropdef
752 redef fun do_typing(modelbuilder: ModelBuilder)
753 do
754 var mpropdef = self.mpropdef
755 if mpropdef == null then return # skip error
756
757 var v = new TypeVisitor(modelbuilder, mpropdef.mclassdef.mmodule, mpropdef)
758 self.selfvariable = v.selfvariable
759
760 var mmethoddef = self.mpropdef.as(not null)
761 var msignature = mmethoddef.msignature
762 if msignature == null then return # skip error
763 for i in [0..msignature.arity[ do
764 var mtype = msignature.mparameters[i].mtype
765 if msignature.vararg_rank == i then
766 var arrayclass = v.get_mclass(self.n_signature.n_params[i], "Array")
767 if arrayclass == null then return # Skip error
768 mtype = arrayclass.get_mtype([mtype])
769 end
770 var variable = self.n_signature.n_params[i].variable
771 assert variable != null
772 variable.declared_type = mtype
773 end
774
775 var nblock = self.n_block
776 if nblock == null then return
777
778 loop
779 v.dirty = false
780 v.visit_stmt(nblock)
781 if not v.has_loop or not v.dirty then break
782 end
783
784 var post_visitor = new PostTypingVisitor(v)
785 post_visitor.enter_visit(self)
786
787 if not nblock.after_flow_context.is_unreachable and msignature.return_mtype != null then
788 # We reach the end of the function without having a return, it is bad
789 v.error(self, "Error: reached end of function; expected `return` with a value.")
790 end
791 end
792 end
793
794 private class PostTypingVisitor
795 super Visitor
796 var type_visitor: TypeVisitor
797 redef fun visit(n) do
798 n.visit_all(self)
799 n.accept_post_typing(type_visitor)
800 if n isa AExpr and n.mtype == null and not n.is_typed then
801 n.is_broken = true
802 end
803 end
804 end
805
806 redef class ANode
807 private fun accept_post_typing(v: TypeVisitor) do end
808
809 # An additional information message to explain the role of a child expression.
810 #
811 # The point of the method is to allow some kind of double dispatch so the parent
812 # choose how to describe its children.
813 private fun bad_expr_message(child: AExpr): nullable String do return null
814 end
815
816 redef class AAttrPropdef
817 redef fun do_typing(modelbuilder: ModelBuilder)
818 do
819 if not has_value then return
820
821 var mpropdef = self.mreadpropdef
822 if mpropdef == null or mpropdef.msignature == null then return # skip error
823
824 var v = new TypeVisitor(modelbuilder, mpropdef.mclassdef.mmodule, mpropdef)
825 self.selfvariable = v.selfvariable
826
827 var nexpr = self.n_expr
828 if nexpr != null then
829 var mtype = self.mtype
830 v.visit_expr_subtype(nexpr, mtype)
831 end
832 var nblock = self.n_block
833 if nblock != null then
834 v.visit_stmt(nblock)
835 if not nblock.after_flow_context.is_unreachable then
836 # We reach the end of the init without having a return, it is bad
837 v.error(self, "Error: reached end of block; expected `return`.")
838 end
839 end
840 end
841 end
842
843 ###
844
845 redef class AExpr
846 # The static type of the expression.
847 # null if self is a statement or in case of error
848 var mtype: nullable MType = null
849
850 # Is the statement correctly typed?
851 # Used to distinguish errors and statements when `mtype == null`
852 var is_typed: Bool = false
853
854 # If required, the following implicit cast `.as(XXX)`
855 # Such a cast may by required after evaluating the expression when
856 # a unsafe operation is detected (silently accepted by the Nit language).
857 # The attribute is computed by `check_subtype`
858 var implicit_cast_to: nullable MType = null
859
860 # Return the variable read (if any)
861 # Used to perform adaptive typing
862 fun its_variable: nullable Variable do return null
863
864 private fun accept_typing(v: TypeVisitor)
865 do
866 v.error(self, "no implemented accept_typing for {self.class_name}")
867 end
868
869 # Is non-null if `self` is a leaf of a comprehension array construction.
870 # In this case, the enclosing literal array node is designated.
871 # The result of the evaluation of `self` must be
872 # stored inside the designated array (there is an implicit `push`)
873 var comprehension: nullable AArrayExpr = null
874
875 # It indicates the number of arguments collected as a vararg.
876 #
877 # When 0, the argument is used as is, without transformation.
878 # When 1, the argument is transformed into an singleton array.
879 # Above 1, the arguments and the next ones are transformed into a common array.
880 #
881 # This attribute is meaning less on expressions not used as attributes.
882 var vararg_decl: Int = 0
883 end
884
885 redef class ABlockExpr
886 redef fun accept_typing(v)
887 do
888 for e in self.n_expr do v.visit_stmt(e)
889 self.is_typed = true
890 end
891
892 # The type of a blockexpr is the one of the last expression (or null if empty)
893 redef fun mtype
894 do
895 if self.n_expr.is_empty then return null
896 return self.n_expr.last.mtype
897 end
898 end
899
900 redef class AVardeclExpr
901 redef fun accept_typing(v)
902 do
903 var variable = self.variable
904 if variable == null then return # Skip error
905
906 var ntype = self.n_type
907 var mtype: nullable MType
908 if ntype == null then
909 mtype = null
910 else
911 mtype = v.resolve_mtype(ntype)
912 if mtype == null then return # Skip error
913 end
914
915 var nexpr = self.n_expr
916 if nexpr != null then
917 if mtype != null then
918 var etype = v.visit_expr_subtype(nexpr, mtype)
919 if etype == mtype then
920 assert ntype != null
921 v.modelbuilder.advice(ntype, "useless-type", "Warning: useless type definition for variable `{variable.name}`")
922 end
923 else
924 mtype = v.visit_expr(nexpr)
925 if mtype == null then return # Skip error
926 end
927 end
928
929 var decltype = mtype
930 if mtype == null or mtype isa MNullType then
931 var objclass = v.get_mclass(self, "Object")
932 if objclass == null then return # skip error
933 decltype = objclass.mclass_type.as_nullable
934 if mtype == null then mtype = decltype
935 end
936
937 variable.declared_type = decltype
938 v.set_variable(self, variable, mtype)
939
940 #debug("var {variable}: {mtype}")
941
942 self.mtype = mtype
943 self.is_typed = true
944 end
945 end
946
947 redef class AVarExpr
948 redef fun its_variable do return self.variable
949 redef fun accept_typing(v)
950 do
951 var variable = self.variable
952 if variable == null then return # Skip error
953
954 var mtype = v.get_variable(self, variable)
955 if mtype != null then
956 #debug("{variable} is {mtype}")
957 else
958 #debug("{variable} is untyped")
959 end
960
961 self.mtype = mtype
962 end
963 end
964
965 redef class AVarAssignExpr
966 redef fun accept_typing(v)
967 do
968 var variable = self.variable
969 assert variable != null
970
971 var mtype = v.visit_expr_subtype(n_value, variable.declared_type)
972
973 v.set_variable(self, variable, mtype)
974
975 self.is_typed = true
976 end
977 end
978
979 redef class AReassignFormExpr
980 # The method designed by the reassign operator.
981 var reassign_callsite: nullable CallSite
982
983 var read_type: nullable MType = null
984
985 # Determine the `reassign_property`
986 # `readtype` is the type of the reading of the left value.
987 # `writetype` is the type of the writing of the left value.
988 # (Because of `ACallReassignExpr`, both can be different.
989 # Return the static type of the value to store.
990 private fun resolve_reassignment(v: TypeVisitor, readtype, writetype: MType): nullable MType
991 do
992 var reassign_name = self.n_assign_op.operator
993
994 self.read_type = readtype
995
996 var callsite = v.get_method(self.n_assign_op, readtype, reassign_name, false)
997 if callsite == null then return null # Skip error
998 self.reassign_callsite = callsite
999
1000 var msignature = callsite.msignature
1001 var rettype = msignature.return_mtype
1002 assert msignature.arity == 1 and rettype != null
1003
1004 var value_type = v.visit_expr_subtype(self.n_value, msignature.mparameters.first.mtype)
1005 if value_type == null then return null # Skip error
1006
1007 v.check_subtype(self, rettype, writetype, false)
1008 return rettype
1009 end
1010 end
1011
1012 redef class AVarReassignExpr
1013 redef fun accept_typing(v)
1014 do
1015 var variable = self.variable
1016 assert variable != null
1017
1018 var readtype = v.get_variable(self, variable)
1019 if readtype == null then return
1020
1021 read_type = readtype
1022
1023 var writetype = variable.declared_type
1024 if writetype == null then return
1025
1026 var rettype = self.resolve_reassignment(v, readtype, writetype)
1027
1028 v.set_variable(self, variable, rettype)
1029
1030 self.is_typed = rettype != null
1031 end
1032 end
1033
1034
1035 redef class AContinueExpr
1036 redef fun accept_typing(v)
1037 do
1038 var nexpr = self.n_expr
1039 if nexpr != null then
1040 v.visit_expr(nexpr)
1041 end
1042 self.is_typed = true
1043 end
1044 end
1045
1046 redef class ABreakExpr
1047 redef fun accept_typing(v)
1048 do
1049 var nexpr = self.n_expr
1050 if nexpr != null then
1051 v.visit_expr(nexpr)
1052 end
1053 self.is_typed = true
1054 end
1055 end
1056
1057 redef class AReturnExpr
1058 redef fun accept_typing(v)
1059 do
1060 var nexpr = self.n_expr
1061 var ret_type
1062 var mpropdef = v.mpropdef
1063 if mpropdef isa MMethodDef then
1064 ret_type = mpropdef.msignature.return_mtype
1065 else if mpropdef isa MAttributeDef then
1066 ret_type = mpropdef.static_mtype
1067 else
1068 abort
1069 end
1070 if nexpr != null then
1071 if ret_type != null then
1072 v.visit_expr_subtype(nexpr, ret_type)
1073 else
1074 v.visit_expr(nexpr)
1075 v.error(nexpr, "Error: `return` with value in a procedure.")
1076 return
1077 end
1078 else if ret_type != null then
1079 v.error(self, "Error: `return` without value in a function.")
1080 return
1081 end
1082 self.is_typed = true
1083 end
1084 end
1085
1086 redef class AAbortExpr
1087 redef fun accept_typing(v)
1088 do
1089 self.is_typed = true
1090 end
1091 end
1092
1093 redef class AIfExpr
1094 redef fun accept_typing(v)
1095 do
1096 v.visit_expr_bool(n_expr)
1097
1098 v.visit_stmt(n_then)
1099 v.visit_stmt(n_else)
1100
1101 self.is_typed = true
1102
1103 if n_then != null and n_else == null then
1104 self.mtype = n_then.mtype
1105 end
1106 end
1107 end
1108
1109 redef class AIfexprExpr
1110 redef fun accept_typing(v)
1111 do
1112 v.visit_expr_bool(n_expr)
1113
1114 var t1 = v.visit_expr(n_then)
1115 var t2 = v.visit_expr(n_else)
1116
1117 if t1 == null or t2 == null then
1118 return # Skip error
1119 end
1120
1121 var t = v.merge_types(self, [t1, t2])
1122 if t == null then
1123 v.error(self, "Type Error: ambiguous type `{t1}` vs `{t2}`.")
1124 end
1125 self.mtype = t
1126 end
1127 end
1128
1129 redef class ADoExpr
1130 redef fun accept_typing(v)
1131 do
1132 v.visit_stmt(n_block)
1133 v.visit_stmt(n_catch)
1134 self.is_typed = true
1135 end
1136 end
1137
1138 redef class AWhileExpr
1139 redef fun accept_typing(v)
1140 do
1141 v.has_loop = true
1142 v.visit_expr_bool(n_expr)
1143 v.visit_stmt(n_block)
1144 self.is_typed = true
1145 end
1146 end
1147
1148 redef class ALoopExpr
1149 redef fun accept_typing(v)
1150 do
1151 v.has_loop = true
1152 v.visit_stmt(n_block)
1153 self.is_typed = true
1154 end
1155 end
1156
1157 redef class AForExpr
1158 redef fun accept_typing(v)
1159 do
1160 v.has_loop = true
1161
1162 for g in n_groups do
1163 var mtype = v.visit_expr(g.n_expr)
1164 if mtype == null then return
1165 g.do_type_iterator(v, mtype)
1166 if g.is_broken then is_broken = true
1167 end
1168
1169 v.visit_stmt(n_block)
1170
1171 self.mtype = n_block.mtype
1172 self.is_typed = true
1173 end
1174 end
1175
1176 redef class AForGroup
1177 var coltype: nullable MClassType
1178
1179 var method_iterator: nullable CallSite
1180 var method_is_ok: nullable CallSite
1181 var method_item: nullable CallSite
1182 var method_next: nullable CallSite
1183 var method_key: nullable CallSite
1184 var method_finish: nullable CallSite
1185
1186 var method_lt: nullable CallSite
1187 var method_successor: nullable CallSite
1188
1189 private fun do_type_iterator(v: TypeVisitor, mtype: MType)
1190 do
1191 if mtype isa MNullType then
1192 v.error(self, "Type Error: `for` cannot iterate over `null`.")
1193 return
1194 end
1195
1196 # get obj class
1197 var objcla = v.get_mclass(self, "Object")
1198 if objcla == null then return
1199
1200 # check iterator method
1201 var itdef = v.get_method(self, mtype, "iterator", n_expr isa ASelfExpr)
1202 if itdef == null then
1203 v.error(self, "Type Error: `for` expects a type providing an `iterator` method, got `{mtype}`.")
1204 return
1205 end
1206 self.method_iterator = itdef
1207
1208 # check that iterator return something
1209 var ittype = itdef.msignature.return_mtype
1210 if ittype == null then
1211 v.error(self, "Type Error: `for` expects the method `iterator` to return an `Iterator` or `MapIterator` type.")
1212 return
1213 end
1214
1215 # get iterator type
1216 var colit_cla = v.try_get_mclass(self, "Iterator")
1217 var mapit_cla = v.try_get_mclass(self, "MapIterator")
1218 var is_col = false
1219 var is_map = false
1220
1221 if colit_cla != null and v.is_subtype(ittype, colit_cla.get_mtype([objcla.mclass_type.as_nullable])) then
1222 # Iterator
1223 var coltype = ittype.supertype_to(v.mmodule, v.anchor, colit_cla)
1224 var variables = self.variables
1225 if variables.length != 1 then
1226 v.error(self, "Type Error: `for` expects only one variable when using `Iterator`.")
1227 else
1228 variables.first.declared_type = coltype.arguments.first
1229 end
1230 is_col = true
1231 end
1232
1233 if mapit_cla != null and v.is_subtype(ittype, mapit_cla.get_mtype([objcla.mclass_type.as_nullable, objcla.mclass_type.as_nullable])) then
1234 # Map Iterator
1235 var coltype = ittype.supertype_to(v.mmodule, v.anchor, mapit_cla)
1236 var variables = self.variables
1237 if variables.length != 2 then
1238 v.error(self, "Type Error: `for` expects two variables when using `MapIterator`.")
1239 else
1240 variables[0].declared_type = coltype.arguments[0]
1241 variables[1].declared_type = coltype.arguments[1]
1242 end
1243 is_map = true
1244 end
1245
1246 if not is_col and not is_map then
1247 v.error(self, "Type Error: `for` expects the method `iterator` to return an `Iterator` or `MapIterator` type.")
1248 return
1249 end
1250
1251 # anchor formal and virtual types
1252 if mtype.need_anchor then mtype = v.anchor_to(mtype)
1253
1254 mtype = mtype.undecorate
1255 self.coltype = mtype.as(MClassType)
1256
1257 # get methods is_ok, next, item
1258 var ikdef = v.get_method(self, ittype, "is_ok", false)
1259 if ikdef == null then
1260 v.error(self, "Type Error: `for` expects a method `is_ok` in type `{ittype}`.")
1261 return
1262 end
1263 self.method_is_ok = ikdef
1264
1265 var itemdef = v.get_method(self, ittype, "item", false)
1266 if itemdef == null then
1267 v.error(self, "Type Error: `for` expects a method `item` in type `{ittype}`.")
1268 return
1269 end
1270 self.method_item = itemdef
1271
1272 var nextdef = v.get_method(self, ittype, "next", false)
1273 if nextdef == null then
1274 v.error(self, "Type Error: `for` expects a method `next` in type {ittype}.")
1275 return
1276 end
1277 self.method_next = nextdef
1278
1279 self.method_finish = v.try_get_method(self, ittype, "finish", false)
1280
1281 if is_map then
1282 var keydef = v.get_method(self, ittype, "key", false)
1283 if keydef == null then
1284 v.error(self, "Type Error: `for` expects a method `key` in type `{ittype}`.")
1285 return
1286 end
1287 self.method_key = keydef
1288 end
1289
1290 if self.variables.length == 1 and n_expr isa ARangeExpr then
1291 var variable = variables.first
1292 var vtype = variable.declared_type.as(not null)
1293
1294 if n_expr isa AOrangeExpr then
1295 self.method_lt = v.get_method(self, vtype, "<", false)
1296 else
1297 self.method_lt = v.get_method(self, vtype, "<=", false)
1298 end
1299
1300 self.method_successor = v.get_method(self, vtype, "successor", false)
1301 end
1302 end
1303 end
1304
1305 redef class AWithExpr
1306 var method_start: nullable CallSite
1307 var method_finish: nullable CallSite
1308
1309 redef fun accept_typing(v: TypeVisitor)
1310 do
1311 var mtype = v.visit_expr(n_expr)
1312 if mtype == null then return
1313
1314 method_start = v.get_method(self, mtype, "start", n_expr isa ASelfExpr)
1315 method_finish = v.get_method(self, mtype, "finish", n_expr isa ASelfExpr)
1316
1317 v.visit_stmt(n_block)
1318 self.mtype = n_block.mtype
1319 self.is_typed = true
1320 end
1321 end
1322
1323 redef class AAssertExpr
1324 redef fun accept_typing(v)
1325 do
1326 v.visit_expr_bool(n_expr)
1327
1328 v.visit_stmt(n_else)
1329 self.is_typed = true
1330 end
1331 end
1332
1333 redef class AOrExpr
1334 redef fun accept_typing(v)
1335 do
1336 v.visit_expr_bool(n_expr)
1337 v.visit_expr_bool(n_expr2)
1338 self.mtype = v.type_bool(self)
1339 end
1340 end
1341
1342 redef class AImpliesExpr
1343 redef fun accept_typing(v)
1344 do
1345 v.visit_expr_bool(n_expr)
1346 v.visit_expr_bool(n_expr2)
1347 self.mtype = v.type_bool(self)
1348 end
1349 end
1350
1351 redef class AAndExpr
1352 redef fun accept_typing(v)
1353 do
1354 v.visit_expr_bool(n_expr)
1355 v.visit_expr_bool(n_expr2)
1356 self.mtype = v.type_bool(self)
1357 end
1358 end
1359
1360
1361 redef class ANotExpr
1362 redef fun accept_typing(v)
1363 do
1364 v.visit_expr_bool(n_expr)
1365 self.mtype = v.type_bool(self)
1366 end
1367 end
1368
1369 redef class AOrElseExpr
1370 redef fun accept_typing(v)
1371 do
1372 var t1 = v.visit_expr(n_expr)
1373 var t2 = v.visit_expr(n_expr2)
1374
1375 if t1 == null or t2 == null then
1376 return # Skip error
1377 end
1378
1379 if t1 isa MNullType then
1380 self.mtype = t2
1381 return
1382 else if v.can_be_null(t1) then
1383 t1 = t1.as_notnull
1384 end
1385
1386 var t = v.merge_types(self, [t1, t2])
1387 if t == null then
1388 var c = v.get_mclass(self, "Object")
1389 if c == null then return # forward error
1390 t = c.mclass_type
1391 if v.can_be_null(t2) then
1392 t = t.as_nullable
1393 end
1394 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1395 end
1396 self.mtype = t
1397 end
1398
1399 redef fun accept_post_typing(v)
1400 do
1401 var t1 = n_expr.mtype
1402 if t1 == null then
1403 return
1404 else
1405 v.check_can_be_null(n_expr, t1)
1406 end
1407 end
1408 end
1409
1410 redef class ATrueExpr
1411 redef fun accept_typing(v)
1412 do
1413 self.mtype = v.type_bool(self)
1414 end
1415 end
1416
1417 redef class AFalseExpr
1418 redef fun accept_typing(v)
1419 do
1420 self.mtype = v.type_bool(self)
1421 end
1422 end
1423
1424 redef class AIntegerExpr
1425 redef fun accept_typing(v)
1426 do
1427 var mclass: nullable MClass = null
1428 if value isa Byte then
1429 mclass = v.get_mclass(self, "Byte")
1430 else if value isa Int then
1431 mclass = v.get_mclass(self, "Int")
1432 else if value isa Int8 then
1433 mclass = v.get_mclass(self, "Int8")
1434 else if value isa Int16 then
1435 mclass = v.get_mclass(self, "Int16")
1436 else if value isa UInt16 then
1437 mclass = v.get_mclass(self, "UInt16")
1438 else if value isa Int32 then
1439 mclass = v.get_mclass(self, "Int32")
1440 else if value isa UInt32 then
1441 mclass = v.get_mclass(self, "UInt32")
1442 end
1443 if mclass == null then return # Forward error
1444 self.mtype = mclass.mclass_type
1445 end
1446 end
1447
1448 redef class AFloatExpr
1449 redef fun accept_typing(v)
1450 do
1451 var mclass = v.get_mclass(self, "Float")
1452 if mclass == null then return # Forward error
1453 self.mtype = mclass.mclass_type
1454 end
1455 end
1456
1457 redef class ACharExpr
1458 redef fun accept_typing(v) do
1459 var mclass: nullable MClass = null
1460 if is_ascii then
1461 mclass = v.get_mclass(self, "Byte")
1462 else if is_code_point then
1463 mclass = v.get_mclass(self, "Int")
1464 else
1465 mclass = v.get_mclass(self, "Char")
1466 end
1467 if mclass == null then return # Forward error
1468 self.mtype = mclass.mclass_type
1469 end
1470 end
1471
1472 redef class AugmentedStringFormExpr
1473 super AExpr
1474
1475 # Text::to_re, used for prefix `re`
1476 var to_re: nullable CallSite = null
1477 # Regex::ignore_case, used for suffix `i` on `re`
1478 var ignore_case: nullable CallSite = null
1479 # Regex::newline, used for suffix `m` on `re`
1480 var newline: nullable CallSite = null
1481 # Regex::extended, used for suffix `b` on `re`
1482 var extended: nullable CallSite = null
1483 # NativeString::to_bytes_with_copy, used for prefix `b`
1484 var to_bytes_with_copy: nullable CallSite = null
1485
1486 redef fun accept_typing(v) do
1487 var mclass = v.get_mclass(self, "String")
1488 if mclass == null then return # Forward error
1489 if is_bytestring then
1490 to_bytes_with_copy = v.get_method(self, v.mmodule.native_string_type, "to_bytes_with_copy", false)
1491 mclass = v.get_mclass(self, "Bytes")
1492 else if is_re then
1493 to_re = v.get_method(self, mclass.mclass_type, "to_re", false)
1494 for i in suffix.chars do
1495 mclass = v.get_mclass(self, "Regex")
1496 if mclass == null then
1497 v.error(self, "Error: `Regex` class unknown")
1498 return
1499 end
1500 var service = ""
1501 if i == 'i' then
1502 service = "ignore_case="
1503 ignore_case = v.get_method(self, mclass.mclass_type, service, false)
1504 else if i == 'm' then
1505 service = "newline="
1506 newline = v.get_method(self, mclass.mclass_type, service, false)
1507 else if i == 'b' then
1508 service = "extended="
1509 extended = v.get_method(self, mclass.mclass_type, service, false)
1510 else
1511 v.error(self, "Type Error: Unrecognized suffix {i} in prefixed Regex")
1512 abort
1513 end
1514 end
1515 end
1516 if mclass == null then return # Forward error
1517 mtype = mclass.mclass_type
1518 end
1519 end
1520
1521 redef class ASuperstringExpr
1522 redef fun accept_typing(v)
1523 do
1524 super
1525 var objclass = v.get_mclass(self, "Object")
1526 if objclass == null then return # Forward error
1527 var objtype = objclass.mclass_type
1528 for nexpr in self.n_exprs do
1529 v.visit_expr_subtype(nexpr, objtype)
1530 end
1531 end
1532 end
1533
1534 redef class AArrayExpr
1535 # The `with_capacity` method on Array
1536 var with_capacity_callsite: nullable CallSite
1537
1538 # The `push` method on arrays
1539 var push_callsite: nullable CallSite
1540
1541 # The element of each type
1542 var element_mtype: nullable MType
1543
1544 # Set that `self` is a part of comprehension array `na`
1545 # If `self` is a `for`, or a `if`, then `set_comprehension` is recursively applied.
1546 private fun set_comprehension(n: nullable AExpr)
1547 do
1548 if n == null then
1549 return
1550 else if n isa AForExpr then
1551 set_comprehension(n.n_block)
1552 else if n isa AIfExpr then
1553 set_comprehension(n.n_then)
1554 set_comprehension(n.n_else)
1555 else
1556 # is a leave
1557 n.comprehension = self
1558 end
1559 end
1560 redef fun accept_typing(v)
1561 do
1562 var mtype: nullable MType = null
1563 var ntype = self.n_type
1564 if ntype != null then
1565 mtype = v.resolve_mtype(ntype)
1566 if mtype == null then return # Skip error
1567 end
1568 var mtypes = new Array[nullable MType]
1569 var useless = false
1570 for e in self.n_exprs do
1571 var t = v.visit_expr(e)
1572 if t == null then
1573 return # Skip error
1574 end
1575 set_comprehension(e)
1576 if mtype != null then
1577 if v.check_subtype(e, t, mtype, false) == null then return # Forward error
1578 if t == mtype then useless = true
1579 else
1580 mtypes.add(t)
1581 end
1582 end
1583 if mtype == null then
1584 # Ensure monotony for type adaptation on loops
1585 if self.element_mtype != null then mtypes.add self.element_mtype
1586 mtype = v.merge_types(self, mtypes)
1587 end
1588 if mtype == null or mtype isa MNullType then
1589 v.error(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1590 return
1591 end
1592 if useless then
1593 assert ntype != null
1594 v.modelbuilder.warning(ntype, "useless-type", "Warning: useless type declaration `{mtype}` in literal Array since it can be inferred from the elements type.")
1595 end
1596
1597 self.element_mtype = mtype
1598
1599 var mclass = v.get_mclass(self, "Array")
1600 if mclass == null then return # Forward error
1601 var array_mtype = mclass.get_mtype([mtype])
1602
1603 with_capacity_callsite = v.get_method(self, array_mtype, "with_capacity", false)
1604 push_callsite = v.get_method(self, array_mtype, "push", false)
1605
1606 self.mtype = array_mtype
1607 end
1608 end
1609
1610 redef class ARangeExpr
1611 var init_callsite: nullable CallSite
1612
1613 redef fun accept_typing(v)
1614 do
1615 var discrete_class = v.get_mclass(self, "Discrete")
1616 if discrete_class == null then return # Forward error
1617 var discrete_type = discrete_class.intro.bound_mtype
1618 var t1 = v.visit_expr_subtype(self.n_expr, discrete_type)
1619 var t2 = v.visit_expr_subtype(self.n_expr2, discrete_type)
1620 if t1 == null or t2 == null then return
1621 var mclass = v.get_mclass(self, "Range")
1622 if mclass == null then return # Forward error
1623 var mtype
1624 if v.is_subtype(t1, t2) then
1625 mtype = mclass.get_mtype([t2])
1626 else if v.is_subtype(t2, t1) then
1627 mtype = mclass.get_mtype([t1])
1628 else
1629 v.error(self, "Type Error: cannot create range: `{t1}` vs `{t2}`.")
1630 return
1631 end
1632
1633 self.mtype = mtype
1634
1635 # get the constructor
1636 var callsite
1637 if self isa ACrangeExpr then
1638 callsite = v.get_method(self, mtype, "init", false)
1639 else if self isa AOrangeExpr then
1640 callsite = v.get_method(self, mtype, "without_last", false)
1641 else
1642 abort
1643 end
1644 init_callsite = callsite
1645 end
1646 end
1647
1648 redef class ANullExpr
1649 redef fun accept_typing(v)
1650 do
1651 self.mtype = v.mmodule.model.null_type
1652 end
1653 end
1654
1655 redef class AIsaExpr
1656 # The static type to cast to.
1657 # (different from the static type of the expression that is `Bool`).
1658 var cast_type: nullable MType
1659 redef fun accept_typing(v)
1660 do
1661 v.visit_expr(n_expr)
1662
1663 var mtype = v.resolve_mtype(n_type)
1664
1665 self.cast_type = mtype
1666
1667 var variable = self.n_expr.its_variable
1668 if variable != null then
1669 var orig = self.n_expr.mtype
1670 #var from = if orig != null then orig.to_s else "invalid"
1671 #var to = if mtype != null then mtype.to_s else "invalid"
1672 #debug("adapt {variable}: {from} -> {to}")
1673
1674 # Do not adapt if there is no information gain (i.e. adapt to a supertype)
1675 if mtype == null or orig == null or not v.is_subtype(orig, mtype) then
1676 self.after_flow_context.when_true.set_var(v, variable, mtype)
1677 end
1678 end
1679
1680 self.mtype = v.type_bool(self)
1681 end
1682
1683 redef fun accept_post_typing(v)
1684 do
1685 v.check_expr_cast(self, self.n_expr, self.n_type)
1686 end
1687 end
1688
1689 redef class AAsCastExpr
1690 redef fun accept_typing(v)
1691 do
1692 v.visit_expr(n_expr)
1693
1694 self.mtype = v.resolve_mtype(n_type)
1695 end
1696
1697 redef fun accept_post_typing(v)
1698 do
1699 v.check_expr_cast(self, self.n_expr, self.n_type)
1700 end
1701 end
1702
1703 redef class AAsNotnullExpr
1704 redef fun accept_typing(v)
1705 do
1706 var mtype = v.visit_expr(self.n_expr)
1707 if mtype == null then return # Forward error
1708
1709 if mtype isa MNullType then
1710 v.error(self, "Type Error: `as(not null)` on `null`.")
1711 return
1712 end
1713
1714 if v.can_be_null(mtype) then
1715 mtype = mtype.as_notnull
1716 end
1717
1718 self.mtype = mtype
1719 end
1720
1721 redef fun accept_post_typing(v)
1722 do
1723 var mtype = n_expr.mtype
1724 if mtype == null then return
1725 v.check_can_be_null(n_expr, mtype)
1726 end
1727 end
1728
1729 redef class AParExpr
1730 redef fun accept_typing(v)
1731 do
1732 self.mtype = v.visit_expr(self.n_expr)
1733 end
1734 end
1735
1736 redef class AOnceExpr
1737 redef fun accept_typing(v)
1738 do
1739 self.mtype = v.visit_expr(self.n_expr)
1740 end
1741 end
1742
1743 redef class ASelfExpr
1744 redef var its_variable: nullable Variable
1745 redef fun accept_typing(v)
1746 do
1747 if v.is_toplevel_context and not self isa AImplicitSelfExpr then
1748 v.error(self, "Error: `self` cannot be used in top-level method.")
1749 end
1750 var variable = v.selfvariable
1751 self.its_variable = variable
1752 self.mtype = v.get_variable(self, variable)
1753 end
1754 end
1755
1756 redef class AImplicitSelfExpr
1757 # Is the implicit receiver `sys`?
1758 #
1759 # By default, the implicit receiver is `self`.
1760 # But when there is not method for `self`, `sys` is used as a fall-back.
1761 # Is this case this flag is set to `true`.
1762 var is_sys = false
1763 end
1764
1765 ## MESSAGE SENDING AND PROPERTY
1766
1767 redef class ASendExpr
1768 # The property invoked by the send.
1769 var callsite: nullable CallSite
1770
1771 redef fun bad_expr_message(child)
1772 do
1773 if child == self.n_expr then
1774 return "to be the receiver of `{self.property_name}`"
1775 end
1776 return null
1777 end
1778
1779 redef fun accept_typing(v)
1780 do
1781 var nrecv = self.n_expr
1782 var recvtype = v.visit_expr(nrecv)
1783 var name = self.property_name
1784 var node = self.property_node
1785
1786 if recvtype == null then return # Forward error
1787
1788 var callsite = null
1789 var unsafe_type = v.anchor_to(recvtype)
1790 var mproperty = v.try_get_mproperty_by_name2(node, unsafe_type, name)
1791 if mproperty == null and nrecv isa AImplicitSelfExpr then
1792 # Special fall-back search in `sys` when noting found in the implicit receiver.
1793 var sysclass = v.try_get_mclass(node, "Sys")
1794 if sysclass != null then
1795 var systype = sysclass.mclass_type
1796 mproperty = v.try_get_mproperty_by_name2(node, systype, name)
1797 if mproperty != null then
1798 callsite = v.get_method(node, systype, name, false)
1799 if callsite == null then return # Forward error
1800 # Update information, we are looking at `sys` now, not `self`
1801 nrecv.is_sys = true
1802 nrecv.its_variable = null
1803 nrecv.mtype = systype
1804 recvtype = systype
1805 end
1806 end
1807 end
1808 if callsite == null then
1809 # If still nothing, just exit
1810 callsite = v.get_method(node, recvtype, name, nrecv isa ASelfExpr)
1811 if callsite == null then return
1812 end
1813
1814 self.callsite = callsite
1815 var msignature = callsite.msignature
1816
1817 var args = compute_raw_arguments
1818
1819 callsite.check_signature(v, node, args)
1820
1821 if callsite.mproperty.is_init then
1822 var vmpropdef = v.mpropdef
1823 if not (vmpropdef isa MMethodDef and vmpropdef.mproperty.is_init) then
1824 v.error(node, "Error: an `init` can only be called from another `init`.")
1825 end
1826 if vmpropdef isa MMethodDef and vmpropdef.mproperty.is_root_init and not callsite.mproperty.is_root_init then
1827 v.error(node, "Error: `{vmpropdef}` cannot call a factory `{callsite.mproperty}`.")
1828 end
1829 end
1830
1831 var ret = msignature.return_mtype
1832 if ret != null then
1833 self.mtype = ret
1834 else
1835 self.is_typed = true
1836 end
1837 end
1838
1839 # The name of the property
1840 # Each subclass simply provide the correct name.
1841 private fun property_name: String is abstract
1842
1843 # The node identifying the name (id, operator, etc) for messages.
1844 #
1845 # Is `self` by default
1846 private fun property_node: ANode do return self
1847
1848 # An array of all arguments (excluding self)
1849 fun raw_arguments: Array[AExpr] do return compute_raw_arguments
1850
1851 private fun compute_raw_arguments: Array[AExpr] is abstract
1852 end
1853
1854 redef class ABinopExpr
1855 redef fun compute_raw_arguments do return [n_expr2]
1856 redef fun property_name do return operator
1857 redef fun property_node do return n_op
1858 end
1859
1860 redef class AEqFormExpr
1861 redef fun accept_typing(v)
1862 do
1863 super
1864 v.null_test(self)
1865 end
1866
1867 redef fun accept_post_typing(v)
1868 do
1869 var mtype = n_expr.mtype
1870 var mtype2 = n_expr2.mtype
1871
1872 if mtype == null or mtype2 == null then return
1873
1874 if not mtype2 isa MNullType then return
1875
1876 v.check_can_be_null(n_expr, mtype)
1877 end
1878 end
1879
1880 redef class AUnaryopExpr
1881 redef fun property_name do return "unary {operator}"
1882 redef fun compute_raw_arguments do return new Array[AExpr]
1883 end
1884
1885
1886 redef class ACallExpr
1887 redef fun property_name do return n_qid.n_id.text
1888 redef fun property_node do return n_qid
1889 redef fun compute_raw_arguments do return n_args.to_a
1890 end
1891
1892 redef class ACallAssignExpr
1893 redef fun property_name do return n_qid.n_id.text + "="
1894 redef fun property_node do return n_qid
1895 redef fun compute_raw_arguments
1896 do
1897 var res = n_args.to_a
1898 res.add(n_value)
1899 return res
1900 end
1901 end
1902
1903 redef class ABraExpr
1904 redef fun property_name do return "[]"
1905 redef fun compute_raw_arguments do return n_args.to_a
1906 end
1907
1908 redef class ABraAssignExpr
1909 redef fun property_name do return "[]="
1910 redef fun compute_raw_arguments
1911 do
1912 var res = n_args.to_a
1913 res.add(n_value)
1914 return res
1915 end
1916 end
1917
1918 redef class ASendReassignFormExpr
1919 # The property invoked for the writing
1920 var write_callsite: nullable CallSite
1921
1922 redef fun accept_typing(v)
1923 do
1924 var recvtype = v.visit_expr(self.n_expr)
1925 var name = self.property_name
1926 var node = self.property_node
1927
1928 if recvtype == null then return # Forward error
1929
1930 var for_self = self.n_expr isa ASelfExpr
1931 var callsite = v.get_method(node, recvtype, name, for_self)
1932
1933 if callsite == null then return
1934 self.callsite = callsite
1935
1936 var args = compute_raw_arguments
1937
1938 callsite.check_signature(v, node, args)
1939
1940 var readtype = callsite.msignature.return_mtype
1941 if readtype == null then
1942 v.error(node, "Error: `{name}` is not a function.")
1943 return
1944 end
1945
1946 var wcallsite = v.get_method(node, recvtype, name + "=", self.n_expr isa ASelfExpr)
1947 if wcallsite == null then return
1948 self.write_callsite = wcallsite
1949
1950 var wtype = self.resolve_reassignment(v, readtype, wcallsite.msignature.mparameters.last.mtype)
1951 if wtype == null then return
1952
1953 args = args.to_a # duplicate so raw_arguments keeps only the getter args
1954 args.add(self.n_value)
1955 wcallsite.check_signature(v, node, args)
1956
1957 self.is_typed = true
1958 end
1959 end
1960
1961 redef class ACallReassignExpr
1962 redef fun property_name do return n_qid.n_id.text
1963 redef fun property_node do return n_qid.n_id
1964 redef fun compute_raw_arguments do return n_args.to_a
1965 end
1966
1967 redef class ABraReassignExpr
1968 redef fun property_name do return "[]"
1969 redef fun compute_raw_arguments do return n_args.to_a
1970 end
1971
1972 redef class AInitExpr
1973 redef fun property_name do return "init"
1974 redef fun property_node do return n_kwinit
1975 redef fun compute_raw_arguments do return n_args.to_a
1976 end
1977
1978 redef class AExprs
1979 fun to_a: Array[AExpr] do return self.n_exprs.to_a
1980 end
1981
1982 ###
1983
1984 redef class ASuperExpr
1985 # The method to call if the super is in fact a 'super init call'
1986 # Note: if the super is a normal call-next-method, then this attribute is null
1987 var callsite: nullable CallSite
1988
1989 # The method to call is the super is a standard `call-next-method` super-call
1990 # Note: if the super is a special super-init-call, then this attribute is null
1991 var mpropdef: nullable MMethodDef
1992
1993 redef fun accept_typing(v)
1994 do
1995 var anchor = v.anchor
1996 assert anchor != null
1997 var recvtype = v.get_variable(self, v.selfvariable)
1998 assert recvtype != null
1999 var mproperty = v.mpropdef.mproperty
2000 if not mproperty isa MMethod then
2001 v.error(self, "Error: `super` only usable in a `method`.")
2002 return
2003 end
2004 var superprops = mproperty.lookup_super_definitions(v.mmodule, anchor)
2005 if superprops.length == 0 then
2006 if mproperty.is_init and v.mpropdef.is_intro then
2007 process_superinit(v)
2008 return
2009 end
2010 v.error(self, "Error: no super method to call for `{mproperty}`.")
2011 return
2012 end
2013 # FIXME: covariance of return type in linear extension?
2014 var superprop = superprops.first
2015
2016 var msignature = superprop.msignature.as(not null)
2017 msignature = v.resolve_for(msignature, recvtype, true).as(MSignature)
2018 var args = self.n_args.to_a
2019 if args.length > 0 then
2020 signaturemap = v.check_signature(self, args, mproperty, msignature)
2021 end
2022 self.mtype = msignature.return_mtype
2023 self.is_typed = true
2024 v.mpropdef.has_supercall = true
2025 mpropdef = v.mpropdef.as(MMethodDef)
2026 end
2027
2028 # The mapping used on the call to associate arguments to parameters.
2029 # If null then no specific association is required.
2030 var signaturemap: nullable SignatureMap
2031
2032 private fun process_superinit(v: TypeVisitor)
2033 do
2034 var anchor = v.anchor
2035 assert anchor != null
2036 var recvtype = v.get_variable(self, v.selfvariable)
2037 assert recvtype != null
2038 var mpropdef = v.mpropdef
2039 assert mpropdef isa MMethodDef
2040 var mproperty = mpropdef.mproperty
2041 var superprop: nullable MMethodDef = null
2042 for msupertype in mpropdef.mclassdef.supertypes do
2043 msupertype = msupertype.anchor_to(v.mmodule, anchor)
2044 var errcount = v.modelbuilder.toolcontext.error_count
2045 var candidate = v.try_get_mproperty_by_name2(self, msupertype, mproperty.name).as(nullable MMethod)
2046 if candidate == null then
2047 if v.modelbuilder.toolcontext.error_count > errcount then return # Forward error
2048 continue # Try next super-class
2049 end
2050 if superprop != null and candidate.is_root_init then
2051 continue
2052 end
2053 if superprop != null and superprop.mproperty != candidate and not superprop.mproperty.is_root_init then
2054 v.error(self, "Error: conflicting super constructor to call for `{mproperty}`: `{candidate.full_name}`, `{superprop.mproperty.full_name}`")
2055 return
2056 end
2057 var candidatedefs = candidate.lookup_definitions(v.mmodule, anchor)
2058 if superprop != null and superprop.mproperty == candidate then
2059 if superprop == candidatedefs.first then continue
2060 candidatedefs.add(superprop)
2061 end
2062 if candidatedefs.length > 1 then
2063 v.error(self, "Error: conflicting property definitions for property `{mproperty}` in `{recvtype}`: {candidatedefs.join(", ")}")
2064 return
2065 end
2066 superprop = candidatedefs.first
2067 end
2068 if superprop == null then
2069 v.error(self, "Error: no super method to call for `{mproperty}`.")
2070 return
2071 end
2072
2073 var msignature = superprop.new_msignature or else superprop.msignature.as(not null)
2074 msignature = v.resolve_for(msignature, recvtype, true).as(MSignature)
2075
2076 var callsite = new CallSite(hot_location, recvtype, v.mmodule, v.anchor, true, superprop.mproperty, superprop, msignature, false)
2077 self.callsite = callsite
2078
2079 var args = self.n_args.to_a
2080 if args.length > 0 then
2081 callsite.check_signature(v, self, args)
2082 else
2083 # Check there is at least enough parameters
2084 if mpropdef.msignature.arity < msignature.arity then
2085 v.error(self, "Error: not enough implicit arguments to pass. Got `{mpropdef.msignature.arity}`, expected at least `{msignature.arity}`. Signature is `{msignature}`.")
2086 return
2087 end
2088 # Check that each needed parameter is conform
2089 var i = 0
2090 for sp in msignature.mparameters do
2091 var p = mpropdef.msignature.mparameters[i]
2092 if not v.is_subtype(p.mtype, sp.mtype) then
2093 v.error(self, "Type Error: expected argument #{i} of type `{sp.mtype}`, got implicit argument `{p.name}` of type `{p.mtype}`. Signature is {msignature}")
2094 return
2095 end
2096 i += 1
2097 end
2098 end
2099
2100 self.is_typed = true
2101 end
2102 end
2103
2104 ####
2105
2106 redef class ANewExpr
2107 # The constructor invoked by the new.
2108 var callsite: nullable CallSite
2109
2110 # The designated type
2111 var recvtype: nullable MClassType
2112
2113 redef fun accept_typing(v)
2114 do
2115 var recvtype = v.resolve_mtype(self.n_type)
2116 if recvtype == null then return
2117
2118 if not recvtype isa MClassType then
2119 if recvtype isa MNullableType then
2120 v.error(self, "Type Error: cannot instantiate the nullable type `{recvtype}`.")
2121 return
2122 else if recvtype isa MFormalType then
2123 v.error(self, "Type Error: cannot instantiate the formal type `{recvtype}`.")
2124 return
2125 else
2126 v.error(self, "Type Error: cannot instantiate the type `{recvtype}`.")
2127 return
2128 end
2129 end
2130
2131 self.recvtype = recvtype
2132 var kind = recvtype.mclass.kind
2133
2134 var name: String
2135 var nqid = self.n_qid
2136 var node: ANode
2137 if nqid != null then
2138 name = nqid.n_id.text
2139 node = nqid
2140 else
2141 name = "new"
2142 node = self.n_kwnew
2143 end
2144 if name == "intern" then
2145 if kind != concrete_kind then
2146 v.error(self, "Type Error: cannot instantiate {kind} {recvtype}.")
2147 return
2148 end
2149 if n_args.n_exprs.not_empty then
2150 v.error(n_args, "Type Error: the intern constructor expects no arguments.")
2151 return
2152 end
2153 # Our job is done
2154 self.mtype = recvtype
2155 return
2156 end
2157
2158 var callsite = v.get_method(node, recvtype, name, false)
2159 if callsite == null then return
2160
2161 if not callsite.mproperty.is_new then
2162 if kind != concrete_kind then
2163 v.error(self, "Type Error: cannot instantiate {kind} `{recvtype}`.")
2164 return
2165 end
2166 self.mtype = recvtype
2167 else
2168 self.mtype = callsite.msignature.return_mtype
2169 assert self.mtype != null
2170 end
2171
2172 self.callsite = callsite
2173
2174 if not callsite.mproperty.is_init_for(recvtype.mclass) then
2175 v.error(self, "Error: `{name}` is not a constructor.")
2176 return
2177 end
2178
2179 var args = n_args.to_a
2180 callsite.check_signature(v, node, args)
2181 end
2182 end
2183
2184 ####
2185
2186 redef class AAttrFormExpr
2187 # The attribute accessed.
2188 var mproperty: nullable MAttribute
2189
2190 # The static type of the attribute.
2191 var attr_type: nullable MType
2192
2193 # Resolve the attribute accessed.
2194 private fun resolve_property(v: TypeVisitor)
2195 do
2196 var recvtype = v.visit_expr(self.n_expr)
2197 if recvtype == null then return # Skip error
2198 var node = self.n_id
2199 var name = node.text
2200 if recvtype isa MNullType then
2201 v.error(node, "Error: attribute `{name}` access on `null`.")
2202 return
2203 end
2204
2205 var unsafe_type = v.anchor_to(recvtype)
2206 var mproperty = v.try_get_mproperty_by_name2(node, unsafe_type, name)
2207 if mproperty == null then
2208 v.modelbuilder.error(node, "Error: attribute `{name}` does not exist in `{recvtype}`.")
2209 return
2210 end
2211 assert mproperty isa MAttribute
2212 self.mproperty = mproperty
2213
2214 var mpropdefs = mproperty.lookup_definitions(v.mmodule, unsafe_type)
2215 assert mpropdefs.length == 1
2216 var mpropdef = mpropdefs.first
2217 var attr_type = mpropdef.static_mtype
2218 if attr_type == null then return # skip error
2219 attr_type = v.resolve_for(attr_type, recvtype, self.n_expr isa ASelfExpr)
2220 self.attr_type = attr_type
2221 end
2222 end
2223
2224 redef class AAttrExpr
2225 redef fun accept_typing(v)
2226 do
2227 self.resolve_property(v)
2228 self.mtype = self.attr_type
2229 end
2230 end
2231
2232
2233 redef class AAttrAssignExpr
2234 redef fun accept_typing(v)
2235 do
2236 self.resolve_property(v)
2237 var mtype = self.attr_type
2238
2239 v.visit_expr_subtype(self.n_value, mtype)
2240 self.is_typed = mtype != null
2241 end
2242 end
2243
2244 redef class AAttrReassignExpr
2245 redef fun accept_typing(v)
2246 do
2247 self.resolve_property(v)
2248 var mtype = self.attr_type
2249 if mtype == null then return # Skip error
2250
2251 var rettype = self.resolve_reassignment(v, mtype, mtype)
2252
2253 self.is_typed = rettype != null
2254 end
2255 end
2256
2257 redef class AIssetAttrExpr
2258 redef fun accept_typing(v)
2259 do
2260 self.resolve_property(v)
2261 var mtype = self.attr_type
2262 if mtype == null then return # Skip error
2263
2264 var recvtype = self.n_expr.mtype.as(not null)
2265 var bound = v.resolve_for(mtype, recvtype, false)
2266 if bound isa MNullableType then
2267 v.error(n_id, "Type Error: `isset` on a nullable attribute.")
2268 end
2269 self.mtype = v.type_bool(self)
2270 end
2271 end
2272
2273 redef class AVarargExpr
2274 redef fun accept_typing(v)
2275 do
2276 # This kind of pseudo-expression can be only processed trough a signature
2277 # See `check_signature`
2278 # Other cases are a syntax error.
2279 v.error(self, "Syntax Error: unexpected `...`.")
2280 end
2281 end
2282
2283 ###
2284
2285 redef class ADebugTypeExpr
2286 redef fun accept_typing(v)
2287 do
2288 var expr = v.visit_expr(self.n_expr)
2289 if expr == null then return
2290 var unsafe = v.anchor_to(expr)
2291 var ntype = self.n_type
2292 var mtype = v.resolve_mtype(ntype)
2293 if mtype != null and mtype != expr then
2294 var umtype = v.anchor_to(mtype)
2295 v.modelbuilder.warning(self, "debug", "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
2296 end
2297 self.is_typed = true
2298 end
2299 end