From 1d3a89d720ac639f1252fbfb27b6d13de7c510e9 Mon Sep 17 00:00:00 2001 From: Alexandre Terrasa Date: Fri, 21 Jun 2013 02:01:45 -0400 Subject: [PATCH] ni: extracted model facilities in their own module Signed-off-by: Alexandre Terrasa --- src/model_utils.nit | 133 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/ni.nit | 118 ++------------------------------------------- 2 files changed, 137 insertions(+), 114 deletions(-) create mode 100644 src/model_utils.nit diff --git a/src/model_utils.nit b/src/model_utils.nit new file mode 100644 index 0000000..6bdc558 --- /dev/null +++ b/src/model_utils.nit @@ -0,0 +1,133 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2008 Jean Privat +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Model exploration and traversing facilities +module model_utils + +import toolcontext +import exprbuilder + +redef class MModule + # Get the list of mclasses refined in 'self'. + fun redef_mclasses: Set[MClass] do + var mclasses = new HashSet[MClass] + for c in mclassdefs do + if not c.is_intro then mclasses.add(c.mclass) + end + return mclasses + end + + # Get the list of all mclasses imported by 'self'. + fun imported_mclasses: Set[MClass] do + var mclasses = new HashSet[MClass] + for m in in_importation.greaters do + for c in m.mclassdefs do mclasses.add(c.mclass) + end + return mclasses + end +end + +redef class MClass + + # Get direct parents of 'self'. + fun parents: Set[MClass] do + var ret = new HashSet[MClass] + for mclassdef in mclassdefs do + for mclasstype in mclassdef.supertypes do + ret.add(mclasstype.mclass) + end + end + return ret + end + + # Get all ancestors of 'self'. + fun ancestors: Set[MClass] do + var lst = new HashSet[MClass] + for mclassdef in self.mclassdefs do + for super_mclassdef in mclassdef.in_hierarchy.greaters do + if super_mclassdef == mclassdef then continue # skip self + lst.add(super_mclassdef.mclass) + end + end + return lst + end + + # Get the list of constructors available for 'self'. + fun constructors: Set[MMethod] do + var res = new HashSet[MMethod] + for mclassdef in mclassdefs do + for mpropdef in mclassdef.mpropdefs do + if mpropdef isa MMethodDef then + if mpropdef.mproperty.is_init then res.add(mpropdef.mproperty) + end + end + end + return res + end + + # Get the list of methods introduced in 'self'. + fun intro_methods: Set[MMethod] do + var res = new HashSet[MMethod] + for mclassdef in mclassdefs do + for mpropdef in mclassdef.mpropdefs do + if mpropdef isa MMethodDef then + if mpropdef.is_intro and not mpropdef.mproperty.is_init then res.add(mpropdef.mproperty) + end + end + end + return res + end + + # Get the list of locally refined methods in 'self'. + fun redef_methods: Set[MMethod] do + var res = new HashSet[MMethod] + for mclassdef in mclassdefs do + for mpropdef in mclassdef.mpropdefs do + if mpropdef isa MMethodDef then + if not mpropdef.is_intro and not mpropdef.mproperty.is_init then res.add(mpropdef.mproperty) + end + end + end + return res + end + + # Get the list of methods inherited by 'self'. + fun inherited_methods: Set[MMethod] do + var res = new HashSet[MMethod] + for s in ancestors do + for m in s.intro_methods do + if not self.intro_methods.has(m) and not self.redef_methods.has(m) then res.add(m) + end + end + return res + end + + fun is_class: Bool do + return self.kind == concrete_kind or self.kind == abstract_kind + end + + fun is_interface: Bool do + return self.kind == interface_kind + end + + fun is_enum: Bool do + return self.kind == enum_kind + end + + fun is_abstract: Bool do + return self.kind == abstract_kind + end +end diff --git a/src/ni.nit b/src/ni.nit index c9fc166..5b6aa9a 100644 --- a/src/ni.nit +++ b/src/ni.nit @@ -16,8 +16,7 @@ module ni -import toolcontext -import exprbuilder +import model_utils private class Pager var content: String = "" @@ -126,7 +125,7 @@ class NitIndex var cats = new HashMap[String, Collection[MClass]] cats["introduced classes"] = mmodule.intro_mclasses cats["refined classes"] = mmodule.redef_mclasses - cats["inherited classes"] = mmodule.inherited_mclasses + cats["inherited classes"] = mmodule.imported_mclasses for cat, list in cats do if not list.is_empty then @@ -194,26 +193,7 @@ class NitIndex end end -# Model traversing - -redef class MModule - # Get the list of mclasses refined in self - private fun redef_mclasses: Set[MClass] do - var mclasses = new HashSet[MClass] - for c in mclassdefs do - if not c.is_intro then mclasses.add(c.mclass) - end - return mclasses - end - - private fun inherited_mclasses: Set[MClass] do - var mclasses = new HashSet[MClass] - for m in in_importation.greaters do - for c in m.mclassdefs do mclasses.add(c.mclass) - end - return mclasses - end -end +# Printing facilities redef class MClass @@ -234,97 +214,9 @@ redef class MClass if visibility.to_s == "public" then ret = "{ret}{to_s.green}" if visibility.to_s == "private" then ret = "{ret}{to_s.red}" if visibility.to_s == "protected" then ret = "{ret}{to_s.yellow}" - ret = "{ret} super {supers.join(", ")}" - return ret - end - - private fun supers: Set[MClass] do - var ret = new HashSet[MClass] - for mclassdef in mclassdefs do - for mclasstype in mclassdef.supertypes do - ret.add(mclasstype.mclass) - end - end + ret = "{ret} super {parents.join(", ")}" return ret end - - # Get ancestors of the class (all super classes) - fun ancestors: Set[MClass] do - var lst = new HashSet[MClass] - for mclassdef in self.mclassdefs do - for super_mclassdef in mclassdef.in_hierarchy.greaters do - if super_mclassdef == mclassdef then continue # skip self - lst.add(super_mclassdef.mclass) - end - end - return lst - end - - # Get the list of class constructors - private fun constructors: Set[MMethod] do - var res = new HashSet[MMethod] - for mclassdef in mclassdefs do - for mpropdef in mclassdef.mpropdefs do - if mpropdef isa MMethodDef then - if mpropdef.mproperty.is_init then res.add(mpropdef.mproperty) - end - end - end - return res - end - - # Get the list of intro methods - private fun intro_methods: Set[MMethod] do - var res = new HashSet[MMethod] - for mclassdef in mclassdefs do - for mpropdef in mclassdef.mpropdefs do - if mpropdef isa MMethodDef then - if mpropdef.is_intro and not mpropdef.mproperty.is_init then res.add(mpropdef.mproperty) - end - end - end - return res - end - - # Get the list of locally refined methods - private fun redef_methods: Set[MMethod] do - var res = new HashSet[MMethod] - for mclassdef in mclassdefs do - for mpropdef in mclassdef.mpropdefs do - if mpropdef isa MMethodDef then - if not mpropdef.is_intro and not mpropdef.mproperty.is_init then res.add(mpropdef.mproperty) - end - end - end - return res - end - - # Get the list of locally refined methods - private fun inherited_methods: Set[MMethod] do - var res = new HashSet[MMethod] - for s in ancestors do - for m in s.intro_methods do - if not self.intro_methods.has(m) and not self.redef_methods.has(m) then res.add(m) - end - end - return res - end - - private fun is_class: Bool do - return self.kind == concrete_kind or self.kind == abstract_kind - end - - private fun is_interface: Bool do - return self.kind == interface_kind - end - - private fun is_enum: Bool do - return self.kind == enum_kind - end - - private fun is_abstract: Bool do - return self.kind == abstract_kind - end end redef class MClassDef @@ -333,8 +225,6 @@ redef class MClassDef end end -# ANode printing - redef class AModule private fun comment: String do var ret = "" -- 1.7.9.5