45edb3d288e20cb94ab886791fde91b4882e5d12
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 #
303 redef class Nclass_header
304 redef fun accept_visitor
(v
)
306 v
.declaration_type
= "class_header"
307 v
.declaration_element
= "id"
310 # Exit class declaration
311 v
.declaration_type
= null
312 v
.declaration_element
= null
314 v
.java_class
.class_type
= v
.class_type
318 # Extends declaration in the class header
319 redef class Nextends_declaration
320 redef fun accept_visitor
(v
)
322 v
.declaration_element
= "extends"
324 v
.declaration_element
= null
328 # Implements declaration in the class header
329 redef class Nimplements_declaration
330 redef fun accept_visitor
(v
)
332 v
.declaration_element
= "implements"
334 v
.declaration_element
= null
339 # F I E L D D E C L A R A T I O N S #
342 # Method declaration in the field declarations
343 redef class Nmethod_declaration
344 redef fun accept_visitor
(v
)
346 v
.declaration_type
= "method"
348 v
.declaration_type
= null
350 if v
.method_return_type
.has_unresolved_types
then v
.method_return_type
.resolve_types
(v
.generic_map
)
351 v
.java_class
.add_method
(v
.method_id
, v
.method_return_type
, v
.method_params
)
353 v
.method_params
.clear
355 v
.method_return_type
= new JavaType(v
.converter
)
359 # Constructor declaration in the field declarations
360 redef class Nconstructor_declaration
361 redef fun accept_visitor
(v
)
363 v
.declaration_type
= "constructor"
365 v
.declaration_type
= null
369 # Variable declaration in the field declarations
370 redef class Nvariable_declaration
371 redef fun accept_visitor
(v
)
373 v
.declaration_type
= "variable"
375 v
.declaration_type
= null
377 v
.java_class
.attributes
[v
.variable_id
] = v
.variable_type
380 v
.variable_type
= new JavaType(v
.converter
)
384 # Static declaration in the field declarations
385 redef class Nstatic_declaration
386 redef fun accept_visitor
(v
)
388 v
.declaration_type
= "static"
390 v
.declaration_type
= null
394 # Identifier of the field
395 redef class Nvariable_id
396 redef fun accept_visitor
(v
)
398 v
.declaration_element
= "id"
400 v
.declaration_element
= null
404 # Identifier of the method
405 redef class Nmethod_id
406 redef fun accept_visitor
(v
)
408 v
.declaration_element
= "id"
410 v
.declaration_element
= null
415 redef fun accept_visitor
(v
)
417 if v
.declaration_type
== "variable" and v
.declaration_element
!= "id" then
418 v
.declaration_element
= "type"
421 if v
.declaration_type
== "method" and v
.declaration_element
== null then
422 # Makes sure it is not the generic return type definition
423 if not (v
.method_return_type
.identifier
.is_empty
and v
.is_generic_param
) then
424 v
.declaration_element
= "return_type"
430 if v
.declaration_element
== "variable" then
431 v
.declaration_element
= null
436 redef class Ngeneric_param
437 redef fun accept_visitor
(v
)
439 # Ignore the weird generic return type declaration
440 if v
.declaration_type
== "method" then
441 if v
.declaration_element
== null then
442 v
.is_generic_param
= true
444 v
.is_generic_param
= true
445 v
.gen_params_index
= 0
447 if v
.declaration_element
== "return_type" then
448 v
.method_return_type
.generic_params
= new Array[JavaType]
449 else if v
.declaration_element
== "parameter_list" then
450 v
.method_params
[v
.param_index
].generic_params
= new Array[JavaType]
453 else if v
.declaration_type
== "variable" then
454 if v
.declaration_element
== "type" then
455 v
.is_generic_param
= true
456 v
.gen_params_index
= 0
457 v
.variable_type
.generic_params
= new Array[JavaType]
463 v
.declaration_element
= null
464 v
.is_generic_param
= false
468 redef class Ngeneric_identifier
469 redef fun accept_visitor
(v
)
471 if v
.declaration_type
== "method" then
472 if v
.declaration_element
== null then
473 v
.is_generic_id
= true
479 v
.is_generic_id
= false
484 redef class Nparameter_list
485 redef fun accept_visitor
(v
)
487 v
.declaration_element
= "parameter_list"
490 v
.declaration_element
= null
495 redef class Nparameter
496 redef fun accept_visitor
(v
)
498 if v
.declaration_type
== "method" then
499 if v
.declaration_element
== "parameter_list" then
500 if v
.is_generic_param
then
501 v
.method_params
[v
.param_index
].generic_params
.add
(new JavaType(v
.converter
))
505 v
.gen_params_index
+= 1
507 v
.method_params
.add
(new JavaType(v
.converter
))
513 else if v
.declaration_element
== "return_type" and v
.is_generic_param
then
515 v
.method_return_type
.generic_params
.add
(new JavaType(v
.converter
))
519 v
.gen_params_index
+= 1
521 # For generic return type definition
522 else if v
.declaration_element
== null then
525 else if v
.declaration_type
== "variable" then
526 if v
.declaration_element
== "type" and v
.is_generic_param
then
527 v
.variable_type
.generic_params
.add
(new JavaType(v
.converter
))
531 v
.gen_params_index
+= 1