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 # Model of all the analyzed classes
34 # Java class in construction
35 var java_class
: JavaClass is noinit
37 redef fun visit
(n
) do n
.accept_visitor
(self)
41 private fun accept_visitor
(v
: JavaVisitor) do visit_children
(v
)
47 redef class Nclass_declaration
48 redef fun accept_visitor
(v
)
50 var jtype
= n_full_class_name
.to_java_type
52 v
.java_class
= new JavaClass(jtype
)
53 v
.model
.classes
.add v
.java_class
55 # Visit all properties
60 # Extends declaration in the class header
61 redef class Nextends_declaration
62 redef fun accept_visitor
(v
)
68 # Implements declaration in the class header
69 redef class Nimplements_declaration
70 redef fun accept_visitor
(v
)
80 redef class Nproperty_declaration_method
81 redef fun accept_visitor
(v
)
83 var id
= n_identifier
.text
84 var return_jtype
= n_type
.to_java_type
87 var n_parameters
= n_parameters
89 if n_parameters
!= null then
90 params
= n_parameters
.to_a
91 else params
= new Array[JavaType]
93 var method
= new JavaMethod(return_jtype
, params
)
94 v
.java_class
.methods
[id
].add method
98 # Constructor declaration
99 redef class Nproperty_declaration_constructor
100 redef fun accept_visitor
(v
)
106 # Variable property declaration
107 redef class Nproperty_declaration_attribute
108 redef fun accept_visitor
(v
)
110 var id
= n_identifier
.text
111 var jtype
= n_type
.to_java_type
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
117 v
.java_class
.attributes
[id
] = jtype
121 # Static property declaration
122 redef class Nproperty_declaration_static
123 redef fun accept_visitor
(v
)
133 private fun to_java_type
: JavaType
135 var jtype
= n_base_type
.to_java_type
137 var brackets
= n_brackets
138 if brackets
!= null then jtype
.array_dimension
+= brackets
.children
.length
144 redef class Nbase_type
145 private fun to_java_type
: JavaType
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"])
155 redef class Nbase_type_class
156 redef fun to_java_type
do return n_full_class_name
.to_java_type
159 redef class Nbase_type_primitive
160 redef fun to_java_type
162 # All the concrete nodes under this production are tokens
164 if not node
isa NToken then continue
166 var jtype
= new JavaType
167 jtype
.identifier
.add node
.text
175 redef class Nbase_type_void
176 redef fun to_java_type
178 var jtype
= new JavaType
184 redef class Nfull_class_name
185 # All the identifiers composing this class name
186 private fun to_a
: Array[String] is abstract
188 # Access `n_class_name` on both alternatives
189 private fun n_class_name_common
: Nclass_name is abstract
191 private fun to_java_type
: JavaType
193 var jtype
= new JavaType
194 jtype
.identifier
= to_a
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
204 redef class Nfull_class_name_head
205 redef fun n_class_name_common
do return n_class_name
207 redef fun to_a
do return [n_class_name
.n_identifier
.text
]
210 redef class Nfull_class_name_tail
211 redef fun n_class_name_common
do return n_class_name
215 var a
= n_full_class_name
.to_a
216 a
.add n_class_name
.n_identifier
.text
221 redef class Nparameters
222 # Get the types composing this list of parameters
224 # This is used both on methods signatures and type parameters.
225 private fun to_a
: Array[JavaType] is abstract
228 redef class Nparameters_head
229 redef fun to_a
do return [n_parameter
.to_java_type
]
232 redef class Nparameters_tail
235 var a
= n_parameters
.to_a
236 a
.add n_parameter
.to_java_type
241 redef class Nparameter
242 private fun to_java_type
: JavaType
244 var jtype
= n_type
.to_java_type
247 if dots
!= null then jtype
.is_vararg
= true