1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
21 # AST visitor building `model` from the parsed class headers
25 # `ObjcModel` in construction
26 var model
= new ObjcModel
28 # `ObjcClass` in construction, if any
29 private var objc_class
: nullable ObjcClass = null
31 redef fun visit
(n
) do n
.accept_objc
(self)
35 private fun accept_objc
(v
: ObjcVisitor) do visit_children
(v
)
42 redef class Nlines_interface
43 redef fun accept_objc
(v
)
45 var interface_block
= n_interface_block
46 if interface_block
== null then return
48 # If reopening a class, continue with the existing one
50 for objc_class
in v
.model
.classes
do
51 if objc_class
.name
== n_class
.text
then
58 c
= new ObjcClass(n_class
.text
)
63 # Visit superclass declarations
64 var inheritance_block
= n_inheritance
65 if inheritance_block
!= null then v
.enter_visit
(inheritance_block
)
68 v
.enter_visit
(interface_block
)
72 # Method or function declaration
73 redef class Nsignature_block_signature
74 redef fun accept_objc
(v
)
79 var method
= new ObjcMethod(c
)
80 method
.return_type
= n_signature_return_type
.to_type
81 method
.is_class_property
= n_scope
.is_class_property
83 for n_param
in n_parameter
.children
do
84 var param
= n_param
.to_param
87 # Unsupported parameter format
88 method
.is_commented
= true
90 # Use a placeholder for easier debugging
92 param
.name
= "UNKNOWN"
93 param
.return_type
= "UNKNOWN"
94 param
.variable_name
= "UNKNOWN"
97 method
.params
.add param
104 # Class variable/attribute declaration (inside alternative node)
105 redef class Nproperty_property
106 redef fun accept_objc
(v
)
111 var attr
= new ObjcAttribute(c
)
112 attr
.return_type
= n_type
.to_type
113 attr
.name
= n_left
.collect_text
115 c
.attributes
.add attr
119 # Class variable/attribute declaration (outside with @property)
120 redef class Nproperty_declaration_property
121 redef fun accept_objc
(v
)
123 # TODO property attribute readonly, copy, etc.
132 # Append all tokens under this node in a `String`
133 private fun collect_text
: String
135 var buf
= new FlatBuffer
136 for node
in depth
do if node
isa NToken then buf
.append node
.text
142 # Do not visit other lines, they are only to be eaten
143 redef fun accept_objc
(v
) do end
147 # Does this mark a class property (+)? Otherwise it's an instance property (-).
148 private fun is_class_property
: Bool do return false
151 redef class Nscope_class
152 redef fun is_class_property
do return true
155 redef class Nsignature_return_type
156 # Get type from this node TODO return an ObjcType
157 private fun to_type
: String do return collect_text
160 redef class Nsignature_return_type_return
161 redef fun to_type
do return n_type
.to_type
164 redef class Nparameter
165 # Return null if type is not yet unsupported
166 private fun to_param
: nullable ObjcParam do return null
169 # Parameters with both a public and an internal name
170 redef class Nparameter_named
173 var param
= new ObjcParam
174 param
.variable_name
= n_right
.collect_text
175 param
.name
= n_left
.collect_text
177 var n_type
= n_parameter_type_in_par
178 param
.return_type
= if n_type
!= null then
179 n_type
.n_parameter_type
.to_type
186 # Usually the name of a method without parameters
187 redef class Nparameter_single
190 var param
= new ObjcParam
191 param
.name
= n_term
.collect_text
192 param
.is_single
= true
197 redef class Nparameter_type
198 # Get type from this node TODO return an ObjcType
199 private fun to_type
: String
201 # FIXME taking the first token skips pointers
202 for child
in children
do
203 if child
isa Ntype then
213 # Get type from this node TODO return an ObjcType
214 private fun to_type
: String do return collect_text