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.
24 # Path to the output file
25 var opt_output
= new OptionString("Output file", "-o")
27 # Shall `init` methods/constructors be wrapped as methods?
29 # By default, these methods/constructors are wrapped as extern constructors.
30 # So initializing an extern Objective-C object looks like:
32 # var o = new NSArray.init_with_array(some_other_array)
35 # If this option is set, the object must first be allocated and then initialized.
36 # This is closer to the Objective-C behavior:
39 # o.init_with_array(some_other_array)
41 var opt_init_as_methods
= new OptionBool(
42 "Wrap `init...` constructors as Nit methods instead of Nit constructors",
45 private var objc_to_nit_types
: Map[String, String] is lazy
do
46 var types
= new HashMap[String, String]
47 types
["char"] = "Byte"
48 types
["short"] = "Int"
49 types
["short int"] = "Int"
52 types
["long int"] = "Int"
53 types
["long long"] = "Int"
54 types
["long long int"] = "Int"
55 types
["float"] = "Float"
56 types
["double"] = "Float"
57 types
["long double"] = "Float"
59 types
["NSUInteger"] = "Int"
60 types
["BOOL"] = "Bool"
61 types
["id"] = "NSObject"
62 types
["constid"] = "NSObject"
63 types
["SEL"] = "NSObject"
64 types
["void"] = "Pointer"
71 redef fun knows_type
(objc_type
) do return super or
72 objc_to_nit_types
.keys
.has
(objc_type
)
81 # Generate Nit code to wrap `classes`
84 var classes
= model
.classes
86 # Open specified path or stdin
88 var path
= opt_output
.value
90 if path
.file_extension
!= "nit" then
91 print_error
"Warning: output file path does not end with '.nit'"
94 file
= new FileWriter.open
(path
)
100 file
.write
"import cocoa::foundation\n"
101 for classe
in classes
do
102 write_class
(classe
, file
)
105 if path
!= null then file
.close
108 private fun write_class
(classe
: ObjcClass, file
: Writer)
113 extern class {{{classe.name}}} in "ObjC" `{ {{{classe.name}}} * `}
117 for super_name
in classe
.super_names
do file
.write
"""
118 super {{{super_name}}}
120 if classe
.super_names
.is_empty
then file
.write
"""
124 # Constructor or constructors
125 write_constructors
(classe
, file
)
128 for attribute
in classe
.attributes
do
129 write_attribute
(attribute
, file
)
132 # Instance methods '-'
133 for method
in classe
.methods
do
134 if not model
.knows_all_types
(method
) then method
.is_commented
= true
136 if not opt_init_as_methods
.value
and method
.is_init
then continue
137 if method
.is_class_property
then continue
139 write_method_signature
(method
, file
)
140 write_objc_method_call
(method
, file
)
148 for method
in classe
.methods
do
149 if not method
.is_class_property
then continue
151 write_method_signature
(method
, file
)
152 write_objc_method_call
(method
, file
)
156 private fun write_constructors
(classe
: ObjcClass, file
: Writer)
158 if opt_init_as_methods
.value
then
159 # A single constructor for `alloc`
163 return [{{{classe.name}}} alloc];
169 # A constructor per `init...` method
170 for method
in classe
.methods
do
171 if not method
.is_init
then continue
173 if not model
.knows_all_types
(method
) then method
.is_commented
= true
175 write_method_signature
(method
, file
)
177 write_objc_init_call
(classe
.name
, method
, file
)
181 private fun write_attribute
(attribute
: ObjcAttribute, file
: Writer)
183 if not model
.knows_type
(attribute
.return_type
) then attribute
.is_commented
= true
185 write_attribute_getter
(attribute
, file
)
186 # TODO write_attribute_setter if there is no `readonly` annotation
189 private fun write_attribute_getter
(attribute
: ObjcAttribute, file
: Writer)
191 var nit_attr_name
= attribute
.name
.to_snake_case
192 var nit_attr_type
= attribute
.return_type
.objc_to_nit_type
194 var c
= attribute
.comment_str
199 {{{c}}} fun {{{nit_attr_name}}}: {{{nit_attr_type}}} in "ObjC" `{
200 {{{c}}} return [self {{{attribute.name}}}];
205 private fun write_attribute_setter
(attribute
: ObjcAttribute, file
: Writer)
207 var nit_attr_name
= attribute
.name
.to_snake_case
208 var nit_attr_type
= attribute
.return_type
.objc_to_nit_type
210 var c
= attribute
.comment_str
215 {{{c}}} fun {{{nit_attr_name}}}=(value: {{{nit_attr_type}}}) in "ObjC" `{
216 {{{c}}} return self.{{{attribute.name}}} = value;
221 private fun write_method_signature
(method
: ObjcMethod, file
: Writer)
223 var c
= method
.comment_str
225 # Build Nit method name
227 for param
in method
.params
do
228 name
+= param
.name
[0].to_upper
.to_s
+ param
.name
.substring_from
(1)
230 name
= name
.to_snake_case
232 if name
== "init" then name
= ""
234 # If class method, prefix with class name
235 if method
.is_class_property
then name
= "{method.objc_class.name.to_snake_case}_{name}"
238 var fun_keyword
= "fun"
239 if not opt_init_as_methods
.value
and method
.is_init
then
244 var params
= new Array[String]
245 for param
in method
.params
do
246 if param
.is_single
then break
247 params
.add
"{param.variable_name}: {param.return_type.objc_to_nit_type}"
250 var params_with_par
= ""
251 if params
.not_empty
then params_with_par
= "({params.join(", ")})"
255 if method
.return_type
!= "void" and fun_keyword
!= "new" then
256 ret
= ": {method.return_type.objc_to_nit_type}"
262 {{{c}}}{{{fun_keyword}}} {{{name}}}{{{params_with_par}}}{{{ret}}} in "ObjC" `{
266 # Write a combined call to alloc and to a constructor/method
267 private fun write_objc_init_call
(class_name
: String, method
: ObjcMethod, file
: Writer)
269 # Method name and other params
270 var params
= new Array[String]
271 for param
in method
.params
do
272 if not param
.is_single
then
273 params
.add
"{param.name}: {param.variable_name}"
274 else params
.add param
.name
277 var c
= method
.comment_str
280 {{{c}}} return [[{{{class_name}}} alloc] {{{params.join(" ")}}}];
285 private fun write_objc_method_call
(method
: ObjcMethod, file
: Writer)
287 # Is there a value to return?
289 if method
.return_type
!= "void" then ret
= "return "
291 # Method name and other params
292 var params
= new Array[String]
293 for param
in method
.params
do
294 if not param
.is_single
then
295 params
.add
"{param.name}: {param.variable_name}"
296 else params
.add param
.name
299 # Receiver, instance or class
301 if method
.is_class_property
then recv
= method
.objc_class
.name
303 var c
= method
.comment_str
306 {{{c}}} {{{ret}}}[{{{recv}}} {{{params.join(" ")}}}];
313 # Nit equivalent to this type
314 private fun objc_to_nit_type
: String
316 var types
= sys
.objc_to_nit_types
318 if types
.has_key
(self) then
326 redef class ObjcProperty
327 private fun comment_str
: String do if is_commented
then
331 # Full documentation to be generated for the Nit code
332 private fun doc
: String is abstract
335 redef class ObjcMethod
336 private fun indent
: String do return if is_class_property
then "" else "\t"
338 redef fun comment_str
do return indent
+ super
342 var recv
= if is_class_property
then objc_class
.name
else "self"
343 return "{indent}# Wraps: `[{recv} {params.join(" ")}]`"
347 redef class ObjcAttribute
348 redef fun doc
do return "\t# Wraps: `{objc_class.name}.{name}`"