compiler: Changed types of Char to uint32_t and NativeString to unsigned char*
authorLucas Bajolet <r4pass@hotmail.com>
Mon, 1 Jun 2015 18:03:56 +0000 (14:03 -0400)
committerLucas Bajolet <r4pass@hotmail.com>
Tue, 2 Jun 2015 15:05:39 +0000 (11:05 -0400)
Signed-off-by: Lucas Bajolet <r4pass@hotmail.com>

13 files changed:
lib/android/bundle/bundle.nit
lib/android/intent/intent_api10.nit
lib/cpp.nit
src/compiler/abstract_compiler.nit
src/compiler/compiler_ffi/light.nit
src/compiler/separate_compiler.nit
src/ffi/java.nit
src/ffi/light_ffi_base.nit
src/nitni/nitni_base.nit
tests/sav/neo_doxygen_dump_args4.res
tests/sav/neo_doxygen_dump_args5.res
tests/test_ffi_cpp_duplicated_callback_a.nit
tests/test_ffi_cpp_duplicated_callback_b.nit

index edb0c88..76babb4 100644 (file)
@@ -63,7 +63,7 @@ extern class NativeBundle in "Java" `{ android.os.Bundle `}
        `}
        # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
        fun put_char(key: JavaString, value: Char) in "Java" `{
-               self.putChar(key, value);
+               self.putChar(key, (char)value);
        `}
        fun put_short(key: JavaString, value: Int) in "Java" `{
                self.putShort(key, (short) value);
@@ -148,7 +148,7 @@ extern class NativeBundle in "Java" `{ android.os.Bundle `}
                char[] java_array = new char[(int)Array_of_Char_length(value)];
 
                for(int i=0; i < java_array.length; ++i)
-                       java_array[i] = Array_of_Char__index(value, i);
+                       java_array[i] = (char)Array_of_Char__index(value, i);
 
                self.putCharArray(key, java_array);
        `}
@@ -218,10 +218,10 @@ extern class NativeBundle in "Java" `{ android.os.Bundle `}
                return self.getByte(key, (byte) def_value);
        `}
        # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
-       fun get_char(key: JavaString): Char in "Java" `{ return self.getChar(key); `}
+       fun get_char(key: JavaString): Char in "Java" `{ return (int)self.getChar(key); `}
        # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
        fun get_char_with_def_value(key: JavaString, def_value: Char): Char in "Java" `{
-               return self.getChar(key, def_value);
+               return (int)self.getChar(key, (char)def_value);
        `}
        fun get_short(key: JavaString): Int in "Java" `{ return (short) self.getShort(key); `}
        fun get_short_with_def_value(key: JavaString, def_value: Int): Int in "Java" `{
@@ -335,7 +335,7 @@ extern class NativeBundle in "Java" `{ android.os.Bundle `}
                if (java_array == null) return nit_array;
 
                for(int i=0; i < java_array.length; ++i)
-                       Array_of_Char_add(nit_array, java_array[i]);
+                       Array_of_Char_add(nit_array, (int)java_array[i]);
 
                return nit_array;
        `}
index 2d536fd..8c6a928 100644 (file)
@@ -81,7 +81,7 @@ extern class NativeIntent in "Java" `{ android.content.Intent `}
        `}
        # FIXME: Java's `char` are encoded on 16-bits whereas Nit's are on 8-bits.
        fun char_extra(name: JavaString, def_value: Char): Char in "Java" `{
-               return self.getCharExtra(name, def_value);
+               return (int)self.getCharExtra(name, (char)def_value);
        `}
        fun char_sequence_array_extra(name: JavaString): Array[String]
          import StringCopyArray, StringCopyArray.add, StringCopyArray.collection in "Java" `{
@@ -244,7 +244,7 @@ extern class NativeIntent in "Java" `{ android.content.Intent `}
                char[] java_array = new char[(int)Array_of_Char_length(value)];
 
                for (int i=0; i < java_array.length; ++i)
-                       java_array[i] = Array_of_Char__index(value, i);
+                       java_array[i] = (char)Array_of_Char__index(value, i);
 
                return self.putExtra(name, java_array);
        `}
index a2896d3..c64ae33 100644 (file)
@@ -31,6 +31,6 @@ end
 redef class NativeString
        # Get `self` as a `CppString`
        fun to_cpp_string(length: Int): CppString in "C++" `{
-               return new std::string(self, length);
+               return new std::string(reinterpret_cast<char*>(self), length);
        `}
 end
index 2cf47e2..0a4d430 100644 (file)
@@ -623,6 +623,7 @@ abstract class AbstractCompiler
                self.header.add_decl("#include <string.h>")
                self.header.add_decl("#include <sys/types.h>\n")
                self.header.add_decl("#include <unistd.h>\n")
+               self.header.add_decl("#include <stdint.h>\n")
                self.header.add_decl("#include \"gc_chooser.h\"")
                self.header.add_decl("#ifdef ANDROID")
                self.header.add_decl("  #include <android/log.h>")
@@ -1860,13 +1861,13 @@ redef class MClassType
                else if mclass.name == "Bool" then
                        return "short int"
                else if mclass.name == "Char" then
-                       return "char"
+                       return "uint32_t"
                else if mclass.name == "Float" then
                        return "double"
                else if mclass.name == "Byte" then
                        return "unsigned char"
                else if mclass.name == "NativeString" then
-                       return "char*"
+                       return "unsigned char*"
                else if mclass.name == "NativeArray" then
                        return "val*"
                else
@@ -2131,12 +2132,12 @@ redef class AMethPropdef
                                v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
                                return true
                        else if pname == "ascii" then
-                               v.ret(v.new_expr("{arguments[0]}", ret.as(not null)))
+                               v.ret(v.new_expr("(uint32_t){arguments[0]}", ret.as(not null)))
                                return true
                        end
                else if cname == "Char" then
                        if pname == "output" then
-                               v.add("printf(\"%c\", {arguments.first});")
+                               v.add("printf(\"%c\", ((unsigned char){arguments.first}));")
                                return true
                        else if pname == "object_id" then
                                v.ret(v.new_expr("(long){arguments.first}", ret.as(not null)))
@@ -2170,7 +2171,7 @@ redef class AMethPropdef
                                v.ret(v.new_expr("{arguments[0]}-'0'", ret.as(not null)))
                                return true
                        else if pname == "ascii" then
-                               v.ret(v.new_expr("(unsigned char){arguments[0]}", ret.as(not null)))
+                               v.ret(v.new_expr("(long){arguments[0]}", ret.as(not null)))
                                return true
                        end
                else if cname == "Byte" then
@@ -2310,10 +2311,10 @@ redef class AMethPropdef
                        end
                else if cname == "NativeString" then
                        if pname == "[]" then
-                               v.ret(v.new_expr("{arguments[0]}[{arguments[1]}]", ret.as(not null)))
+                               v.ret(v.new_expr("(uint32_t){arguments[0]}[{arguments[1]}]", ret.as(not null)))
                                return true
                        else if pname == "[]=" then
-                               v.add("{arguments[0]}[{arguments[1]}]={arguments[2]};")
+                               v.add("{arguments[0]}[{arguments[1]}]=(unsigned char){arguments[2]};")
                                return true
                        else if pname == "copy_to" then
                                v.add("memmove({arguments[1]}+{arguments[4]},{arguments[0]}+{arguments[3]},{arguments[2]});")
@@ -2325,7 +2326,7 @@ redef class AMethPropdef
                                v.ret(v.new_expr("{arguments[0]} + {arguments[1]}", ret.as(not null)))
                                return true
                        else if pname == "new" then
-                               v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
+                               v.ret(v.new_expr("(unsigned char*)nit_alloc({arguments[1]})", ret.as(not null)))
                                return true
                        end
                else if cname == "NativeArray" then
@@ -2339,7 +2340,7 @@ redef class AMethPropdef
                        v.ret(v.new_expr("glob_sys", ret.as(not null)))
                        return true
                else if pname == "calloc_string" then
-                       v.ret(v.new_expr("(char*)nit_alloc({arguments[1]})", ret.as(not null)))
+                       v.ret(v.new_expr("(unsigned char*)nit_alloc({arguments[1]})", ret.as(not null)))
                        return true
                else if pname == "calloc_array" then
                        v.calloc_array(ret.as(not null), arguments)
index 01579b0..1f59771 100644 (file)
@@ -43,6 +43,7 @@ redef class MModule
                ensure_compile_nitni_base(v)
 
                nitni_ccu.header_c_types.add("#include \"{c_name}._ffi.h\"\n")
+               nitni_ccu.header_c_types.add("#include <stdint.h>\n")
                nitni_ccu.header_c_types.add """
 extern void nitni_global_ref_incr(void*);
 extern void nitni_global_ref_decr(void*);
index 31a4f20..f50a2a7 100644 (file)
@@ -1193,7 +1193,7 @@ class SeparateCompilerVisitor
                                if mtype.name == "Int" then
                                        return self.new_expr("(long)({value})>>2", mtype)
                                else if mtype.name == "Char" then
-                                       return self.new_expr("(char)((long)({value})>>2)", mtype)
+                                       return self.new_expr("(uint32_t)((long)({value})>>2)", mtype)
                                else if mtype.name == "Bool" then
                                        return self.new_expr("(short int)((long)({value})>>2)", mtype)
                                else
index 3f2a21e..d5852f6 100644 (file)
@@ -480,7 +480,7 @@ redef class MClassType
                if ftype isa ForeignJavaType then return ftype.java_type.
                        replace('/', ".").replace('$', ".").replace(' ', "").replace('\n',"")
                if mclass.name == "Bool" then return "boolean"
-               if mclass.name == "Char" then return "char"
+               if mclass.name == "Char" then return "int"
                if mclass.name == "Int" then return "long"
                if mclass.name == "Float" then return "double"
                if mclass.name == "Byte" then return "byte"
@@ -492,7 +492,7 @@ redef class MClassType
                var ftype = mclass.ftype
                if ftype isa ForeignJavaType then return "jobject"
                if mclass.name == "Bool" then return "jboolean"
-               if mclass.name == "Char" then return "jchar"
+               if mclass.name == "Char" then return "jint"
                if mclass.name == "Int" then return "jlong"
                if mclass.name == "Float" then return "jdouble"
                if mclass.name == "Byte" then return "jbyte"
@@ -552,7 +552,7 @@ redef class MClassType
                        return "L{jni_type};"
                end
                if mclass.name == "Bool" then return "Z"
-               if mclass.name == "Char" then return "C"
+               if mclass.name == "Char" then return "I"
                if mclass.name == "Int" then return "J"
                if mclass.name == "Float" then return "D"
                if mclass.name == "Byte" then return "B"
@@ -565,7 +565,7 @@ redef class MClassType
 
                if ftype isa ForeignJavaType then return "Object"
                if mclass.name == "Bool" then return "Boolean"
-               if mclass.name == "Char" then return "Char"
+               if mclass.name == "Char" then return "Int"
                if mclass.name == "Int" then return "Long"
                if mclass.name == "Float" then return "Double"
                if mclass.name == "Byte" then return "Byte"
index 6791e5c..3385301 100644 (file)
@@ -161,10 +161,10 @@ redef class CCompilationUnit
 
                var h_file = "{base_name}.h"
                var guard = "{mmodule.c_name.to_upper}_NIT_H"
-               write_header_to_file(mmodule, "{compdir}/{h_file}", new Array[String], guard)
+               write_header_to_file(mmodule, "{compdir}/{h_file}", ["<stdint.h>"], guard)
 
                var c_file = "{base_name}.c"
-               write_body_to_file(mmodule, "{compdir}/{c_file}", ["<stdlib.h>", "<stdio.h>", "\"{h_file}\""])
+               write_body_to_file(mmodule, "{compdir}/{c_file}", ["<stdlib.h>", "<stdio.h>", "<stdint.h>", "\"{h_file}\""])
 
                files.add( "{compdir}/{c_file}" )
        end
index 7cf6bbc..cc30171 100644 (file)
@@ -89,11 +89,11 @@ redef class MClassType
        do
                var name = mclass.name
                if name == "Bool" then return "int"
-               if name == "Char" then return "char"
+               if name == "Char" then return "uint32_t"
                if name == "Float" then return "double"
                if name == "Int" then return "long"
                if name == "Byte" then return "unsigned char"
-               if name == "NativeString" then return "char*"
+               if name == "NativeString" then return "unsigned char*"
                if mclass.kind == extern_kind then
                        var ctype = mclass.ctype
                        assert ctype != null
@@ -105,11 +105,11 @@ redef class MClassType
        redef fun cname_blind do
                var name = mclass.name
                if name == "Bool" then return "int"
-               if name == "Char" then return "char"
+               if name == "Char" then return "uint32_t"
                if name == "Float" then return "double"
                if name == "Int" then return "long"
                if name == "Byte" then return "unsigned char"
-               if name == "NativeString" then return "char*"
+               if name == "NativeString" then return "unsigned char*"
                if mclass.kind == extern_kind then return "void*"
                return "struct nitni_instance *"
        end
index 0d6a211..a732359 100644 (file)
@@ -1457,7 +1457,7 @@ Edge
 8:MPropDef
 13:MAttributeDef
 =properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
 ----
 =to=Entity#0:
 =labels=Array(4):
@@ -1481,7 +1481,7 @@ Edge
 8:MPropDef
 13:MAttributeDef
 =properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
 ----
 =to=Entity#0:
 =labels=Array(4):
@@ -1722,7 +1722,7 @@ Edge
 8:MPropDef
 13:MAttributeDef
 =properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
 
 
 Edge
index 70cda4d..5e63fb7 100644 (file)
@@ -1457,7 +1457,7 @@ Edge
 8:MPropDef
 13:MAttributeDef
 =properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
 ----
 =to=Entity#0:
 =labels=Array(4):
@@ -1481,7 +1481,7 @@ Edge
 8:MPropDef
 13:MAttributeDef
 =properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
 ----
 =to=Entity#0:
 =labels=Array(4):
@@ -1722,7 +1722,7 @@ Edge
 8:MPropDef
 13:MAttributeDef
 =properties=JsonObject(5):
-{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["\u000e2\u00080\u0009cAnswer to the Ultimate Question of Life, the Universe, and Everything.","\u000e2\u00080\u0009c"],"is_intro":true}
+{"location":"%SOURCE_DIRECTORY%\/org\/example\/foo\/C.java:25,1---1,1","visibility":"public","name":"THE_ANSWER","mdoc":["“Answer to the Ultimate Question of Life, the Universe, and Everything.","“"],"is_intro":true}
 
 
 Edge
index 76c1172..16375e3 100644 (file)
@@ -17,7 +17,7 @@ in "C++ Header" `{
 `}
 
 fun print_a(str: String) import String.to_cstring in "C++" `{
-       puts(String_to_cstring(str));
+       puts(reinterpret_cast<char*>(String_to_cstring(str)));
 `}
 
 print_a "Hello from `a`."
index 01eb25c..e4631dc 100644 (file)
@@ -19,7 +19,7 @@ in "C++ header" `{
 `}
 
 fun print_b(str: String) import String.to_cstring in "C++" `{
-       puts(String_to_cstring(str));
+       puts(reinterpret_cast<char*>(String_to_cstring(str)));
 `}
 
 print_a "Hello from `a`."