contrib/objcwrapper: add some doc and license
[nit.git] / contrib / objcwrapper / src / objc_visitor.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 # AST visitor
16 module objc_visitor
17
18 import objc_model
19 import objc_parser
20
21 class Interpretor
22 super Visitor
23
24 var is_variable = false
25 var is_method = false
26 var is_parameter_name = false
27 var class_objc: nullable ObjcClass = null
28 var method_objc: ObjcMethod is noinit
29 var attribute_objc: ObjcAttribute is noinit
30 var param: Param is noinit
31 var model = new ObjcModel
32
33 redef fun visit(n) do n.accept_objc(self)
34 end
35
36 redef class Node
37 fun accept_objc(v: Interpretor) do visit_children(v)
38 end
39
40 redef class Nlines
41 redef fun accept_objc(v) do
42 end
43 end
44
45 redef class Nlines_interface
46 redef fun accept_objc(v) do
47 var interface_block = n_interface_block
48 var inheritance_block = n_inheritance
49 v.class_objc = null
50 if interface_block != null then
51 for class_objc in v.model.classes do
52 if class_objc.name == n_class.text then
53 v.class_objc = class_objc
54 end
55 end
56 if v.class_objc == null then
57 v.class_objc = new ObjcClass(n_class.text)
58 v.model.classes.add(v.class_objc)
59 end
60 if inheritance_block != null then v.enter_visit(inheritance_block)
61 v.enter_visit(interface_block)
62 end
63 end
64 end
65
66 redef class Ninheritance_add
67 redef fun accept_objc(v) do
68 var additional = n_additional
69 v.enter_visit(n_classe)
70 if additional != null then v.enter_visit(additional)
71 end
72 end
73
74 redef class Nclasse_class
75 redef fun accept_objc(v) do
76 v.class_objc.super_names.add(n_class.text)
77 end
78 end
79
80 redef class Nadditional_add
81 redef fun accept_objc(v) do
82 v.enter_visit(n_classe)
83 end
84 end
85
86 redef class Ninterface_block_instance
87 redef fun accept_objc(v) do
88 v.enter_visit(n_instance_declaration)
89 end
90 end
91
92 redef class Ninstance_declaration_signature
93 redef fun accept_objc(v) do
94 v.enter_visit(n_signature_block)
95 end
96 end
97
98 redef class Ninstance_declaration_property
99 redef fun accept_objc(v) do
100 v.enter_visit(n_property_declaration)
101 end
102 end
103
104 redef class Nproperty_declaration_property
105 redef fun accept_objc(v) do
106 v.enter_visit(n_property)
107 end
108 end
109
110 redef class Nsignature_block_signature
111 redef fun accept_objc(v) do
112 if n_signature.children.to_s.has("signature_named") or n_signature.children.to_s.has("signature_single") then
113 v.method_objc = new ObjcMethod
114
115 v.enter_visit(n_scope)
116 v.is_method = true
117 var signature_return_type = n_signature_return_type
118 if signature_return_type != null then v.enter_visit(signature_return_type)
119 v.is_method = false
120 v.enter_visit(n_signature)
121
122 v.class_objc.methods.add(v.method_objc)
123 end
124 end
125 end
126
127 redef class Nscope_instance
128 redef fun accept_objc(v) do
129 v.method_objc.scope = '-'
130 end
131 end
132
133 redef class Nscope_class
134 redef fun accept_objc(v) do
135 v.method_objc.scope = '+'
136 end
137 end
138
139 redef class Nsignature_return_type_return
140 redef fun accept_objc(v) do
141 v.enter_visit(n_type)
142 end
143 end
144
145 redef class Nsignature_named
146 redef fun accept_objc(v) do
147 v.param = new Param
148
149 v.enter_visit(n_left)
150 v.is_parameter_name = true
151 v.enter_visit(n_right)
152 v.is_parameter_name = false
153 v.enter_visit(n_signature_type)
154
155 v.method_objc.params.add(v.param)
156 end
157 end
158
159 redef class Nsignature_single
160 redef fun accept_objc(v) do
161 v.param = new Param
162 v.param.is_single = true
163 v.enter_visit(n_term)
164 v.method_objc.params.add(v.param)
165 end
166 end
167
168 redef class Nsignature_type_anonymous
169 redef fun accept_objc(v) do
170 v.enter_visit(n_type)
171 v.method_objc.is_commented = true
172 end
173 end
174
175 redef class Nsignature_type_table
176 redef fun accept_objc(v) do
177 v.enter_visit(n_type)
178 v.param.is_table = true
179 end
180 end
181
182 redef class Nsignature_type_pointer
183 redef fun accept_objc(v) do
184 v.enter_visit(n_type)
185 v.param.is_pointer = true
186 end
187 end
188
189 redef class Nsignature_type_unsigned
190 redef fun accept_objc(v) do
191 v.method_objc.is_commented = true
192 v.param.is_pointer = true
193 end
194 end
195
196 redef class Nsignature_type_protocol
197 redef fun accept_objc(v) do
198 v.enter_visit(n_type)
199 v.method_objc.is_commented = true
200 end
201 end
202
203 redef class Nsignature_type_normal
204 redef fun accept_objc(v) do
205 v.enter_visit(n_type)
206 end
207 end
208
209 redef class Nterm_private
210 redef fun accept_objc(v) do
211 if v.is_parameter_name then
212 v.param.variable_name = n_private.text
213 else if v.is_variable then
214 v.attribute_objc.name = n_private.text
215 else
216 v.param.name = n_private.text
217 end
218 end
219 end
220
221 redef class Nterm_class
222 redef fun accept_objc(v) do
223 if v.is_parameter_name then
224 v.param.variable_name = n_class.text
225 else if v.is_variable then
226 v.attribute_objc.name = n_class.text
227 else
228 v.param.name = n_class.text
229 end
230 end
231 end
232
233 redef class Nterm_var
234 redef fun accept_objc(v) do
235 if v.is_parameter_name then
236 v.param.variable_name = n_id.text
237 else if v.is_variable then
238 v.attribute_objc.name = n_id.text
239 else
240 v.param.name = n_id.text
241 end
242 end
243 end
244
245 redef class Nproperty_property
246 redef fun accept_objc(v) do
247 var protocol = n_protocols
248 if protocol == null then
249 v.is_variable = true
250 v.attribute_objc = new ObjcAttribute
251 v.enter_visit(n_left)
252 v.enter_visit(n_type)
253 v.class_objc.attributes.add(v.attribute_objc)
254 v.is_variable = false
255 end
256 end
257 end
258
259 redef class Ntype_type
260 redef fun accept_objc(v) do
261 v.enter_visit(n_data_type)
262 end
263 end
264
265 redef class Ndata_type_more
266 redef fun accept_objc(v) do
267 v.enter_visit(n_more_type)
268 end
269 end
270
271 redef class Ndata_type_otype
272 redef fun accept_objc(v) do
273 if v.is_variable then
274 v.attribute_objc.return_type = n_class.text
275 else if v.is_method then
276 v.method_objc.return_type = n_class.text
277 else
278 v.param.return_type = n_class.text
279 end
280 end
281 end
282
283 redef class Nmore_type_stype
284 redef fun accept_objc(v) do
285 v.enter_visit(n_specific_type)
286 end
287 end
288
289 redef class Nmore_type_ptype
290 redef fun accept_objc(v) do
291 v.enter_visit(n_primitive_type)
292 end
293 end
294
295 redef class Nspecific_type_i
296 redef fun accept_objc(v) do
297 if v.is_variable then
298 v.attribute_objc.return_type = "id"
299 else if v.is_method then
300 v.method_objc.return_type = "id"
301 else
302 v.param.return_type = "id"
303 end
304 end
305 end
306
307 redef class Nspecific_type_b
308 redef fun accept_objc(v) do
309 if v.is_variable then
310 v.attribute_objc.return_type = "Bool"
311 else if v.is_method then
312 v.method_objc.return_type = "Bool"
313 else
314 v.param.return_type = "Bool"
315 end
316 end
317 end
318
319 redef class Nspecific_type_val
320 redef fun accept_objc(v) do
321 if v.is_variable then
322 v.attribute_objc.return_type = "va_list"
323 else if v.is_method then
324 v.method_objc.return_type = "va_list"
325 else
326 v.param.return_type = "va_list"
327 end
328 end
329 end
330
331 redef class Nspecific_type_v
332 redef fun accept_objc(v) do
333 if v.is_variable then
334 v.attribute_objc.return_type = "void"
335 else if v.is_method then
336 v.method_objc.return_type = "void"
337 else
338 v.param.return_type = "void"
339 end
340 end
341 end
342
343 redef class Nprimitive_type_ui8
344 redef fun accept_objc(v) do
345 if v.is_variable then
346 v.attribute_objc.return_type = "uint8_t"
347 else if v.is_method then
348 v.method_objc.return_type = "uint8_t"
349 else
350 v.param.return_type = "uint8_t"
351 end
352 end
353 end
354
355 redef class Nprimitive_type_ui16
356 redef fun accept_objc(v) do
357 if v.is_variable then
358 v.attribute_objc.return_type = "uint16_t"
359 else if v.is_method then
360 v.method_objc.return_type = "uint16_t"
361 else
362 v.param.return_type = "uint16_t"
363 end
364 end
365 end
366
367 redef class Nprimitive_type_ui32
368 redef fun accept_objc(v) do
369 if v.is_variable then
370 v.attribute_objc.return_type = "uint32_t"
371 else if v.is_method then
372 v.method_objc.return_type = "uint32_t"
373 else
374 v.param.return_type = "uint32_t"
375 end
376 end
377 end
378
379 redef class Nprimitive_type_ui64
380 redef fun accept_objc(v) do
381 if v.is_variable then
382 v.attribute_objc.return_type = "uint64_t"
383 else if v.is_method then
384 v.method_objc.return_type = "uint64_t"
385 else
386 v.param.return_type = "uint64_t"
387 end
388 end
389 end
390
391 redef class Nprimitive_type_i8
392 redef fun accept_objc(v) do
393 if v.is_variable then
394 v.attribute_objc.return_type = "int8_t"
395 else if v.is_method then
396 v.method_objc.return_type = "int8_t"
397 else
398 v.param.return_type = "int8_t"
399 end
400 end
401 end
402
403 redef class Nprimitive_type_i16
404 redef fun accept_objc(v) do
405 if v.is_variable then
406 v.attribute_objc.return_type = "int16_t"
407 else if v.is_method then
408 v.method_objc.return_type = "int16_t"
409 else
410 v.param.return_type = "int16_t"
411 end
412 end
413 end
414
415 redef class Nprimitive_type_i32
416 redef fun accept_objc(v) do
417 if v.is_variable then
418 v.attribute_objc.return_type = "int32_t"
419 else if v.is_method then
420 v.method_objc.return_type = "int32_t"
421 else
422 v.param.return_type = "int32_t"
423 end
424 end
425 end
426
427 redef class Nprimitive_type_i64
428 redef fun accept_objc(v) do
429 if v.is_variable then
430 v.attribute_objc.return_type = "int64_t"
431 else if v.is_method then
432 v.method_objc.return_type = "int64_t"
433 else
434 v.param.return_type = "int64_t"
435 end
436 end
437 end
438
439 redef class Nprimitive_type_uc
440 redef fun accept_objc(v) do
441 if v.is_variable then
442 v.attribute_objc.return_type = "unichar"
443 else if v.is_method then
444 v.method_objc.return_type = "unichar"
445 else
446 v.param.return_type = "unichar"
447 end
448 end
449 end
450
451 redef class Nprimitive_type_c
452 redef fun accept_objc(v) do
453 if v.is_variable then
454 v.attribute_objc.return_type = "char"
455 else if v.is_method then
456 v.method_objc.return_type = "char"
457 else
458 v.param.return_type = "char"
459 end
460 end
461 end
462
463 redef class Nprimitive_type_s
464 redef fun accept_objc(v) do
465 if v.is_variable then
466 v.attribute_objc.return_type = "short"
467 else if v.is_method then
468 v.method_objc.return_type = "short"
469 else
470 v.param.return_type = "short"
471 end
472 end
473 end
474
475 redef class Nprimitive_type_si
476 redef fun accept_objc(v) do
477 if v.is_variable then
478 v.attribute_objc.return_type = "short int"
479 else if v.is_method then
480 v.method_objc.return_type = "short int"
481 else
482 v.param.return_type = "short int"
483 end
484 end
485 end
486
487 redef class Nprimitive_type_i
488 redef fun accept_objc(v) do
489 if v.is_variable then
490 v.attribute_objc.return_type = "int"
491 else if v.is_method then
492 v.method_objc.return_type = "int"
493 else
494 v.param.return_type = "int"
495 end
496 end
497 end
498
499 redef class Nprimitive_type_l
500 redef fun accept_objc(v) do
501 if v.is_variable then
502 v.attribute_objc.return_type = "long"
503 else if v.is_method then
504 v.method_objc.return_type = "long"
505 else
506 v.param.return_type = "long"
507 end
508 end
509 end
510
511 redef class Nprimitive_type_li
512 redef fun accept_objc(v) do
513 if v.is_variable then
514 v.attribute_objc.return_type = "long int"
515 else if v.is_method then
516 v.method_objc.return_type = "long int"
517 else
518 v.param.return_type = "long int"
519 end
520 end
521 end
522
523 redef class Nprimitive_type_ll
524 redef fun accept_objc(v) do
525 if v.is_variable then
526 v.attribute_objc.return_type = "long long"
527 else if v.is_method then
528 v.method_objc.return_type = "long long"
529 else
530 v.param.return_type = "long long"
531 end
532 end
533 end
534
535 redef class Nprimitive_type_lli
536 redef fun accept_objc(v) do
537 if v.is_variable then
538 v.attribute_objc.return_type = "long long int"
539 else if v.is_method then
540 v.method_objc.return_type = "long long int"
541 else
542 v.param.return_type = "long long int"
543 end
544 end
545 end
546
547 redef class Nprimitive_type_f
548 redef fun accept_objc(v) do
549 if v.is_variable then
550 v.attribute_objc.return_type = "float"
551 else if v.is_method then
552 v.method_objc.return_type = "float"
553 else
554 v.param.return_type = "float"
555 end
556 end
557 end
558
559 redef class Nprimitive_type_d
560 redef fun accept_objc(v) do
561 if v.is_variable then
562 v.attribute_objc.return_type = "double"
563 else if v.is_method then
564 v.method_objc.return_type = "double"
565 else
566 v.param.return_type = "double"
567 end
568 end
569 end
570
571 redef class Nprimitive_type_ld
572 redef fun accept_objc(v) do
573 if v.is_variable then
574 v.attribute_objc.return_type = "long double"
575 else if v.is_method then
576 v.method_objc.return_type = "long double"
577 else
578 v.param.return_type = "long double"
579 end
580 end
581 end