compiler: handle multi-iterators
[nit.git] / contrib / jwrapper / src / model.nit
index 95b54c5..74f90b5 100644 (file)
@@ -252,9 +252,44 @@ end
 # Model of all the Java class analyzed in one run
 class JavaModel
 
-       # All analyzed classes
+       # Classes analyzed in this pass
        var classes = new HashMap[String, JavaClass]
 
+       # All classes, from this pass and from other passes
+       var all_classes: HashMap[String, JavaClass] is noserialize, lazy do
+               var classes = new HashMap[String, JavaClass]
+               classes.recover_with self.classes
+
+               for model_path in sys.opt_load_models.value do
+                       if not model_path.file_exists then
+                               print_error "Error: model file '{model_path}' does not exist"
+                               continue
+                       end
+
+                       var file = model_path.to_path.open_ro
+                       var d = new BinaryDeserializer(file)
+                       var model = d.deserialize
+                       file.close
+
+                       if d.errors.not_empty then
+                               print_error "Error: failed to deserialize model file '{model_path}' with: {d.errors.join(", ")}"
+                               continue
+                       end
+
+                       if not model isa JavaModel then
+                               print_error "Error: model file contained a '{if model == null then "null" else model.class_name}'"
+                               continue
+                       end
+
+                       classes.recover_with model.classes
+               end
+
+               return classes
+       end
+
+       # Does this model have access to the `java.lang.Object`?
+       var knows_the_object_class: Bool = all_classes.keys.has("java.lang.Object") is lazy
+
        # Add a class in `classes`
        fun add_class(jclass: JavaClass)
        do
@@ -341,7 +376,7 @@ class JavaModel
                object_type.identifier = ["java","lang","Object"]
 
                # Fill POSet
-               for name, java_class in classes do
+               for name, java_class in all_classes do
                        # Skip anonymous classes
                        if java_class.class_type.is_anonymous then continue
 
@@ -355,19 +390,20 @@ class JavaModel
                        # Remove unavailable super classes
                        for super_type in super_classes.reverse_iterator do
                                # Is the super class available?
-                               if not classes.keys.has(super_type.package_name) then super_classes.remove(super_type)
+                               if not all_classes.keys.has(super_type.package_name) then super_classes.remove(super_type)
                        end
 
-                       # If the is no explicit supers, add `java.lang.Object`
-                       if super_classes.is_empty and java_class.class_type != object_type then
+                       # If the is no explicit supers, add `java.lang.Object` (if it is known)
+                       if super_classes.is_empty and java_class.class_type != object_type and
+                          knows_the_object_class then
                                super_classes.add object_type
                        end
 
                        for super_type in super_classes do
                                # Is the super class available?
-                               if not classes.keys.has(super_type.package_name) then continue
+                               if not all_classes.keys.has(super_type.package_name) then continue
 
-                               var super_class = classes[super_type.package_name]
+                               var super_class = all_classes[super_type.package_name]
                                class_hierarchy.add_edge(java_class, super_class)
                        end
                end
@@ -377,8 +413,7 @@ class JavaModel
 
                # Methods intro
                for java_class in linearized do
-                       var in_hierarchy = class_hierarchy[java_class]
-                       var greaters = in_hierarchy.greaters
+                       var greaters = java_class.in_hierarchy.greaters
 
                        for mid, signatures in java_class.methods do
                                for signature in signatures do
@@ -527,6 +562,9 @@ redef class Sys
 
        # Generate the primitive array version of each class up to the given depth
        var opt_arrays = new OptionInt("Depth of the primitive array for each wrapped class (default: 1)", 1, "-a")
+
+       # Generate the primitive array version of each class up to the given depth
+       var opt_load_models = new OptionArray("Saved models to search for super-classes", "-m")
 end
 
 redef abstract class Text