redef fun super_elements(element, elements) do return self.mmodule.super_mclasses(element)
end
-# MClass coloring
-class ClassColoring
- super AbstractColorer[MClass]
-
- type T: MClass
-
- private var mmodule: MModule
-
- # caches
- private var super_elements_cache: Map[T, Set[T]] = new HashMap[T, Set[T]]
- private var parent_elements_cache: Map[T, Set[T]] = new HashMap[T, Set[T]]
- private var sub_elements_cache: Map[T, Set[T]] = new HashMap[T, Set[T]]
-
- init(mainmodule: MModule) do self.mmodule = mainmodule
-
- redef fun super_elements(element, elements) do return self.mmodule.super_mclasses(element)
- fun parent_elements(element: MClass): Set[MClass] do return self.mmodule.parent_mclasses(element)
- redef fun is_element_mi(element, elements) do return self.parent_elements(element).length > 1
- redef fun sub_elements(element, elements) do do return self.mmodule.sub_mclasses(element)
- redef fun linearize(elements) do return self.mmodule.linearize_mclasses(elements)
- redef fun reverse_linearize(elements) do return self.mmodule.reverse_linearize_mclasses(elements)
-end
-
-# MProperty coloring
-class PropertyColoring
-
- type MPROP: MProperty
- type MPROPDEF: MPropDef
-
- private var mmodule: MModule
- private var class_coloring: ClassColoring
- private var coloration_result: Map[MPROP, Int] = new HashMap[MPROP, Int]
-
- init(mmodule: MModule, class_coloring: ClassColoring) do
- self.mmodule = mmodule
- self.class_coloring = class_coloring
- end
-
- fun colorize: Map[MPROP, Int] do
- colorize_core_properties
- colorize_crown_properties
- return self.coloration_result
- end
-
- # Colorize properties of the core hierarchy
- private fun colorize_core_properties do
- var mclasses = self.class_coloring.core
- var min_color = 0
-
- for mclass in mclasses do
- var color = min_color
-
- # if the class is root, get the minimal color
- if self.class_coloring.parent_elements(mclass).length == 0 then
- colorize_elements(self.properties(mclass), color)
- else
- # check last color used by parents
- color = max_color(color, self.class_coloring.parent_elements(mclass))
- # check max color used in conflicts
- if self.class_coloring.conflicts_graph.has_key(mclass) then
- color = max_color(color, self.class_coloring.conflicts_graph[mclass])
- end
-
- # colorize
- colorize_elements(self.properties(mclass), color)
- end
- end
- end
-
- # Colorize properties of the crown hierarchy
- private fun colorize_crown_properties do
- for mclass in self.class_coloring.crown do
- colorize_elements(self.properties(mclass), max_color(0, self.class_coloring.parent_elements(mclass)))
- end
- end
-
- # Colorize a collection of properties given a starting color
- private fun colorize_elements(elements: Collection[MPROP], start_color: Int) do
- for element in elements do
- if self.coloration_result.has_key(element) then continue
- self.coloration_result[element] = start_color
- start_color += 1
- end
- end
-
- private fun max_color(min_color: Int, mclasses: Collection[MClass]): Int do
- var max_color = min_color
-
- for mclass in mclasses do
- for mproperty in self.properties(mclass) do
- var color = min_color
- if self.coloration_result.has_key(mproperty) then
- color = self.coloration_result[mproperty]
- if color >= max_color then max_color = color + 1
- end
- end
- end
- return max_color
- end
-
- # All 'mproperties' associated to all 'mclassdefs' of the class
- private fun properties(mclass: MClass): Set[MPROP] do
- var properties = new HashSet[MPROP]
- for mprop in self.mmodule.properties(mclass) do
- if mprop isa MPROP then properties.add(mprop)
- end
- return properties
- end
-end
-
-# MMethod coloring
-class MethodColoring
- super PropertyColoring
-
- redef type MPROP: MMethod
- redef type MPROPDEF: MMethodDef
- init(mmodule: MModule, class_coloring: ClassColoring) do super
-end
-
-# MAttribute coloring
-class AttributeColoring
- super PropertyColoring
-
- redef type MPROP: MAttribute
- redef type MPROPDEF: MAttributeDef
- init(mmodule: MModule, class_coloring: ClassColoring) do super
-end
-
-# MVirtualTypeProp coloring
-class VTColoring
- super PropertyColoring
-
- redef type MPROP: MVirtualTypeProp
- redef type MPROPDEF: MVirtualTypeDef
- init(mmodule: MModule, class_coloring: ClassColoring) do super
-end
-
-class NaiveVTColoring
- super VTColoring
-
- init(mmodule: MModule, class_coloring: ClassColoring) do super
-
- redef fun colorize: Map[MPROP, Int] do
- var mclasses = new HashSet[MClass]
- mclasses.add_all(self.class_coloring.core)
- mclasses.add_all(self.class_coloring.crown)
- var min_color = 0
-
- for mclass in mclasses do
- min_color = max_color(min_color, mclasses)
- colorize_elements(self.properties(mclass), min_color)
- end
- return self.coloration_result
- end
-end
-
-abstract class VTPerfectHashing
- super VTColoring
-
- private var masks: Map[MClass, Int] = new HashMap[MClass, Int]
-
- init(mmodule: MModule, class_coloring: ClassColoring) do super
-
- redef fun colorize: Map[MPROP, Int] do
- var mclasses = new HashSet[MClass]
- mclasses.add_all(self.class_coloring.core)
- mclasses.add_all(self.class_coloring.crown)
- for mclass in mclasses do
- var vts = self.properties(mclass)
- for vt in vts do
- if coloration_result.has_key(vt) then continue
- coloration_result[vt] = coloration_result.length + 1
- end
- end
- return self.coloration_result
- end
-
- fun compute_masks: Map[MClass, Int] do
- var mclasses = new HashSet[MClass]
- mclasses.add_all(self.class_coloring.core)
- mclasses.add_all(self.class_coloring.crown)
- for mclass in mclasses do
- self.masks[mclass] = compute_mask(self.properties(mclass))
- end
- return self.masks
- end
-
- private fun compute_mask(vts: Set[MPROP]): Int do
- var mask = 0
- loop
- var used = new List[Int]
- for vt in vts do
- var res = op(mask, self.coloration_result[vt])
- if used.has(res) then
- break
- else
- used.add(res)
- end
- end
- if used.length == vts.length then break
- mask += 1
- end
- return mask
- end
-
- fun build_property_tables: Map[MClass, Array[nullable MPROPDEF]] do
- var tables = new HashMap[MClass, Array[nullable MPROPDEF]]
-
- for mclass in self.class_coloring.coloration_result.keys do
- var table = new Array[nullable MPROPDEF]
- # first, fill table from parents by reverse linearization order
- var parents = self.class_coloring.mmodule.super_mclasses(mclass)
- var lin = self.class_coloring.reverse_linearize(parents)
- for parent in lin do
- for mproperty in self.properties(parent) do
- var color = phash(self.coloration_result[mproperty], masks[mclass])
- if table.length <= color then
- for i in [table.length .. color[ do
- table[i] = null
- end
- end
- for mpropdef in mproperty.mpropdefs do
- if mpropdef.mclassdef.mclass == parent then
- table[color] = mpropdef
- end
- end
- end
- end
-
- # then override with local properties
- for mproperty in self.properties(mclass) do
- var color = phash(self.coloration_result[mproperty], masks[mclass])
- if table.length <= color then
- for i in [table.length .. color[ do
- table[i] = null
- end
- end
- for mpropdef in mproperty.mpropdefs do
- if mpropdef.mclassdef.mclass == mclass then
- table[color] = mpropdef
- end
- end
- end
- tables[mclass] = table
- end
- return tables
- end
-
- private fun op(mask: Int, id:Int): Int is abstract
- private fun phash(id: Int, mask: Int): Int do return op(mask, id)
-
-end
-
-class VTModPerfectHashing
- super VTPerfectHashing
- init(mmodule: MModule, class_coloring: ClassColoring) do super
- redef fun op(mask, id) do return mask % id
-end
-
-class VTAndPerfectHashing
- super VTPerfectHashing
- init(mmodule: MModule, class_coloring: ClassColoring) do super
- redef fun op(mask, id) do return mask.bin_and(id)
-end
-
# live unanchored coloring
class UnanchoredTypeColoring