X-Git-Url: http://nitlanguage.org diff --git a/src/metamodel/abstractmetamodel.nit b/src/metamodel/abstractmetamodel.nit index 4ce08ef..8d53ece 100644 --- a/src/metamodel/abstractmetamodel.nit +++ b/src/metamodel/abstractmetamodel.nit @@ -38,12 +38,10 @@ class MMContext # All known modules readable attr _modules: Array[MMModule] = new Array[MMModule] - # Register a new module with the modules it depends on + # Register a new module with the modules it depends on meth add_module(module: MMModule, supers: Array[MMModule]) do - assert supers != null _module_hierarchy.add(module, _module_hierarchy.select_smallests(supers)) - assert module.name != null _modules.add(module) module._mhe = _module_hierarchy[module] end @@ -54,7 +52,6 @@ class MMContext # Register a local class meth add_local_class(c: MMLocalClass, sup: Array[MMLocalClass]) do - assert sup != null var csup = new Array[MMLocalClass] var csups = new Array[String] for s in sup do @@ -82,10 +79,10 @@ class MMDirectory # Parent directory # null if none - readable attr _parent: MMDirectory + readable attr _parent: nullable MMDirectory # The module that introduces the directory if any - readable writable attr _owner: MMModule + readable writable attr _owner: nullable MMModule = null # Known modules in the directory readable attr _modules: Map[Symbol, MMModule] = new HashMap[Symbol, MMModule] @@ -97,10 +94,7 @@ class MMDirectory _modules[module.name] = module end - # Directory hierarchy element - readable attr _dhe: PartialOrderElement[MMDirectory] - - init(name: Symbol, path: String, parent: MMDirectory) do + init(name: Symbol, path: String, parent: nullable MMDirectory) do _name = name _path = path _parent = parent @@ -126,8 +120,11 @@ class MMModule # The directory of the module readable attr _directory: MMDirectory + # The filename of the module + readable attr _filename: String + # Module dependence hierarchy element - readable attr _mhe: PartialOrderElement[MMModule] + readable attr _mhe: nullable PartialOrderElement[MMModule] # All global classes of the module (defined and imported) readable attr _global_classes: Array[MMGlobalClass] = new Array[MMGlobalClass] @@ -156,17 +153,13 @@ class MMModule # Dictionary of global classes attr _global_class_by_name: Map[Symbol, MMGlobalClass] = new HashMap[Symbol, MMGlobalClass] - protected init(name: Symbol, dir: MMDirectory, context: MMContext) + protected init(name: Symbol, dir: MMDirectory, context: MMContext, filename: String) do _name = name _directory = dir _context = context - - if dir == null then - _full_name = name - else - _full_name = dir.full_name_for(name) - end + _full_name = dir.full_name_for(name) + _filename = filename end # Register that a module is imported with a visibility @@ -215,20 +208,7 @@ class MMModule # Get the local class associated with a global class meth [](c: MMGlobalClass): MMLocalClass 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 - end - - # Register a local class to the module - meth add_local_class(c: MMLocalClass) - do - c._module = self - _local_classes.add(c) + return _local_class_by_global[c] end # Get a local class by its name @@ -247,11 +227,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 @@ -259,7 +235,6 @@ class MMModule # Assign super_classes for a local class meth set_supers_class(c: MMLocalClass, supers: Array[MMLocalClass]) do - assert supers != null var tab = _class_specialization_hierarchy.select_smallests(supers) c._cshe = _class_specialization_hierarchy.add(c,tab) var tab_all = c.crhe.direct_greaters.to_a @@ -270,7 +245,6 @@ class MMModule # Register a local class and its global class in the module private meth register_global_class(c: MMLocalClass) do - assert c.global != null _local_class_by_global[c.global] = c end end @@ -310,9 +284,7 @@ class MMGlobalClass # register a new Local class to local class hierarchy (set the crhe value) private meth register_local_class(c: MMLocalClass) do - assert c.module != null - assert c.crhe == null - var sup = new Array[MMLocalClass] + var sup = new Array[MMLocalClass] for s in class_refinement_hierarchy do if c.module.mhe < s.module and s isa MMConcreteClass then sup.add(s) @@ -361,35 +333,38 @@ class MMLocalClass # The module of the local class readable attr _module: MMModule - # Is the class abstract - readable writable attr _abstract: Bool - # The global class of the local class - readable attr _global: MMGlobalClass + meth global: MMGlobalClass do return _global.as(not null) + attr _global: nullable MMGlobalClass # The local class refinement hierarchy element - readable attr _crhe: PartialOrderElement[MMLocalClass] + meth crhe: PartialOrderElement[MMLocalClass] do return _crhe.as(not null) + attr _crhe: nullable PartialOrderElement[MMLocalClass] # The local class specialization hierarchy element - readable attr _cshe: PartialOrderElement[MMLocalClass] + meth cshe: PartialOrderElement[MMLocalClass] do return _cshe.as(not null) + attr _cshe: nullable PartialOrderElement[MMLocalClass] # The local class full hierarchy element - readable attr _che: PartialOrderElement[MMLocalClass] + meth che: PartialOrderElement[MMLocalClass] do return _che.as(not null) + attr _che: nullable PartialOrderElement[MMLocalClass] # Association between local properties and global properties - readable attr _local_property_by_global: Map[MMGlobalProperty, MMLocalProperty] + attr _local_property_by_global: Map[MMGlobalProperty, MMLocalProperty] = new HashMap[MMGlobalProperty, MMLocalProperty] # All known global properties - readable attr _global_properties: Set[MMGlobalProperty] + readable attr _global_properties: Set[MMGlobalProperty] = new HashSet[MMGlobalProperty] # Dictionnary of global properties - readable attr _properties_by_name: Map[Symbol, Array[MMGlobalProperty]] + attr _properties_by_name: Map[Symbol, Array[MMGlobalProperty]] = new HashMap[Symbol, Array[MMGlobalProperty]] # Create a new class with a given name and arity - protected init(name: Symbol, arity: Int) + protected init(mod: MMModule, name: Symbol, arity: Int) do + _module = mod _name = name _arity = arity + mod._local_classes.add(self) end # The corresponding local class in another module @@ -412,7 +387,6 @@ class MMLocalClass # refined classes for this class meth set_global(g: MMGlobalClass) do - assert g != null _global = g _global.register_local_class(self) _module.register_global_class(self) @@ -422,18 +396,15 @@ class MMLocalClass # TODO: Will disapear when qualified names will be available meth has_global_property_by_name(n: Symbol): Bool do - var props = _properties_by_name[n] - return props != null + 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 abort var props = _properties_by_name[n] - if props == null or props.length > 1 then - return null - end return props.first end @@ -448,22 +419,14 @@ class MMLocalClass # TODO: Will disapear when qualified names will be available meth method(na: Symbol): MMGlobalProperty do - assert _properties_by_name != null - var props = _properties_by_name[na] - if props != null then - return props.first - end - - return null + return _properties_by_name[na].first end # Select a method from its name # TODO: Will disapear when qualified names will be available meth select_method(name: Symbol): MMMethod do - assert name != null var gp = method(name) - if gp == null then return null var res = self[gp] assert res isa MMMethod return res @@ -473,9 +436,7 @@ class MMLocalClass # TODO: Will disapear when qualified names will be available meth select_attribute(name: Symbol): MMAttribute do - assert name != null var gp = attribute(name) - if gp == null then return null var res = self[gp] assert res isa MMAttribute return res @@ -487,9 +448,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] @@ -504,11 +463,7 @@ class MMLocalClass # Register a local property and associate it with its global property meth register_local_property(p: MMLocalProperty) do - assert p.global != null - # FIXME: Why a test? - if not _local_property_by_global.has_key(p.global) then - _local_property_by_global[p.global] = p - end + _local_property_by_global[p.global] = p end # Register a global property and associate it with its name @@ -516,25 +471,25 @@ class MMLocalClass do var prop = glob.intro var name = prop.name - var props = _properties_by_name[name] - if props == null then - _properties_by_name[name] = [glob] - else + if _properties_by_name.has_key(name) then _properties_by_name[name].add(glob) + else + _properties_by_name[name] = [glob] end _global_properties.add(glob) register_local_property(prop) end + # Does the global property belong to the class? + meth has_global_property(glob: MMGlobalProperty): Bool + do + return _global_properties.has(glob) + end + # 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 @@ -554,10 +509,10 @@ class MMGlobalProperty readable attr _intro: MMLocalProperty # The local class that introduces the global property - meth local_class: MMLocalClass - do - return intro.local_class - end + meth local_class: MMLocalClass + do + return intro.local_class + end # The property redefinition hierarchy readable attr _property_hierarchy: PartialOrder[MMLocalProperty] = new PartialOrder[MMLocalProperty] @@ -565,10 +520,6 @@ class MMGlobalProperty # Create a new global property introduced by a local one protected init(p: MMLocalProperty) do - assert p != null - - _property_hierarchy = new PartialOrder[MMLocalProperty] - _intro = p add_local_property(p, new Array[MMLocalProperty]) end @@ -578,8 +529,6 @@ class MMGlobalProperty # Register a new local property meth add_local_property(i: MMLocalProperty, sup: Array[MMLocalProperty]) do - assert i != null - assert sup != null i._prhe = _property_hierarchy.add(i,sup) end @@ -590,7 +539,7 @@ class MMGlobalProperty meth is_method: Bool do return intro isa MMMethod # Is the global property a constructor (thus also a method)? - readable writable attr _is_init: Bool + readable writable attr _is_init: Bool = false # Is the global property a constructor for a given class? meth is_init_for(c: MMLocalClass): Bool @@ -617,10 +566,15 @@ class MMLocalProperty readable attr _local_class: MMLocalClass # The global property where belong the local property - readable attr _global: MMGlobalProperty + attr _global: nullable MMGlobalProperty + + meth global: MMGlobalProperty do return _global.as(not null) + meth is_global_set: Bool do return _global != null # Redefinition hierarchy of the local property - readable attr _prhe: PartialOrderElement[MMLocalProperty] + attr _prhe: nullable PartialOrderElement[MMLocalProperty] + + meth prhe: PartialOrderElement[MMLocalProperty] do return _prhe.as(not null) # The module of the local property meth module: MMModule do return _local_class.module @@ -628,7 +582,7 @@ class MMLocalProperty # Full expanded name with all qualifications meth full_name: String do - if global == null then + if _global == null then return "{local_class.module}::{local_class}::(?::{name})" else if global.intro == self then return "{local_class.module}::{local_class}::{name}" @@ -640,7 +594,6 @@ class MMLocalProperty # set the global property for this property meth set_global(g: MMGlobalProperty) do - assert g != null _global = g _local_class.register_local_property(self) end @@ -649,10 +602,11 @@ class MMLocalProperty meth new_global do assert _global == null - _global = new MMGlobalProperty(self) - _local_class.register_global_property(_global) + var global = new MMGlobalProperty(self) + _global = global + _local_class.register_global_property(global) end - + redef meth to_s do return name.to_s # Is the concrete property contain a `super' in the body?