metamodel: fast-fail in getters
authorJean Privat <jean@pryen.org>
Thu, 11 Jun 2009 15:45:43 +0000 (11:45 -0400)
committerJean Privat <jean@pryen.org>
Tue, 16 Jun 2009 14:58:18 +0000 (10:58 -0400)
Instead of testing and returning null among getters, just do stuff and
abort if called methods abort.

syntax modules are adapted to use preventive methods (has_*).

Signed-off-by: Jean Privat <jean@pryen.org>

src/metamodel/abstractmetamodel.nit
src/syntax/mmbuilder.nit
src/syntax/typing.nit

index 4e3dbff..260ea30 100644 (file)
@@ -217,11 +217,7 @@ class MMModule
        do
                assert _local_class_by_global != null
                assert c != null
-               if _local_class_by_global.has_key(c) then
-                       return _local_class_by_global[c]
-               else
-                       return null
-               end
+               return _local_class_by_global[c]
        end
 
        # Register a local class to the module
@@ -247,11 +243,7 @@ class MMModule
        # Return null if not class match this name
        meth global_class_named(n: Symbol): MMGlobalClass
        do
-               if _global_class_by_name.has_key(n) then
-                       return _global_class_by_name[n]
-               else
-                       return null
-               end
+               return _global_class_by_name[n]
        end
 
        redef meth to_s do return name.to_s
@@ -419,16 +411,15 @@ class MMLocalClass
        # TODO: Will disapear when qualified names will be available
        meth has_global_property_by_name(n: Symbol): Bool
        do
-               return _properties_by_name.has_key(n)
+               return _properties_by_name.has_key(n) and _properties_by_name[n].length == 1
        end
 
        # Get a global property by its name
        # TODO: Will disapear when qualified names will be available
        meth get_property_by_name(n: Symbol): MMGlobalProperty
        do
-               if not has_global_property_by_name(n) then return null
+               if not has_global_property_by_name(n) then abort
                var props = _properties_by_name[n]
-               if props.length > 1 then return null
                return props.first
        end
 
@@ -444,11 +435,7 @@ class MMLocalClass
        meth method(na: Symbol): MMGlobalProperty
        do
                assert _properties_by_name != null
-               if _properties_by_name.has_key(na) then
-                       return _properties_by_name[na].first
-               end
-
-               return null
+               return _properties_by_name[na].first
        end
 
        # Select a method from its name
@@ -457,7 +444,6 @@ class MMLocalClass
        do
                assert name != null
                var gp = method(name)
-               if gp == null then return null
                var res = self[gp]
                assert res isa MMMethod
                return res
@@ -469,7 +455,6 @@ class MMLocalClass
        do
                assert name != null
                var gp = attribute(name)
-               if gp == null then return null
                var res = self[gp]
                assert res isa MMAttribute
                return res
@@ -481,9 +466,7 @@ class MMLocalClass
        do
                var classes = new Array[MMLocalClass]
                for c in cshe.greaters do
-                       var g = c.method(n)
-                       if g == null then continue
-                       classes.add(c)
+                       if c.has_global_property_by_name(n) then classes.add(c)
                end
                classes = cshe.order.select_smallests(classes)
                var res = new Array[MMLocalProperty]
@@ -522,12 +505,7 @@ class MMLocalClass
        # Get a local proprty by its global property
        meth [](glob: MMGlobalProperty): MMLocalProperty
        do
-               assert _local_property_by_global != null
-               assert glob != null
-               if _local_property_by_global.has_key(glob) then
-                       return _local_property_by_global[glob]
-               end
-               return null
+               return _local_property_by_global[glob]
        end
 
        # The current MMContext
index b0ad5ff..db15e8b 100644 (file)
@@ -173,8 +173,9 @@ redef class MMSrcLocalClass
                                if not gp.is_init then continue
                                super_constructors.add(gp)
                        end
-                       var gp = sc.get_property_by_name(once ("init".to_symbol))
-                       if gp != null then
+                       var initname = once ("init".to_symbol)
+                       if sc.has_global_property_by_name(initname) then
+                               var gp = sc.get_property_by_name(initname)
                                super_inits.add(self[gp])
                        end
                end
index b44f62a..d8ca9de 100644 (file)
@@ -553,11 +553,12 @@ redef class AReassignFormExpr
                        return
                end
                var name = n_assign_op.method_name
-               var prop = type_lvalue.local_class.select_method(name)
-               if prop == null then
+               var lc = type_lvalue.local_class
+               if not lc.has_global_property_by_name(name) then
                        v.error(self, "Error: Method '{name}' doesn't exists in {type_lvalue}.")
                        return
                end
+               var prop = lc.select_method(name)
                prop.global.check_visibility(v, self, v.module, false)
                var psig = prop.signature_for(type_lvalue)
                _assign_method = prop
@@ -876,11 +877,13 @@ redef class AAttrFormExpr
                if not v.check_expr(n_expr) then return
                var type_recv = n_expr.stype
                var name = n_id.to_symbol
-               var prop = type_recv.local_class.select_attribute(name)
-               if prop == null then
+               var lc = type_recv.local_class
+               if not lc.has_global_property_by_name(name) then
                        v.error(self, "Error: Attribute {name} doesn't exists in {type_recv}.")
                        return
-               else if v.module.visibility_for(prop.global.local_class.module) < 3 then
+               end
+               var prop = lc.select_attribute(name)
+               if v.module.visibility_for(prop.global.local_class.module) < 3 then
                        v.error(self, "Error: Attribute {name} from {prop.global.local_class.module} is invisible in {v.module}")
                end
                _prop = prop
@@ -947,7 +950,9 @@ special PExpr
        private meth get_property(v: TypingVisitor, type_recv: MMType, is_implicit_self: Bool, name: Symbol): MMMethod
        do
                if type_recv == null then return null
-               var prop = type_recv.local_class.select_method(name)
+               var lc = type_recv.local_class
+               var prop: MMMethod = null
+               if lc.has_global_property_by_name(name) then prop = lc.select_method(name)
                if prop == null and v.local_property.global.is_init then
                        var props = type_recv.local_class.super_methods_named(name)
                        if props.length > 1 then