1 # This file is part of NIT (http://www.nitlanguage.org).
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 # Copyright 2015 Alexis Laferrière <alexis.laf@xymus.net>
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
10 # http://www.apache.org/licenses/LICENSE-2.0
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.
18 # Uses a visitor to extract data from the javap output AST
19 # It sends the data to `code_generator` module
22 import javap_test_parser
24 import jtype_converter
27 # Visitor of the AST generated from javap output
31 var converter
: JavaTypeConverter
33 # Model of all the analyzed classes
36 var java_class
: JavaClass is noinit
38 var declaration_type
: nullable String = null
39 var declaration_element
: nullable String = null
40 var class_type
: JavaType is noinit
43 var variable_type
= new JavaType(self.converter
) is lazy
45 var is_generic_param
= false
46 var is_generic_id
= false
48 var gen_params_index
= 0
50 # Used to resolve generic return types (T -> foo.faz.Bar)
51 var generic_map
= new HashMap[String, Array[String]]
53 var is_primitive_array
= false
56 var method_return_type
= new JavaType(self.converter
) is lazy
57 var method_params
= new Array[JavaType]
60 redef fun visit
(n
) do n
.accept_visitor
(self)
62 # Add the identifier from `token` to the current context
63 fun add_identifier
(token
: NToken)
65 if declaration_type
== "variable" then
66 if declaration_element
== "type" then
67 variable_type
.identifier
.add
(token
.text
)
69 else if declaration_type
== "method" then
70 if declaration_element
== "return_type" then
71 method_return_type
.identifier
.add
(token
.text
)
72 else if declaration_element
== "parameter_list" then
73 method_params
[param_index
].identifier
.add
(token
.text
)
80 fun accept_visitor
(v
: JavaVisitor) do visit_children
(v
)
83 redef class Nidentifier
84 redef fun accept_visitor
(v
)
86 if v
.declaration_type
== "class_header" then
88 if v
.declaration_element
== "id" then
89 v
.class_type
.identifier
.add
(self.text
)
92 else if v
.declaration_type
== "variable" then
94 if v
.declaration_element
== "id" then
95 v
.variable_id
+= self.text
96 else if v
.declaration_element
== "type" then
97 if v
.is_generic_param
then
98 v
.variable_type
.generic_params
[v
.gen_params_index
].identifier
.add
(self.text
)
100 v
.variable_type
.identifier
.add
(self.text
)
104 else if v
.declaration_type
== "method" then
106 if v
.declaration_element
== "id" then
107 v
.method_id
= self.text
108 else if v
.declaration_element
== "return_type" then
109 if self.text
== "void" then
110 v
.method_return_type
.is_void
= true
111 else if v
.is_generic_param
then
112 v
.method_return_type
.generic_params
[v
.gen_params_index
].identifier
.add
(self.text
)
114 v
.method_return_type
.identifier
.add
(self.text
)
116 else if v
.declaration_element
== "parameter_list" then
117 if v
.is_generic_param
then
118 v
.method_params
[v
.param_index
].generic_params
[v
.gen_params_index
].identifier
.add
(self.text
)
120 v
.method_params
[v
.param_index
].identifier
.add
(self.text
)
123 # Creates a map to resolve generic return types
124 # Exemple : public **<T extends android/os/Bundle>** T foo();
125 else if v
.is_generic_param
then
126 if v
.is_generic_id
then
127 v
.generic_id
= self.text
128 v
.generic_map
[self.text
] = new Array[String]
130 if not v
.method_return_type
.has_unresolved_types
then v
.method_return_type
.has_unresolved_types
= true
132 v
.generic_map
[v
.generic_id
].add
(self.text
)
142 # Primitive array node
143 redef class Nbrackets
144 redef fun accept_visitor
(v
)
146 if v
.declaration_type
== "variable" then
147 if v
.declaration_element
== "type" then
148 if v
.is_generic_param
then
149 v
.variable_type
.generic_params
[v
.gen_params_index
].array_dimension
+= 1
151 v
.variable_type
.array_dimension
+= 1
155 else if v
.declaration_type
== "method" then
157 if v
.declaration_element
== "return_type" then
158 if v
.is_generic_param
then
159 v
.method_return_type
.generic_params
[v
.gen_params_index
].array_dimension
+= 1
161 v
.method_return_type
.array_dimension
+= 1
163 else if v
.declaration_element
== "parameter_list" then
164 if v
.is_generic_param
then
165 v
.method_params
[v
.param_index
].generic_params
[v
.gen_params_index
].array_dimension
+= 1
167 v
.method_params
[v
.param_index
].array_dimension
+= 1
177 redef class N_39dchar_39d
178 redef fun accept_visitor
(v
) do v
.add_identifier
self
181 redef class N_39dboolean_39d
182 redef fun accept_visitor
(v
) do v
.add_identifier
self
185 redef class N_39dfloat_39d
186 redef fun accept_visitor
(v
) do v
.add_identifier
self
189 redef class N_39ddouble_39d
190 redef fun accept_visitor
(v
) do v
.add_identifier
self
193 redef class N_39dbyte_39d
194 redef fun accept_visitor
(v
) do v
.add_identifier
self
197 redef class N_39dshort_39d
198 redef fun accept_visitor
(v
) do v
.add_identifier
self
201 redef class N_39dint_39d
202 redef fun accept_visitor
(v
) do v
.add_identifier
self
205 redef class N_39dlong_39d
206 redef fun accept_visitor
(v
) do v
.add_identifier
self
210 # C L A S S H E A D E R #
213 redef class Nclass_declaration
214 redef fun accept_visitor
(v
)
216 v
.java_class
= new JavaClass
217 v
.model
.classes
.add v
.java_class
218 v
.class_type
= new JavaType(v
.converter
)
220 v
.declaration_type
= "class_header"
221 v
.declaration_element
= "id"
224 # Exit class declaration
225 v
.declaration_type
= null
226 v
.declaration_element
= null
228 v
.java_class
.class_type
= v
.class_type
232 # Extends declaration in the class header
233 redef class Nextends_declaration
234 redef fun accept_visitor
(v
)
236 v
.declaration_element
= "extends"
238 v
.declaration_element
= null
242 # Implements declaration in the class header
243 redef class Nimplements_declaration
244 redef fun accept_visitor
(v
)
246 v
.declaration_element
= "implements"
248 v
.declaration_element
= null
253 # P R O P E R T Y D E C L A R A T I O N S #
257 redef class Nproperty_declaration_method
258 redef fun accept_visitor
(v
)
260 v
.declaration_type
= "method"
261 v
.declaration_element
= null
263 v
.declaration_type
= null
265 if v
.method_return_type
.has_unresolved_types
then v
.method_return_type
.resolve_types
(v
.generic_map
)
267 var method
= new JavaMethod(v
.method_return_type
, v
.method_params
.clone
)
268 v
.java_class
.methods
[v
.method_id
].add method
270 v
.method_params
.clear
272 v
.method_return_type
= new JavaType(v
.converter
)
276 # Constructor declaration
277 redef class Nproperty_declaration_constructor
278 redef fun accept_visitor
(v
)
280 v
.declaration_type
= "constructor"
282 v
.declaration_type
= null
286 # Variable property declaration
287 redef class Nproperty_declaration_attribute
288 redef fun accept_visitor
(v
)
290 v
.declaration_type
= "variable"
292 v
.declaration_type
= null
294 v
.java_class
.attributes
[v
.variable_id
] = v
.variable_type
297 v
.variable_type
= new JavaType(v
.converter
)
301 # Static property declaration
302 redef class Nproperty_declaration_static
303 redef fun accept_visitor
(v
)
305 v
.declaration_type
= "static"
307 v
.declaration_type
= null
311 # Identifier of a variable
312 redef class Nattribute_id
313 redef fun accept_visitor
(v
)
315 v
.declaration_element
= "id"
317 v
.declaration_element
= null
321 # Identifier of the method
322 redef class Nmethod_id
323 redef fun accept_visitor
(v
)
325 v
.declaration_element
= "id"
327 v
.declaration_element
= null
332 redef fun accept_visitor
(v
)
334 if v
.declaration_type
== "variable" and v
.declaration_element
!= "id" then
335 v
.declaration_element
= "type"
338 if v
.declaration_type
== "method" and v
.declaration_element
== null then
339 # Makes sure it is not the generic return type definition
340 if not (v
.method_return_type
.identifier
.is_empty
and v
.is_generic_param
) then
341 v
.declaration_element
= "return_type"
347 if v
.declaration_element
== "variable" then
348 v
.declaration_element
= null
353 redef class Ngeneric_param
354 redef fun accept_visitor
(v
)
356 # Ignore the weird generic return type declaration
357 if v
.declaration_type
== "method" then
358 if v
.declaration_element
== null then
359 v
.is_generic_param
= true
361 v
.is_generic_param
= true
362 v
.gen_params_index
= 0
364 if v
.declaration_element
== "return_type" then
365 v
.method_return_type
.generic_params
= new Array[JavaType]
366 else if v
.declaration_element
== "parameter_list" then
367 v
.method_params
[v
.param_index
].generic_params
= new Array[JavaType]
370 else if v
.declaration_type
== "variable" then
371 if v
.declaration_element
== "type" then
372 v
.is_generic_param
= true
373 v
.gen_params_index
= 0
374 v
.variable_type
.generic_params
= new Array[JavaType]
380 v
.declaration_element
= null
381 v
.is_generic_param
= false
385 redef class Ngeneric_identifier
386 redef fun accept_visitor
(v
)
388 if v
.declaration_type
== "method" then
389 if v
.declaration_element
== null then
390 v
.is_generic_id
= true
396 v
.is_generic_id
= false
401 redef class Nparameter_list
402 redef fun accept_visitor
(v
)
404 v
.declaration_element
= "parameter_list"
407 v
.declaration_element
= null
412 redef class Nparameter
413 redef fun accept_visitor
(v
)
415 if v
.declaration_type
== "method" then
416 if v
.declaration_element
== "parameter_list" then
417 if v
.is_generic_param
then
418 v
.method_params
[v
.param_index
].generic_params
.add
(new JavaType(v
.converter
))
422 v
.gen_params_index
+= 1
424 v
.method_params
.add
(new JavaType(v
.converter
))
430 else if v
.declaration_element
== "return_type" and v
.is_generic_param
then
432 v
.method_return_type
.generic_params
.add
(new JavaType(v
.converter
))
436 v
.gen_params_index
+= 1
438 # For generic return type definition
439 else if v
.declaration_element
== null then
442 else if v
.declaration_type
== "variable" then
443 if v
.declaration_element
== "type" and v
.is_generic_param
then
444 v
.variable_type
.generic_params
.add
(new JavaType(v
.converter
))
448 v
.gen_params_index
+= 1