# Concrete NIT source local classs by name
readable attr _src_local_classes: Map[Symbol, MMSrcLocalClass]
- init(c: MMContext, source: AModule, dir: MMDirectory, name: Symbol)
+ init(c: MMContext, source: AModule, dir: MMDirectory, name: Symbol, filename: String)
do
- super(name, dir, c)
+ super(name, dir, c, filename)
_node = source
_src_local_classes = new HashMap[Symbol, MMSrcLocalClass]
end
if subtype < stype then
return true
end
- #error(n, "Type error: expected {stype}'{stype.module}, got {subtype}'{subtype.module}")
- #abort
+ # Do not enforce nullable subtype rules yet
+ if subtype isa MMTypeNone or subtype.as_notnull < stype.as_notnull then
+ warning(n, "Nullable type warning: expected {stype}, got {subtype}")
+ return true
+ end
error(n, "Type error: expected {stype}, got {subtype}")
return false
end
-
+
# 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
- # FIXME: The tc.error_count is a workaround since currently there is no way
- # to distingate statements from buggy expressions: both have a null stype
- if tc.error_count == 0 and n.stype == null then
+ 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
# 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: MMType, nodes: Collection[PExpr]): MMType
do
var node: 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 (ntype != null and stype < ntype) then
stype = ntype
node = n
var name = n_id.to_symbol
var mod = v.module
var cla = v.local_class
+ var t: MMType
if cla.formal_dict.has_key(name) then
if n_types.length > 0 then
v.error(self, "Type error: formal type {name} cannot have formal parameters.")
return null
end
- var formal = cla.formal_dict[name]
- _stype_cache = formal
- return formal
+ t = cla.formal_dict[name]
+ if n_kwnullable != null then t = t.as_nullable
+ _stype_cache = t
+ return t
end
if cla.global_properties != null and cla.has_global_property_by_name(name) then
v.error(self, "Type error: formal type {name} cannot have formal parameters.")
return null
end
- var t = cla.get_type.local_class.select_virtual_type(name).stype_for(cla.get_type)
+ t = cla.get_type.local_class.select_virtual_type(name).stype_for(cla.get_type)
if t == null then
v.error(self, "Type error: circular definition in formal type {name}.")
return null
end
+ if n_kwnullable != null then t = t.as_nullable
_stype_cache = t
return t
end
for p in n_types do
tab.add(p.get_unchecked_stype(v))
end
- var t = local_class.get_instantiate_type(tab)
- _stype_cache = t
- return t
+ t = local_class.get_instantiate_type(tab)
else
- var t = local_class.get_type
- _stype_cache = t
- return t
+ t = local_class.get_type
end
+ if n_kwnullable != null then t = t.as_nullable
+ _stype_cache = t
+ return t
end
redef meth get_stype(v)
do
var t = get_unchecked_stype(v)
- if t != null then check_conform(v)
+ if t == null then return null
+ if not t.is_valid then return null
+ check_conform(v)
return t
end
for i in [0..arity[ do
var p = n_types[i]
var pt = p.get_stype(v)
- var bt = local_class.get_formal(i).bound
- if bt == null then return
+ var b = local_class.get_formal(i)
+ if not b.is_valid then return
+ var bt = b.bound
bt = bt.adapt_to(st) # We need to abapt because of F-genericity
v.check_conform(p, pt, bt)
end
end
redef class PExpr
- # Static type
- # Is null for statement and for erronus expression
+ # Is the expression node correcly typed
+ # Return false if typed was not yet computed or
+ # if an error occured during the typing computation
+ meth is_typed: Bool is abstract
+
+ # Is the expression node a statement? (ie has no return value)
+ # require: is_typed
+ meth is_statement: Bool is abstract
+
+ # The static type of the expression
+ # require: is_typed and not is_statement
meth stype: MMType is abstract
end