contrib/jwrapper: Added static overload support
[nit.git] / contrib / jwrapper / src / javap_visitor.nit
1 # This file is part of NIT (http://www.nitlanguage.org).
2 #
3 # Copyright 2014 Frédéric Vachon <fredvac@gmail.com>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Uses a visitor to extract data from the javap output AST
18 # It sends the data to `code_generator` module
19 module javap_visitor
20
21 import javap_test_parser
22 import code_generator
23 intrude import types
24
25 class JavaVisitor
26 super Visitor
27
28 var java_class = new JavaClass
29 var declaration_type: nullable String = null
30 var declaration_element: nullable String = null
31 var full_class_name = new Array[String]
32
33 var variable_id = ""
34 var variable_type = new JavaType
35
36 var is_generic_param = false
37 var gen_params_index = 0
38
39 var is_primitive_array = false
40
41 var method_id = ""
42 var method_return_type = new JavaType
43 var method_params = new Array[JavaType]
44 var param_index = 0
45
46 redef fun visit(n) do n.accept_visitor(self)
47 end
48
49 redef class Node
50 fun accept_visitor(v: JavaVisitor) do visit_children(v)
51 end
52
53 redef class Nidentifier
54 redef fun accept_visitor(v)
55 do
56 if v.declaration_type == "class_header" then
57
58 if v.declaration_element == "id" then
59 v.full_class_name.add(self.text)
60 end
61
62 else if v.declaration_type == "variable" then
63
64 if v.declaration_element == "id" then
65 v.variable_id += self.text
66 else if v.declaration_element == "type" then
67 if v.is_generic_param then
68 v.variable_type.generic_params[v.gen_params_index].identifier.add(self.text)
69 else
70 v.variable_type.identifier.add(self.text)
71 end
72 end
73
74 else if v.declaration_type == "method" then
75
76 if v.declaration_element == "id" then
77 v.method_id = self.text
78 else if v.declaration_element == "return_type" then
79 if self.text == "void" then
80 v.method_return_type.is_void = true
81 else if v.is_generic_param then
82 v.method_return_type.generic_params[v.gen_params_index].identifier.add(self.text)
83 else
84 v.method_return_type.identifier.add(self.text)
85 end
86 else if v.declaration_element == "parameter_list" then
87 if v.is_generic_param then
88 v.method_params[v.param_index].generic_params[v.gen_params_index].identifier.add(self.text)
89 else
90 v.method_params[v.param_index].identifier.add(self.text)
91 end
92 end
93
94 end
95
96 super
97 end
98 end
99
100 # Primitive array node
101 redef class N_39d_91d_93d_39d
102 redef fun accept_visitor(v)
103 do
104 if v.declaration_type == "variable" then
105 if v.declaration_element == "type" then
106 if v.is_generic_param then
107 v.variable_type.generic_params[v.gen_params_index].array_dimension += 1
108 else
109 v.variable_type.array_dimension += 1
110 end
111 end
112
113 else if v.declaration_type == "method" then
114
115 if v.declaration_element == "return_type" then
116 if v.is_generic_param then
117 v.method_return_type.generic_params[v.gen_params_index].array_dimension += 1
118 else
119 v.method_return_type.array_dimension += 1
120 end
121 else if v.declaration_element == "parameter_list" then
122 if v.is_generic_param then
123 v.method_params[v.param_index].generic_params[v.gen_params_index].array_dimension += 1
124 else
125 v.method_params[v.param_index].array_dimension += 1
126 end
127 end
128
129 end
130
131 super
132 end
133 end
134
135 redef class N_39dchar_39d
136 redef fun accept_visitor(v)
137 do
138 if v.declaration_type == "variable" then
139 if v.declaration_element == "type" then
140 v.variable_type.identifier.add(self.text)
141 end
142 else if v.declaration_type == "method" then
143 if v.declaration_element == "return_type" then
144 v.method_return_type.identifier.add(self.text)
145 else if v.declaration_element == "parameter_list" then
146 v.method_params[v.param_index].identifier.add(self.text)
147 end
148 end
149 end
150 end
151
152 redef class N_39dboolean_39d
153 redef fun accept_visitor(v)
154 do
155 if v.declaration_type == "variable" then
156 if v.declaration_element == "type" then
157 v.variable_type.identifier.add(self.text)
158 end
159 else if v.declaration_type == "method" then
160 if v.declaration_element == "return_type" then
161 v.method_return_type.identifier.add(self.text)
162 else if v.declaration_element == "parameter_list" then
163 v.method_params[v.param_index].identifier.add(self.text)
164 end
165 end
166 end
167 end
168
169 redef class N_39dfloat_39d
170 redef fun accept_visitor(v)
171 do
172 if v.declaration_type == "variable" then
173 if v.declaration_element == "type" then
174 v.variable_type.identifier.add(self.text)
175 end
176 else if v.declaration_type == "method" then
177 if v.declaration_element == "return_type" then
178 v.method_return_type.identifier.add(self.text)
179 else if v.declaration_element == "parameter_list" then
180 v.method_params[v.param_index].identifier.add(self.text)
181 end
182 end
183 end
184 end
185
186 redef class N_39ddouble_39d
187 redef fun accept_visitor(v)
188 do
189 if v.declaration_type == "variable" then
190 if v.declaration_element == "type" then
191 v.variable_type.identifier.add(self.text)
192 end
193 else if v.declaration_type == "method" then
194 if v.declaration_element == "return_type" then
195 v.method_return_type.identifier.add(self.text)
196 else if v.declaration_element == "parameter_list" then
197 v.method_params[v.param_index].identifier.add(self.text)
198 end
199 end
200 end
201 end
202
203 redef class N_39dbyte_39d
204 redef fun accept_visitor(v)
205 do
206 if v.declaration_type == "variable" then
207 if v.declaration_element == "type" then
208 v.variable_type.identifier.add(self.text)
209 end
210 else if v.declaration_type == "method" then
211 if v.declaration_element == "return_type" then
212 v.method_return_type.identifier.add(self.text)
213 else if v.declaration_element == "parameter_list" then
214 v.method_params[v.param_index].identifier.add(self.text)
215 end
216 end
217 end
218 end
219
220 redef class N_39dshort_39d
221 redef fun accept_visitor(v)
222 do
223 if v.declaration_type == "variable" then
224 if v.declaration_element == "type" then
225 v.variable_type.identifier.add(self.text)
226 end
227 else if v.declaration_type == "method" then
228 if v.declaration_element == "return_type" then
229 v.method_return_type.identifier.add(self.text)
230 else if v.declaration_element == "parameter_list" then
231 v.method_params[v.param_index].identifier.add(self.text)
232 end
233 end
234 end
235 end
236
237 redef class N_39dint_39d
238 redef fun accept_visitor(v)
239 do
240 if v.declaration_type == "variable" then
241 if v.declaration_element == "type" then
242 v.variable_type.identifier.add(self.text)
243 end
244 else if v.declaration_type == "method" then
245 if v.declaration_element == "return_type" then
246 v.method_return_type.identifier.add(self.text)
247 else if v.declaration_element == "parameter_list" then
248 v.method_params[v.param_index].identifier.add(self.text)
249 end
250 end
251 end
252 end
253
254 redef class N_39dlong_39d
255 redef fun accept_visitor(v)
256 do
257 if v.declaration_type == "variable" then
258 if v.declaration_element == "type" then
259 v.variable_type.identifier.add(self.text)
260 end
261 else if v.declaration_type == "method" then
262 if v.declaration_element == "return_type" then
263 v.method_return_type.identifier.add(self.text)
264 else if v.declaration_element == "parameter_list" then
265 v.method_params[v.param_index].identifier.add(self.text)
266 end
267 end
268 end
269 end
270
271 # #
272 # C L A S S H E A D E R #
273 # #
274 redef class Nclass_header
275 redef fun accept_visitor(v)
276 do
277 v.declaration_type = "class_header"
278 v.declaration_element = "id"
279 super
280
281 # Exit class declaration
282 v.declaration_type = null
283 v.declaration_element = null
284
285 v.java_class.name = v.full_class_name
286 end
287 end
288
289 # Extends declaration in the class header
290 redef class Nextends_declaration
291 redef fun accept_visitor(v)
292 do
293 v.declaration_element = "extends"
294 super
295 v.declaration_element = null
296 end
297 end
298
299 # Implements declaration in the class header
300 redef class Nimplements_declaration
301 redef fun accept_visitor(v)
302 do
303 v.declaration_element = "implements"
304 super
305 v.declaration_element = null
306 end
307 end
308
309 # #
310 # F I E L D D E C L A R A T I O N S #
311 # #
312
313 # Method declaration in the field declarations
314 redef class Nmethod_declaration
315 redef fun accept_visitor(v)
316 do
317 v.declaration_type = "method"
318 super
319 v.declaration_type = null
320
321 v.java_class.add_method(v.method_id, v.method_return_type, v.method_params)
322
323 v.method_params.clear
324 v.method_id = ""
325 v.method_return_type = new JavaType
326 end
327 end
328
329 # Constructor declaration in the field declarations
330 redef class Nconstructor_declaration
331 redef fun accept_visitor(v)
332 do
333 v.declaration_type = "constructor"
334 super
335 v.declaration_type = null
336 end
337 end
338
339 # Variable declaration in the field declarations
340 redef class Nvariable_declaration
341 redef fun accept_visitor(v)
342 do
343 v.declaration_type = "variable"
344 super
345 v.declaration_type = null
346
347 v.java_class.attributes[v.variable_id] = v.variable_type
348
349 v.variable_id = ""
350 v.variable_type = new JavaType
351 end
352 end
353
354 # Static declaration in the field declarations
355 redef class Nstatic_declaration
356 redef fun accept_visitor(v)
357 do
358 v.declaration_type = "static"
359 super
360 v.declaration_type = null
361 end
362 end
363
364 # Identifier of the field
365 redef class Nvariable_id
366 redef fun accept_visitor(v)
367 do
368 v.declaration_element = "id"
369 super
370 v.declaration_element = null
371 end
372 end
373
374 # Identifier of the method
375 redef class Nmethod_id
376 redef fun accept_visitor(v)
377 do
378 v.declaration_element = "id"
379 super
380 v.declaration_element = null
381 end
382 end
383
384 redef class Ntype
385 redef fun accept_visitor(v)
386 do
387 if v.declaration_type == "variable" and v.declaration_element != "id" then
388 v.declaration_element = "type"
389 end
390
391 if v.declaration_type == "method" and v.declaration_element == null then
392 v.declaration_element = "return_type"
393 end
394
395 super
396
397 if v.declaration_element == "variable" then
398 v.declaration_element = null
399 end
400 end
401 end
402
403 redef class Ngeneric_param
404 redef fun accept_visitor(v)
405 do
406 # Ignore the weird generic return type declaration
407 if v.declaration_type == "method" then
408 if v.declaration_element == null then
409 v.declaration_element = "ignore"
410 else
411 v.is_generic_param = true
412 v.gen_params_index = 0
413
414 if v.declaration_element == "return_type" then
415 v.method_return_type.generic_params = new Array[JavaType]
416 else if v.declaration_element == "parameter_list" then
417 v.method_params[v.param_index].generic_params = new Array[JavaType]
418 end
419 end
420 else if v.declaration_type == "variable" then
421 if v.declaration_element == "type" then
422 v.is_generic_param = true
423 v.gen_params_index = 0
424 v.variable_type.generic_params = new Array[JavaType]
425 end
426 end
427
428 super
429
430 v.declaration_element = null
431 v.is_generic_param = false
432 end
433 end
434
435 redef class Nparameter_list
436 redef fun accept_visitor(v)
437 do
438 v.declaration_element = "parameter_list"
439 v.param_index = 0
440 super
441 v.declaration_element = null
442 v.param_index = 0
443 end
444 end
445
446 redef class Nparameter
447 redef fun accept_visitor(v)
448 do
449 if v.declaration_type == "method" then
450 if v.declaration_element == "parameter_list" then
451 if v.is_generic_param then
452 v.method_params[v.param_index].generic_params.add(new JavaType)
453
454 super
455
456 v.gen_params_index += 1
457 else
458 v.method_params.add(new JavaType)
459
460 super
461
462 v.param_index += 1
463 end
464 else if v.declaration_element == "return_type" and v.is_generic_param then
465
466 v.method_return_type.generic_params.add(new JavaType)
467
468 super
469
470 v.gen_params_index += 1
471 end
472 else if v.declaration_type == "variable" then
473 if v.declaration_element == "type" and v.is_generic_param then
474 v.variable_type.generic_params.add(new JavaType)
475
476 super
477
478 v.gen_params_index += 1
479 end
480 else
481 super
482 end
483 end
484 end
485
486 var p = new TestParser_javap
487 var tree = p.main
488
489 var visitor = new JavaVisitor
490 visitor.enter_visit(tree)
491
492 var generator = new CodeGenerator("bundle.nit", visitor.java_class)
493 generator.generate