init(mainmodule: MModule) do self.mmodule = mainmodule
- # Build type tables
- fun build_type_tables(mclasses: Set[T], colors: Map[T, Int]): Map[T, Array[nullable T]] do
- var tables = new HashMap[T, Array[nullable T]]
-
- for mclasse in mclasses do
- var table = new Array[nullable T]
- var supers = new HashSet[T]
- supers.add_all(self.super_elements(mclasse, mclasses))
- supers.add(mclasse)
- for sup in supers do
- var color = colors[sup]
- if table.length <= color then
- for i in [table.length .. color[ do
- table[i] = null
- end
- end
- table[color] = sup
- end
- tables[mclasse] = table
- end
- return tables
- end
-
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
return self.coloration_result
end
- # Build type tables
- fun hash_type_tables(mtypes: Set[T], ids: Map[T, Int], masks: Map[T, Int]): Map[T, Array[nullable T]] do
- var tables = new HashMap[T, Array[nullable T]]
-
- for mtype in mtypes do
- var table = new Array[nullable T]
- var supers = new HashSet[T]
- supers.add_all(self.super_elements(mtype, mtypes))
- supers.add(mtype)
-
- for sup in supers do
- var color = phash(ids[sup], masks[mtype])
- if table.length <= color then
- for i in [table.length .. color[ do
- table[i] = null
- end
- end
- table[color] = sup
- end
- tables[mtype] = table
- end
- return tables
- end
-
private fun compute_mask(mtypes: Set[T], ids: Map[T, Int]): Int do
var mask = 0
loop
# Separate compilation of a Nit program with generic type erasure
module separate_erasure_compiler
-import separate_compiler
+intrude import separate_compiler
# Add separate erased compiler specific options
redef class ToolContext
var class_coloring = new ClassModPerfectHashing(mainmodule)
self.class_colors = class_coloring.compute_masks(mclasses, class_ids)
- self.class_tables = class_coloring.hash_type_tables(mclasses, class_ids, class_colors)
+ self.class_tables = self.hash_class_typing_tables(mclasses, class_ids, class_colors, class_coloring)
self.header.add_decl("#define HASH(mask, id) ((mask)%(id))")
else if modelbuilder.toolcontext.opt_phand_typing.value then
var class_coloring = new ClassAndPerfectHashing(mainmodule)
self.class_colors = class_coloring.compute_masks(mclasses, class_ids)
- self.class_tables = class_coloring.hash_type_tables(mclasses, class_ids, class_colors)
+ self.class_tables = self.hash_class_typing_tables(mclasses, class_ids, class_colors, class_coloring)
self.header.add_decl("#define HASH(mask, id) ((mask)&(id))")
else
self.class_ids[mclass] = self.class_ids.length + 1
end
self.class_colors = class_coloring.colorize(mclasses)
- self.class_tables = class_coloring.build_type_tables(mclasses, class_colors)
+ self.class_tables = self.build_class_typing_tables(mclasses, class_colors, class_coloring)
end
end
+ # Build type tables
+ fun build_class_typing_tables(mclasses: Set[MClass], colors: Map[MClass, Int], colorer: ClassColoring): Map[MClass, Array[nullable MClass]] do
+ var tables = new HashMap[MClass, Array[nullable MClass]]
+
+ for mclasse in mclasses do
+ var table = new Array[nullable MClass]
+ var supers = new HashSet[MClass]
+ supers.add_all(colorer.super_elements(mclasse, mclasses))
+ supers.add(mclasse)
+ for sup in supers do
+ var color = colors[sup]
+ if table.length <= color then
+ for i in [table.length .. color[ do
+ table[i] = null
+ end
+ end
+ table[color] = sup
+ end
+ tables[mclasse] = table
+ end
+ return tables
+ end
+
+ # Build type tables
+ fun hash_class_typing_tables(mtypes: Set[MClass], ids: Map[MClass, Int], masks: Map[MClass, Int], colorer: ClassPerfectHashing): Map[MClass, Array[nullable MClass]] do
+ var tables = new HashMap[MClass, Array[nullable MClass]]
+
+ for mtype in mtypes do
+ var table = new Array[nullable MClass]
+ var supers = new HashSet[MClass]
+ supers.add_all(colorer.super_elements(mtype, mtypes))
+ supers.add(mtype)
+
+ for sup in supers do
+ var color = colorer.phash(ids[sup], masks[mtype])
+ if table.length <= color then
+ for i in [table.length .. color[ do
+ table[i] = null
+ end
+ end
+ table[color] = sup
+ end
+ tables[mtype] = table
+ end
+ return tables
+ end
+
redef fun compile_header_structs do
self.header.add_decl("typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */")
self.compile_header_attribute_structs