From 6b52ea3d5ca77dbcc6edbbf71c1aa9419a019bac Mon Sep 17 00:00:00 2001 From: Lucas Bajolet Date: Mon, 1 Jun 2015 14:03:56 -0400 Subject: [PATCH] compiler: Changed types of Char to uint32_t and NativeString to unsigned char* Signed-off-by: Lucas Bajolet --- lib/android/bundle/bundle.nit | 10 +++++----- lib/android/intent/intent_api10.nit | 4 ++-- lib/cpp.nit | 2 +- src/compiler/abstract_compiler.nit | 19 ++++++++++--------- src/compiler/compiler_ffi/light.nit | 1 + src/compiler/separate_compiler.nit | 2 +- src/ffi/java.nit | 8 ++++---- src/ffi/light_ffi_base.nit | 4 ++-- src/nitni/nitni_base.nit | 8 ++++---- tests/sav/neo_doxygen_dump_args4.res | 6 +++--- tests/sav/neo_doxygen_dump_args5.res | 6 +++--- tests/test_ffi_cpp_duplicated_callback_a.nit | 2 +- tests/test_ffi_cpp_duplicated_callback_b.nit | 2 +- 13 files changed, 38 insertions(+), 36 deletions(-) diff --git a/lib/android/bundle/bundle.nit b/lib/android/bundle/bundle.nit index edb0c88..76babb4 100644 --- a/lib/android/bundle/bundle.nit +++ b/lib/android/bundle/bundle.nit @@ -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; `} diff --git a/lib/android/intent/intent_api10.nit b/lib/android/intent/intent_api10.nit index 2d536fd..8c6a928 100644 --- a/lib/android/intent/intent_api10.nit +++ b/lib/android/intent/intent_api10.nit @@ -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); `} diff --git a/lib/cpp.nit b/lib/cpp.nit index a2896d3..c64ae33 100644 --- a/lib/cpp.nit +++ b/lib/cpp.nit @@ -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(self), length); `} end diff --git a/src/compiler/abstract_compiler.nit b/src/compiler/abstract_compiler.nit index 2cf47e2..0a4d430 100644 --- a/src/compiler/abstract_compiler.nit +++ b/src/compiler/abstract_compiler.nit @@ -623,6 +623,7 @@ abstract class AbstractCompiler self.header.add_decl("#include ") self.header.add_decl("#include \n") self.header.add_decl("#include \n") + self.header.add_decl("#include \n") self.header.add_decl("#include \"gc_chooser.h\"") self.header.add_decl("#ifdef ANDROID") self.header.add_decl(" #include ") @@ -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) diff --git a/src/compiler/compiler_ffi/light.nit b/src/compiler/compiler_ffi/light.nit index 01579b0..1f59771 100644 --- a/src/compiler/compiler_ffi/light.nit +++ b/src/compiler/compiler_ffi/light.nit @@ -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 \n") nitni_ccu.header_c_types.add """ extern void nitni_global_ref_incr(void*); extern void nitni_global_ref_decr(void*); diff --git a/src/compiler/separate_compiler.nit b/src/compiler/separate_compiler.nit index 31a4f20..f50a2a7 100644 --- a/src/compiler/separate_compiler.nit +++ b/src/compiler/separate_compiler.nit @@ -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 diff --git a/src/ffi/java.nit b/src/ffi/java.nit index 3f2a21e..d5852f6 100644 --- a/src/ffi/java.nit +++ b/src/ffi/java.nit @@ -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" diff --git a/src/ffi/light_ffi_base.nit b/src/ffi/light_ffi_base.nit index 6791e5c..3385301 100644 --- a/src/ffi/light_ffi_base.nit +++ b/src/ffi/light_ffi_base.nit @@ -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}", [""], guard) var c_file = "{base_name}.c" - write_body_to_file(mmodule, "{compdir}/{c_file}", ["", "", "\"{h_file}\""]) + write_body_to_file(mmodule, "{compdir}/{c_file}", ["", "", "", "\"{h_file}\""]) files.add( "{compdir}/{c_file}" ) end diff --git a/src/nitni/nitni_base.nit b/src/nitni/nitni_base.nit index 7cf6bbc..cc30171 100644 --- a/src/nitni/nitni_base.nit +++ b/src/nitni/nitni_base.nit @@ -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 diff --git a/tests/sav/neo_doxygen_dump_args4.res b/tests/sav/neo_doxygen_dump_args4.res index 0d6a211..a732359 100644 --- a/tests/sav/neo_doxygen_dump_args4.res +++ b/tests/sav/neo_doxygen_dump_args4.res @@ -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 diff --git a/tests/sav/neo_doxygen_dump_args5.res b/tests/sav/neo_doxygen_dump_args5.res index 70cda4d..5e63fb7 100644 --- a/tests/sav/neo_doxygen_dump_args5.res +++ b/tests/sav/neo_doxygen_dump_args5.res @@ -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 diff --git a/tests/test_ffi_cpp_duplicated_callback_a.nit b/tests/test_ffi_cpp_duplicated_callback_a.nit index 76c1172..16375e3 100644 --- a/tests/test_ffi_cpp_duplicated_callback_a.nit +++ b/tests/test_ffi_cpp_duplicated_callback_a.nit @@ -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(String_to_cstring(str))); `} print_a "Hello from `a`." diff --git a/tests/test_ffi_cpp_duplicated_callback_b.nit b/tests/test_ffi_cpp_duplicated_callback_b.nit index 01eb25c..e4631dc 100644 --- a/tests/test_ffi_cpp_duplicated_callback_b.nit +++ b/tests/test_ffi_cpp_duplicated_callback_b.nit @@ -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(String_to_cstring(str))); `} print_a "Hello from `a`." -- 1.7.9.5