contrib/objcwrapper: avoid conflicts with Nit keywords and top-level methods
[nit.git] / contrib / objcwrapper / src / objc_generator.nit
index f28a49e..d6be6f5 100644 (file)
@@ -16,6 +16,7 @@
 module objc_generator
 
 import opts
+import gen_nit
 
 import objc_model
 
@@ -42,7 +43,7 @@ redef class Sys
                "Wrap `init...` constructors as Nit methods instead of Nit constructors",
                "--init-as-methods")
 
-       private var nit_to_java_types: Map[String, String] is lazy do
+       private var objc_to_nit_types: Map[String, String] is lazy do
                var types = new HashMap[String, String]
                types["char"] = "Byte"
                types["short"] = "Int"
@@ -57,7 +58,10 @@ redef class Sys
                types["long double"] = "Float"
 
                types["NSUInteger"] = "Int"
+               types["NSInteger"] = "Int"
+               types["CGFloat"] = "Float"
                types["BOOL"] = "Bool"
+
                types["id"] = "NSObject"
                types["constid"] = "NSObject"
                types["SEL"] = "NSObject"
@@ -69,7 +73,7 @@ end
 
 redef class ObjcModel
        redef fun knows_type(objc_type) do return super or
-               nit_to_java_types.keys.has(objc_type)
+               objc_to_nit_types.keys.has(objc_type)
 end
 
 # Wrapper generator
@@ -97,7 +101,13 @@ class CodeGenerator
                end
 
                # Generate code
-               file.write "import cocoa::foundation\n\n"
+               file.write """
+# File generated by objcwrapper with the following command:
+# {{{program_name}}} {{{args.join(" ")}}}
+
+"""
+
+               file.write "import cocoa::foundation\n"
                for classe in classes do
                        write_class(classe, file)
                end
@@ -121,8 +131,6 @@ extern class {{{classe.name}}} in "ObjC" `{ {{{classe.name}}} * `}
        super NSObject
 """
 
-               file.write "\n"
-
                # Constructor or constructors
                write_constructors(classe, file)
 
@@ -131,11 +139,12 @@ extern class {{{classe.name}}} in "ObjC" `{ {{{classe.name}}} * `}
                        write_attribute(attribute, file)
                end
 
-               # Methods
+               # Instance methods '-'
                for method in classe.methods do
                        if not model.knows_all_types(method) then method.is_commented = true
 
                        if not opt_init_as_methods.value and method.is_init then continue
+                       if method.is_class_property then continue
 
                        write_method_signature(method, file)
                        write_objc_method_call(method, file)
@@ -144,6 +153,14 @@ extern class {{{classe.name}}} in "ObjC" `{ {{{classe.name}}} * `}
                file.write """
 end
 """
+
+               # Class methods '+'
+               for method in classe.methods do
+                       if not method.is_class_property then continue
+
+                       write_method_signature(method, file)
+                       write_objc_method_call(method, file)
+               end
        end
 
        private fun write_constructors(classe: ObjcClass, file: Writer)
@@ -151,10 +168,10 @@ end
                if opt_init_as_methods.value then
                        # A single constructor for `alloc`
                        file.write """
+
        new in "ObjC" `{
                return [{{{classe.name}}} alloc];
        `}
-
 """
                        return
                end
@@ -167,7 +184,7 @@ end
 
                        write_method_signature(method, file)
 
-                               write_objc_init_call(classe.name, method, file)
+                       write_objc_init_call(classe.name, method, file)
                end
        end
 
@@ -182,30 +199,32 @@ end
        private fun write_attribute_getter(attribute: ObjcAttribute, file: Writer)
        do
                var nit_attr_name = attribute.name.to_snake_case
-               var nit_attr_type = attribute.return_type.to_nit_type
+               var nit_attr_type = attribute.return_type.objc_to_nit_type
 
                var c = attribute.comment_str
 
                file.write """
+
+{{{attribute.doc}}}
 {{{c}}}        fun {{{nit_attr_name}}}: {{{nit_attr_type}}} in "ObjC" `{
 {{{c}}}                return [self {{{attribute.name}}}];
 {{{c}}}        `}
-
 """
        end
 
        private fun write_attribute_setter(attribute: ObjcAttribute, file: Writer)
        do
                var nit_attr_name = attribute.name.to_snake_case
-               var nit_attr_type = attribute.return_type.to_nit_type
+               var nit_attr_type = attribute.return_type.objc_to_nit_type
 
                var c = attribute.comment_str
 
                file.write """
+
+{{{attribute.doc}}}
 {{{c}}}        fun {{{nit_attr_name}}}=(value: {{{nit_attr_type}}}) in "ObjC" `{
 {{{c}}}                return self.{{{attribute.name}}} = value;
 {{{c}}}        `}
-
 """
        end
 
@@ -222,6 +241,11 @@ end
 
                if name == "init" then name = ""
 
+               name = name.to_nit_name(property=true, pointer=true)
+
+               # If class method, prefix with class name
+               if method.is_class_property then name = "{method.objc_class.name.to_snake_case}_{name}"
+
                # Kind of method
                var fun_keyword = "fun"
                if not opt_init_as_methods.value and method.is_init then
@@ -232,7 +256,7 @@ end
                var params = new Array[String]
                for param in method.params do
                        if param.is_single then break
-                       params.add "{param.variable_name}: {param.return_type.to_nit_type}"
+                       params.add "{param.nit_variable_name}: {param.return_type.objc_to_nit_type}"
                end
 
                var params_with_par = ""
@@ -241,11 +265,13 @@ end
                # Return
                var ret = ""
                if method.return_type != "void" and fun_keyword != "new" then
-                       ret = ": {method.return_type.to_nit_type}"
+                       ret = ": {method.return_type.objc_to_nit_type}"
                end
 
                file.write """
-{{{c}}}        {{{fun_keyword}}} {{{name}}}{{{params_with_par}}}{{{ret}}} in "ObjC" `{
+
+{{{method.doc}}}
+{{{c}}}{{{fun_keyword}}} {{{name}}}{{{params_with_par}}}{{{ret}}} in "ObjC" `{
 """
        end
 
@@ -256,16 +282,15 @@ end
                var params = new Array[String]
                for param in method.params do
                        if not param.is_single then
-                               params.add "{param.name}: {param.variable_name}"
+                               params.add "{param.name}: {param.nit_variable_name}"
                        else params.add param.name
                end
 
                var c = method.comment_str
 
                file.write """
-{{{c}}}                return [[{{{class_name}}} alloc] {{{params.join(" ")}}}];
-{{{c}}}        `}
-
+{{{c}}}        return [[{{{class_name}}} alloc] {{{params.join(" ")}}}];
+{{{c}}}`}
 """
        end
 
@@ -279,25 +304,28 @@ end
                var params = new Array[String]
                for param in method.params do
                        if not param.is_single then
-                               params.add "{param.name}: {param.variable_name}"
+                               params.add "{param.name}: {param.nit_variable_name}"
                        else params.add param.name
                end
 
+               # Receiver, instance or class
+               var recv = "self"
+               if method.is_class_property then recv = method.objc_class.name
+
                var c = method.comment_str
 
                file.write """
-{{{c}}}                {{{ret}}}[self {{{params.join(" ")}}}];
-{{{c}}}        `}
-
+{{{c}}}        {{{ret}}}[{{{recv}}} {{{params.join(" ")}}}];
+{{{c}}}`}
 """
        end
 end
 
 redef class Text
        # Nit equivalent to this type
-       private fun to_nit_type: String
+       private fun objc_to_nit_type: String
        do
-               var types = sys.nit_to_java_types
+               var types = sys.objc_to_nit_types
 
                if types.has_key(self) then
                        return types[self]
@@ -305,10 +333,52 @@ redef class Text
                        return to_s
                end
        end
+
+       # Convert to a safe Nit name for a `property`, a property in a subclass of `pointer` or a variable
+       private fun to_nit_name(property, pointer: nullable Bool): String
+       do
+               var name = to_s
+               name = name.to_snake_case
+
+               while not name.is_empty and name.chars.first == '_' do name = name.substring_from(1)
+
+               if keywords.has(name) then name = name + "0"
+
+               if property == true then
+                       if methods_in_object.has(name) then name = name + "0"
+                       if pointer == true and methods_in_pointer.has(name) then name = name + "0"
+               end
+
+               return name.to_s
+       end
 end
 
-redef class Property
+redef class ObjcProperty
        private fun comment_str: String do if is_commented then
                return "#"
        else return ""
+
+       # Full documentation to be generated for the Nit code
+       private fun doc: String is abstract
+end
+
+redef class ObjcMethod
+       private fun indent: String do return if is_class_property then "" else "\t"
+
+       redef fun comment_str do return indent + super
+
+       redef fun doc
+       do
+               var recv = if is_class_property then objc_class.name else "self"
+               return "{indent}# Wraps: `[{recv} {params.join(" ")}]`"
+       end
+end
+
+redef class ObjcAttribute
+       redef fun doc do return "\t# Wraps: `{objc_class.name}.{name}`"
+end
+
+redef class ObjcParam
+       # `variable_name` mangled for the Nit language
+       private fun nit_variable_name: String do return variable_name.to_nit_name
 end