1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2012 Jean Privat <jean@pryen.org>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Intraprocedural resolution of static types and OO-services
18 # By OO-services we mean message sending, attribute access, instantiation, etc.
22 import modelize_property
26 redef class ToolContext
27 var typing_phase
: Phase = new TypingPhase(self, [flow_phase
, modelize_property_phase
, local_var_init_phase
])
31 # Does the MPropDef contains a call to super or a call of a super-constructor?
32 var has_supercall
: Bool = false
35 private class TypingPhase
37 redef fun process_npropdef
(npropdef
) do npropdef
.do_typing
(toolcontext
.modelbuilder
)
40 private class TypeVisitor
41 var modelbuilder
: ModelBuilder
42 var nclassdef
: AClassdef
43 var mpropdef
: MPropDef
45 var selfvariable
: Variable = new Variable("self")
47 init(modelbuilder
: ModelBuilder, nclassdef
: AClassdef, mpropdef
: MPropDef)
49 self.modelbuilder
= modelbuilder
50 self.nclassdef
= nclassdef
51 self.mpropdef
= mpropdef
53 var mclass
= nclassdef
.mclassdef
.mclass
55 var selfvariable
= new Variable("self")
56 self.selfvariable
= selfvariable
57 selfvariable
.declared_type
= mclass
.mclass_type
60 fun mmodule
: MModule do return self.nclassdef
.mclassdef
.mmodule
62 fun anchor
: MClassType do return self.nclassdef
.mclassdef
.bound_mtype
64 fun anchor_to
(mtype
: MType): MType
66 var mmodule
= self.nclassdef
.mclassdef
.mmodule
67 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
68 return mtype
.anchor_to
(mmodule
, anchor
)
71 fun is_subtype
(sub
, sup
: MType): Bool
73 var mmodule
= self.nclassdef
.mclassdef
.mmodule
74 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
75 return sub
.is_subtype
(mmodule
, anchor
, sup
)
78 fun resolve_for
(mtype
, subtype
: MType, for_self
: Bool): MType
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
)
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.
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
94 return self.resolve_for
(mmethoddef
.msignature
.as(not null), recv
, for_self
).as(MSignature)
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`.
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
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}")
113 self.modelbuilder
.error
(node
, "Type error: expected {sup}, got {sub}")
117 # Visit an expression and do not care about the return value
118 fun visit_stmt
(nexpr
: nullable AExpr)
120 if nexpr
== null then return
121 nexpr
.accept_typing
(self)
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
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.")
140 return null # forward error
142 self.error
(nexpr
, "Type error: expected expression.")
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
153 var sub
= visit_expr
(nexpr
)
154 if sub
== null then return null # Forward error
156 if sup
== null then return null # Forward error
158 var res
= check_subtype
(nexpr
, sub
, sup
)
160 nexpr
.implicit_cast_to
= res
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
172 return self.visit_expr_subtype
(nexpr
, self.type_bool
(nexpr
))
176 private fun visit_expr_cast
(node
: ANode, nexpr
: AExpr, ntype
: AType): nullable MType
178 var sub
= visit_expr
(nexpr
)
179 if sub
== null then return null # Forward error
181 var sup
= self.resolve_mtype
(ntype
)
182 if sup
== null then return null # Forward error
184 var mmodule
= self.nclassdef
.mclassdef
.mmodule
185 var anchor
= self.nclassdef
.mclassdef
.bound_mtype
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}.")
194 fun try_get_mproperty_by_name2
(anode
: ANode, mtype
: MType, name
: String): nullable MProperty
196 return self.modelbuilder
.try_get_mproperty_by_name2
(anode
, self.nclassdef
.mclassdef
.mmodule
, mtype
, name
)
199 fun resolve_mtype
(node
: AType): nullable MType
201 return self.modelbuilder
.resolve_mtype
(self.nclassdef
, node
)
204 fun try_get_mclass
(node
: ANode, name
: String): nullable MClass
206 var mmodule
= self.nclassdef
.mclassdef
.mmodule
207 var mclass
= modelbuilder
.try_get_mclass_by_name
(node
, mmodule
, name
)
211 fun get_mclass
(node
: ANode, name
: String): nullable MClass
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}'.")
221 fun type_bool
(node
: ANode): nullable MType
223 var mclass
= self.get_mclass
(node
, "Bool")
224 if mclass
== null then return null
225 return mclass
.mclass_type
228 fun get_method
(node
: ANode, recvtype
: MType, name
: String, recv_is_self
: Bool): nullable CallSite
230 var unsafe_type
= self.anchor_to
(recvtype
)
232 #debug("recv: {recvtype} (aka {unsafe_type})")
233 if recvtype
isa MNullType then
234 self.error
(node
, "Error: Method '{name}' call on 'null'.")
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})")
242 self.modelbuilder
.error
(node
, "Error: Method or variable '{name}' unknown in {recvtype}.")
244 self.modelbuilder
.error
(node
, "Error: Method '{name}' doesn't exists in {recvtype}.")
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)}")
255 var propdefs
= mproperty
.lookup_definitions
(self.mmodule
, unsafe_type
)
257 if propdefs
.length
== 0 then
258 self.modelbuilder
.error
(node
, "Type error: no definition found for property {name} in {unsafe_type}")
260 else if propdefs
.length
== 1 then
261 mpropdef
= propdefs
.first
263 self.modelbuilder
.warning
(node
, "Warning: confliting property definitions for property {name} in {unsafe_type}: {propdefs.join(" ")}")
264 mpropdef
= mproperty
.intro
268 var msignature
= self.resolve_signature_for
(mpropdef
, recvtype
, recv_is_self
)
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}")
282 var callsite
= new CallSite(node
, recvtype
, recv_is_self
, mproperty
, mpropdef
, msignature
, erasure_cast
)
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
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}'")
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}")
303 #debug("CALL {unsafe_type}.{msignature}")
305 var vararg_decl
= args
.length
- msignature
.arity
306 for i
in [0..msignature
.arity
[ do
308 if i
== vararg_rank
then continue # skip the vararg
309 if i
> vararg_rank
then
312 var paramtype
= msignature
.mparameters
[i
].mtype
313 self.visit_expr_subtype
(args
[j
], paramtype
)
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
320 self.visit_expr_subtype
(args
[j
], paramtype
)
326 fun error
(node
: ANode, message
: String)
328 self.modelbuilder
.toolcontext
.error
(node
.hot_location
, message
)
331 fun get_variable
(node
: AExpr, variable
: Variable): nullable MType
333 var flow
= node
.after_flow_context
335 self.error
(node
, "No context!")
339 if flow
.vars
.has_key
(variable
) then
340 return flow
.vars
[variable
]
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
350 var res
= merge_types
(node
,mtypes
)
351 if res
== null then res
= variable
.declared_type
357 fun set_variable
(node
: AExpr, variable
: Variable, mtype
: nullable MType)
359 var flow
= node
.after_flow_context
362 flow
.set_var
(variable
, mtype
)
365 fun merge_types
(node
: ANode, col
: Array[nullable MType]): nullable MType
367 if col
.length
== 1 then return col
.first
368 var res
= new Array[nullable MType]
370 if t1
== null then continue # return null
373 if t2
== null then continue # return null
374 if t2
isa MNullableType or t2
isa MNullType then
377 if not is_subtype
(t2
, t1
) then found
= false
380 #print "merge {col.join(" ")} -> {t1}"
384 #self.modelbuilder.warning(node, "Type Error: {col.length} conflicting types: <{col.join(", ")}>")
389 # A specific method call site with its associated informations.
391 # The assiciated node for location
394 # The statis type of the receiver
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
402 # The designated method
403 var mproperty
: MMethod
405 # The statically designated method definition
406 # The most specif one, it is.
407 var mpropdef
: MMethodDef
409 # The resolved signature for the receiver
410 var msignature
: MSignature
412 # Is a implicit cast required on erasure typing policy?
413 var erasure_cast
: Bool
415 private fun check_signature
(v
: TypeVisitor, args
: Array[AExpr]): Bool
417 return v
.check_signature
(self.node
, args
, self.mproperty
.name
, self.msignature
)
422 # The declared type of the variable
423 var declared_type
: nullable MType
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]]
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)
436 self.vars
[variable
] = mtype
437 self.cache
.keys
.remove
(variable
)
440 private fun collect_types
(variable
: Variable): nullable Array[nullable MType]
442 if cache
.has_key
(variable
) then
443 return cache
[variable
]
445 var res
: nullable Array[nullable MType] = null
446 if vars
.has_key
(variable
) then
447 var mtype
= vars
[variable
]
449 else if self.previous
.is_empty
then
451 res
= [variable
.declared_type
]
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
461 if not res
.has
(t
) then res
.add
(t
)
466 cache
[variable
] = res
472 # The entry point of the whole typing analysis
473 fun do_typing
(modelbuilder
: ModelBuilder)
477 # The variable associated to the reciever (if any)
478 var selfvariable
: nullable Variable
481 redef class AConcreteMethPropdef
482 redef fun do_typing
(modelbuilder
: ModelBuilder)
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
489 var nblock
= self.n_block
490 if nblock
== null then return
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
])
500 var variable
= self.n_signature
.n_params
[i
].variable
501 assert variable
!= null
502 variable
.declared_type
= mtype
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
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).")
519 redef class AAttrPropdef
520 redef fun do_typing
(modelbuilder
: ModelBuilder)
522 var nclassdef
= self.parent
.as(AClassdef)
523 var v
= new TypeVisitor(modelbuilder
, nclassdef
, self.mpropdef
.as(not null))
524 self.selfvariable
= v
.selfvariable
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
)
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
541 # Is the statement correctly typed?
542 # Used to distinguish errors and statements when `mtype == null`
543 var is_typed
: Bool = false
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
551 # Return the variable read (if any)
552 # Used to perform adaptive typing
553 fun its_variable
: nullable Variable do return null
555 private fun accept_typing
(v
: TypeVisitor)
557 v
.error
(self, "no implemented accept_typing for {self.class_name}")
561 redef class ABlockExpr
562 redef fun accept_typing
(v
)
564 for e
in self.n_expr
do v
.visit_stmt
(e
)
568 # The type of a blockexpr is the one of the last expression (or null if empty)
571 if self.n_expr
.is_empty
then return null
572 return self.n_expr
.last
.mtype
576 redef class AVardeclExpr
577 redef fun accept_typing
(v
)
579 var variable
= self.variable
580 if variable
== null then return # Skip error
582 var ntype
= self.n_type
583 var mtype
: nullable MType
584 if ntype
== null then
587 mtype
= v
.resolve_mtype
(ntype
)
588 if mtype
== null then return # Skip error
591 var nexpr
= self.n_expr
592 if nexpr
!= null then
593 if mtype
!= null then
594 v
.visit_expr_subtype
(nexpr
, mtype
)
596 mtype
= v
.visit_expr
(nexpr
)
597 if mtype
== null then return # Skip error
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
607 variable
.declared_type
= decltype
608 v
.set_variable
(self, variable
, mtype
)
610 #debug("var {variable}: {mtype}")
617 redef fun its_variable
do return self.variable
618 redef fun accept_typing
(v
)
620 var variable
= self.variable
621 if variable
== null then return # Skip error
623 var mtype
= v
.get_variable
(self, variable
)
624 if mtype
!= null then
625 #debug("{variable} is {mtype}")
627 #debug("{variable} is untyped")
634 redef class AVarAssignExpr
635 redef fun accept_typing
(v
)
637 var variable
= self.variable
638 assert variable
!= null
640 var mtype
= v
.visit_expr_subtype
(n_value
, variable
.declared_type
)
642 v
.set_variable
(self, variable
, mtype
)
648 redef class AReassignFormExpr
649 # @depreciated use `reassign_callsite`
650 fun reassign_property
: nullable MMethodDef do return self.reassign_callsite
.mpropdef
652 # The method designed by the reassign operator.
653 var reassign_callsite
: nullable CallSite
655 var read_type
: nullable MType = null
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
664 var reassign_name
: String
665 if self.n_assign_op
isa APlusAssignOp then
667 else if self.n_assign_op
isa AMinusAssignOp then
673 self.read_type
= readtype
675 if readtype
isa MNullType then
676 v
.error
(self, "Error: Method '{reassign_name}' call on 'null'.")
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
684 var msignature
= callsite
.msignature
685 var rettype
= msignature
.return_mtype
686 assert msignature
.arity
== 1 and rettype
!= null
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
691 v
.check_subtype
(self, rettype
, writetype
)
696 redef class AVarReassignExpr
697 redef fun accept_typing
(v
)
699 var variable
= self.variable
700 assert variable
!= null
702 var readtype
= v
.get_variable
(self, variable
)
703 if readtype
== null then return
705 var writetype
= variable
.declared_type
706 if writetype
== null then return
708 var rettype
= self.resolve_reassignment
(v
, readtype
, writetype
)
710 v
.set_variable
(self, variable
, rettype
)
717 redef class AContinueExpr
718 redef fun accept_typing
(v
)
720 var nexpr
= self.n_expr
721 if nexpr
!= null then
722 var mtype
= v
.visit_expr
(nexpr
)
728 redef class ABreakExpr
729 redef fun accept_typing
(v
)
731 var nexpr
= self.n_expr
732 if nexpr
!= null then
733 var mtype
= v
.visit_expr
(nexpr
)
739 redef class AReturnExpr
740 redef fun accept_typing
(v
)
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
)
748 var mtype
= v
.visit_expr
(nexpr
)
749 v
.error
(self, "Error: Return with value in a procedure.")
751 else if ret_type
!= null then
752 v
.error
(self, "Error: Return without value in a function.")
758 redef class AAbortExpr
759 redef fun accept_typing
(v
)
766 redef fun accept_typing
(v
)
768 v
.visit_expr_bool
(n_expr
)
776 redef class AIfexprExpr
777 redef fun accept_typing
(v
)
779 v
.visit_expr_bool
(n_expr
)
781 var t1
= v
.visit_expr
(n_then
)
782 var t2
= v
.visit_expr
(n_else
)
784 if t1
== null or t2
== null then
788 var t
= v
.merge_types
(self, [t1
, t2
])
790 v
.error
(self, "Type Error: ambiguous type {t1} vs {t2}")
797 redef fun accept_typing
(v
)
799 v
.visit_stmt
(n_block
)
804 redef class AWhileExpr
805 redef fun accept_typing
(v
)
807 v
.visit_expr_bool
(n_expr
)
809 v
.visit_stmt
(n_block
)
814 redef class ALoopExpr
815 redef fun accept_typing
(v
)
817 v
.visit_stmt
(n_block
)
823 var coltype
: nullable MClassType
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
831 private fun do_type_iterator
(v
: TypeVisitor, mtype
: MType)
834 var objcla
= v
.get_mclass
(self, "Object")
835 if objcla
== null then return
837 # check iterator method
838 var unsafe_type
= v
.anchor_to
(mtype
)
839 if v
.try_get_mproperty_by_name2
(self, unsafe_type
, "iterator") == null then
840 if v
.try_get_mproperty_by_name2
(self, unsafe_type
, "iterate") == null then
841 v
.error
(self, "Type Error: Expected method 'iterator' in type {mtype}")
843 v
.modelbuilder
.error
(self, "NOT YET IMPLEMENTED: Do 'for' on {mtype}")
848 var itdef
= v
.get_method
(self, mtype
, "iterator", true)
849 if itdef
== null then
850 v
.error
(self, "Type Error: Expected method 'iterator' in type {mtype}")
853 self.method_iterator
= itdef
.mproperty
855 # check that iterator return something
856 var ittype
= itdef
.msignature
.return_mtype
857 if ittype
== null then
858 v
.error
(self, "Type Error: Expected method 'iterator' to return an Iterator or MapIterator type")
863 var colit_cla
= v
.try_get_mclass
(self, "Iterator")
864 var mapit_cla
= v
.try_get_mclass
(self, "MapIterator")
868 if colit_cla
!= null and v
.is_subtype
(ittype
, colit_cla
.get_mtype
([objcla
.mclass_type
.as_nullable
])) then
870 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, colit_cla
)
871 var variables
= self.variables
872 if variables
.length
!= 1 then
873 v
.error
(self, "Type Error: Expected one variable")
875 variables
.first
.declared_type
= coltype
.arguments
.first
880 if mapit_cla
!= null and v
.is_subtype
(ittype
, mapit_cla
.get_mtype
([objcla
.mclass_type
, objcla
.mclass_type
.as_nullable
])) then
882 var coltype
= ittype
.supertype_to
(v
.mmodule
, v
.anchor
, mapit_cla
)
883 var variables
= self.variables
884 if variables
.length
!= 2 then
885 v
.error
(self, "Type Error: Expected two variables")
887 variables
[0].declared_type
= coltype
.arguments
[0]
888 variables
[1].declared_type
= coltype
.arguments
[1]
893 if not is_col
and not is_map
then
894 v
.error
(self, "Type Error: Expected method 'iterator' to return an Iterator of MapIterator type")
898 # anchor formal and virtual types
899 if mtype
.need_anchor
then mtype
= v
.anchor_to
(mtype
)
901 self.coltype
= mtype
.as(MClassType)
903 # get methods is_ok, next, item
904 var ikdef
= v
.get_method
(self, ittype
, "is_ok", false)
905 if ikdef
== null then
906 v
.error
(self, "Type Error: Expected method 'is_ok' in Iterator type {ittype}")
909 self.method_is_ok
= ikdef
.mproperty
911 var itemdef
= v
.get_method
(self, ittype
, "item", false)
912 if itemdef
== null then
913 v
.error
(self, "Type Error: Expected method 'item' in Iterator type {ittype}")
916 self.method_item
= itemdef
.mproperty
918 var nextdef
= v
.get_method
(self, ittype
, "next", false)
919 if nextdef
== null then
920 v
.error
(self, "Type Error: Expected method 'next' in Iterator type {ittype}")
923 self.method_next
= nextdef
.mproperty
926 var keydef
= v
.get_method
(self, ittype
, "key", false)
927 if keydef
== null then
928 v
.error
(self, "Type Error: Expected method 'key' in Iterator type {ittype}")
931 self.method_key
= keydef
.mproperty
935 redef fun accept_typing
(v
)
937 var mtype
= v
.visit_expr
(n_expr
)
938 if mtype
== null then return
940 self.do_type_iterator
(v
, mtype
)
942 v
.visit_stmt
(n_block
)
947 redef class AAssertExpr
948 redef fun accept_typing
(v
)
950 v
.visit_expr_bool
(n_expr
)
958 redef fun accept_typing
(v
)
960 v
.visit_expr_bool
(n_expr
)
961 v
.visit_expr_bool
(n_expr2
)
962 self.mtype
= v
.type_bool
(self)
966 redef class AImpliesExpr
967 redef fun accept_typing
(v
)
969 v
.visit_expr_bool
(n_expr
)
970 v
.visit_expr_bool
(n_expr2
)
971 self.mtype
= v
.type_bool
(self)
976 redef fun accept_typing
(v
)
978 v
.visit_expr_bool
(n_expr
)
979 v
.visit_expr_bool
(n_expr2
)
980 self.mtype
= v
.type_bool
(self)
986 redef fun accept_typing
(v
)
988 v
.visit_expr_bool
(n_expr
)
989 self.mtype
= v
.type_bool
(self)
993 redef class AOrElseExpr
994 redef fun accept_typing
(v
)
996 var t1
= v
.visit_expr
(n_expr
)
997 var t2
= v
.visit_expr
(n_expr2
)
999 if t1
== null or t2
== null then
1003 if t1
isa MNullableType then
1007 var t
= v
.merge_types
(self, [t1
, t2
])
1009 t
= v
.mmodule
.object_type
1010 if t2
isa MNullableType then
1013 #v.error(self, "Type Error: ambiguous type {t1} vs {t2}")
1020 redef fun accept_typing
(v
)
1022 v
.visit_expr
(n_expr
)
1023 v
.visit_expr
(n_expr2
)
1024 self.mtype
= v
.type_bool
(self)
1028 redef class ATrueExpr
1029 redef fun accept_typing
(v
)
1031 self.mtype
= v
.type_bool
(self)
1035 redef class AFalseExpr
1036 redef fun accept_typing
(v
)
1038 self.mtype
= v
.type_bool
(self)
1042 redef class AIntExpr
1043 redef fun accept_typing
(v
)
1045 var mclass
= v
.get_mclass
(self, "Int")
1046 if mclass
== null then return # Forward error
1047 self.mtype
= mclass
.mclass_type
1051 redef class AFloatExpr
1052 redef fun accept_typing
(v
)
1054 var mclass
= v
.get_mclass
(self, "Float")
1055 if mclass
== null then return # Forward error
1056 self.mtype
= mclass
.mclass_type
1060 redef class ACharExpr
1061 redef fun accept_typing
(v
)
1063 var mclass
= v
.get_mclass
(self, "Char")
1064 if mclass
== null then return # Forward error
1065 self.mtype
= mclass
.mclass_type
1069 redef class AStringFormExpr
1070 redef fun accept_typing
(v
)
1072 var mclass
= v
.get_mclass
(self, "String")
1073 if mclass
== null then return # Forward error
1074 self.mtype
= mclass
.mclass_type
1078 redef class ASuperstringExpr
1079 redef fun accept_typing
(v
)
1081 var mclass
= v
.get_mclass
(self, "String")
1082 if mclass
== null then return # Forward error
1083 self.mtype
= mclass
.mclass_type
1084 for nexpr
in self.n_exprs
do
1085 var t
= v
.visit_expr
(nexpr
)
1090 redef class AArrayExpr
1091 redef fun accept_typing
(v
)
1093 var mtypes
= new Array[nullable MType]
1094 for e
in self.n_exprs
.n_exprs
do
1095 var t
= v
.visit_expr
(e
)
1101 var mtype
= v
.merge_types
(self, mtypes
)
1102 if mtype
== null then
1103 v
.error
(self, "Type Error: ambiguous array type {mtypes.join(" ")}")
1106 var mclass
= v
.get_mclass
(self, "Array")
1107 if mclass
== null then return # Forward error
1108 self.mtype
= mclass
.get_mtype
([mtype
])
1112 redef class ARangeExpr
1113 redef fun accept_typing
(v
)
1115 var discrete_class
= v
.get_mclass
(self, "Discrete")
1116 if discrete_class
== null then return # Forward error
1117 var discrete_type
= discrete_class
.intro
.bound_mtype
1118 var t1
= v
.visit_expr_subtype
(self.n_expr
, discrete_type
)
1119 var t2
= v
.visit_expr_subtype
(self.n_expr2
, discrete_type
)
1120 if t1
== null or t2
== null then return
1121 var mclass
= v
.get_mclass
(self, "Range")
1122 if mclass
== null then return # Forward error
1123 if v
.is_subtype
(t1
, t2
) then
1124 self.mtype
= mclass
.get_mtype
([t2
])
1125 else if v
.is_subtype
(t2
, t1
) then
1126 self.mtype
= mclass
.get_mtype
([t1
])
1128 v
.error
(self, "Type Error: Cannot create range: {t1} vs {t2}")
1133 redef class ANullExpr
1134 redef fun accept_typing
(v
)
1136 self.mtype
= v
.mmodule
.model
.null_type
1140 redef class AIsaExpr
1141 # The static type to cast to.
1142 # (different from the static type of the expression that is `Bool`).
1143 var cast_type
: nullable MType
1144 redef fun accept_typing
(v
)
1146 var mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1147 self.cast_type
= mtype
1149 var variable
= self.n_expr
.its_variable
1150 if variable
!= null then
1151 var orig
= self.n_expr
.mtype
1152 var from
= if orig
!= null then orig
.to_s
else "invalid"
1153 var to
= if mtype
!= null then mtype
.to_s
else "invalid"
1154 #debug("adapt {variable}: {from} -> {to}")
1155 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1158 self.mtype
= v
.type_bool
(self)
1162 redef class AAsCastExpr
1163 redef fun accept_typing
(v
)
1165 self.mtype
= v
.visit_expr_cast
(self, self.n_expr
, self.n_type
)
1169 redef class AAsNotnullExpr
1170 redef fun accept_typing
(v
)
1172 var mtype
= v
.visit_expr
(self.n_expr
)
1173 if mtype
isa MNullType then
1174 v
.error
(self, "Type error: as(not null) on null")
1177 if mtype
isa MNullableType then
1178 self.mtype
= mtype
.mtype
1181 # TODO: warn on useless as not null
1186 redef class AProxyExpr
1187 redef fun accept_typing
(v
)
1189 self.mtype
= v
.visit_expr
(self.n_expr
)
1193 redef class ASelfExpr
1194 redef var its_variable
: nullable Variable
1195 redef fun accept_typing
(v
)
1197 var variable
= v
.selfvariable
1198 self.its_variable
= variable
1199 self.mtype
= v
.get_variable
(self, variable
)
1203 ## MESSAGE SENDING AND PROPERTY
1205 redef class ASendExpr
1206 # @depreciated: use `callsite`
1207 fun mproperty
: nullable MMethod do return callsite
.mproperty
1209 # The property invoked by the send.
1210 var callsite
: nullable CallSite
1212 redef fun accept_typing
(v
)
1214 var recvtype
= v
.visit_expr
(self.n_expr
)
1215 var name
= self.property_name
1217 if recvtype
== null then return # Forward error
1218 if recvtype
isa MNullType then
1219 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1223 var callsite
= v
.get_method
(self, recvtype
, name
, self.n_expr
isa ASelfExpr)
1224 if callsite
== null then return
1225 self.callsite
= callsite
1226 var msignature
= callsite
.msignature
1228 var args
= compute_raw_arguments
1229 self.raw_arguments
= args
1231 callsite
.check_signature
(v
, args
)
1233 if callsite
.mproperty
.is_init
then
1234 var vmpropdef
= v
.mpropdef
1235 if not (vmpropdef
isa MMethodDef and vmpropdef
.mproperty
.is_init
) then
1236 v
.error
(self, "Can call a init only in another init")
1240 var ret
= msignature
.return_mtype
1244 self.is_typed
= true
1247 if self.n_closure_defs
.length
== msignature
.mclosures
.length
then
1248 for i
in [0..self.n_closure_defs
.length
[ do
1249 self.n_closure_defs
[i
].accept_typing
(v
, msignature
.mclosures
[i
])
1252 debug
("closure: got {self.n_closure_defs.length}, want {msignature.mclosures.length}")
1256 # The name of the property
1257 # Each subclass simply provide the correct name.
1258 private fun property_name
: String is abstract
1260 # An array of all arguments (excluding self)
1261 var raw_arguments
: nullable Array[AExpr]
1263 private fun compute_raw_arguments
: Array[AExpr] is abstract
1266 redef class ABinopExpr
1267 redef fun compute_raw_arguments
do return [n_expr2
]
1270 redef fun property_name
do return "=="
1271 redef fun accept_typing
(v
)
1275 var variable
= self.n_expr
.its_variable
1276 if variable
== null then return
1277 var mtype
= self.n_expr2
.mtype
1278 if not mtype
isa MNullType then return
1279 var vartype
= v
.get_variable
(self, variable
)
1280 if not vartype
isa MNullableType then return
1281 self.after_flow_context
.when_true
.set_var
(variable
, mtype
)
1282 self.after_flow_context
.when_false
.set_var
(variable
, vartype
.mtype
)
1283 #debug("adapt {variable}:{vartype} ; true->{mtype} false->{vartype.mtype}")
1287 redef fun property_name
do return "!="
1288 redef fun accept_typing
(v
)
1292 var variable
= self.n_expr
.its_variable
1293 if variable
== null then return
1294 var mtype
= self.n_expr2
.mtype
1295 if not mtype
isa MNullType then return
1296 var vartype
= v
.get_variable
(self, variable
)
1297 if not vartype
isa MNullableType then return
1298 self.after_flow_context
.when_false
.set_var
(variable
, mtype
)
1299 self.after_flow_context
.when_true
.set_var
(variable
, vartype
.mtype
)
1300 #debug("adapt {variable}:{vartype} ; true->{vartype.mtype} false->{mtype}")
1304 redef fun property_name
do return "<"
1307 redef fun property_name
do return "<="
1310 redef fun property_name
do return "<<"
1313 redef fun property_name
do return ">"
1316 redef fun property_name
do return ">="
1319 redef fun property_name
do return ">>"
1321 redef class APlusExpr
1322 redef fun property_name
do return "+"
1324 redef class AMinusExpr
1325 redef fun property_name
do return "-"
1327 redef class AStarshipExpr
1328 redef fun property_name
do return "<=>"
1330 redef class AStarExpr
1331 redef fun property_name
do return "*"
1333 redef class ASlashExpr
1334 redef fun property_name
do return "/"
1336 redef class APercentExpr
1337 redef fun property_name
do return "%"
1340 redef class AUminusExpr
1341 redef fun property_name
do return "unary -"
1342 redef fun compute_raw_arguments
do return new Array[AExpr]
1346 redef class ACallExpr
1347 redef fun property_name
do return n_id
.text
1348 redef fun compute_raw_arguments
do return n_args
.to_a
1351 redef class ACallAssignExpr
1352 redef fun property_name
do return n_id
.text
+ "="
1353 redef fun compute_raw_arguments
1355 var res
= n_args
.to_a
1361 redef class ABraExpr
1362 redef fun property_name
do return "[]"
1363 redef fun compute_raw_arguments
do return n_args
.to_a
1366 redef class ABraAssignExpr
1367 redef fun property_name
do return "[]="
1368 redef fun compute_raw_arguments
1370 var res
= n_args
.to_a
1376 redef class ASendReassignFormExpr
1377 # @depreciated use `write_callsite`
1378 fun write_mproperty
: nullable MMethod do return write_callsite
.mproperty
1380 # The property invoked for the writing
1381 var write_callsite
: nullable CallSite
1383 redef fun accept_typing
(v
)
1385 var recvtype
= v
.visit_expr
(self.n_expr
)
1386 var name
= self.property_name
1388 if recvtype
== null then return # Forward error
1389 if recvtype
isa MNullType then
1390 v
.error
(self, "Error: Method '{name}' call on 'null'.")
1394 var for_self
= self.n_expr
isa ASelfExpr
1395 var callsite
= v
.get_method
(self, recvtype
, name
, for_self
)
1397 if callsite
== null then return
1398 self.callsite
= callsite
1400 var args
= compute_raw_arguments
1401 self.raw_arguments
= args
1403 callsite
.check_signature
(v
, args
)
1405 var readtype
= callsite
.msignature
.return_mtype
1406 if readtype
== null then
1407 v
.error
(self, "Error: {name} is not a function")
1411 var wcallsite
= v
.get_method
(self, recvtype
, name
+ "=", self.n_expr
isa ASelfExpr)
1412 if wcallsite
== null then return
1413 self.write_callsite
= wcallsite
1415 var wtype
= self.resolve_reassignment
(v
, readtype
, wcallsite
.msignature
.mparameters
.last
.mtype
)
1416 if wtype
== null then return
1418 args
= args
.to_a
# duplicate so raw_arguments keeps only the getter args
1419 args
.add
(self.n_value
)
1420 wcallsite
.check_signature
(v
, args
)
1422 self.is_typed
= true
1426 redef class ACallReassignExpr
1427 redef fun property_name
do return n_id
.text
1428 redef fun compute_raw_arguments
do return n_args
.to_a
1431 redef class ABraReassignExpr
1432 redef fun property_name
do return "[]"
1433 redef fun compute_raw_arguments
do return n_args
.to_a
1436 redef class AInitExpr
1437 redef fun property_name
do return "init"
1438 redef fun compute_raw_arguments
do return n_args
.to_a
1442 fun to_a
: Array[AExpr] do return self.n_exprs
.to_a
1447 redef class ASuperExpr
1448 # The method to call if the super is in fact a 'super init call'
1449 # Note: if the super is a normal call-next-method, then this attribute is null
1450 var mproperty
: nullable MMethod
1452 redef fun accept_typing
(v
)
1454 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1455 var mproperty
= v
.mpropdef
.mproperty
1456 if not mproperty
isa MMethod then
1457 v
.error
(self, "Error: super only usable in a method")
1460 var superprops
= mproperty
.lookup_super_definitions
(v
.mmodule
, recvtype
)
1461 if superprops
.length
== 0 then
1462 if mproperty
.is_init
and v
.mpropdef
.is_intro
then
1463 process_superinit
(v
)
1466 v
.error
(self, "Error: No super method to call for {mproperty}.")
1469 # FIXME: covariance of return type in linear extension?
1470 var superprop
= superprops
.first
1471 assert superprop
isa MMethodDef
1473 var msignature
= v
.resolve_signature_for
(superprop
, recvtype
, true)
1474 var args
= self.n_args
.to_a
1475 if args
.length
> 0 then
1476 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1478 self.mtype
= msignature
.return_mtype
1479 self.is_typed
= true
1480 v
.mpropdef
.has_supercall
= true
1483 private fun process_superinit
(v
: TypeVisitor)
1485 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1486 var mproperty
= v
.mpropdef
.mproperty
1487 var superprop
: nullable MMethodDef = null
1488 for msupertype
in v
.nclassdef
.mclassdef
.supertypes
do
1489 msupertype
= msupertype
.anchor_to
(v
.mmodule
, recvtype
)
1490 var errcount
= v
.modelbuilder
.toolcontext
.error_count
1491 var candidate
= v
.try_get_mproperty_by_name2
(self, msupertype
, mproperty
.name
).as(nullable MMethod)
1492 if candidate
== null then
1493 if v
.modelbuilder
.toolcontext
.error_count
> errcount
then return # Forard error
1494 continue # Try next super-class
1496 if superprop
!= null and superprop
.mproperty
!= candidate
then
1497 v
.error
(self, "Error: conflicting super constructor to call for {mproperty}: {candidate.full_name}, {superprop.mproperty.full_name}")
1500 var candidatedefs
= candidate
.lookup_definitions
(v
.mmodule
, recvtype
)
1501 if superprop
!= null then
1502 if superprop
== candidatedefs
.first
then continue
1503 candidatedefs
.add
(superprop
)
1505 if candidatedefs
.length
> 1 then
1506 v
.error
(self, "Error: confliting property definitions for property {mproperty} in {recvtype}: {candidatedefs.join(", ")}")
1509 superprop
= candidatedefs
.first
1511 if superprop
== null then
1512 v
.error
(self, "Error: No super method to call for {mproperty}.")
1515 self.mproperty
= superprop
.mproperty
1517 var args
= self.n_args
.to_a
1518 var msignature
= v
.resolve_signature_for
(superprop
, recvtype
, true)
1519 if args
.length
> 0 then
1520 v
.check_signature
(self, args
, mproperty
.name
, msignature
)
1522 # TODO: Check signature
1525 self.is_typed
= true
1531 redef class ANewExpr
1532 # @depreciated use `callsite`
1533 fun mproperty
: nullable MMethod do return self.callsite
.mproperty
1535 # The constructor invoked by the new.
1536 var callsite
: nullable CallSite
1538 redef fun accept_typing
(v
)
1540 var recvtype
= v
.resolve_mtype
(self.n_type
)
1541 if recvtype
== null then return
1542 self.mtype
= recvtype
1544 if not recvtype
isa MClassType then
1545 if recvtype
isa MNullableType then
1546 v
.error
(self, "Type error: cannot instantiate the nullable type {recvtype}.")
1549 v
.error
(self, "Type error: cannot instantiate the formal type {recvtype}.")
1561 var callsite
= v
.get_method
(self, recvtype
, name
, false)
1562 if callsite
== null then return
1564 self.callsite
= callsite
1566 if not callsite
.mproperty
.is_init_for
(recvtype
.mclass
) then
1567 v
.error
(self, "Error: {name} is not a constructor.")
1571 var args
= n_args
.to_a
1572 callsite
.check_signature
(v
, args
)
1578 redef class AAttrFormExpr
1579 # The attribute acceded.
1580 var mproperty
: nullable MAttribute
1582 # The static type of the attribute.
1583 var attr_type
: nullable MType
1585 # Resolve the attribute acceded.
1586 private fun resolve_property
(v
: TypeVisitor)
1588 var recvtype
= v
.visit_expr
(self.n_expr
)
1589 if recvtype
== null then return # Skip error
1590 var name
= self.n_id
.text
1591 if recvtype
isa MNullType then
1592 v
.error
(self, "Error: Attribute '{name}' access on 'null'.")
1596 var unsafe_type
= v
.anchor_to
(recvtype
)
1597 var mproperty
= v
.try_get_mproperty_by_name2
(self, unsafe_type
, name
)
1598 if mproperty
== null then
1599 v
.modelbuilder
.error
(self, "Error: Attribute {name} doesn't exists in {recvtype}.")
1602 assert mproperty
isa MAttribute
1603 self.mproperty
= mproperty
1605 var mpropdefs
= mproperty
.lookup_definitions
(v
.mmodule
, unsafe_type
)
1606 assert mpropdefs
.length
== 1
1607 var mpropdef
= mpropdefs
.first
1608 var attr_type
= mpropdef
.static_mtype
.as(not null)
1609 attr_type
= v
.resolve_for
(attr_type
, recvtype
, self.n_expr
isa ASelfExpr)
1610 self.attr_type
= attr_type
1614 redef class AAttrExpr
1615 redef fun accept_typing
(v
)
1617 self.resolve_property
(v
)
1618 self.mtype
= self.attr_type
1623 redef class AAttrAssignExpr
1624 redef fun accept_typing
(v
)
1626 self.resolve_property
(v
)
1627 var mtype
= self.attr_type
1629 v
.visit_expr_subtype
(self.n_value
, mtype
)
1630 self.is_typed
= true
1634 redef class AAttrReassignExpr
1635 redef fun accept_typing
(v
)
1637 self.resolve_property
(v
)
1638 var mtype
= self.attr_type
1639 if mtype
== null then return # Skip error
1641 self.resolve_reassignment
(v
, mtype
, mtype
)
1643 self.is_typed
= true
1647 redef class AIssetAttrExpr
1648 redef fun accept_typing
(v
)
1650 self.resolve_property
(v
)
1651 var mtype
= self.attr_type
1652 if mtype
== null then return # Skip error
1654 var recvtype
= self.n_expr
.mtype
.as(not null)
1655 var bound
= v
.resolve_for
(mtype
, recvtype
, false)
1656 if bound
isa MNullableType then
1657 v
.error
(self, "Error: isset on a nullable attribute.")
1659 self.mtype
= v
.type_bool
(self)
1665 redef class AClosureCallExpr
1666 redef fun accept_typing
(v
)
1668 var variable
= self.variable
1669 if variable
== null then return # Skip error
1671 var recvtype
= v
.nclassdef
.mclassdef
.bound_mtype
1672 var msignature
= variable
.declared_type
.as(not null)
1673 msignature
= v
.resolve_for
(msignature
, recvtype
, false).as(MSignature)
1675 var args
= n_args
.to_a
1676 v
.check_signature
(self, args
, variable
.name
, msignature
)
1678 self.is_typed
= true
1679 self.mtype
= msignature
.return_mtype
1683 redef class AClosureDef
1684 var mclosure
: nullable MParameter
1686 private fun accept_typing
(v
: TypeVisitor, mparameter
: MParameter)
1688 var variables
= self.variables
1689 if variables
== null then return
1691 self.mclosure
= mparameter
1692 var msignature
= mparameter
.mtype
.as(MSignature)
1694 if msignature
.arity
!= variables
.length
then
1695 v
.error
(self, "Type error: closure {mparameter.name} expects {msignature.arity} parameters, {variables.length} given")
1699 for i
in [0..variables
.length
[ do
1700 variables
[i
].declared_type
= msignature
.mparameters
[i
].mtype
1703 v
.visit_stmt
(self.n_expr
)
1709 redef class ADebugTypeExpr
1710 redef fun accept_typing
(v
)
1712 var expr
= v
.visit_expr
(self.n_expr
)
1713 if expr
== null then return
1714 var unsafe
= v
.anchor_to
(expr
)
1715 var ntype
= self.n_type
1716 var mtype
= v
.resolve_mtype
(ntype
)
1717 if mtype
!= null and mtype
!= expr
then
1718 var umtype
= v
.anchor_to
(mtype
)
1719 v
.modelbuilder
.warning
(self, "Found type {expr} (-> {unsafe}), expected {mtype} (-> {umtype})")
1721 self.is_typed
= true