+ # Check that an expression has a static type and that
+ # Display an error and return false if n is a statement
+ # Require that the static type of n is known
+ meth check_expr(n: PExpr): Bool
+ do
+ if not n.is_typed then
+ if tc.error_count == 0 then
+ print("{n.locate} not typed but not error")
+ abort
+ end
+ # An error occured in a sub node,
+ # sillently cascade fail
+ return false
+ else if n.is_statement then
+ error(n, "Type error: expected expression.")
+ return false
+ end
+ return true
+ end
+
+ # Combine check_conform and check_expr
+ meth check_conform_expr(n: PExpr, stype: nullable MMType): Bool
+ do
+ if stype == null then return false
+ if check_expr(n) then return check_conform(n, n.stype, stype) else return false
+ end
+
+ # Check conformance between multiple expressions and a static type
+ # Conformance is granted if among them there is a most general type
+ # Return the most general type if a conformance is found
+ # Display an error and return null if no conformance is found
+ # The only allowed combinaison is with the nullable marker
+ # @param stype is a possible additional type (without node)
+ # Examples:
+ # Int, Int, Object => return Object
+ # Int, Float => display error, return null
+ # nullable Int, Object => return nullable Object
+ meth check_conform_multiexpr(stype: nullable MMType, nodes: Collection[PExpr]): nullable MMType
+ do
+ var node: nullable PExpr = null # candidate node
+ for n in nodes do
+ if not check_expr(n) then return null
+ var ntype = n.stype
+ if stype != null and stype.is_nullable != ntype.is_nullable then
+ # nullable combinaison: if one of them is nulable, considers that both are
+ stype = stype.as_nullable
+ ntype = ntype.as_nullable
+ end
+ if stype == null or stype < ntype then
+ stype = ntype
+ node = n
+ end
+ end
+ for n in nodes do
+ if not n.stype < stype.as(not null) then
+ if node == null then
+ error(n, "Type error: no most general type. Got {n.stype} and {stype}.")
+ else
+ error(n, "Type error: no most general type. Got {n.stype} and {stype} at {node.locate}.")
+ end
+ return null
+ end
+ end
+ return stype
+ end