contrib/objcwrapper: comment out properties depending on types availability
[nit.git] / contrib / objcwrapper / src / objc_generator.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Code generation
16 module objc_generator
17
18 import opts
19
20 import objc_model
21
22 redef class Sys
23
24 # Path to the output file
25 var opt_output = new OptionString("Output file", "-o")
26
27 # Shall `init` methods/constructors be wrapped as methods?
28 #
29 # By default, these methods/constructors are wrapped as extern constructors.
30 # So initializing an extern Objective-C object looks like:
31 # ~~~nitish
32 # var o = new NSArray.init_with_array(some_other_array)
33 # ~~~
34 #
35 # If this option is set, the object must first be allocated and then initialized.
36 # This is closer to the Objective-C behavior:
37 # ~~~nitish
38 # var o = new NSArray
39 # o.init_with_array(some_other_array)
40 # ~~~
41 var opt_init_as_methods = new OptionBool(
42 "Wrap `init...` constructors as Nit methods instead of Nit constructors",
43 "--init-as-methods")
44
45 private var nit_to_java_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"
50 types["int"] = "Int"
51 types["long"] = "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"
58
59 types["NSUInteger"] = "Int"
60 types["BOOL"] = "Bool"
61 types["id"] = "NSObject"
62 types["constid"] = "NSObject"
63 types["SEL"] = "NSObject"
64 types["void"] = "Pointer"
65
66 return types
67 end
68 end
69
70 redef class ObjcModel
71 redef fun knows_type(objc_type) do return super or
72 nit_to_java_types.keys.has(objc_type)
73 end
74
75 # Wrapper generator
76 class CodeGenerator
77
78 # `ObjcModel` to wrap
79 var model: ObjcModel
80
81 # Generate Nit code to wrap `classes`
82 fun generate
83 do
84 var classes = model.classes
85
86 # Open specified path or stdin
87 var file
88 var path = opt_output.value
89 if path != null then
90 if path.file_extension != "nit" then
91 print_error "Warning: output file path does not end with '.nit'"
92 end
93
94 file = new FileWriter.open(path)
95 else
96 file = stdout
97 end
98
99 # Generate code
100 file.write "import cocoa::foundation\n\n"
101 for classe in classes do
102 write_class(classe, file)
103 end
104
105 if path != null then file.close
106 end
107
108 private fun write_class(classe: ObjcClass, file: Writer)
109 do
110 # Class header
111 file.write """
112
113 extern class {{{classe.name}}} in "ObjC" `{ {{{classe.name}}} * `}
114 """
115
116 # Supers
117 for super_name in classe.super_names do file.write """
118 super {{{super_name}}}
119 """
120 if classe.super_names.is_empty then file.write """
121 super NSObject
122 """
123
124 file.write "\n"
125
126 # Constructor or constructors
127 write_constructors(classe, file)
128
129 # Attributes
130 for attribute in classe.attributes do
131 write_attribute(attribute, file)
132 end
133
134 # Methods
135 for method in classe.methods do
136 if not model.knows_all_types(method) then method.is_commented = true
137
138 if not opt_init_as_methods.value and method.is_init then continue
139
140 write_method_signature(method, file)
141 write_objc_method_call(method, file)
142 end
143
144 file.write """
145 end
146 """
147 end
148
149 private fun write_constructors(classe: ObjcClass, file: Writer)
150 do
151 if opt_init_as_methods.value then
152 # A single constructor for `alloc`
153 file.write """
154 new in "ObjC" `{
155 return [{{{classe.name}}} alloc];
156 `}
157
158 """
159 return
160 end
161
162 # A constructor per `init...` method
163 for method in classe.methods do
164 if not method.is_init then continue
165
166 if not model.knows_all_types(method) then method.is_commented = true
167
168 write_method_signature(method, file)
169
170 write_objc_init_call(classe.name, method, file)
171 end
172 end
173
174 private fun write_attribute(attribute: ObjcAttribute, file: Writer)
175 do
176 if not model.knows_type(attribute.return_type) then attribute.is_commented = true
177
178 write_attribute_getter(attribute, file)
179 # TODO write_attribute_setter if there is no `readonly` annotation
180 end
181
182 private fun write_attribute_getter(attribute: ObjcAttribute, file: Writer)
183 do
184 var nit_attr_name = attribute.name.to_snake_case
185 var nit_attr_type = attribute.return_type.to_nit_type
186
187 var c = attribute.comment_str
188
189 file.write """
190 {{{c}}} fun {{{nit_attr_name}}}: {{{nit_attr_type}}} in "ObjC" `{
191 {{{c}}} return [self {{{attribute.name}}}];
192 {{{c}}} `}
193
194 """
195 end
196
197 private fun write_attribute_setter(attribute: ObjcAttribute, file: Writer)
198 do
199 var nit_attr_name = attribute.name.to_snake_case
200 var nit_attr_type = attribute.return_type.to_nit_type
201
202 var c = attribute.comment_str
203
204 file.write """
205 {{{c}}} fun {{{nit_attr_name}}}=(value: {{{nit_attr_type}}}) in "ObjC" `{
206 {{{c}}} return self.{{{attribute.name}}} = value;
207 {{{c}}} `}
208
209 """
210 end
211
212 private fun write_method_signature(method: ObjcMethod, file: Writer)
213 do
214 var c = method.comment_str
215
216 # Build Nit method name
217 var name = ""
218 for param in method.params do
219 name += param.name[0].to_upper.to_s + param.name.substring_from(1)
220 end
221 name = name.to_snake_case
222
223 if name == "init" then name = ""
224
225 # Kind of method
226 var fun_keyword = "fun"
227 if not opt_init_as_methods.value and method.is_init then
228 fun_keyword = "new"
229 end
230
231 # Params
232 var params = new Array[String]
233 for param in method.params do
234 if param.is_single then break
235 params.add "{param.variable_name}: {param.return_type.to_nit_type}"
236 end
237
238 var params_with_par = ""
239 if params.not_empty then params_with_par = "({params.join(", ")})"
240
241 # Return
242 var ret = ""
243 if method.return_type != "void" and fun_keyword != "new" then
244 ret = ": {method.return_type.to_nit_type}"
245 end
246
247 file.write """
248 {{{c}}} {{{fun_keyword}}} {{{name}}}{{{params_with_par}}}{{{ret}}} in "ObjC" `{
249 """
250 end
251
252 # Write a combined call to alloc and to a constructor/method
253 private fun write_objc_init_call(class_name: String, method: ObjcMethod, file: Writer)
254 do
255 # Method name and other params
256 var params = new Array[String]
257 for param in method.params do
258 if not param.is_single then
259 params.add "{param.name}: {param.variable_name}"
260 else params.add param.name
261 end
262
263 var c = method.comment_str
264
265 file.write """
266 {{{c}}} return [[{{{class_name}}} alloc] {{{params.join(" ")}}}];
267 {{{c}}} `}
268
269 """
270 end
271
272 private fun write_objc_method_call(method: ObjcMethod, file: Writer)
273 do
274 # Is there a value to return?
275 var ret = ""
276 if method.return_type != "void" then ret = "return "
277
278 # Method name and other params
279 var params = new Array[String]
280 for param in method.params do
281 if not param.is_single then
282 params.add "{param.name}: {param.variable_name}"
283 else params.add param.name
284 end
285
286 var c = method.comment_str
287
288 file.write """
289 {{{c}}} {{{ret}}}[self {{{params.join(" ")}}}];
290 {{{c}}} `}
291
292 """
293 end
294 end
295
296 redef class Text
297 # Nit equivalent to this type
298 private fun to_nit_type: String
299 do
300 var types = sys.nit_to_java_types
301
302 if types.has_key(self) then
303 return types[self]
304 else
305 return to_s
306 end
307 end
308 end
309
310 redef class Property
311 private fun comment_str: String do if is_commented then
312 return "#"
313 else return ""
314 end