1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # Uses a visitor to extract data from the javap output AST
18 # It sends the data to `code_generator` module
21 import javap_test_parser
23 import jtype_converter
29 var converter
: JavaTypeConverter
31 var java_class
= new JavaClass
32 var declaration_type
: nullable String = null
33 var declaration_element
: nullable String = null
34 var class_type
: JavaType
37 var variable_type
: JavaType
39 var is_generic_param
= false
40 var is_generic_id
= false
42 var gen_params_index
= 0
44 # Used to resolve generic return types (T -> foo.faz.Bar)
45 var generic_map
= new HashMap[String, Array[String]]
47 var is_primitive_array
= false
50 var method_return_type
: JavaType
51 var method_params
= new Array[JavaType]
54 redef fun visit
(n
) do n
.accept_visitor
(self)
56 init(converter
: JavaTypeConverter)
58 self.converter
= converter
59 self.class_type
= new JavaType(self.converter
)
60 self.method_return_type
= new JavaType(self.converter
)
61 self.variable_type
= new JavaType(self.converter
)
67 fun accept_visitor
(v
: JavaVisitor) do visit_children
(v
)
70 redef class Nidentifier
71 redef fun accept_visitor
(v
)
73 if v
.declaration_type
== "class_header" then
75 if v
.declaration_element
== "id" then
76 v
.class_type
.identifier
.add
(self.text
)
79 else if v
.declaration_type
== "variable" then
81 if v
.declaration_element
== "id" then
82 v
.variable_id
+= self.text
83 else if v
.declaration_element
== "type" then
84 if v
.is_generic_param
then
85 v
.variable_type
.generic_params
[v
.gen_params_index
].identifier
.add
(self.text
)
87 v
.variable_type
.identifier
.add
(self.text
)
91 else if v
.declaration_type
== "method" then
93 if v
.declaration_element
== "id" then
94 v
.method_id
= self.text
95 else if v
.declaration_element
== "return_type" then
96 if self.text
== "void" then
97 v
.method_return_type
.is_void
= true
98 else if v
.is_generic_param
then
99 v
.method_return_type
.generic_params
[v
.gen_params_index
].identifier
.add
(self.text
)
101 v
.method_return_type
.identifier
.add
(self.text
)
103 else if v
.declaration_element
== "parameter_list" then
104 if v
.is_generic_param
then
105 v
.method_params
[v
.param_index
].generic_params
[v
.gen_params_index
].identifier
.add
(self.text
)
107 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
110 # Creates a map to resolve generic return types
111 # Exemple : public **<T extends android/os/Bundle>** T foo();
112 else if v
.is_generic_param
then
113 if v
.is_generic_id
then
114 v
.generic_id
= self.text
115 v
.generic_map
[self.text
] = new Array[String]
117 if not v
.method_return_type
.has_unresolved_types
then v
.method_return_type
.has_unresolved_types
= true
119 v
.generic_map
[v
.generic_id
].add
(self.text
)
129 # Primitive array node
130 redef class N_39d_91d_93d_39d
131 redef fun accept_visitor
(v
)
133 if v
.declaration_type
== "variable" then
134 if v
.declaration_element
== "type" then
135 if v
.is_generic_param
then
136 v
.variable_type
.generic_params
[v
.gen_params_index
].array_dimension
+= 1
138 v
.variable_type
.array_dimension
+= 1
142 else if v
.declaration_type
== "method" then
144 if v
.declaration_element
== "return_type" then
145 if v
.is_generic_param
then
146 v
.method_return_type
.generic_params
[v
.gen_params_index
].array_dimension
+= 1
148 v
.method_return_type
.array_dimension
+= 1
150 else if v
.declaration_element
== "parameter_list" then
151 if v
.is_generic_param
then
152 v
.method_params
[v
.param_index
].generic_params
[v
.gen_params_index
].array_dimension
+= 1
154 v
.method_params
[v
.param_index
].array_dimension
+= 1
164 redef class N_39dchar_39d
165 redef fun accept_visitor
(v
)
167 if v
.declaration_type
== "variable" then
168 if v
.declaration_element
== "type" then
169 v
.variable_type
.identifier
.add
(self.text
)
171 else if v
.declaration_type
== "method" then
172 if v
.declaration_element
== "return_type" then
173 v
.method_return_type
.identifier
.add
(self.text
)
174 else if v
.declaration_element
== "parameter_list" then
175 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
181 redef class N_39dboolean_39d
182 redef fun accept_visitor
(v
)
184 if v
.declaration_type
== "variable" then
185 if v
.declaration_element
== "type" then
186 v
.variable_type
.identifier
.add
(self.text
)
188 else if v
.declaration_type
== "method" then
189 if v
.declaration_element
== "return_type" then
190 v
.method_return_type
.identifier
.add
(self.text
)
191 else if v
.declaration_element
== "parameter_list" then
192 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
198 redef class N_39dfloat_39d
199 redef fun accept_visitor
(v
)
201 if v
.declaration_type
== "variable" then
202 if v
.declaration_element
== "type" then
203 v
.variable_type
.identifier
.add
(self.text
)
205 else if v
.declaration_type
== "method" then
206 if v
.declaration_element
== "return_type" then
207 v
.method_return_type
.identifier
.add
(self.text
)
208 else if v
.declaration_element
== "parameter_list" then
209 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
215 redef class N_39ddouble_39d
216 redef fun accept_visitor
(v
)
218 if v
.declaration_type
== "variable" then
219 if v
.declaration_element
== "type" then
220 v
.variable_type
.identifier
.add
(self.text
)
222 else if v
.declaration_type
== "method" then
223 if v
.declaration_element
== "return_type" then
224 v
.method_return_type
.identifier
.add
(self.text
)
225 else if v
.declaration_element
== "parameter_list" then
226 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
232 redef class N_39dbyte_39d
233 redef fun accept_visitor
(v
)
235 if v
.declaration_type
== "variable" then
236 if v
.declaration_element
== "type" then
237 v
.variable_type
.identifier
.add
(self.text
)
239 else if v
.declaration_type
== "method" then
240 if v
.declaration_element
== "return_type" then
241 v
.method_return_type
.identifier
.add
(self.text
)
242 else if v
.declaration_element
== "parameter_list" then
243 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
249 redef class N_39dshort_39d
250 redef fun accept_visitor
(v
)
252 if v
.declaration_type
== "variable" then
253 if v
.declaration_element
== "type" then
254 v
.variable_type
.identifier
.add
(self.text
)
256 else if v
.declaration_type
== "method" then
257 if v
.declaration_element
== "return_type" then
258 v
.method_return_type
.identifier
.add
(self.text
)
259 else if v
.declaration_element
== "parameter_list" then
260 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
266 redef class N_39dint_39d
267 redef fun accept_visitor
(v
)
269 if v
.declaration_type
== "variable" then
270 if v
.declaration_element
== "type" then
271 v
.variable_type
.identifier
.add
(self.text
)
273 else if v
.declaration_type
== "method" then
274 if v
.declaration_element
== "return_type" then
275 v
.method_return_type
.identifier
.add
(self.text
)
276 else if v
.declaration_element
== "parameter_list" then
277 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
283 redef class N_39dlong_39d
284 redef fun accept_visitor
(v
)
286 if v
.declaration_type
== "variable" then
287 if v
.declaration_element
== "type" then
288 v
.variable_type
.identifier
.add
(self.text
)
290 else if v
.declaration_type
== "method" then
291 if v
.declaration_element
== "return_type" then
292 v
.method_return_type
.identifier
.add
(self.text
)
293 else if v
.declaration_element
== "parameter_list" then
294 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
301 # C L A S S H E A D E R #
304 redef class Nclass_header
305 redef fun accept_visitor
(v
)
307 v
.declaration_type
= "class_header"
308 v
.declaration_element
= "id"
311 # Exit class declaration
312 v
.declaration_type
= null
313 v
.declaration_element
= null
315 v
.java_class
.class_type
= v
.class_type
319 # Extends declaration in the class header
320 redef class Nextends_declaration
321 redef fun accept_visitor
(v
)
323 v
.declaration_element
= "extends"
325 v
.declaration_element
= null
329 # Implements declaration in the class header
330 redef class Nimplements_declaration
331 redef fun accept_visitor
(v
)
333 v
.declaration_element
= "implements"
335 v
.declaration_element
= null
340 # P R O P E R T Y D E C L A R A T I O N S #
344 redef class Nmethod_declaration
345 redef fun accept_visitor
(v
)
347 v
.declaration_type
= "method"
349 v
.declaration_type
= null
351 if v
.method_return_type
.has_unresolved_types
then v
.method_return_type
.resolve_types
(v
.generic_map
)
352 v
.java_class
.add_method
(v
.method_id
, v
.method_return_type
, v
.method_params
)
354 v
.method_params
.clear
356 v
.method_return_type
= new JavaType(v
.converter
)
360 # Constructor declaration
361 redef class Nconstructor_declaration
362 redef fun accept_visitor
(v
)
364 v
.declaration_type
= "constructor"
366 v
.declaration_type
= null
370 # Variable property declaration
371 redef class Nvariable_declaration
372 redef fun accept_visitor
(v
)
374 v
.declaration_type
= "variable"
376 v
.declaration_type
= null
378 v
.java_class
.attributes
[v
.variable_id
] = v
.variable_type
381 v
.variable_type
= new JavaType(v
.converter
)
385 # Static property declaration
386 redef class Nstatic_declaration
387 redef fun accept_visitor
(v
)
389 v
.declaration_type
= "static"
391 v
.declaration_type
= null
395 # Identifier of a variable
396 redef class Nvariable_id
397 redef fun accept_visitor
(v
)
399 v
.declaration_element
= "id"
401 v
.declaration_element
= null
405 # Identifier of the method
406 redef class Nmethod_id
407 redef fun accept_visitor
(v
)
409 v
.declaration_element
= "id"
411 v
.declaration_element
= null
416 redef fun accept_visitor
(v
)
418 if v
.declaration_type
== "variable" and v
.declaration_element
!= "id" then
419 v
.declaration_element
= "type"
422 if v
.declaration_type
== "method" and v
.declaration_element
== null then
423 # Makes sure it is not the generic return type definition
424 if not (v
.method_return_type
.identifier
.is_empty
and v
.is_generic_param
) then
425 v
.declaration_element
= "return_type"
431 if v
.declaration_element
== "variable" then
432 v
.declaration_element
= null
437 redef class Ngeneric_param
438 redef fun accept_visitor
(v
)
440 # Ignore the weird generic return type declaration
441 if v
.declaration_type
== "method" then
442 if v
.declaration_element
== null then
443 v
.is_generic_param
= true
445 v
.is_generic_param
= true
446 v
.gen_params_index
= 0
448 if v
.declaration_element
== "return_type" then
449 v
.method_return_type
.generic_params
= new Array[JavaType]
450 else if v
.declaration_element
== "parameter_list" then
451 v
.method_params
[v
.param_index
].generic_params
= new Array[JavaType]
454 else if v
.declaration_type
== "variable" then
455 if v
.declaration_element
== "type" then
456 v
.is_generic_param
= true
457 v
.gen_params_index
= 0
458 v
.variable_type
.generic_params
= new Array[JavaType]
464 v
.declaration_element
= null
465 v
.is_generic_param
= false
469 redef class Ngeneric_identifier
470 redef fun accept_visitor
(v
)
472 if v
.declaration_type
== "method" then
473 if v
.declaration_element
== null then
474 v
.is_generic_id
= true
480 v
.is_generic_id
= false
485 redef class Nparameter_list
486 redef fun accept_visitor
(v
)
488 v
.declaration_element
= "parameter_list"
491 v
.declaration_element
= null
496 redef class Nparameter
497 redef fun accept_visitor
(v
)
499 if v
.declaration_type
== "method" then
500 if v
.declaration_element
== "parameter_list" then
501 if v
.is_generic_param
then
502 v
.method_params
[v
.param_index
].generic_params
.add
(new JavaType(v
.converter
))
506 v
.gen_params_index
+= 1
508 v
.method_params
.add
(new JavaType(v
.converter
))
514 else if v
.declaration_element
== "return_type" and v
.is_generic_param
then
516 v
.method_return_type
.generic_params
.add
(new JavaType(v
.converter
))
520 v
.gen_params_index
+= 1
522 # For generic return type definition
523 else if v
.declaration_element
== null then
526 else if v
.declaration_type
== "variable" then
527 if v
.declaration_element
== "type" and v
.is_generic_param
then
528 v
.variable_type
.generic_params
.add
(new JavaType(v
.converter
))
532 v
.gen_params_index
+= 1