25bd84fa93b98c7c9358b1bed461f9334c4ceb82
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.
23 # Path to the output file
24 var opt_output
= new OptionString("Output file", "-o")
28 # Merge the calls to `alloc` and `init...` in a single constructor?
30 # If `true`, also the default behavior, initializing an extern Objective-C object looks like:
32 # var o = new NSArray.init_with_array(some_other_array)
35 # If `false`, 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 init_with_alloc
= true is writable
43 # Generate Nit code to wrap `classes`
44 fun generate
(classes
: Array[ObjcClass])
46 # Open specified path or stdin
48 var path
= opt_output
.value
50 if path
.file_extension
!= "nit" then
51 print_error
"Warning: output file path does not end with '.nit'"
54 file
= new FileWriter.open
(path
)
60 file
.write
"import cocoa::foundation\n\n"
61 for classe
in classes
do
62 write_class
(classe
, file
)
65 if path
!= null then file
.close
68 private fun type_convertor
(type_word
: String): String
70 var types
= new HashMap[String, String]
71 types
["char"] = "Byte"
72 types
["short"] = "Int"
73 types
["short int"] = "Int"
76 types
["long int"] = "Int"
77 types
["long long"] = "Int"
78 types
["long long int"] = "Int"
79 types
["float"] = "Float"
80 types
["double"] = "Float"
81 types
["long double"] = "Float"
83 types
["NSUInteger"] = "Int"
84 types
["BOOL"] = "Bool"
85 types
["id"] = "NSObject"
87 if types
.has_key
(type_word
) then
88 return types
[type_word
]
94 private fun write_class
(classe
: ObjcClass, file
: Writer)
96 var commented_methods
= new Array[ObjcMethod]
97 file
.write
"extern class " + classe
.name
+ """ in "ObjC" `{ """ + classe.name + """ * `}\n"""
98 for super_name
in classe
.super_names
do
99 file
.write
""" super """ + super_name
+ "\n"
101 if classe
.super_names
.is_empty
then file
.write
""" super NSObject\n"""
102 write_constructor
(classe
, file
)
104 for attribute
in classe
.attributes
do
105 write_attribute
(attribute
, file
)
107 for method
in classe
.methods
do
108 if method
.is_commented
then
109 commented_methods
.add
(method
)
111 if init_with_alloc
and method
.params
.first
.name
.has
("init") then continue
113 write_method
(method
, file
)
114 file
.write
""" in "ObjC" `{\n """
115 write_objc_method_call
(method
, file
)
117 if method
!= classe
.methods
.last
then file
.write
"\n\n"
120 for commented_method
in commented_methods
do
121 if commented_method
== commented_methods
.first
then file
.write
"\n"
123 write_method
(commented_method
, file
)
124 if commented_method
!= commented_methods
.last
then file
.write
"\n"
129 private fun write_constructor
(classe
: ObjcClass, file
: Writer)
131 if init_with_alloc
then
132 for method
in classe
.methods
do
133 if method
.params
.first
.name
.has
("init") and not method
.is_commented
then
135 if method
.params
.first
.name
== "init" then
138 write_method
(method
, file
)
140 file
.write
""" in "ObjC" `{\n"""
141 write_objc_init_call
(classe
.name
, method
, file
)
142 file
.write
""" `}\n"""
146 file
.write
"""\n new in "ObjC"`{\n"""
147 file
.write
""" return [""" + classe
.name
+ " alloc];\n"
148 file
.write
""" `}\n"""
152 private fun write_attribute
(attribute
: ObjcAttribute, file
: Writer)
154 write_attribute_getter
(attribute
, file
)
155 # TODO write_attribute_setter if there is no `readonly` annotation
159 private fun write_attribute_getter
(attribute
: ObjcAttribute, file
: Writer)
161 file
.write
""" fun """ + attribute
.name
.to_snake_case
+ ": " + type_convertor
(attribute
.return_type
)
162 file
.write
""" in "ObjC" `{\n"""
163 file
.write
""" return [self """ + attribute
.name
+ "];\n"
164 file
.write
""" `}\n"""
167 private fun write_attribute_setter
(attribute
: ObjcAttribute, file
: Writer)
169 file
.write
""" fun """ + attribute
.name
.to_snake_case
+ "=(value: " + type_convertor
(attribute
.return_type
) + ")"
170 file
.write
" in \"ObjC\
" `\{\n"
171 file
.write
""" self.""" + attribute
.name
+ " = value;\n"
172 file
.write
""" `}\n"""
175 private fun write_method
(method
: ObjcMethod, file
: Writer)
178 for param
in method
.params
do
179 name
+= param
.name
[0].to_upper
.to_s
+ param
.name
.substring_from
(1)
180 name
= name
.to_snake_case
182 if name
.has
("init") and init_with_alloc
then
185 if not init_with_alloc
and name
== "init" then name
= "init_0"
189 for param
in method
.params
do
190 if param
== method
.params
.first
and not param
.is_single
then
191 file
.write
"(" + param
.variable_name
+ ": " + type_convertor
(param
.return_type
)
193 if param
!= method
.params
.first
and not param
.is_single
then
194 file
.write
", " + param
.variable_name
+ ": " + type_convertor
(param
.return_type
)
196 if param
== method
.params
.last
and not param
.is_single
then
200 if method
.return_type
!= "void" and not method
.params
.first
.name
.has
("init") then
201 file
.write
": " + type_convertor
(method
.return_type
)
205 private fun write_objc_init_call
(classe_name
: String, method
: ObjcMethod, file
: Writer)
207 file
.write
""" return [[""" + classe_name
+ " alloc] "
208 for param
in method
.params
do
209 if not param
.is_single
then
210 file
.write param
.name
+ ":" + param
.variable_name
211 if not param
== method
.params
.last
then file
.write
" "
213 file
.write param
.name
219 private fun write_objc_method_call
(method
: ObjcMethod, file
: Writer)
221 if method
.return_type
!= "void" then file
.write
"return "
223 for param
in method
.params
do
224 if not param
.is_single
then
225 file
.write param
.name
+ ":" + param
.variable_name
226 if not param
== method
.params
.last
then file
.write
" "
228 file
.write param
.name