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 # Merge the calls to `alloc` and `init...` in a single constructor?
23 # If `true`, also the default behavior, initializing an extern Objective-C object looks like:
25 # var o = new NSArray.init_with_array(some_other_array)
28 # If `false`, the object must first be allocated and then initialized.
29 # This is closer to the Objective-C behavior:
32 # o.init_with_array(some_other_array)
34 var init_with_alloc
= true is writable
36 fun generator
(classes
: Array[nullable ObjcClass]) do
37 for classe
in classes
do
38 var file
= new FileWriter.open
(classe
.name
+ ".nit")
39 nit_class_generator
(classe
, file
, init_with_alloc
)
44 fun type_convertor
(type_word
: String): String do
45 var types
= new HashMap[String, String]
46 types
["char"] = "Byte"
47 types
["short"] = "Int"
48 types
["short int"] = "Int"
51 types
["long int"] = "Int"
52 types
["long long"] = "Int"
53 types
["long long int"] = "Int"
54 types
["float"] = "Float"
55 types
["double"] = "Float"
56 types
["long double"] = "Float"
58 types
["NSUInteger"] = "Int"
59 types
["BOOL"] = "Bool"
60 types
["id"] = "NSObject"
62 if types
.has_key
(type_word
) then
63 return types
[type_word
]
69 fun nit_class_generator
(classe
: nullable ObjcClass, file
: FileWriter, init_with_alloc
: Bool) do
70 var commented_methods
= new Array[ObjcMethod]
71 file
.write
"import cocoa::foundation\n\n"
72 file
.write
("extern class " + classe
.name
+ """ in "ObjC" `{ """ + classe.name + """ * `}\n""")
73 for super_name
in classe
.super_names
do
74 file
.write
(""" super """ + super_name
+ "\n")
76 if classe
.super_names
.is_empty
then file
.write
(""" super NSObject\n""")
77 new_nit_generator
(classe
, file
, init_with_alloc
)
79 for attribute
in classe
.attributes
do
80 nit_attribute_generator
(attribute
, file
)
82 for method
in classe
.methods
do
83 if method
.is_commented
then
84 commented_methods
.add
(method
)
86 if init_with_alloc
and method
.params
.first
.name
.has
("init") then continue
88 nit_method_generator
(method
, file
, init_with_alloc
)
89 file
.write
(""" in "ObjC" `{\n """)
90 objc_method_generator
(method
, file
)
92 if method
!= classe
.methods
.last
then file
.write
("\n\n")
95 for commented_method
in commented_methods
do
96 if commented_method
== commented_methods
.first
then file
.write
("\n")
98 nit_method_generator
(commented_method
, file
, init_with_alloc
)
99 if commented_method
!= commented_methods
.last
then file
.write
("\n")
104 fun new_nit_generator
(classe
: nullable ObjcClass, file
: FileWriter, init_with_alloc
: Bool) do
105 if init_with_alloc
then
106 for method
in classe
.methods
do
107 if method
.params
.first
.name
.has
("init") and not method
.is_commented
then
109 if method
.params
.first
.name
== "init" then
112 nit_method_generator
(method
, file
, init_with_alloc
)
114 file
.write
""" in "ObjC" `{\n"""
115 new_alloc_init_objc_generator
(classe
.name
, method
, file
)
116 file
.write
""" `}\n"""
120 file
.write
"""\n new in "ObjC"`{\n"""
121 new_objc_generator
(classe
, file
)
122 file
.write
""" `}\n"""
126 fun nit_attribute_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
127 nit_attribute_setter_generator
(attribute
, file
)
131 fun nit_attribute_setter_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
132 file
.write
(""" fun """ + attribute
.name
.to_snake_case
+ ": " + type_convertor
(attribute
.return_type
))
133 file
.write
""" in "ObjC" `{\n"""
134 objc_attribute_setter_generator
(attribute
, file
)
135 file
.write
""" `}\n"""
138 fun nit_attribute_getter_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
139 file
.write
(""" fun """ + attribute
.name
.to_snake_case
+ "=(value: " + type_convertor
(attribute
.return_type
) + ")")
140 file
.write
" in \"ObjC\
" `\{\n"
141 objc_attribute_getter_generator
(attribute
, file
)
142 file
.write
""" `}\n"""
145 fun nit_method_generator
(method
: ObjcMethod, file
: FileWriter, init_with_alloc
: Bool) do
147 for param
in method
.params
do
148 name
+= param
.name
[0].to_upper
.to_s
+ param
.name
.substring_from
(1)
149 name
= name
.to_snake_case
151 if name
.has
("init") and init_with_alloc
then
154 if not init_with_alloc
and name
== "init" then name
= "init_0"
158 for param
in method
.params
do
159 if param
== method
.params
.first
and not param
.is_single
then
160 file
.write
("(" + param
.variable_name
+ ": " + type_convertor
(param
.return_type
))
162 if param
!= method
.params
.first
and not param
.is_single
then
163 file
.write
(", " + param
.variable_name
+ ": " + type_convertor
(param
.return_type
))
165 if param
== method
.params
.last
and not param
.is_single
then
169 if method
.return_type
!= "void" and not method
.params
.first
.name
.has
("init") then file
.write
(": " + type_convertor
(method
.return_type
))
172 fun new_alloc_init_objc_generator
(classe_name
: String, method
: ObjcMethod, file
: FileWriter) do
173 file
.write
(""" return [[""" + classe_name
+ " alloc] ")
174 for param
in method
.params
do
175 if not param
.is_single
then
176 file
.write
(param
.name
+ ":" + param
.variable_name
)
177 if not param
== method
.params
.last
then file
.write
(" ")
179 file
.write param
.name
185 fun new_objc_generator
(classe
: nullable ObjcClass, file
: FileWriter) do
186 file
.write
(""" return [""" + classe
.name
+ " alloc];\n")
189 fun objc_attribute_setter_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
190 file
.write
(""" return [self """ + attribute
.name
+ "];\n")
193 fun objc_attribute_getter_generator
(attribute
: ObjcAttribute, file
: FileWriter) do
194 file
.write
(""" self.""" + attribute
.name
+ " = value;\n")
197 fun objc_method_generator
(method
: ObjcMethod, file
: FileWriter) do
198 if method
.return_type
!= "void" then file
.write
("return ")
200 for param
in method
.params
do
201 if not param
.is_single
then
202 file
.write
(param
.name
+ ":" + param
.variable_name
)
203 if not param
== method
.params
.last
then file
.write
" "
205 file
.write
(param
.name
)