compiler: handle multi-iterators
[nit.git] / contrib / jwrapper / src / model.nit
index c9d0af6..74f90b5 100644 (file)
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-# Contains the java and nit type representation used to convert java to nit code
-module model
+# Model of the parsed Java classes and their corresponding Nit types
+module model is serialize
 
 import more_collections
 import opts
+import poset
+import binary::serialization
 
 import jtype_converter
 
@@ -40,6 +42,16 @@ class JavaType
        # Is this type a vararg?
        var is_vararg = false is writable
 
+       # Is this type based on an anonymous class?
+       var is_anonymous: Bool is lazy do
+               for id in identifier do
+                       for part in id.split("$") do
+                               if part.chars.first.is_digit then return true
+                       end
+               end
+               return false
+       end
+
        # Has some generic type to be resolved (T extends foo => T is resolved to foo)
        var has_unresolved_types = false
 
@@ -117,18 +129,6 @@ class JavaType
                return id
        end
 
-       fun resolve_types(conversion_map: HashMap[String, Array[String]])
-       do
-               if identifier.length == 1 then
-                       var resolved_id = conversion_map.get_or_null(self.id)
-                       if resolved_id != null then self.identifier = new Array[String].from(resolved_id)
-               end
-
-               if self.has_generic_params then
-                       for params in generic_params do params.resolve_types(conversion_map)
-               end
-       end
-
        # Get a copy of `self`
        redef fun clone
        do
@@ -144,7 +144,7 @@ class JavaType
        # Comparison based on fully qualified named
        redef fun ==(other) do return other isa JavaType and
                self.package_name == other.package_name and
-               self.is_primitive_array == other.is_primitive_array
+               self.array_dimension == other.array_dimension
 
        redef fun hash do return self.package_name.hash
 end
@@ -173,21 +173,123 @@ class JavaClass
        # Methods of this class organized by their name
        var methods = new MultiHashMap[String, JavaMethod]
 
+       # Methods signatures introduced by this class
+       var local_intro_methods = new MultiHashMap[String, JavaMethod]
+
        # Constructors of this class
        var constructors = new Array[JavaConstructor]
 
        # Importations from this class
        var imports = new HashSet[NitModule]
 
+       # Interfaces implemented by this class
+       var implements = new HashSet[JavaType]
+
+       # Super classes of this class
+       var extends = new HashSet[JavaType]
+
+       # Position of self in `model.class_hierarchy`
+       var in_hierarchy: nullable POSetElement[JavaClass] = null is noserialize
+
        redef fun to_s do return class_type.to_s
+
+       # Resolve the types in `other` in the context of this class
+       private fun resolve_types_of(other: JavaClass)
+       do
+               # Methods
+               for mid, method in other.methods do
+                       for signature in method do
+                               self.resolve(signature.return_type, signature.generic_params)
+                               for param in signature.params do self.resolve(param, signature.generic_params)
+                       end
+               end
+
+               # Constructors
+               for signature in other.constructors do
+                       for param in signature.params do self.resolve(param, signature.generic_params)
+               end
+
+               # Attributes
+               for aid, attribute in other.attributes do
+                       self.resolve attribute.java_type
+               end
+       end
+
+       # Resolve `java_type` in the context of this class
+       #
+       # Replace, in place, parameter types by their bound.
+       private fun resolve(java_type: JavaType, property_generic_params: nullable Array[JavaType])
+       do
+               # Skip types with a full package name
+               if java_type.identifier.length != 1 then return
+
+               # Skip primitive types
+               if converter.type_map.keys.has(java_type.id) then return
+
+               # Gather the generic parameters of the method, then the class
+               var params = new Array[JavaType]
+               if property_generic_params != null then params.add_all property_generic_params
+               var class_generic_params = class_type.generic_params
+               if class_generic_params != null then params.add_all class_generic_params
+
+               # Skip if there is not parameters usable to resolve
+               if params.is_empty then return
+
+               for param in params do
+                       if param.identifier == java_type.identifier then
+                               # Found a marching parameter type
+                               # TODO use a more precise bound
+                               java_type.identifier = ["java", "lang", "Object"]
+                               return
+                       end
+               end
+       end
+
+       redef fun hash do return class_type.hash
+       redef fun ==(o) do return o isa JavaClass and o.class_type == class_type
 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
@@ -196,10 +298,10 @@ class JavaModel
        end
 
        # Unknown types, not already wrapped and not in this pass
-       private var unknown_types = new HashMap[JavaType, NitType]
+       var unknown_types = new HashMap[JavaType, NitType] is noserialize
 
        # Wrapped types, or classes analyzed in this pass
-       private var known_types = new HashMap[JavaType, NitType]
+       var known_types = new HashMap[JavaType, NitType] is noserialize
 
        # Get the `NitType` corresponding to the `JavaType`
        #
@@ -245,6 +347,94 @@ class JavaModel
                unknown_types[jtype] = nit_type
                return nit_type
        end
+
+       # Resolve the types in methods and attributes of this class
+       fun resolve_types
+       do
+               for id, java_class in classes do
+                       java_class.resolve_types_of java_class
+
+                       # Ask nester classes for resolve too
+                       var matches = id.search_all("$")
+                       for match in matches do
+                               var nester_name = id.substring(0, match.from)
+                               if classes.keys.has(nester_name) then
+                                       var nester = classes[nester_name]
+                                       nester.resolve_types_of java_class
+                               end
+                       end
+               end
+       end
+
+       # Specialization hierarchy of `classes`
+       var class_hierarchy = new POSet[JavaClass] is noserialize
+
+       # Fill `class_hierarchy`
+       fun build_class_hierarchy
+       do
+               var object_type = new JavaType
+               object_type.identifier = ["java","lang","Object"]
+
+               # Fill POSet
+               for name, java_class in all_classes do
+                       # Skip anonymous classes
+                       if java_class.class_type.is_anonymous then continue
+
+                       java_class.in_hierarchy = class_hierarchy.add_node(java_class)
+
+                       # Collect explicit super classes
+                       var super_classes = new Array[JavaType]
+                       super_classes.add_all java_class.implements
+                       super_classes.add_all java_class.extends
+
+                       # Remove unavailable super classes
+                       for super_type in super_classes.reverse_iterator do
+                               # Is the super class available?
+                               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 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 all_classes.keys.has(super_type.package_name) then continue
+
+                               var super_class = all_classes[super_type.package_name]
+                               class_hierarchy.add_edge(java_class, super_class)
+                       end
+               end
+
+               # Flatten classes from the top one (Object like)
+               var linearized = class_hierarchy.linearize(class_hierarchy)
+
+               # Methods intro
+               for java_class in linearized do
+                       var greaters = java_class.in_hierarchy.greaters
+
+                       for mid, signatures in java_class.methods do
+                               for signature in signatures do
+                                       if signature.is_static then continue
+
+                                       # Check if this signature exists in a parent
+                                       for parent in greaters do
+                                               if parent == java_class then continue
+
+                                               if not parent.methods.keys.has(mid) then continue
+
+                                               if parent.methods[mid].has(signature) then continue label
+                                       end
+
+                                       # This is an introduction! register it
+                                       java_class.local_intro_methods[mid].add signature
+
+                               end label
+                       end
+               end
+       end
 end
 
 # A property to a Java class
@@ -263,6 +453,12 @@ class JavaMethod
 
        # Type of the arguments of the method
        var params: Array[JavaType]
+
+       # Generic parameters of this method
+       var generic_params: Array[JavaType]
+
+       redef fun ==(o) do return o isa JavaMethod and o.is_static == is_static and o.params == params
+       redef fun hash do return params.hash
 end
 
 # An attribute in a Java class
@@ -277,6 +473,9 @@ end
 class JavaConstructor
        # Type of the parameters of this constructor
        var params: Array[JavaType]
+
+       # Generic parameters of this constructor
+       var generic_params: Array[JavaType]
 end
 
 # A Nit module, use to import the referenced extern classes
@@ -363,9 +562,12 @@ 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 class Text
+redef abstract class Text
        # Get a copy of `self` where the first letter is capitalized
        fun simple_capitalized: String
        do