fun get_primitive_class(name: String): MClass
do
var cla = self.model.get_mclasses_by_name(name)
- if cla == null then
+ # Filter classes by introducing module
+ if cla != null then cla = [for c in cla do if self.in_importation <= c.intro_mmodule then c]
+ if cla == null or cla.is_empty then
if name == "Bool" and self.model.get_mclasses_by_name("Object") != null then
# Bool is injected because it is needed by engine to code the result
# of the implicit casts.
cladef.add_in_hierarchy
return c
end
- print("Fatal Error: no primitive class {name}")
+ print("Fatal Error: no primitive class {name} in {self}")
exit(1)
end
if cla.length != 1 then
- var msg = "Fatal Error: more than one primitive class {name}:"
+ var msg = "Fatal Error: more than one primitive class {name} in {self}:"
for c in cla do msg += " {c.full_name}"
print msg
#exit(1)
#
# Warning: such a definition may not exist in the early life of the object.
# In this case, the method will abort.
+ #
+ # Use `try_intro` instead
var intro: MClassDef is noinit
+ # The definition that introduces the class or null if not yet known.
+ #
+ # See `intro`
+ fun try_intro: nullable MClassDef do
+ if isset _intro then return _intro else return null
+ end
+
# Return the class `self` in the class hierarchy of the module `mmodule`.
#
# SEE: `MModule::flatten_mclass_hierarchy`
var in_hierarchy: nullable POSetElement[MClassDef] = null
# Is the definition the one that introduced `mclass`?
- fun is_intro: Bool do return mclass.intro == self
+ fun is_intro: Bool do return isset mclass._intro and mclass.intro == self
# All properties introduced by the classdef
var intro_mproperties = new Array[MProperty]
if sup isa MNullableType then
sup_accept_null = true
sup = sup.mtype
+ else if sup isa MNotNullType then
+ sup = sup.mtype
else if sup isa MNullType then
sup_accept_null = true
end
# Can `sub` provide null or not?
# Thus we can match with `sup_accept_null`
# Also discard the nullable marker if it exists
+ var sub_reject_null = false
if sub isa MNullableType then
if not sup_accept_null then return false
sub = sub.mtype
+ else if sub isa MNotNullType then
+ sub_reject_null = true
+ sub = sub.mtype
else if sub isa MNullType then
return sup_accept_null
end
assert anchor != null
sub = sub.lookup_bound(mmodule, anchor)
+ if sub_reject_null then sub = sub.as_notnull
#print "3.is {sub} a {sup}?"
# Manage the second layer of null/nullable
if sub isa MNullableType then
- if not sup_accept_null then return false
+ if not sup_accept_null and not sub_reject_null then return false
+ sub = sub.mtype
+ else if sub isa MNotNullType then
+ sub_reject_null = true
sub = sub.mtype
else if sub isa MNullType then
return sup_accept_null
end
#print "4.is {sub} a {sup}? <- no more resolution"
- assert sub isa MClassType # It is the only remaining type
+ assert sub isa MClassType else print "{sub} <? {sub}" # It is the only remaining type
# A unfixed formal type can only accept itself
if sup isa MFormalType then
return res
end
- # Return the not nullable version of the type
- # Is the type is already not nullable, then self is returned.
+ # Remove the base type of a decorated (proxy) type.
+ # Is the type is not decorated, then self is returned.
#
- # Note: this just remove the `nullable` notation, but the result can still contains null.
+ # Most of the time it is used to return the not nullable version of a nullable type.
+ # In this case, this just remove the `nullable` notation, but the result can still contains null.
# For instance if `self isa MNullType` or self is a formal type bounded by a nullable type.
- fun as_notnullable: MType
+ # If you really want to exclude the `null` value, then use `as_notnull`
+ fun undecorate: MType
do
return self
end
+ # Returns the not null version of the type.
+ # That is `self` minus the `null` value.
+ #
+ # For most types, this return `self`.
+ # For formal types, this returns a special `MNotNullType`
+ fun as_notnull: MType do return self
+
private var as_nullable_cache: nullable MType = null
# and need a context (anchor and mmodule) to be useful.
abstract class MFormalType
super MType
+
+ redef var as_notnull = new MNotNullType(self) is lazy
end
# A virtual formal type.
redef fun lookup_fixed(mmodule: MModule, resolved_receiver: MType): MType
do
assert not resolved_receiver.need_anchor
- resolved_receiver = resolved_receiver.as_notnullable
+ resolved_receiver = resolved_receiver.undecorate
assert resolved_receiver isa MClassType # It is the only remaining type
var prop = lookup_single_definition(mmodule, resolved_receiver)
redef fun lookup_bound(mmodule: MModule, resolved_receiver: MType): MType
do
assert not resolved_receiver.need_anchor
- resolved_receiver = resolved_receiver.as_notnullable
+ resolved_receiver = resolved_receiver.undecorate
assert resolved_receiver isa MClassType # It is the only remaining type
var goalclass = self.mclass
if resolved_receiver.mclass == goalclass then
redef fun lookup_fixed(mmodule: MModule, resolved_receiver: MType): MType
do
assert not resolved_receiver.need_anchor
- resolved_receiver = resolved_receiver.as_notnullable
+ resolved_receiver = resolved_receiver.undecorate
assert resolved_receiver isa MClassType # It is the only remaining type
var res = self.resolve_for(resolved_receiver.mclass.mclass_type, resolved_receiver, mmodule, false)
return res
end
end
-# A type that decorate an other type.
+# A type that decorates another type.
#
# The point of this class is to provide a common implementation of sevices that just forward to the original type.
# Specific decorator are expected to redefine (or to extend) the default implementation as this suit them.
redef fun model do return self.mtype.model
redef fun need_anchor do return mtype.need_anchor
redef fun as_nullable do return mtype.as_nullable
- redef fun as_notnullable do return mtype.as_notnullable
+ redef fun as_notnull do return mtype.as_notnull
+ redef fun undecorate do return mtype.undecorate
redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
do
var res = self.mtype.resolve_for(mtype, anchor, mmodule, cleanup_virtual)
end
end
+# A non-null version of a formal type.
+#
+# When a formal type in bounded to a nullable type, this is the type of the not null version of it.
+class MNotNullType
+ super MProxyType
+
+ redef fun to_s do return "not null {mtype}"
+ redef var full_name is lazy do return "not null {mtype.full_name}"
+ redef var c_name is lazy do return "notnull__{mtype.c_name}"
+
+ redef fun as_notnull do return self
+
+ redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual)
+ do
+ var res = super
+ return res.as_notnull
+ end
+
+ # Efficiently returns `mtype.lookup_fixed(mmodule, resolved_receiver).as_notnull`
+ redef fun lookup_fixed(mmodule, resolved_receiver)
+ do
+ var t = super
+ if t == mtype then return self
+ return t.as_notnull
+ end
+end
+
# The type of the only value null
#
# The is only one null type per model, see `MModel::null_type`.
redef fun full_name do return "null"
redef fun c_name do return "null"
redef fun as_nullable do return self
+
+ # Aborts on `null`
+ redef fun as_notnull do abort # sorry...
redef fun need_anchor do return false
redef fun resolve_for(mtype, anchor, mmodule, cleanup_virtual) do return self
redef fun can_resolve_for(mtype, anchor, mmodule) do return true
fun lookup_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
do
assert not mtype.need_anchor
- mtype = mtype.as_notnullable
+ mtype = mtype.undecorate
var cache = self.lookup_definitions_cache[mmodule, mtype]
if cache != null then return cache
fun lookup_super_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
do
assert not mtype.need_anchor
- mtype = mtype.as_notnullable
+ mtype = mtype.undecorate
# First, select all candidates
var candidates = new Array[MPROPDEF]
# REQUIRE: `mtype.has_mproperty(mmodule, self)`
fun lookup_all_definitions(mmodule: MModule, mtype: MType): Array[MPROPDEF]
do
- mtype = mtype.as_notnullable
+ mtype = mtype.undecorate
var cache = self.lookup_all_definitions_cache[mmodule, mtype]
if cache != null then return cache