comparison generator/cppimplgenerator.cpp @ 33:cf8a415f3f32

gui package is fully wrapped now. lots of fixes in the generator
author eldar
date Sat, 16 May 2009 17:12:06 +0000
parents e747af2e6b46
children 17cc45b32686
comparison
equal deleted inserted replaced
32:a293cd68d56c 33:cf8a415f3f32
494 // initializing 494 // initializing
495 AbstractMetaClassList interfaces = java_class->interfaces(); 495 AbstractMetaClassList interfaces = java_class->interfaces();
496 if (!interfaces.isEmpty()) { 496 if (!interfaces.isEmpty()) {
497 for (int i=0; i<interfaces.size(); ++i) { 497 for (int i=0; i<interfaces.size(); ++i) {
498 AbstractMetaClass *iface = interfaces.at(i); 498 AbstractMetaClass *iface = interfaces.at(i);
499 s << "extern \"C\" DLL_PUBLIC " << iface->qualifiedCppName() << "* qtd_" << java_class->name() << "_cast_to_" << iface->qualifiedCppName() 499
500 << "(" << java_class->name() << " *ptr)" << endl << "{" << endl; 500 // in case of renamed class
501 InterfaceTypeEntry *ite = static_cast<InterfaceTypeEntry*>(iface->typeEntry());
502 QString real_name = ite->origin()->qualifiedCppName();
503
504
505
506 /*const TypeEntry* te = TypeDatabase::instance()->findType();
507 if(te)
508 real_name = te->qualifiedCppName();
509 else
510 real_name = iface->qualifiedCppName();*/
511
512 s << "extern \"C\" DLL_PUBLIC " << real_name << "* qtd_" << java_class->name() << "_cast_to_" << iface->qualifiedCppName()
513 << "(" << java_class->qualifiedCppName() << " *ptr)" << endl << "{" << endl;
501 Indentation indent(INDENT); 514 Indentation indent(INDENT);
502 s << INDENT << "return dynamic_cast<"<< iface->qualifiedCppName()<< "*>(ptr);" << endl; 515 s << INDENT << "return dynamic_cast<" << real_name << "*>(ptr);" << endl;
503 s << "}" << endl << endl; 516 s << "}" << endl << endl;
504 } 517 }
505 } 518 }
506 } 519 }
507 520
1913 1926
1914 QString arg_name = argument->indexedName(); 1927 QString arg_name = argument->indexedName();
1915 1928
1916 if (nativeArgCount > 0) 1929 if (nativeArgCount > 0)
1917 s << "," << endl << " "; 1930 s << "," << endl << " ";
1918 // if has QString argument we have to pass char* and str.length to QString constructor 1931 // if QString argument we have to pass DArrat
1919 if (argument->type()->isTargetLangString() 1932 if ((te && te->qualifiedCppName() == "QString") || d_type->isTargetLangString())
1920 || (argument->type()->typeEntry() && argument->type()->typeEntry()->qualifiedCppName() == "QString")) { 1933 s << "string " << arg_name;
1921 s << QString("char* %1, uint %1_size").arg(arg_name); 1934 else if (d_type->isContainer()) {
1922 } else if (d_type->isContainer()) {
1923 const ContainerTypeEntry *cte = 1935 const ContainerTypeEntry *cte =
1924 static_cast<const ContainerTypeEntry *>(te); 1936 static_cast<const ContainerTypeEntry *>(te);
1925 if(isLinearContainer(cte)) 1937 if(isLinearContainer(cte))
1926 s << QString("void *%1, size_t %1_size").arg(arg_name); 1938 s << QString("void *%1, size_t %1_size").arg(arg_name);
1927 } else { 1939 } else {
2107 } 2119 }
2108 2120
2109 } else { 2121 } else {
2110 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option, 2122 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option,
2111 extra_param); 2123 extra_param);
2112 /* qtd 2124 }
2113 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) -> leaving: " 2125 writeRefArguments(s, java_function);
2114 << java_function_signature << "\");" << endl;
2115 */
2116 }
2117 } 2126 }
2118 } 2127 }
2119 if(!java_function->argumentReplaced(0).isEmpty()) { 2128 if(!java_function->argumentReplaced(0).isEmpty()) {
2120 s << INDENT << "return 0;" << endl; 2129 s << INDENT << "return 0;" << endl;
2121 } 2130 }
2122 2131
2123 s << endl << "}"; 2132 s << endl << "}";
2124 s << endl << endl; 2133 s << endl << endl;
2125 } 2134 }
2135
2136 void CppImplGenerator::writeRefArguments(QTextStream &s, const AbstractMetaFunction *java_function)
2137 {
2138 AbstractMetaArgumentList arguments = java_function->arguments();
2139 foreach (const AbstractMetaArgument *argument, arguments) {
2140 AbstractMetaType *d_type = argument->type();
2141 const TypeEntry *te = d_type->typeEntry();
2142 if ((te && d_type->isNativePointer() && te->name() == "QString"))
2143 s << QString(" _d_toUtf8(__qt_%1.utf16(), __qt_%1.size(), &%1);").arg(argument->indexedName()) << endl;
2144 }
2145 }
2146
2126 2147
2127 void CppImplGenerator::writeAssignment(QTextStream &s, const QString &destName, const QString &srcName, 2148 void CppImplGenerator::writeAssignment(QTextStream &s, const QString &destName, const QString &srcName,
2128 const AbstractMetaType *java_type) 2149 const AbstractMetaType *java_type)
2129 { 2150 {
2130 if (java_type->isArray()) { 2151 if (java_type->isArray()) {
2194 else 2215 else
2195 qt_return_value += getter->name() + "_getter()"; 2216 qt_return_value += getter->name() + "_getter()";
2196 s << qt_return_value << ";" << endl; 2217 s << qt_return_value << ";" << endl;
2197 2218
2198 writeQtToJava(s, getter->type(), tmp_name, java_return_value, 0, -1, EnumAsInts); 2219 writeQtToJava(s, getter->type(), tmp_name, java_return_value, 0, -1, EnumAsInts);
2199 if (getter->type()->isTargetLangString()) 2220 if (getter->type()->isTargetLangString() || getter->type()->name() == "QModelIndex")
2200 ; 2221 ;
2201 else if(getter->type()->typeEntry()->isStructInD()) 2222 else if(getter->type()->typeEntry()->isStructInD())
2202 s << INDENT << "return " << tmp_name << ";" << endl; 2223 s << INDENT << "return " << tmp_name << ";" << endl;
2203 else 2224 else
2204 s << INDENT << "return " << java_return_value << ";" << endl; 2225 s << INDENT << "return " << java_return_value << ";" << endl;
2248 dest = shellClassName(setter->ownerClass()) + "::"; 2269 dest = shellClassName(setter->ownerClass()) + "::";
2249 else 2270 else
2250 dest = "__qt_object->"; 2271 dest = "__qt_object->";
2251 2272
2252 QString src; 2273 QString src;
2253 if (!argument->type()->isPrimitive() && !argument->type()->typeEntry()->isStructInD()) 2274 if (!argument->type()->isPrimitive() && !argument->type()->typeEntry()->isStructInD()) {
2254 src = "__qt_" + argument->indexedName(); 2275 src = "__qt_" + argument->indexedName();
2255 else 2276 } else if (argument->type()->name() == "QModelIndex") {
2277 src = "qtd_to_QModelIndex(" + argument->indexedName() + ")";
2278 } else
2256 src = argument->indexedName(); 2279 src = argument->indexedName();
2257 2280
2258 if (setter->wasPublic()) 2281 if (setter->wasPublic())
2259 writeAssignment(s, dest + java_field->name(), src, argument->type()); 2282 writeAssignment(s, dest + java_field->name(), src, argument->type());
2260 else 2283 else
2687 << java_name << ");" << endl; 2710 << java_name << ");" << endl;
2688 } else if (java_type->typeEntry()->isStructInD()) { 2711 } else if (java_type->typeEntry()->isStructInD()) {
2689 // empty 2712 // empty
2690 } else if (java_type->typeEntry() && java_type->typeEntry()->qualifiedCppName() == "QString") { 2713 } else if (java_type->typeEntry() && java_type->typeEntry()->qualifiedCppName() == "QString") {
2691 s << INDENT << "QString " << qt_name 2714 s << INDENT << "QString " << qt_name
2692 << " = " << "QString::fromUtf8(" << java_name << ", " << java_name << "_size);" << endl; 2715 << " = " << QString("QString::fromUtf8((const char *)%1.ptr, %1.length);").arg(java_name) << endl;
2693 } else if (java_type->isJObjectWrapper()) { 2716 } else if (java_type->isJObjectWrapper()) {
2694 s << INDENT << "JObjectWrapper " << qt_name 2717 s << INDENT << "JObjectWrapper " << qt_name
2695 << " = qtjambi_to_jobjectwrapper(__jni_env, " << java_name << ");" << endl; 2718 << " = qtjambi_to_jobjectwrapper(__jni_env, " << java_name << ");" << endl;
2696 } else if (java_type->isVariant()) { 2719 } else if (java_type->isVariant()) {
2697 s << INDENT << "QVariant " << qt_name 2720 s << INDENT << "QVariant " << qt_name
2698 << " = " << java_name << " == NULL ? QVariant() : QVariant(*" << java_name << ");" << endl; 2721 << " = " << java_name << " == NULL ? QVariant() : QVariant(*" << java_name << ");" << endl;
2699 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { 2722 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) {
2700 AbstractMetaType *elementType = java_type->arrayElementType(); 2723 AbstractMetaType *elementType = java_type->arrayElementType();
2701 2724
2702 // ### Don't assert on wrong array lengths 2725 // ### Don't assert on wrong array lengths
2703 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; 2726 // s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl;
2704 s << INDENT; 2727 s << INDENT;
2705 writeTypeInfo(s, elementType); 2728 writeTypeInfo(s, elementType);
2706 s << " " << qt_name << "[" << java_type->arrayElementCount() << "];" << endl; 2729 s << " *" << qt_name << " = (";
2707 2730 writeTypeInfo(s, elementType);
2731 s << "*) " << java_name << ";" << endl;
2732 /*
2708 s << INDENT << "__jni_env->" << getXxxArrayRegion(elementType) << "( (" << translateType(java_type, options) 2733 s << INDENT << "__jni_env->" << getXxxArrayRegion(elementType) << "( (" << translateType(java_type, options)
2709 << ")" << java_name << ", 0, " << java_type->arrayElementCount() << ", " 2734 << ")" << java_name << ", 0, " << java_type->arrayElementCount() << ", "
2710 << "(" << translateType(elementType, options) << " *" << ")" 2735 << "(" << translateType(elementType, options) << " *" << ")"
2711 << qt_name << ");" << endl; 2736 << qt_name << ");" << endl;
2712 2737 */
2713 } else if (java_type->isArray()) { 2738 } else if (java_type->isArray()) {
2714 AbstractMetaType *elementType = java_type->arrayElementType(); 2739 AbstractMetaType *elementType = java_type->arrayElementType();
2715 2740
2716 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; 2741 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl;
2717 writeTypeInfo(s, elementType); 2742 writeTypeInfo(s, elementType);
2749 s << INDENT << qualified_name << " " << qt_name 2774 s << INDENT << qualified_name << " " << qt_name
2750 << " = (" << qualified_name << ") "; 2775 << " = (" << qualified_name << ") ";
2751 } 2776 }
2752 2777
2753 if ((options & EnumAsInts) == 0 && (java_type->isTargetLangEnum() || java_type->isTargetLangFlags())) { 2778 if ((options & EnumAsInts) == 0 && (java_type->isTargetLangEnum() || java_type->isTargetLangFlags())) {
2754 s << "qtjambi_to_enumerator(__jni_env, " << java_name << ");" << endl; 2779 s << java_name << ";" << endl;
2755 2780
2756 } else if (options & BoxedPrimitive) { 2781 } else if (options & BoxedPrimitive) {
2757 const PrimitiveTypeEntry *pentry = TypeDatabase::instance()->findTargetLangPrimitiveType("int"); 2782 const PrimitiveTypeEntry *pentry = TypeDatabase::instance()->findTargetLangPrimitiveType("int");
2758 Q_ASSERT(pentry); 2783 Q_ASSERT(pentry);
2759 2784
2959 } else if(java_type->typeEntry()->isStructInD()) { 2984 } else if(java_type->typeEntry()->isStructInD()) {
2960 s << INDENT << java_type->typeEntry()->name() << " *" << java_name << " = (" << java_type->typeEntry()->name() << " *) &" 2985 s << INDENT << java_type->typeEntry()->name() << " *" << java_name << " = (" << java_type->typeEntry()->name() << " *) &"
2961 << qt_name << ";" << endl; // do nothing 2986 << qt_name << ";" << endl; // do nothing
2962 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { 2987 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) {
2963 AbstractMetaType *elementType = java_type->arrayElementType(); 2988 AbstractMetaType *elementType = java_type->arrayElementType();
2964 2989 /* qtd
2965 s << INDENT << translateType(java_type, option) << " " << java_name << " = __jni_env->" << newXxxArray(elementType) 2990 s << INDENT << translateType(java_type, option) << " " << java_name << " = __jni_env->" << newXxxArray(elementType)
2966 << "(" << java_type->arrayElementCount() << ");" << endl; 2991 << "(" << java_type->arrayElementCount() << ");" << endl;
2967 2992
2968 s << INDENT << "__jni_env->" << setXxxArrayRegion(elementType) << "(" 2993 s << INDENT << "__jni_env->" << setXxxArrayRegion(elementType) << "("
2969 << "(" << translateType(java_type, option) << ")" << java_name 2994 << "(" << translateType(java_type, option) << ")" << java_name
2970 << ", 0, " << java_type->arrayElementCount() << ", " 2995 << ", 0, " << java_type->arrayElementCount() << ", "
2971 << "(" << translateType(elementType, option) << " *" << ")" 2996 << "(" << translateType(elementType, option) << " *" << ")"
2972 << qt_name << ");" << endl; 2997 << qt_name << ");" << endl;
2973 2998 */
2999 s << INDENT << translateType(java_type, option) << " " << java_name << " = " << qt_name << ";" <<endl;
2974 } else if (java_type->isArray()) { 3000 } else if (java_type->isArray()) {
2975 AbstractMetaType *elementType = java_type->arrayElementType(); 3001 AbstractMetaType *elementType = java_type->arrayElementType();
2976 3002
2977 s << INDENT << "jobject " << java_name << " = __jni_env->NewObjectArray(" 3003 s << INDENT << "jobject " << java_name << " = __jni_env->NewObjectArray("
2978 << java_type->arrayElementCount() << ");" << endl; 3004 << java_type->arrayElementCount() << ");" << endl;
3361 3387
3362 s << INDENT << "for (int i=0; i<" << java_name << "_size; ++i) {" << endl; 3388 s << INDENT << "for (int i=0; i<" << java_name << "_size; ++i) {" << endl;
3363 { 3389 {
3364 Indentation indent(INDENT); 3390 Indentation indent(INDENT);
3365 if(targ->isTargetLangString()) 3391 if(targ->isTargetLangString())
3366 s << INDENT << "char* __d_element;" << endl 3392 s << INDENT << "string __d_element;" << endl
3367 << INDENT << "size_t __d_element_size;" << endl 3393 << INDENT << "qtd_get_string_from_array(" << java_name << ", i, &__d_element);" << endl;
3368 << INDENT << "qtd_get_string_from_array(" << java_name << ", i, &__d_element, &__d_element_size);" << endl;
3369 else { 3394 else {
3370 s << INDENT; 3395 s << INDENT;
3371 writeTypeInfo(s, targ, Option(VirtualDispatch | ForcePointer)); 3396 writeTypeInfo(s, targ, Option(VirtualDispatch | ForcePointer | EnumAsInts));
3372 QString cast_string = ""; 3397 QString cast_string = "";
3373 const TypeEntry* centry = targ->typeEntry(); 3398 const TypeEntry* centry = targ->typeEntry();
3374 if (centry->isComplex() && (centry->isObject() || centry->isValue() || centry->isInterface())) 3399 if (centry->isComplex() && (centry->isObject() || centry->isValue() || centry->isInterface()))
3375 cast_string = "(void**)"; 3400 cast_string = "(void**)";
3376 s << "__d_element;" << endl 3401 s << "__d_element;" << endl
3617 3642
3618 if (java_type->isPrimitive() 3643 if (java_type->isPrimitive()
3619 || java_type->isTargetLangString() 3644 || java_type->isTargetLangString()
3620 || java_type->isVariant() 3645 || java_type->isVariant()
3621 || java_type->isJObjectWrapper() 3646 || java_type->isJObjectWrapper()
3622 || java_type->isTargetLangChar() 3647 || java_type->isTargetLangChar()) {
3623 || java_type->isArray()) {
3624 return d_name; 3648 return d_name;
3649 } else if (java_type->isArray()) {
3650 return java_type->arrayElementType()->name() + "*";
3625 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags() 3651 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags()
3626 || ((option & EnumAsInts) && (java_type->isEnum() || java_type->isFlags()))) { 3652 || ((option & EnumAsInts) && (java_type->isEnum() || java_type->isFlags()))) {
3627 return "int"; 3653 return "int";
3628 } else if (java_type->isReference()) { 3654 } else if (java_type->isReference()) {
3629 if (java_type->typeEntry()->isValue()) 3655 if (java_type->typeEntry()->isValue())
3630 return "void*"; 3656 return "void*";
3631 else 3657 else
3632 return d_name + " "+ QString(java_type->actualIndirections(), '*'); 3658 return d_name + " "+ QString(java_type->actualIndirections(), '*');
3633 } else if (java_type->isNativePointer()) { 3659 } else if (java_type->isNativePointer()) {
3634 if (java_type->typeEntry()->isValue()) 3660 if (java_type->typeEntry()->isValue())
3635 return "void*"; 3661 return "void*";
3636 else if (java_type->typeEntry()->isEnum() && d_export) 3662 else if (java_type->typeEntry()->isEnum() && d_export)
3637 return "int" + QString(java_type->indirections(), '*'); 3663 return "int" + QString(java_type->indirections(), '*');
3638 else 3664 else
3639 return d_name + QString(java_type->indirections(), '*'); 3665 return d_name + QString(java_type->indirections(), '*');
3640 } else { 3666 } else {
3641 return d_name + QString(java_type->indirections(), '*'); 3667 return d_name + QString(java_type->indirections(), '*');
3642 } 3668 }
3643 } 3669 }
3644 3670
3645 void CppImplGenerator::writeExtraIncludes(QTextStream &s, const AbstractMetaClass *java_class) 3671 void CppImplGenerator::writeExtraIncludes(QTextStream &s, const AbstractMetaClass *java_class)
3646 { 3672 {
3647 IncludeList includes = java_class->typeEntry()->extraIncludes(); 3673 IncludeList includes = java_class->typeEntry()->extraIncludes();