contrib/jwrapper: big rewrite of the AST visitor
[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.classes.add 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 # TODO
103 end
104 end
105
106 # Variable property declaration
107 redef class Nproperty_declaration_attribute
108 redef fun accept_visitor(v)
109 do
110 var id = n_identifier.text
111 var jtype = n_type.to_java_type
112
113 # Manually count the array depth as it is after the id
114 var brackets = n_brackets
115 if brackets != null then jtype.array_dimension += brackets.children.length
116
117 v.java_class.attributes[id] = jtype
118 end
119 end
120
121 # Static property declaration
122 redef class Nproperty_declaration_static
123 redef fun accept_visitor(v)
124 do
125 # TODO
126 end
127 end
128
129 # ---
130 # Services
131
132 redef class Ntype
133 private fun to_java_type: JavaType
134 do
135 var jtype = n_base_type.to_java_type
136
137 var brackets = n_brackets
138 if brackets != null then jtype.array_dimension += brackets.children.length
139
140 return jtype
141 end
142 end
143
144 redef class Nbase_type
145 private fun to_java_type: JavaType
146 do
147 # By default, everything is bound by object
148 # TODO use a more precise bound
149 var jtype = new JavaType
150 jtype.identifier.add_all(["java", "lang", "object"])
151 return jtype
152 end
153 end
154
155 redef class Nbase_type_class
156 redef fun to_java_type do return n_full_class_name.to_java_type
157 end
158
159 redef class Nbase_type_primitive
160 redef fun to_java_type
161 do
162 # All the concrete nodes under this production are tokens
163 for node in depth do
164 if not node isa NToken then continue
165
166 var jtype = new JavaType
167 jtype.identifier.add node.text
168 return jtype
169 end
170
171 abort
172 end
173 end
174
175 redef class Nbase_type_void
176 redef fun to_java_type
177 do
178 var jtype = new JavaType
179 jtype.is_void = true
180 return jtype
181 end
182 end
183
184 redef class Nfull_class_name
185 # All the identifiers composing this class name
186 private fun to_a: Array[String] is abstract
187
188 # Access `n_class_name` on both alternatives
189 private fun n_class_name_common: Nclass_name is abstract
190
191 private fun to_java_type: JavaType
192 do
193 var jtype = new JavaType
194 jtype.identifier = to_a
195
196 # Generic parameters
197 var n_params = n_class_name_common.n_generic_parameters
198 if n_params != null then jtype.generic_params = n_params.n_parameters.to_a
199
200 return jtype
201 end
202 end
203
204 redef class Nfull_class_name_head
205 redef fun n_class_name_common do return n_class_name
206
207 redef fun to_a do return [n_class_name.n_identifier.text]
208 end
209
210 redef class Nfull_class_name_tail
211 redef fun n_class_name_common do return n_class_name
212
213 redef fun to_a
214 do
215 var a = n_full_class_name.to_a
216 a.add n_class_name.n_identifier.text
217 return a
218 end
219 end
220
221 redef class Nparameters
222 # Get the types composing this list of parameters
223 #
224 # This is used both on methods signatures and type parameters.
225 private fun to_a: Array[JavaType] is abstract
226 end
227
228 redef class Nparameters_head
229 redef fun to_a do return [n_parameter.to_java_type]
230 end
231
232 redef class Nparameters_tail
233 redef fun to_a
234 do
235 var a = n_parameters.to_a
236 a.add n_parameter.to_java_type
237 return a
238 end
239 end
240
241 redef class Nparameter
242 private fun to_java_type: JavaType
243 do
244 var jtype = n_type.to_java_type
245
246 var dots = n_dots
247 if dots != null then jtype.is_vararg = true
248
249 return jtype
250 end
251 end