c3e5f98fb0fe06627ce676dacfe3cb784caad835
[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 # 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 # Uses a visitor to extract data from the javap output AST
19 # It sends the data to `code_generator` module
20 module javap_visitor
21
22 import javap_test_parser
23 import code_generator
24 import jtype_converter
25 intrude import model
26
27 # Visitor of the AST generated from javap output
28 class JavaVisitor
29 super Visitor
30
31 # Model of all the analyzed classes
32 var model: JavaModel
33
34 # Java class in construction
35 var java_class: JavaClass is noinit
36
37 redef fun visit(n) do n.accept_visitor(self)
38 end
39
40 redef class Node
41 private fun accept_visitor(v: JavaVisitor) do visit_children(v)
42 end
43
44 # ---
45 # Class Header
46
47 redef class Nclass_declaration
48 redef fun accept_visitor(v)
49 do
50 var jtype = n_full_class_name.to_java_type
51
52 v.java_class = new JavaClass(jtype)
53 v.model.add_class v.java_class
54
55 # Visit all properties
56 super
57 end
58 end
59
60 # Extends declaration in the class header
61 redef class Nextends_declaration
62 redef fun accept_visitor(v)
63 do
64 # TODO
65 end
66 end
67
68 # Implements declaration in the class header
69 redef class Nimplements_declaration
70 redef fun accept_visitor(v)
71 do
72 # TODO
73 end
74 end
75
76 # ---
77 # Properties
78
79 # Method declaration
80 redef class Nproperty_declaration_method
81 redef fun accept_visitor(v)
82 do
83 var id = n_identifier.text
84 var return_jtype = n_type.to_java_type
85
86 # Collect parameters
87 var n_parameters = n_parameters
88 var params
89 if n_parameters != null then
90 params = n_parameters.to_a
91 else params = new Array[JavaType]
92
93 var method = new JavaMethod(return_jtype, params)
94 v.java_class.methods[id].add method
95 end
96 end
97
98 # Constructor declaration
99 redef class Nproperty_declaration_constructor
100 redef fun accept_visitor(v)
101 do
102 # Collect parameters
103 var n_parameters = n_parameters
104 var params
105 if n_parameters != null then
106 params = n_parameters.to_a
107 else params = new Array[JavaType]
108
109 var method = new JavaConstructor(params)
110 v.java_class.constructors.add method
111 end
112 end
113
114 # Variable property declaration
115 redef class Nproperty_declaration_attribute
116 redef fun accept_visitor(v)
117 do
118 var id = n_identifier.text
119 var jtype = n_type.to_java_type
120
121 # Manually count the array depth as it is after the id
122 var brackets = n_brackets
123 if brackets != null then jtype.array_dimension += brackets.children.length
124
125 v.java_class.attributes[id] = jtype
126 end
127 end
128
129 # Static property declaration
130 redef class Nproperty_declaration_static
131 redef fun accept_visitor(v)
132 do
133 # TODO
134 end
135 end
136
137 # ---
138 # Services
139
140 redef class Ntype
141 private fun to_java_type: JavaType
142 do
143 var jtype = n_base_type.to_java_type
144
145 var brackets = n_brackets
146 if brackets != null then jtype.array_dimension += brackets.children.length
147
148 return jtype
149 end
150 end
151
152 redef class Nbase_type
153 private fun to_java_type: JavaType
154 do
155 # By default, everything is bound by object
156 # TODO use a more precise bound
157 var jtype = new JavaType
158 jtype.identifier.add_all(["java", "lang", "object"])
159 return jtype
160 end
161 end
162
163 redef class Nbase_type_class
164 redef fun to_java_type do return n_full_class_name.to_java_type
165 end
166
167 redef class Nbase_type_primitive
168 redef fun to_java_type
169 do
170 # All the concrete nodes under this production are tokens
171 for node in depth do
172 if not node isa NToken then continue
173
174 var jtype = new JavaType
175 jtype.identifier.add node.text
176 return jtype
177 end
178
179 abort
180 end
181 end
182
183 redef class Nbase_type_void
184 redef fun to_java_type
185 do
186 var jtype = new JavaType
187 jtype.is_void = true
188 return jtype
189 end
190 end
191
192 redef class Nfull_class_name
193 # All the identifiers composing this class name
194 private fun to_a: Array[String] is abstract
195
196 # Access `n_class_name` on both alternatives
197 private fun n_class_name_common: Nclass_name is abstract
198
199 private fun to_java_type: JavaType
200 do
201 var jtype = new JavaType
202 jtype.identifier = to_a
203
204 # Generic parameters
205 var n_params = n_class_name_common.n_generic_parameters
206 if n_params != null then jtype.generic_params = n_params.n_parameters.to_a
207
208 return jtype
209 end
210 end
211
212 redef class Nfull_class_name_head
213 redef fun n_class_name_common do return n_class_name
214
215 redef fun to_a do return [n_class_name.n_identifier.text]
216 end
217
218 redef class Nfull_class_name_tail
219 redef fun n_class_name_common do return n_class_name
220
221 redef fun to_a
222 do
223 var a = n_full_class_name.to_a
224 a.add n_class_name.n_identifier.text
225 return a
226 end
227 end
228
229 redef class Nparameters
230 # Get the types composing this list of parameters
231 #
232 # This is used both on methods signatures and type parameters.
233 private fun to_a: Array[JavaType] is abstract
234 end
235
236 redef class Nparameters_head
237 redef fun to_a do return [n_parameter.to_java_type]
238 end
239
240 redef class Nparameters_tail
241 redef fun to_a
242 do
243 var a = n_parameters.to_a
244 a.add n_parameter.to_java_type
245 return a
246 end
247 end
248
249 redef class Nparameter
250 private fun to_java_type: JavaType
251 do
252 var jtype = n_type.to_java_type
253
254 var dots = n_dots
255 if dots != null then jtype.is_vararg = true
256
257 return jtype
258 end
259 end