rename `NativeString` to `CString`
[nit.git] / contrib / jwrapper / src / model.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 # Copyright 2015 Alexis Laferrière <alexis.laf@xymus.net>
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17
18 # Model of the parsed Java classes and their corresponding Nit types
19 module model is serialize
20
21 import more_collections
22 import opts
23 import poset
24 import binary::serialization
25
26 import jtype_converter
27
28 class JavaType
29 super Cloneable
30
31 # Identifiers composing the namespace and class name
32 #
33 # An array of all the names that would be separated by `.`.
34 # Each name may contain `$`.
35 var identifier = new Array[String]
36
37 var generic_params: nullable Array[JavaType] = null
38
39 # Is this a void return type?
40 var is_void = false
41
42 # Is this type a vararg?
43 var is_vararg = false is writable
44
45 # Is this type based on an anonymous class?
46 var is_anonymous: Bool is lazy do
47 for id in identifier do
48 for part in id.split("$") do
49 if part.chars.first.is_digit then return true
50 end
51 end
52 return false
53 end
54
55 # Has some generic type to be resolved (T extends foo => T is resolved to foo)
56 var has_unresolved_types = false
57
58 # Dimension of primitive array: `int[][]` is 2d
59 var array_dimension = 0
60
61 fun is_primitive_array: Bool do return array_dimension > 0
62
63 fun has_generic_params: Bool do return not generic_params == null
64
65 fun return_cast: String do return converter.cast_as_return(self.id)
66
67 fun param_cast: String
68 do
69 if self.has_generic_params then
70 return converter.cast_as_param(self.generic_params[0].id)
71 end
72
73 return converter.cast_as_param(self.id)
74 end
75
76 # Name to give an extern class wrapping this type
77 fun extern_name: String
78 do
79 var name
80 var prefix = extern_class_prefix
81 if prefix == null then
82 # Use the namespace, e.g. java.lang.String -> Java_lang_String
83 assert not identifier.is_empty
84 if identifier.length == 1 then
85 name = identifier.last
86 else
87 var first = identifier.first
88 var last = identifier.last
89 var mid = identifier.subarray(1, identifier.length-2)
90 name = first.simple_capitalized + "_"
91 if mid.not_empty then name += mid.join("_") + "_"
92 name += last
93 end
94 else
95 # Use the prefix and the short class name
96 # e.g. given the prefix Native: java.lang.String -> CString
97 name = prefix + id
98 end
99
100 if is_primitive_array then
101 name += "_" + "Array" * array_dimension
102 end
103
104 name = name.replace("-", "_")
105 name = name.replace("$", "_")
106 return name
107 end
108
109 # Short name of the class, mangled to remove `$` (e.g. `Set`)
110 var id: String is lazy do return identifier.last.replace("$", "")
111
112 # Full name of this class as used in java code (e.g. `java.lang.Set`)
113 var java_full_name: String is lazy do return identifier.join(".").replace("$", ".")
114
115 # Full name of this class as used by jni (e.g. `android.graphics.BitmapFactory$Options`)
116 var jni_full_name: String is lazy do return identifier.join(".")
117
118 # Name of this class for the extern declaration in Nit (e.g. `java.lang.Set[]`)
119 var extern_equivalent: String is lazy do return jni_full_name + "[]" * array_dimension
120
121 # Full name of this class with arrays and generic values (e.g. `java.lang.Set<E>[]`)
122 redef fun to_s do
123 var id = self.java_full_name
124
125 if self.is_primitive_array then
126 id += "[]" * array_dimension
127 else if self.has_generic_params then
128 var params = [for param in generic_params do param.to_s]
129 id += "<{params.join(", ")}>"
130 end
131
132 return id
133 end
134
135 # Get a copy of `self`
136 redef fun clone
137 do
138 var jtype = new JavaType
139 jtype.identifier = identifier
140 jtype.generic_params = generic_params
141 jtype.is_void = is_void
142 jtype.is_vararg = is_vararg
143 jtype.array_dimension = array_dimension
144 return jtype
145 end
146
147 # Comparison based on fully qualified named
148 redef fun ==(other) do return other isa JavaType and
149 self.java_full_name == other.java_full_name and
150 self.array_dimension == other.array_dimension
151
152 redef fun hash do return self.java_full_name.hash
153 end
154
155 class NitType
156 # Nit class name
157 var identifier: String
158
159 # If this NitType was found in `lib/android`, contains the module name to import
160 var mod: nullable NitModuleRef
161
162 # Is this type known, wrapped and available in Nit?
163 var is_known: Bool = true
164
165 redef fun to_s do return identifier
166 end
167
168 # Model of a single Java class
169 class JavaClass
170 # Type of this class
171 var class_type: JavaType
172
173 # Attributes of this class
174 var attributes = new HashMap[String, JavaAttribute]
175
176 # Methods of this class organized by their name
177 var methods = new MultiHashMap[String, JavaMethod]
178
179 # Methods signatures introduced by this class
180 var local_intro_methods = new MultiHashMap[String, JavaMethod]
181
182 # Constructors of this class
183 var constructors = new Array[JavaConstructor]
184
185 # Importations from this class
186 var imports = new HashSet[NitModuleRef]
187
188 # Interfaces implemented by this class
189 var implements = new HashSet[JavaType]
190
191 # Super classes of this class
192 var extends = new HashSet[JavaType]
193
194 # Position of self in `model.class_hierarchy`
195 var in_hierarchy: nullable POSetElement[JavaClass] = null is noserialize
196
197 redef fun to_s do return class_type.to_s
198
199 # Resolve the types in `other` in the context of this class
200 private fun resolve_types_of(other: JavaClass)
201 do
202 # Methods
203 for mid, method in other.methods do
204 for signature in method do
205 self.resolve(signature.return_type, signature.generic_params)
206 for param in signature.params do self.resolve(param, signature.generic_params)
207 end
208 end
209
210 # Constructors
211 for signature in other.constructors do
212 for param in signature.params do self.resolve(param, signature.generic_params)
213 end
214
215 # Attributes
216 for aid, attribute in other.attributes do
217 self.resolve attribute.java_type
218 end
219 end
220
221 # Resolve `java_type` in the context of this class
222 #
223 # Replace, in place, parameter types by their bound.
224 private fun resolve(java_type: JavaType, property_generic_params: nullable Array[JavaType])
225 do
226 # Skip types with a full package name
227 if java_type.identifier.length != 1 then return
228
229 # Skip primitive types
230 if converter.type_map.keys.has(java_type.id) then return
231
232 # Gather the generic parameters of the method, then the class
233 var params = new Array[JavaType]
234 if property_generic_params != null then params.add_all property_generic_params
235 var class_generic_params = class_type.generic_params
236 if class_generic_params != null then params.add_all class_generic_params
237
238 # Skip if there is not parameters usable to resolve
239 if params.is_empty then return
240
241 for param in params do
242 if param.identifier == java_type.identifier then
243 # Found a marching parameter type
244 # TODO use a more precise bound
245 java_type.identifier = ["java", "lang", "Object"]
246 return
247 end
248 end
249 end
250
251 redef fun hash do return class_type.hash
252 redef fun ==(o) do return o isa JavaClass and o.class_type == class_type
253 end
254
255 # Model of all the Java class analyzed in one run
256 class JavaModel
257
258 # Classes analyzed in this pass
259 var classes = new HashMap[String, JavaClass]
260
261 # All classes, from this pass and from other passes
262 var all_classes: HashMap[String, JavaClass] is noserialize, lazy do
263 var classes = new HashMap[String, JavaClass]
264 classes.add_all self.classes
265
266 for model_path in sys.opt_load_models.value do
267 if not model_path.file_exists then
268 print_error "Error: model file '{model_path}' does not exist"
269 continue
270 end
271
272 var file = model_path.to_path.open_ro
273 var d = new BinaryDeserializer(file)
274 var model = d.deserialize
275 file.close
276
277 if d.errors.not_empty then
278 print_error "Error: failed to deserialize model file '{model_path}' with: {d.errors.join(", ")}"
279 continue
280 end
281
282 if not model isa JavaModel then
283 print_error "Error: model file contained a '{if model == null then "null" else model.class_name}'"
284 continue
285 end
286
287 classes.add_all model.classes
288 end
289
290 return classes
291 end
292
293 # Does this model have access to the `java.lang.Object`?
294 var knows_the_object_class: Bool = all_classes.keys.has("java.lang.Object") is lazy
295
296 # Add a class in `classes`
297 fun add_class(jclass: JavaClass)
298 do
299 var key = jclass.class_type.java_full_name
300 classes[key] = jclass
301 end
302
303 # Unknown types, not already wrapped and not in this pass
304 var unknown_types = new HashMap[JavaType, NitType] is noserialize
305
306 # Wrapped types, or classes analyzed in this pass
307 var known_types = new HashMap[JavaType, NitType] is noserialize
308
309 # Get the `NitType` corresponding to the `JavaType`
310 #
311 # Also registers types so they can be reused and
312 # to keep track of unknown types.
313 fun java_to_nit_type(jtype: JavaType): NitType
314 do
315 # Check cache
316 if known_types.keys.has(jtype) then return known_types[jtype]
317 if unknown_types.keys.has(jtype) then return unknown_types[jtype]
318
319 # Is it a compatible primitive type?
320 if not jtype.is_primitive_array then
321 var name = converter.to_nit_type(jtype.id)
322 if name != null then
323 # We got a Nit equivalent
324 var nit_type = new NitType(name)
325 known_types[jtype] = nit_type
326 return nit_type
327 end
328 end
329
330 # Is being wrapped in this pass?
331 var key = jtype.java_full_name
332 if classes.keys.has(key) then
333 if jtype.array_dimension <= opt_arrays.value then
334 var nit_type = new NitType(jtype.extern_name)
335 known_types[jtype] = nit_type
336 return nit_type
337 end
338 end
339
340 # Search in lib
341 var nit_type = find_extern_class[jtype.extern_equivalent]
342 if nit_type != null then
343 known_types[jtype] = nit_type
344 return nit_type
345 end
346
347 # Unknown type
348 nit_type = new NitType(jtype.extern_name)
349 nit_type.is_known = false
350 unknown_types[jtype] = nit_type
351 return nit_type
352 end
353
354 # Resolve the types in methods and attributes of this class
355 fun resolve_types
356 do
357 for id, java_class in classes do
358 java_class.resolve_types_of java_class
359
360 # Ask nester classes for resolve too
361 var matches = id.search_all("$")
362 for match in matches do
363 var nester_name = id.substring(0, match.from)
364 if classes.keys.has(nester_name) then
365 var nester = classes[nester_name]
366 nester.resolve_types_of java_class
367 end
368 end
369 end
370 end
371
372 # Specialization hierarchy of `classes`
373 var class_hierarchy = new POSet[JavaClass] is noserialize
374
375 # Fill `class_hierarchy`
376 fun build_class_hierarchy
377 do
378 var object_type = new JavaType
379 object_type.identifier = ["java","lang","Object"]
380
381 # Fill POSet
382 for name, java_class in all_classes do
383 # Skip anonymous classes
384 if java_class.class_type.is_anonymous then continue
385
386 java_class.in_hierarchy = class_hierarchy.add_node(java_class)
387
388 # Collect explicit super classes
389 var super_classes = new Array[JavaType]
390 super_classes.add_all java_class.implements
391 super_classes.add_all java_class.extends
392
393 # Remove unavailable super classes
394 for super_type in super_classes.reverse_iterator do
395 # Is the super class available?
396 if not all_classes.keys.has(super_type.java_full_name) then super_classes.remove(super_type)
397 end
398
399 # If the is no explicit supers, add `java.lang.Object` (if it is known)
400 if super_classes.is_empty and java_class.class_type != object_type and
401 knows_the_object_class then
402 super_classes.add object_type
403 end
404
405 for super_type in super_classes do
406 # Is the super class available?
407 if not all_classes.keys.has(super_type.java_full_name) then continue
408
409 var super_class = all_classes[super_type.java_full_name]
410 class_hierarchy.add_edge(java_class, super_class)
411 end
412 end
413
414 # Flatten classes from the top one (Object like)
415 var linearized = class_hierarchy.linearize(class_hierarchy)
416
417 # Methods intro
418 for java_class in linearized do
419 var greaters = java_class.in_hierarchy.greaters
420
421 for mid, signatures in java_class.methods do
422 for signature in signatures do
423 if signature.is_static then continue
424
425 # Check if this signature exists in a parent
426 for parent in greaters do
427 if parent == java_class then continue
428
429 if not parent.methods.keys.has(mid) then continue
430
431 if parent.methods[mid].has(signature) then continue label
432 end
433
434 # This is an introduction! register it
435 java_class.local_intro_methods[mid].add signature
436
437 end label
438 end
439 end
440 end
441 end
442
443 # A property to a Java class
444 abstract class JavaProperty
445
446 # Is this property marked static?
447 var is_static: Bool
448 end
449
450 # A Java method, with its signature
451 class JavaMethod
452 super JavaProperty
453
454 # Type returned by the method
455 var return_type: JavaType
456
457 # Type of the arguments of the method
458 var params: Array[JavaType]
459
460 # Generic parameters of this method
461 var generic_params: Array[JavaType]
462
463 redef fun ==(o) do return o isa JavaMethod and o.is_static == is_static and o.params == params
464 redef fun hash do return params.hash
465 end
466
467 # An attribute in a Java class
468 class JavaAttribute
469 super JavaProperty
470
471 # Type of the attribute
472 var java_type: JavaType
473 end
474
475 # A Java method, with its signature
476 class JavaConstructor
477 # Type of the parameters of this constructor
478 var params: Array[JavaType]
479
480 # Generic parameters of this constructor
481 var generic_params: Array[JavaType]
482 end
483
484 # A Nit module, use to import the referenced extern classes
485 class NitModuleRef
486 # Relative path to the module
487 var path: String
488
489 # Name of the module
490 var name: String is lazy do return path.basename(".nit")
491
492 redef fun to_s do return self.name
493 redef fun ==(other) do return other isa NitModuleRef and self.path == other.path
494 redef fun hash do return self.path.hash
495 end
496
497 redef class Sys
498 # Collection of Java classes already wrapped in the library
499 #
500 # * The key uses `JavaType.to_s`.
501 # * The value is the corresponding `NitType`.
502 var find_extern_class: DefaultMap[String, nullable NitType] is lazy do
503 var map = new DefaultMap[String, nullable NitType](null)
504 var modules = new HashMap[String, NitModuleRef]
505
506 var lib_paths = opt_libs.value
507 if lib_paths == null then lib_paths = new Array[String]
508
509 if lib_paths.has("auto") then
510 lib_paths.remove "auto"
511 var nit_dir = "NIT_DIR".environ
512 if nit_dir.is_empty then
513 # Simple heuristic to find the Nit lib
514 var dir = sys.program_name.dirname / "../../../lib/"
515 dir = dir.simplify_path
516 if dir.file_exists then lib_paths.add dir.simplify_path
517 end
518 end
519
520 if lib_paths.is_empty then return map
521
522 # Use grep to find all extern classes implemented in Java
523 var grep_regex = "extern class [a-zA-Z0-9_]\\\+[ ]\\\+in[ ]\\\+\"Java\""
524 var grep_args = ["-r", "--with-filename", grep_regex]
525 grep_args.add_all lib_paths
526
527 var grep = new ProcessReader("grep", grep_args...)
528 var lines = grep.read_lines
529 grep.close
530 grep.wait
531
532 # Sort out the modules, Nit class names and Java types
533 var regex = """(.+):\\s*extern +class +([a-zA-Z0-9_]+) *in *"Java" *`\\{(.+)`\\}""".to_re
534 for line in lines do
535 var matches = line.search_all(regex)
536 for match in matches do
537 var path = match[1].to_s
538 var nit_name = match[2].to_s
539 var java_name = match[3].to_s.trim
540
541 # Debug code
542 # print "+ Found {nit_name}: {java_name} at {path}"
543
544 var mod = modules.get_or_null(path)
545 if mod == null then
546 mod = new NitModuleRef(path)
547 modules[path] = mod
548 end
549
550 map[java_name] = new NitType(nit_name, mod)
551 end
552 end
553
554 return map
555 end
556
557 # Option to set `extern_class_prefix`
558 var opt_extern_class_prefix = new OptionString("Prefix to extern classes (By default uses the full namespace)", "-p")
559
560 # Prefix used to name extern classes, if `null` use the full namespace
561 var extern_class_prefix: nullable String is lazy do return opt_extern_class_prefix.value
562
563 # Libraries to search for existing wrappers
564 var opt_libs = new OptionArray("Paths to libraries with wrappers of Java classes ('auto' to use the full Nit lib)", "-i")
565
566 # Generate the primitive array version of each class up to the given depth
567 var opt_arrays = new OptionInt("Depth of the primitive array for each wrapped class (default: 1)", 1, "-a")
568
569 # Generate the primitive array version of each class up to the given depth
570 var opt_load_models = new OptionArray("Saved models to search for super-classes", "-m")
571 end
572
573 redef abstract class Text
574 # Get a copy of `self` where the first letter is capitalized
575 fun simple_capitalized: String
576 do
577 if is_empty then return to_s
578
579 var c = chars.first.to_upper
580 var s = c.to_s + substring_from(1)
581 return s
582 end
583 end