comparison generator/cppimplgenerator.cpp @ 1:e78566595089

initial import
author mandel
date Mon, 11 May 2009 16:01:50 +0000
parents
children deb0cc1d053d
comparison
equal deleted inserted replaced
0:36fb74dc547d 1:e78566595089
1 /****************************************************************************
2 **
3 ** Copyright (C) 1992-2008 Nokia. All rights reserved.
4 **
5 ** This file is part of Qt Jambi.
6 **
7 ** * Commercial Usage
8 * Licensees holding valid Qt Commercial licenses may use this file in
9 * accordance with the Qt Commercial License Agreement provided with the
10 * Software or, alternatively, in accordance with the terms contained in
11 * a written agreement between you and Nokia.
12 *
13 *
14 * GNU General Public License Usage
15 * Alternatively, this file may be used under the terms of the GNU
16 * General Public License versions 2.0 or 3.0 as published by the Free
17 * Software Foundation and appearing in the file LICENSE.GPL included in
18 * the packaging of this file. Please review the following information
19 * to ensure GNU General Public Licensing requirements will be met:
20 * http://www.fsf.org/licensing/licenses/info/GPLv2.html and
21 * http://www.gnu.org/copyleft/gpl.html. In addition, as a special
22 * exception, Nokia gives you certain additional rights. These rights
23 * are described in the Nokia Qt GPL Exception version 1.2, included in
24 * the file GPL_EXCEPTION.txt in this package.
25 *
26 * Qt for Windows(R) Licensees
27 * As a special exception, Nokia, as the sole copyright holder for Qt
28 * Designer, grants users of the Qt/Eclipse Integration plug-in the
29 * right for the Qt/Eclipse Integration to link to functionality
30 * provided by Qt Designer and its related libraries.
31 *
32 *
33 * If you are unsure which license is appropriate for your use, please
34 * contact the sales department at qt-sales@nokia.com.
35
36 **
37 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
38 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
39 **
40 ****************************************************************************/
41
42 #include "cppimplgenerator.h"
43 #include "dgenerator.h"
44 #include "reporthandler.h"
45 #include <qnativepointer.h>
46
47 #include <QDir>
48 #include <QtDebug>
49 #include <QVariant>
50 #include <iostream>
51 #define VOID_POINTER_ORDINAL 8
52
53 static Indentor INDENT;
54
55 QString jni_signature(const AbstractMetaFunction *function, JNISignatureFormat format)
56 {
57 QString returned = "(";
58 AbstractMetaArgumentList arguments = function->arguments();
59 foreach (const AbstractMetaArgument *argument, arguments) {
60 if (!function->argumentRemoved(argument->argumentIndex() + 1)) {
61 QString modified_type = function->typeReplaced(argument->argumentIndex()+1);
62
63 if (modified_type.isEmpty())
64 returned += jni_signature(argument->type(), format);
65 else
66 returned += jni_signature(modified_type, format);
67 }
68 }
69
70 returned += ")";
71
72 QString modified_type = function->typeReplaced(0);
73 if (modified_type.isEmpty())
74 returned += jni_signature(function->type(), format);
75 else
76 returned += jni_signature(modified_type, format);
77
78 return returned;
79 }
80
81 QString jni_signature(const QString &_full_name, JNISignatureFormat format)
82 {
83 QString signature;
84 QString full_name = _full_name;
85
86 if (full_name.endsWith("[]")) {
87 full_name.chop(2);
88 signature = "[";
89 }
90
91 int start = 0, end = -1;
92 while ( (start = full_name.indexOf("<")) >= 0 && (end = full_name.indexOf(">")) >= 0 ) {
93 full_name.remove(start, end - start + 1);
94 }
95
96 static QHash<QString, QString> table;
97 if (table.isEmpty()) {
98 table["boolean"] = "Z";
99 table["byte"] = "B";
100 table["char"] = "C";
101 table["short"] = "S";
102 table["int"] = "I";
103 table["long"] = "J";
104 table["float"] = "F";
105 table["double"] = "D";
106 }
107
108 if (format == Underscores)
109 signature.replace("[", "_3");
110
111 if (table.contains(full_name)) {
112 signature += table[full_name];
113 } else if (format == Underscores) {
114 signature.replace("[", "_3");
115 signature += "L";
116 signature += QString(full_name).replace("_", "_1").replace('.', '_').replace("$", "_00024");
117 signature += "_2";
118 } else {
119 signature += "L";
120 signature += QString(full_name).replace('.', '/');
121 signature += ";";
122 }
123
124 return signature;
125 }
126
127 QString jni_signature(const AbstractMetaType *java_type, JNISignatureFormat format)
128 {
129 if (!java_type)
130 return "V";
131
132 if (java_type->isArray()) {
133 return "_3" + jni_signature(java_type->arrayElementType(), format);
134 } else if (java_type->isNativePointer()) {
135 if (format == Underscores)
136 return "Lcom_trolltech_qt_QNativePointer_2";
137 else
138 return "Lcom/trolltech/qt/QNativePointer;";
139 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags()
140 || (format == Underscores && (java_type->isEnum() || java_type->isFlags()))) {
141 return "I";
142 } else if (java_type->isThread()) {
143 if (format == Underscores)
144 return "Ljava_lang_Thread_2";
145 else
146 return "Ljava/lang/Thread;";
147 }
148
149
150
151 QString name = java_type->name();
152 if (java_type->isObject()) {
153 if (const InterfaceTypeEntry *ie
154 = static_cast<const ObjectTypeEntry *>(java_type->typeEntry())->designatedInterface())
155 name = ie->targetLangName();
156 } else if (java_type->isTargetLangEnum()) {
157 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry());
158 name = et->javaQualifier() + "$" + et->targetLangName();
159
160 } else if (java_type->isTargetLangFlags()) {
161 const FlagsTypeEntry *ft = static_cast<const FlagsTypeEntry *>(java_type->typeEntry());
162 name = ft->originator()->javaQualifier() + "$" + ft->targetLangName();
163 }
164
165 return jni_signature( (java_type->package().isEmpty() ? QString() : java_type->package() + ".") + name, format);
166 }
167
168 static QHash<QString, QString> table;
169 QString default_return_statement_qt(const AbstractMetaType *java_type, Generator::Option options = Generator::NoOption)
170 {
171 QString returnStr = ((options & Generator::NoReturnStatement) == 0 ? "return" : "");
172 if (!java_type)
173 return returnStr;
174
175 if (table.isEmpty()) {
176 table["bool"] = "false";
177 table["byte"] = "0";
178 table["char"] = "0";
179 table["short"] = "0";
180 table["int"] = "0";
181 table["long"] = "0";
182 table["float"] = "0f";
183 table["double"] = "0.0";
184 table["java.lang.Object"] = "0";
185 }
186
187 QString signature = table.value(java_type->typeEntry()->targetLangName());
188
189 if (!signature.isEmpty())
190 return returnStr + " " + signature;
191
192 Q_ASSERT(!java_type->isPrimitive());
193 if (java_type->isJObjectWrapper())
194 return returnStr + " JObjectWrapper()";
195 if (java_type->isVariant())
196 return returnStr + " QVariant()";
197 if (java_type->isTargetLangString())
198 return returnStr + " QString()";
199 if (java_type->isTargetLangChar())
200 return returnStr + " QChar()";
201 else if (java_type->isEnum())
202 return returnStr + " " + java_type->typeEntry()->name() + "(0)";
203 else if (java_type->isContainer() && ((ContainerTypeEntry *)java_type->typeEntry())->type() == ContainerTypeEntry::StringListContainer)
204 return returnStr + " " + java_type->typeEntry()->name() + "()";
205 else if (java_type->isValue() || java_type->isContainer())
206 return returnStr + " " + java_type->cppSignature() + "()";
207 else
208 return returnStr + " 0";
209 }
210
211 QString default_return_statement_java(const AbstractMetaType *java_type)
212 {
213 if (!java_type)
214 return "return";
215 if (java_type->isArray())
216 return "return null";
217
218 if (table.isEmpty()) {
219 table["boolean"] = "false";
220 table["byte"] = "0";
221 table["char"] = "0";
222 table["short"] = "0";
223 table["int"] = "0";
224 table["long"] = "0";
225 table["float"] = "0f";
226 table["double"] = "0.0";
227 table["java.lang.Object"] = "0";
228 }
229
230 QString signature = table.value(java_type->typeEntry()->targetLangName());
231 if (!signature.isEmpty())
232 return "return " + signature;
233
234 Q_ASSERT(!java_type->isPrimitive());
235 return "return 0";
236 }
237
238 /* Used to decide how which of the Call[Xxx]Method functions to call
239 */
240 QByteArray jniTypeName(const QString &name) {
241 static QHash<QString, const char *> table;
242 if (table.isEmpty()) {
243 table["jboolean"] = "Boolean";
244 table["jbyte"] = "Byte";
245 table["jchar"] = "Char";
246 table["jshort"] = "Short";
247 table["jint"] = "Int";
248 table["jlong"] = "Long";
249 table["jfloat"] = "Float";
250 table["jdouble"] = "Double";
251 table["jobject"] = "Object";
252 }
253
254 return table[name];
255 }
256
257 QByteArray jniName(const QString &name) {
258 TypeEntry *entry = TypeDatabase::instance()->findType(name);
259 if (entry)
260 return entry->name().toLatin1();
261 else
262 return "void *";
263 }
264
265 QString CppImplGenerator::jniReturnName(const AbstractMetaFunction *java_function, uint options, bool d_export)
266 {
267 QString return_type = translateType(java_function->type(), EnumAsInts, d_export);
268 QString new_return_type = java_function->typeReplaced(0);
269 if (!new_return_type.isEmpty()) {
270 return_type = jniName(new_return_type);
271 }
272
273 // qtd
274 AbstractMetaType *f_type = java_function->type();
275 if (f_type) {
276 if (f_type->name() == "QModelIndex")
277 return_type = "void";
278 else if (f_type->typeEntry()->isStructInD())
279 return_type = f_type->typeEntry()->qualifiedCppName();
280 else if (f_type->isObject() || f_type->isReference() || f_type->isValue() || f_type->isQObject())
281 return_type = "void*";
282 if (f_type->isVariant())
283 return_type = "void*";
284 }
285
286 if (options & CppImplGenerator::ExternC && java_function->isConstructor())
287 return_type = "void*";
288 if (options & CppImplGenerator::ExternC && f_type)
289 if (f_type->isTargetLangString() || f_type->typeEntry()->isContainer())
290 return_type = "void";
291 // qtd end
292
293 return return_type;
294 }
295
296 QString CppImplGenerator::jniReturnType(const AbstractMetaType *f_type, uint options)
297 {
298 QString return_type = translateType(f_type, EnumAsInts);
299
300 // qtd
301 if (f_type) {
302 if (f_type->typeEntry()->isStructInD())
303 return_type = f_type->typeEntry()->qualifiedCppName();
304 else if (f_type->isObject() || f_type->isReference() || f_type->isValue() || f_type->isQObject())
305 return_type = "void*";
306 if (f_type->isVariant())
307 return_type = "void*";
308 }
309
310 if (options & CppImplGenerator::ExternC && f_type)
311 if (f_type->isTargetLangString())
312 return_type = "void";
313 // qtd end
314 return return_type;
315 }
316
317 QByteArray jniTypeName(const AbstractMetaType *java_type)
318 {
319 if (!java_type) {
320 return "Void";
321 } else if (java_type->isTargetLangChar()) {
322 return "Char";
323 } else if (java_type->isPrimitive()) {
324 return jniTypeName(java_type->typeEntry()->jniName());
325 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags()) {
326 return "Int";
327 } else {
328 return "Object";
329 }
330 }
331
332 QByteArray newXxxArray(const AbstractMetaType *java_type)
333 {
334 return "New" + jniTypeName(java_type) + "Array";
335 }
336
337 QByteArray setXxxArrayElement(const AbstractMetaType *java_type)
338 {
339 Q_ASSERT(java_type);
340 return "Set" + jniTypeName(java_type) + "ArrayElement";
341 }
342
343 QByteArray getXxxArrayElement(const AbstractMetaType *java_type)
344 {
345 Q_ASSERT(java_type);
346 return "Get" + jniTypeName(java_type) + "ArrayElement";
347 }
348
349 QByteArray getXxxArrayRegion(const AbstractMetaType *java_type)
350 {
351 Q_ASSERT(java_type);
352 return "Get" + jniTypeName(java_type) + "ArrayRegion";
353 }
354
355 QByteArray setXxxArrayRegion(const AbstractMetaType *java_type)
356 {
357 Q_ASSERT(java_type);
358 return "Set" + jniTypeName(java_type) + "ArrayRegion";
359 }
360
361 QByteArray callXxxMethod(const AbstractMetaType *java_type)
362 {
363 return "Call" + jniTypeName(java_type) + "Method";
364 }
365
366 QByteArray callXxxMethod(const QString &name) {
367 TypeEntry *entry = TypeDatabase::instance()->findType(name);
368 if (entry && entry->isPrimitive())
369 return "Call" + jniTypeName(entry->jniName()) + "Method";
370 else
371 return "CallObjectMethod";
372 }
373
374 QString jni_function_signature(QString package, QString class_name,
375 const QString &function_name,
376 const QString &return_type,
377 const QString &mangled_arguments = QString(),
378 uint options = CppImplGenerator::StandardJNISignature)
379 {
380 QString s;
381
382 if (options & CppImplGenerator::ExternC)
383 s += "extern \"C\" DLL_PUBLIC ";
384 /* qtd
385 if (options & CppImplGenerator::JNIExport)
386 s += "Q_DECL_EXPORT ";
387 */
388 if (options & CppImplGenerator::ReturnType) {
389 s += return_type;
390 s += " ";
391 }
392 /* qtd
393 if (options & CppImplGenerator::JNIExport)
394 s += "JNICALL QTJAMBI_FUNCTION_PREFIX(";
395
396 s += "Java_";
397
398 s += package.replace(".", "_"); // qtd .replace("_", "_1")
399 s += '_';
400 s += class_name; // qtd .replace("_", "_1");
401 s += '_';
402 */
403 s += QString(function_name); //.replace("_", "_1");
404 // s += mangled_arguments;
405
406 /* qtd
407 if (options & CppImplGenerator::JNIExport)
408 s += ")";
409 */
410 return s;
411 }
412
413 QString CppImplGenerator::fileNameForClass(const AbstractMetaClass *java_class) const
414 {
415 return QString("%1_shell.cpp").arg(java_class->name());
416 }
417
418 void CppImplGenerator::writeSignalFunction(QTextStream &s, const AbstractMetaFunction *signal, const AbstractMetaClass *cls,
419 int pos)
420 {
421 writeFunctionSignature(s, signal, cls, signalWrapperPrefix(),
422 Option(OriginalName | OriginalTypeDescription),
423 "QtJambi_SignalWrapper_");
424 s << endl << "{" << endl;
425 {
426 AbstractMetaArgumentList arguments = signal->arguments();
427 Indentation indent(INDENT);
428
429 if (arguments.size() > 0)
430 s << INDENT << "jvalue arguments[" << arguments.size() << "];" << endl;
431 else
432 s << INDENT << "jvalue *arguments = 0;" << endl;
433 s << INDENT << "JNIEnv *__jni_env = qtjambi_current_environment();" << endl
434 << INDENT << "__jni_env->PushLocalFrame(100);" << endl;
435
436 for (int i=0; i<arguments.size(); ++i) {
437 const AbstractMetaArgument *argument = arguments.at(i);
438 writeQtToJava(s,
439 argument->type(),
440 argument->indexedName(),
441 "__java_" + argument->indexedName(),
442 signal,
443 argument->argumentIndex() + 1,
444 BoxedPrimitive);
445 s << INDENT << "arguments[" << i << "].l = __java_" << argument->indexedName() << ";" << endl;
446 }
447 s << INDENT << "qtjambi_call_java_signal(__jni_env, m_signals[" << pos << "], arguments);"
448 << endl;
449
450 s << INDENT << "__jni_env->PopLocalFrame(0);" << endl;
451
452 if (signal->type() != 0)
453 s << INDENT << default_return_statement_qt(signal->type()) << ";" << endl;
454 }
455 s << "}" << endl << endl;
456
457 if (signal->implementingClass() == signal->ownerClass())
458 writeFinalFunction(s, signal, cls);
459 }
460
461 bool CppImplGenerator::hasCustomDestructor(const AbstractMetaClass *java_class) const
462 {
463 return !java_class->isQObject() && !java_class->typeEntry()->isValue();
464 }
465
466 void writeQtdEntityFunction(QTextStream &s, const AbstractMetaClass *java_class)
467 {
468 /* if(java_class->baseClass())
469 return; */
470 if (!(java_class->typeEntry()->isObject() || java_class->typeEntry()->isQObject()))
471 return;
472 if (!java_class->hasVirtualFunctions())
473 return;
474 /* if (java_class->name() == "QPainterPath_Element") {
475 foreach (AbstractMetaFunction *function, java_class->virtualOverrideFunctions()) {
476 s << function->name() << endl;
477 }
478 }*/
479
480 s << "extern \"C\" DLL_PUBLIC void *__" << java_class->name() << "_entity(void *q_ptr)" << endl;
481 s << "{" << endl;
482 {
483 Indentation indent(INDENT);
484 s << INDENT << "Qtd_QObjectEntity* a = dynamic_cast<Qtd_QObjectEntity*>((" << java_class->qualifiedCppName() << "*)q_ptr);" << endl
485 << INDENT << "if (a != NULL)" << endl
486 << INDENT << " return a->d_entity();" << endl
487 << INDENT << "else" << endl
488 << INDENT << " return NULL;" << endl;
489 }
490 s << "}" << endl << endl;
491 }
492
493 void CppImplGenerator::writeInterfaceCasts(QTextStream &s, const AbstractMetaClass *java_class)
494 {
495 // pointers to native interface objects for classes that implement interfaces
496 // initializing
497 AbstractMetaClassList interfaces = java_class->interfaces();
498 if (!interfaces.isEmpty()) {
499 for (int i=0; i<interfaces.size(); ++i) {
500 AbstractMetaClass *iface = interfaces.at(i);
501 s << "extern \"C\" DLL_PUBLIC " << iface->qualifiedCppName() << "* qtd_" << java_class->name() << "_cast_to_" << iface->qualifiedCppName()
502 << "(" << java_class->name() << " *ptr)" << endl << "{" << endl;
503 Indentation indent(INDENT);
504 s << INDENT << "return dynamic_cast<"<< iface->qualifiedCppName()<< "*>(ptr);" << endl;
505 s << "}" << endl << endl;
506 }
507 }
508 }
509
510 void CppImplGenerator::writeInitCallbacks(QTextStream &s, const AbstractMetaClass *java_class)
511 {
512 QString initArgs = "pfunc_abstr *virts";
513 if (java_class->isQObject())
514 initArgs += ", pfunc_abstr *sigs, pfunc_abstr qobj_del";
515
516 s << "extern \"C\" DLL_PUBLIC void qtd_" << java_class->name()
517 << QString("_initCallBacks(%1) {").arg(initArgs) << endl;
518
519 // virtual functions handlers
520 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions();
521 for (int pos = 0; pos<virtualFunctions.size(); ++pos) {
522 const AbstractMetaFunction *function = virtualFunctions.at(pos);
523 if (!notWrappedYet(function)) { // qtd2
524 s << " " << function->marshalledName() << "_dispatch = "
525 "(pf" << function->marshalledName() << "_dispatch) virts[" << pos << "];" << endl;
526 }
527 }
528
529 // D-side signal callbacks
530 AbstractMetaFunctionList signal_funcs = signalFunctions(java_class);
531 for(int i = 0; i < signal_funcs.size(); i++)
532 s << " emit_callbacks_" << java_class->name() << "[" << i << "] = (EmitCallback)"
533 "sigs[" << i << "];" << endl;
534
535 if (java_class->isQObject())
536 s << " qtd_D_" << java_class->name() << "_delete = "
537 "(qtd_pf_D_" << java_class->name() << "_delete)qobj_del;" << endl;
538
539
540 s << "}" << endl;
541 }
542
543
544 void CppImplGenerator::write(QTextStream &s, const AbstractMetaClass *java_class)
545 {
546
547 bool shellClass = java_class->generateShellClass();
548
549 // Includes
550 writeExtraIncludes(s, java_class);
551 bool shellInclude = (java_class->generateShellClass()
552 || java_class->queryFunctions(AbstractMetaClass::Signals | AbstractMetaClass::Visible | AbstractMetaClass::NotRemovedFromShell).size() > 0);
553
554 // need to include QPainter for all widgets...
555 {
556 const AbstractMetaClass *qwidget = java_class;
557 while (qwidget && qwidget->name() != "QWidget") {
558 qwidget = qwidget->baseClass();
559 }
560 if (qwidget)
561 s << "#include <QPainter>" << endl << endl;
562 }
563 /*
564 #if defined(QTJAMBI_DEBUG_TOOLS)
565 s << "#include <qtjambidebugtools_p.h>" << endl << endl;
566 #endif
567 */
568 if (shellInclude)
569 s << "#include \"" << java_class->name() << "_shell" << ".h\"" << endl;
570 /* qtd
571 if (java_class->isQObject())
572 s << "#include <qtdynamicmetaobject.h>" << endl;
573 */
574 s << "#include <iostream>" << endl;
575 Include inc = java_class->typeEntry()->include();
576 if (!inc.name.isEmpty()) {
577 s << "#include ";
578 if (inc.type == Include::IncludePath)
579 s << "<";
580 else
581 s << "\"";
582 s << inc.name;
583 if (inc.type == Include::IncludePath)
584 s << ">";
585 else
586 s << "\"";
587 s << endl;
588 }
589 s << endl; // qtd
590 s << "#include \"qtd_core.h\"" << endl
591 << "#include \"ArrayOpsPrimitive.h\"" << endl
592 << "#include \"ArrayOps_qt_core.h\"" << endl;
593
594 // qtd2 hack!!
595 if (java_class->package() == "qt.gui")
596 s << "#include \"ArrayOps_qt_gui.h\"" << endl;
597
598 s << endl;
599
600 writeInterfaceCasts(s, java_class);
601
602 /* qtd2
603 writeShellSignatures(s, java_class);
604
605 writeDefaultConstructedValues(s, java_class);
606
607 if (hasCustomDestructor(java_class)) */
608 writeFinalDestructor(s, java_class);
609
610 if (shellClass) {
611 foreach (AbstractMetaFunction *function, java_class->functions()) {
612 if (function->isConstructor() && !function->isPrivate())
613 writeShellConstructor(s, function);
614 }
615 writeShellDestructor(s, java_class);
616
617 writeQtdEntityFunction(s, java_class);
618
619 if (java_class->isQObject())
620 writeQObjectFunctions(s, java_class);
621
622 // Virtual overrides
623 s << "// Virtual overrides" << endl;
624 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions();
625 for (int pos = 0; pos<virtualFunctions.size(); ++pos) {
626 const AbstractMetaFunction *function = virtualFunctions.at(pos);
627 // qtd writeShellFunction(s, function, java_class, pos);
628 writeShellVirtualFunction(s, function, java_class, pos);
629 }
630
631 if (cpp_shared)
632 writeInitCallbacks(s, java_class);
633
634 // Functions in shell class
635 s << "// Functions in shell class" << endl;
636 AbstractMetaFunctionList shellFunctions = java_class->nonVirtualShellFunctions();
637 for (int i=0; i<shellFunctions.size(); ++i) {
638 const AbstractMetaFunction *function = shellFunctions.at(i);
639 writeShellFunction(s, function, java_class, -1);
640 }
641
642 // Write public overrides for functions that are protected in the base class
643 // so they can be accessed from the native callback
644 s << "// public overrides for functions that are protected in the base class" << endl;
645 AbstractMetaFunctionList public_override_functions = java_class->publicOverrideFunctions();
646 foreach (AbstractMetaFunction *function, public_override_functions) {
647 if(notWrappedYet(function))
648 continue;
649 writePublicFunctionOverride(s, function, java_class);
650 }
651
652 // Write virtual function overries used to decide on static/virtual calls
653 s << "// Write virtual function overries used to decide on static/virtual calls" << endl;
654 AbstractMetaFunctionList virtual_functions = java_class->virtualOverrideFunctions();
655 foreach (const AbstractMetaFunction *function, virtual_functions) {
656 if(notWrappedYet(function))
657 continue;
658 writeVirtualFunctionOverride(s, function, java_class);
659 }
660
661 }
662
663 writeExtraFunctions(s, java_class);
664 /* qtd2
665 writeToStringFunction(s, java_class);
666
667 if (java_class->hasCloneOperator()) {
668 writeCloneFunction(s, java_class);
669 }
670
671 // Signals
672 AbstractMetaFunctionList signal_functions = signalFunctions(java_class);
673 for (int i=0; i<signal_functions.size(); ++i)
674 writeSignalFunction(s, signal_functions.at(i), java_class, i);
675 */
676 s << "// ---externC---" << endl;
677
678 // Native callbacks (all java functions require native callbacks)
679 AbstractMetaFunctionList class_funcs = java_class->functionsInTargetLang();
680 foreach (AbstractMetaFunction *function, class_funcs) {
681 if (!function->isEmptyFunction())
682 writeFinalFunction(s, function, java_class);
683 }
684 s << "// ---externC---end" << endl;
685
686
687 class_funcs = java_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NotRemovedFromTargetLang);
688 foreach (AbstractMetaFunction *function, class_funcs) {
689 if (function->implementingClass() != java_class) {
690 writeFinalFunction(s, function, java_class);
691 }
692 }
693
694 // Field accessors
695 s << "// Field accessors" << endl;
696 foreach (AbstractMetaField *field, java_class->fields()) {
697 if (field->wasPublic() || (field->wasProtected() && !java_class->isFinal()))
698 writeFieldAccessors(s, field);
699 }
700 /*
701 s << "// writeFromNativeFunction" << endl;
702 writeFromNativeFunction(s, java_class);
703
704 if (java_class->isQObject())
705 writeOriginalMetaObjectFunction(s, java_class);
706
707 if (java_class->typeEntry()->isValue())
708 writeFromArrayFunction(s, java_class);
709
710 // generate the __qt_cast_to_Xxx functions
711 if (!java_class->isNamespace() && !java_class->isInterface()) {
712 AbstractMetaClassList interfaces = java_class->interfaces();
713 foreach (AbstractMetaClass *iface, interfaces)
714 writeInterfaceCastFunction(s, java_class, iface);
715 }
716
717 writeSignalInitialization(s, java_class);
718 */
719 // qtd writeJavaLangObjectOverrideFunctions(s, java_class);
720
721 s << endl << endl;
722
723 QString pro_file_name = java_class->package().replace(".", "_") + "/" + java_class->package().replace(".", "_") + ".pri";
724 priGenerator->addSource(pro_file_name, fileNameForClass(java_class));
725 }
726
727 void CppImplGenerator::writeVirtualDispatchFunction(QTextStream &s, const AbstractMetaFunction *function, bool d_export)
728 {
729 uint options2 = ReturnType | ExternC;
730 QString return_type = jniReturnName(function, options2);
731 QString f_name = function->marshalledName() + "_dispatch";
732
733 if(!d_export)
734 s << "extern \"C\" ";
735
736 if (!cpp_shared || d_export) {
737 s << return_type << " " << f_name;
738 writeVirtualDispatchArguments(s, function, d_export);
739 if(!d_export)
740 s << ";";
741 } else if (cpp_shared) {
742 s << "typedef " << return_type << " " << "(*pf" << f_name << ")";
743 writeVirtualDispatchArguments(s, function, false);
744 s << ";" << endl
745 << "pf" << function->marshalledName() << "_dispatch "
746 << function->marshalledName() << "_dispatch;";
747 }
748
749 s << endl;
750 }
751
752 void CppImplGenerator::writeShellVirtualFunction(QTextStream &s, const AbstractMetaFunction *function,
753 const AbstractMetaClass *implementor, int id)
754 {
755 // ----------------------------
756 if(notWrappedYet(function))
757 return;
758
759 AbstractMetaType *f_type = function->type();
760 QString new_return_type = function->typeReplaced(0);
761 bool has_function_type = ((f_type != 0
762 || !new_return_type.isEmpty())
763 && new_return_type != "void");
764
765 writeVirtualDispatchFunction(s, function);
766
767 writeFunctionSignature(s, function, implementor, QString(), OriginalName);
768
769 s << endl
770 << "{" << endl;
771
772 Option options = NoOption;
773
774 Indentation indent(INDENT);
775 //bool static_call = !(options & VirtualCall);
776 //s << "std::cout << \"entering " << function->marshalledName() << " \\n\"; " << endl;
777
778 if (f_type) {
779 if (f_type->isTargetLangString())
780 s << INDENT << "char* ret_str = NULL;" << endl
781 << INDENT << "size_t ret_str_size = 0;" << endl;
782 if (f_type->name() == "QModelIndex")
783 s << INDENT << "QModelIndexAccessor __d_return_value;" << endl;
784 if (f_type->isContainer())
785 s << INDENT << "void* __d_return_value;" << endl
786 << INDENT << "size_t __d_return_value_size;" << endl;
787 }
788
789 AbstractMetaArgumentList arguments = function->arguments();
790 foreach (AbstractMetaArgument *argument, arguments) {
791 if (!function->argumentRemoved(argument->argumentIndex()+1)) {
792 if (!argument->type()->isPrimitive()
793 || !function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) {
794 if(argument->type()->isContainer()) {
795 QString arg_name = argument->indexedName();
796 s << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl
797 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name);
798
799 writeQtToJava(s,
800 argument->type(),
801 arg_name,
802 "__d_" + arg_name,
803 function,
804 argument->argumentIndex() + 1,
805 Option(VirtualDispatch));
806 }
807 }
808 }
809 }
810
811 if ((options & NoReturnStatement) == 0)
812 s << INDENT;
813 /* qtd if (function->isAbstract() && static_call) {
814 s << default_return_statement_qt(function->type(), options) << ";" << endl;
815 } else */
816 {
817 if (f_type) {
818 if (f_type->isTargetLangString() || f_type->isContainer())
819 ;
820 else if ((f_type->isValue() && !f_type->typeEntry()->isStructInD()) ||
821 f_type->isVariant() )
822 s << f_type->name() << " *__qt_return_value = (" << f_type->name() << "*) ";
823 else if (f_type->isObject() || f_type->isQObject())
824 s << "return (" << f_type->name() <<"*) ";
825 else if (f_type->name() == "QModelIndex")
826 ;
827 else if ((options & NoReturnStatement) == 0)
828 s << "return ";
829
830 if (f_type->isEnum() || f_type->isFlags())
831 s << "(" << f_type->typeEntry()->qualifiedCppName() <<") ";
832 }
833
834 s << function->marshalledName() << "_dispatch("
835 << "this->d_entity()";
836
837 if (f_type) {
838 if (f_type->isTargetLangString())
839 s << ", ret_str, ret_str_size";
840 if (f_type->name() == "QModelIndex")
841 s << ", &__d_return_value";
842 if (f_type->isContainer())
843 s << ", &__d_return_value, &__d_return_value_size";
844 }
845
846 if (function->arguments().size() > 0)
847 s << ", ";
848 writeFunctionCallArguments(s, function, QString(), Option(options | ForceEnumCast | SkipRemovedArguments | ExcludeConst | VirtualDispatch));
849
850 s << ");" << endl;
851
852 //s << "std::cout << \"leaving " << function->marshalledName() << " \\n\"; " << endl;
853
854 if (f_type) {
855 if (f_type->name() == "QModelIndex") {
856 s << INDENT << "QModelIndex __qt_return_value = qtd_to_QModelIndex( __d_return_value );" << endl;
857 #ifdef Q_OS_WIN32
858 s << "__qtd_dummy();" << endl; // hack!!!
859 #endif
860 s << INDENT << "return __qt_return_value;" << endl;
861 }
862
863 if (f_type->isContainer()) {
864 writeJavaToQt(s, f_type, "__qt_return_value", "__d_return_value",
865 function, 0, GlobalRefJObject);
866 s << INDENT << "return __qt_return_value;" << endl;
867 }
868
869 if (f_type->isTargetLangString())
870 s << INDENT << "return " << "QString::fromUtf8(ret_str, ret_str_size);" << endl;
871
872 if ( ( f_type->isValue() && !f_type->typeEntry()->isStructInD() ) || f_type->isVariant() )
873 s << INDENT << "return " << f_type->name() << "(*__qt_return_value);" << endl; //" __qt_return_value = ";
874 }
875 }
876
877 s << "}" << endl << endl;
878 // ----------------------------
879 }
880
881 void CppImplGenerator::writeVirtualDispatchArguments(QTextStream &s, const AbstractMetaFunction *d_function, bool d_export)
882 {
883 uint nativeArgCount = 0;
884 AbstractMetaType *ret_type = d_function->type();
885
886 s << "(void *d_entity";
887
888 if (ret_type) {
889 if (ret_type->isTargetLangString())
890 s << ", char* ret_str, size_t ret_str_size";
891 if (ret_type->name() == "QModelIndex")
892 s << ", QModelIndexAccessor *__d_return_value";
893 if (ret_type->isContainer())
894 s << ", void** __d_arr_ptr, size_t* __d_arr_size";
895 }
896
897 if (d_function->arguments().size() > 0)
898 s << ", ";
899
900
901 // the function arguments
902 AbstractMetaArgumentList arguments = d_function->arguments();
903 foreach (const AbstractMetaArgument *argument, arguments) {
904 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) {
905 if (nativeArgCount > 0)
906 s << "," << " ";
907
908 AbstractMetaType *d_type = argument->type();
909 QString arg_name = argument->indexedName();
910
911 if (d_type->name() == "QModelIndex")
912 s << "QModelIndexAccessor" << QString(d_type->actualIndirections(), '*') << " " << arg_name;
913 else if (d_type->isContainer()) {
914 if (d_export) {
915 s << DGenerator::translateType(d_type, d_function->ownerClass(), NoOption) << "* ";
916 } else
917 s << "DArray* ";
918 s << arg_name;
919 } else if (d_type->typeEntry()->isStructInD())
920 s << d_type->typeEntry()->qualifiedCppName() << QString(d_type->actualIndirections(), '*')
921 << " " << arg_name;
922 else if (d_type->isTargetLangString()) {
923 if (d_export)
924 s << "wchar* ";
925 else
926 s << "const unsigned short* ";
927 s << QString("%1, int %1_size").arg(arg_name);
928 } else {
929 if(d_type->isVariant())
930 s << "void*";
931 else if (!d_type->hasNativeId())
932 {
933 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_type->typeEntry());
934 if(d_type->typeEntry()->isObject() && ctype->isAbstract() && d_export)
935 {
936 QString d_name = d_type->typeEntry()->qualifiedCppName();
937 s << d_name + "_ConcreteWrapper" + QString(d_type->indirections(),'*');
938 }
939 else
940 s << translateType(d_type, EnumAsInts);
941 }
942 else
943 s << "void*";
944 s << " " << argument->indexedName();
945 }
946 nativeArgCount++;
947 }
948 }
949 s << ")";
950 }
951
952 void CppImplGenerator::writeJavaLangObjectOverrideFunctions(QTextStream &s, const AbstractMetaClass *cls)
953 {
954 if (cls->hasHashFunction()) {
955 AbstractMetaFunctionList hashcode_functions = cls->queryFunctionsByName("hashCode");
956 bool found = false;
957 foreach (const AbstractMetaFunction *function, hashcode_functions) {
958 if (function->actualMinimumArgumentCount() == 0) {
959 found = true;
960 break;
961 }
962 }
963
964 if (!found) {
965 s << endl
966 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_hashCode", "jint")
967 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl
968 << INDENT << "{" << endl;
969 {
970 Indentation indent(INDENT);
971 s << INDENT << "Q_UNUSED(__jni_env);" << endl
972 << INDENT << cls->qualifiedCppName() << " *__qt_this = ("
973 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl
974 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl
975 << INDENT << "Q_ASSERT(__qt_this);" << endl
976 << INDENT << "return qHash(*__qt_this);" << endl;
977 }
978 s << INDENT << "}" << endl;
979 }
980 }
981
982 // Qt has a standard toString() conversion in QVariant?
983 QVariant::Type type = QVariant::nameToType(cls->qualifiedCppName().toLatin1());
984 if (QVariant(type).canConvert(QVariant::String) && !cls->hasToStringCapability()) {
985 AbstractMetaFunctionList tostring_functions = cls->queryFunctionsByName("toString");
986 bool found = false;
987 foreach (const AbstractMetaFunction *function, tostring_functions) {
988 if (function->actualMinimumArgumentCount() == 0) {
989 found = true;
990 break;
991 }
992 }
993
994 if (!found) {
995 s << endl
996 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_toString", "jstring")
997 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl
998 << INDENT << "{" << endl;
999 {
1000 Indentation indent(INDENT);
1001 s << INDENT << cls->qualifiedCppName() << " *__qt_this = ("
1002 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl
1003 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl
1004 << INDENT << "Q_ASSERT(__qt_this);" << endl
1005 << INDENT << "return qtjambi_from_qstring(__jni_env, QVariant(*__qt_this).toString());" << endl;
1006 }
1007 s << INDENT << "}" << endl;
1008 }
1009 }
1010 }
1011
1012 void CppImplGenerator::writeExtraFunctions(QTextStream &s, const AbstractMetaClass *java_class)
1013 {
1014 const ComplexTypeEntry *class_type = java_class->typeEntry();
1015 Q_ASSERT(class_type);
1016
1017 CodeSnipList code_snips = class_type->codeSnips();
1018 foreach (const CodeSnip &snip, code_snips) {
1019 if (snip.language == TypeSystem::ShellCode || snip.language == TypeSystem::NativeCode) {
1020 snip.formattedCode(s, INDENT) << endl;
1021 }
1022 }
1023 }
1024
1025 void CppImplGenerator::writeToStringFunction(QTextStream &s, const AbstractMetaClass *java_class)
1026 {
1027 FunctionModelItem fun = java_class->hasToStringCapability();
1028 bool core = java_class->package() == QLatin1String("qt.core");
1029 bool qevent = false;
1030
1031 const AbstractMetaClass *cls = java_class;
1032 while (cls) {
1033 if (cls->name() == "QEvent") {
1034 qevent = true;
1035 fun = cls->hasToStringCapability();
1036 break;
1037 }
1038 cls = cls->baseClass();
1039 }
1040
1041 if (!java_class->hasDefaultToStringFunction() && fun && !(qevent && core)) {
1042
1043 int indirections = fun->arguments().at(1)->type().indirections();
1044 QString deref = QLatin1String(indirections == 0 ? "*" : "");
1045
1046 s << endl;
1047 s << "#include <QDebug>" << endl;
1048 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_toString", "jstring")
1049 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl
1050 << INDENT << "{" << endl;
1051 {
1052 Indentation indent(INDENT);
1053 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = ("
1054 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl
1055 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl
1056 << INDENT << "Q_ASSERT(__qt_this);" << endl
1057
1058 << INDENT << "QString res;" << endl
1059 << INDENT << "QDebug d(&res);" << endl
1060 << INDENT << "d << " << deref << "__qt_this;" << endl;
1061 s << INDENT << "return qtjambi_from_qstring(__jni_env, res);" << endl;
1062 }
1063 s << INDENT << "}" << endl << endl;
1064 }
1065 }
1066
1067 void CppImplGenerator::writeCloneFunction(QTextStream &s, const AbstractMetaClass *java_class)
1068 {
1069 s << endl
1070 << jni_function_signature(java_class->package(), java_class->name(), "__qt_clone", "jobject") << endl
1071 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl
1072 << INDENT << "{" << endl;
1073 {
1074 Indentation indent(INDENT);
1075 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = ("
1076
1077 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl
1078 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl
1079 << INDENT << "Q_ASSERT(__qt_this);" << endl
1080 << INDENT << java_class->qualifiedCppName() << " *res = __qt_this;" << endl
1081 << INDENT << "return qtjambi_from_object(__jni_env, res, \"" << java_class->name() << "\", \"" << java_class->package().replace(".", "/") << "/\", true);" << endl;
1082 }
1083 s << INDENT << "}" << endl << endl;
1084 }
1085
1086 void CppImplGenerator::writeShellSignatures(QTextStream &s, const AbstractMetaClass *java_class)
1087 {
1088 bool has_constructors = java_class->hasConstructors();
1089
1090 // Write the function names...
1091 if (has_constructors && java_class->hasVirtualFunctions()) {
1092 AbstractMetaFunctionList virtual_functions = java_class->virtualFunctions();
1093 {
1094 Indentation indent(INDENT);
1095
1096 int pos = -1;
1097 foreach (AbstractMetaFunction *function, virtual_functions) {
1098 ++pos;
1099
1100 if (pos == 0)
1101 s << "static const char *qtjambi_method_names[] = {";
1102 else
1103 s << ",";
1104 s << endl
1105 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ "
1106 << "\"" << function->name() << "\"";
1107 }
1108 if (pos >= 0)
1109 s << endl << "};" << endl << endl;
1110 else
1111 s << "static const char **qtjambi_method_names = 0;" << endl;
1112 }
1113
1114 // Write the function signatures
1115 {
1116 Indentation indent(INDENT);
1117
1118 int pos = -1;
1119 foreach (AbstractMetaFunction *function, virtual_functions) {
1120 ++pos;
1121
1122 if (pos == 0)
1123 s << "static const char *qtjambi_method_signatures[] = {";
1124 else
1125 s << ",";
1126 s << endl
1127 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ "
1128 << "\""
1129 << jni_signature(function, SlashesAndStuff)
1130 << "\"";
1131 }
1132 if (pos >= 0)
1133 s << endl << "};" << endl;
1134 else
1135 s << "static const char **qtjambi_method_signatures = 0;" << endl;
1136 s << "static const int qtjambi_method_count = " << QString::number(pos + 1) << ";" << endl
1137 << endl;
1138 }
1139 }
1140
1141 if (has_constructors && java_class->hasInconsistentFunctions()) {
1142 AbstractMetaFunctionList inconsistents = java_class->cppInconsistentFunctions();
1143 // Write the inconsistent function names...
1144 {
1145 Indentation indent(INDENT);
1146 s << "static const char *qtjambi_inconsistent_names[] = {";
1147 for (int i=0; i<inconsistents.size(); ++i) {
1148 if (i != 0)
1149 s << ",";
1150 s << endl << INDENT << "\"" << inconsistents.at(i)->name() << "\"";
1151 }
1152 s << endl << "};" << endl << endl;
1153 }
1154
1155 // Write the function signatures
1156 {
1157 Indentation indent(INDENT);
1158 s << "static const char *qtjambi_inconsistent_signatures[] = {";
1159 for (int i=0; i<inconsistents.size(); ++i) {
1160 const AbstractMetaFunction *function = inconsistents.at(i);
1161
1162 if (i != 0)
1163 s << ",";
1164 s << endl << INDENT << "\""
1165 << jni_signature(function, SlashesAndStuff)
1166 << "\"";
1167 }
1168 s << endl << "};" << endl << endl;
1169 }
1170 s << "static const int qtjambi_inconsistent_count = " << inconsistents.size() << ";" << endl
1171 << endl;
1172 }
1173
1174
1175 AbstractMetaFunctionList signal_functions = java_class->cppSignalFunctions();
1176 if (signal_functions.size()) {
1177 Indentation indent(INDENT);
1178 s << "static const char *qtjambi_signal_names[] = {";
1179 for (int i=0; i<signal_functions.size(); ++i) {
1180 if (i != 0)
1181 s << ",";
1182
1183 const AbstractMetaFunction *f = signal_functions.at(i);
1184
1185 QString signalName = f->name();
1186
1187 s << endl << INDENT << "\"" << signalName << "\"";
1188 }
1189 s << endl << "};" << endl << endl;
1190
1191 s << "static const int qtjambi_signal_argumentcounts[] = {";
1192 for (int i=0; i<signal_functions.size(); ++i) {
1193 if (i != 0)
1194 s << ",";
1195 s << endl << INDENT << signal_functions.at(i)->arguments().count();
1196 }
1197 s << endl << "};" << endl << endl;
1198 s << "static const int qtjambi_signal_count = " << signal_functions.size() << ";" << endl
1199 << endl;
1200 }
1201 }
1202
1203 void CppImplGenerator::writeQObjectFunctions(QTextStream &s, const AbstractMetaClass *java_class)
1204 {
1205 // QObject::metaObject()
1206 /* s << "const QMetaObject *" << shellClassName(java_class) << "::metaObject() const" << endl
1207 << "{" << endl
1208 << " if (m_meta_object == 0) {" << endl
1209 << " JNIEnv *__jni_env = qtjambi_current_environment();" << endl
1210 << " jobject __obj = m_link != 0 ? m_link->javaObject(__jni_env) : 0;" << endl
1211 << " if (__obj == 0) return " << java_class->qualifiedCppName() << "::metaObject();" << endl
1212 << " else m_meta_object = qtjambi_metaobject_for_class(__jni_env, __jni_env->GetObjectClass(__obj), " << java_class->qualifiedCppName() << "::metaObject());" << endl;
1213
1214
1215 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions();
1216 for (int pos=0; pos<virtualFunctions.size(); ++pos) {
1217 const AbstractMetaFunction *virtualFunction = virtualFunctions.at(pos);
1218 if (virtualFunction->isVirtualSlot()) {
1219 QStringList introspectionCompatibleSignatures = virtualFunction->introspectionCompatibleSignatures();
1220 foreach (QString introspectionCompatibleSignature, introspectionCompatibleSignatures) {
1221 s << " {" << endl
1222 << " int idx = "
1223 << java_class->qualifiedCppName() << "::metaObject()->indexOfMethod(\""
1224 << introspectionCompatibleSignature << "\");" << endl;
1225
1226 s << " if (idx >= 0) m_map.insert(idx, " << pos << ");" << endl
1227 << " }" << endl;
1228 }
1229 }
1230
1231 }
1232
1233 s << " }" << endl
1234 << " return m_meta_object;" << endl
1235 << "}" << endl << endl;
1236
1237 // QObject::qt_metacast()
1238 s << "void *" << shellClassName(java_class) << "::qt_metacast(const char *_clname)" << endl
1239 << "{" << endl
1240 << " if (!_clname) return 0;" << endl
1241 << " if (!strcmp(_clname, \"" << shellClassName(java_class) << "\"))" << endl
1242 << " return static_cast<void*>(const_cast<" << shellClassName(java_class) << "*>(this));" << endl
1243 << " return " << java_class->qualifiedCppName() << "::qt_metacast(_clname);" << endl
1244 << "}" << endl << endl;
1245 */
1246
1247 writeSignalsHandling(s, java_class);
1248
1249 // QObject::qt_metacall()
1250 s << "int " << shellClassName(java_class) << "::qt_metacall(QMetaObject::Call _c, int _id, void **_a)" << endl
1251 << "{" << endl;
1252
1253 s << " _id = " << java_class->qualifiedCppName() << "::qt_metacall(_c, _id, _a);" << endl
1254 << " if (_id < 0 || _c != QMetaObject::InvokeMetaMethod)" << endl
1255 << " return _id;" << endl
1256 // << " Q_ASSERT(_id < 2);" << endl
1257 << " emit_callbacks_" << java_class->name() << "[_id](this->d_entity(), _a);" << endl
1258 << " return -1;" << endl
1259 << "}" << endl << endl;
1260 }
1261
1262 void CppImplGenerator::writeSignalHandler(QTextStream &s, const AbstractMetaClass *d_class, AbstractMetaFunction *function)
1263 {
1264 QString extra_args, extra_call_args, conversion_code;
1265 QTextStream s2(&conversion_code);
1266
1267 Indentation indent(INDENT);
1268
1269 AbstractMetaArgumentList arguments = function->arguments();
1270 foreach (AbstractMetaArgument *argument, arguments) {
1271 if(argument->type()->isContainer()) {
1272 QString arg_name = argument->indexedName();
1273 const AbstractMetaType *arg_type = argument->type();
1274 extra_args += ", DArray " + arg_name;
1275 extra_call_args += ", " + arg_name + "_arr";
1276
1277 s2 << INDENT;
1278 writeTypeInfo(s2, arg_type, NoOption);
1279 s2 << arg_name << " = (*reinterpret_cast< ";
1280 writeTypeInfo(s2, arg_type, ExcludeReference);
1281 s2 << "(*)>(args[" << argument->argumentIndex() + 1 << "]));" << endl
1282 << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl
1283 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name);
1284
1285 writeQtToJava(s2,
1286 arg_type,
1287 arg_name,
1288 "__d_" + arg_name,
1289 function,
1290 argument->argumentIndex() + 1,
1291 Option(VirtualDispatch));
1292 }
1293 }
1294 QString sig_name = signalExternName(d_class, function);
1295 s << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle_in_d(void* d_entity, void** args" << extra_args <<");" << endl
1296 << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle(void* d_entity, void** args)" << endl
1297 << "{" << endl
1298 << conversion_code << endl
1299 << INDENT << sig_name << "_handle_in_d(d_entity, args" << extra_call_args << ");" << endl
1300 << "}" << endl;
1301
1302 }
1303
1304 void CppImplGenerator::writeSignalsHandling(QTextStream &s, const AbstractMetaClass *java_class)
1305 {
1306 s << "extern \"C\" typedef void (*EmitCallback)(void*, void**);" << endl;
1307 AbstractMetaFunctionList signal_funcs = signalFunctions(java_class);
1308
1309 if (cpp_shared)
1310 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "];" << endl;
1311 else {
1312 // D-side signal callbacks
1313 for(int i = 0; i < signal_funcs.size(); i++) {
1314 AbstractMetaFunction *signal = signal_funcs.at(i);
1315 writeSignalHandler(s, java_class, signal);
1316 }
1317
1318 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "] = {" << endl;
1319 for(int i = 0; i < signal_funcs.size(); i++) {
1320 AbstractMetaFunction *signal = signal_funcs.at(i);
1321 s << endl;
1322 if (i != 0)
1323 s << ", ";
1324 s << "&" << signalExternName(java_class, signal) << "_handle";
1325 }
1326 s << endl << "};" << endl << endl;
1327 }
1328
1329 // Functions connecting/disconnecting shell's slots
1330 for(int i = 0; i < signal_funcs.size(); i++) {
1331 AbstractMetaFunction *signal = signal_funcs.at(i);
1332 QString sigExternName = signalExternName(java_class, signal);
1333
1334 s << "extern \"C\" DLL_PUBLIC void " << sigExternName << "_connect"
1335 << "(void* native_id)" << endl << "{" << endl
1336 << " " << shellClassName(java_class) << " *qobj = (" << shellClassName(java_class) << "*) native_id;" << endl
1337 << " const QMetaObject &mo = " << shellClassName(java_class) << "::staticMetaObject;" << endl
1338 << " int signalId = mo.indexOfSignal(\"" << signal->minimalSignature() << "\");" << endl
1339 << " mo.connect(qobj, signalId, qobj, mo.methodCount() + " << i << ");" << endl
1340 << "}" << endl;
1341
1342 s << "extern \"C\" DLL_PUBLIC void " << sigExternName << "_disconnect"
1343 << "(void* native_id)" << endl << "{" << endl
1344 << " " << shellClassName(java_class) << " *qobj = (" << shellClassName(java_class) << "*) native_id;" << endl
1345 << " const QMetaObject &mo = " << shellClassName(java_class) << "::staticMetaObject;" << endl
1346 << " int signalId = mo.indexOfSignal(\"" << signal->minimalSignature() << "\");" << endl
1347 << " mo.disconnect(qobj, signalId, qobj, mo.methodCount() + " << i << ");" << endl
1348 << "}" << endl << endl;
1349 }
1350 }
1351
1352
1353 void CppImplGenerator::writeShellConstructor(QTextStream &s, const AbstractMetaFunction *java_function)
1354 {
1355 if (java_function->isModifiedRemoved(TypeSystem::ShellCode))
1356 return;
1357
1358 const AbstractMetaClass *cls = java_function->ownerClass();
1359 AbstractMetaArgumentList arguments = java_function->arguments();
1360
1361 writeFunctionSignature(s, java_function, cls);
1362
1363 s << endl;
1364 s << " : " << cls->qualifiedCppName() << "(";
1365 for (int i=0; i<arguments.size(); ++i) {
1366 s << arguments.at(i)->indexedName();
1367 if (i != arguments.size() - 1)
1368 s << ", ";
1369 }
1370 s << ")";
1371 if (cls->hasVirtualFunctions())
1372 s << "," << endl << " Qtd_QObjectEntity(d_ptr)";
1373 /* qtd s << " m_meta_object(0)," << endl;
1374 s << " m_vtable(0)," << endl
1375 << " m_link(0)" << endl;
1376 */
1377 s << endl;
1378 s << "{" << endl;
1379 {
1380 Indentation indent(INDENT);
1381 writeCodeInjections(s, java_function, cls, CodeSnip::Beginning, TypeSystem::ShellCode);
1382 writeCodeInjections(s, java_function, cls, CodeSnip::End, TypeSystem::ShellCode);
1383 }
1384 s << "}" << endl << endl;
1385 }
1386
1387 void CppImplGenerator::writeShellDestructor(QTextStream &s, const AbstractMetaClass *java_class)
1388 {
1389
1390 if (java_class->isQObject())
1391 if (cpp_shared)
1392 s << "extern \"C\" typedef void (*qtd_pf_D_" << java_class->name() << "_delete)(void *d_ptr);" << endl
1393 << "qtd_pf_D_" << java_class->name() << "_delete qtd_D_" << java_class->name() << "_delete;" << endl << endl;
1394 else
1395 s << "extern \"C\" void qtd_D_" << java_class->name() << "_delete(void *d_ptr);" << endl << endl;
1396
1397 s << shellClassName(java_class) << "::~"
1398 << shellClassName(java_class) << "()" << endl
1399 << "{" << endl;
1400 {
1401 Indentation indent(INDENT);
1402 if (java_class->isQObject())
1403 s << INDENT << "if (QObject::parent())" << endl
1404 << INDENT << " qtd_D_" << java_class->name() << "_delete(this->d_entity());" << endl;
1405
1406 /* qtd
1407 s << "#ifdef QT_DEBUG" << endl
1408 << INDENT << "if (m_vtable)" << endl
1409 << INDENT << " m_vtable->deref();" << endl
1410 << "#endif" << endl
1411 << INDENT << "if (m_link) {" << endl;
1412
1413 AbstractMetaClassList interfaces = java_class->interfaces();
1414 if (interfaces.size() + (java_class->baseClass() != 0 ? 1 : 0) > 1) {
1415 if (java_class->baseClass() != 0)
1416 interfaces += java_class->baseClass();
1417 foreach (AbstractMetaClass *iface, interfaces) {
1418 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface;
1419 s << INDENT << " m_link->unregisterSubObject((" << impl->qualifiedCppName() << " *) this);" << endl;
1420 }
1421 }
1422
1423 if (!java_class->isQObject()) {
1424 s << INDENT << " JNIEnv *__jni_env = qtjambi_current_environment();" << endl
1425 << INDENT << " if (__jni_env != 0) m_link->nativeShellObjectDestroyed(__jni_env);" << endl;
1426 }
1427
1428 #if defined(QTJAMBI_DEBUG_TOOLS)
1429 s << INDENT << " qtjambi_increase_shellDestructorCalledCount(QString::fromLatin1(\"" << java_class->name() << "\"));" << endl;
1430 #endif
1431
1432 s << INDENT << "}" << endl; */
1433 }
1434 s << "}" << endl << endl;
1435 }
1436
1437 void CppImplGenerator::writeCodeInjections(QTextStream &s, const AbstractMetaFunction *java_function,
1438 const AbstractMetaClass *implementor, CodeSnip::Position position,
1439 TypeSystem::Language language)
1440 {
1441
1442 FunctionModificationList mods;
1443 const AbstractMetaClass *cls = implementor;
1444 while (cls != 0) {
1445 mods += java_function->modifications(cls);
1446
1447 if (cls == cls->baseClass())
1448 break;
1449 cls = cls->baseClass();
1450 }
1451
1452 foreach (FunctionModification mod, mods) {
1453 if (mod.snips.count() <= 0)
1454 continue ;
1455
1456 foreach (CodeSnip snip, mod.snips) {
1457 if (snip.position != position)
1458 continue ;
1459
1460 if ((snip.language & language) == false)
1461 continue ;
1462
1463 if (position == CodeSnip::End)
1464 s << endl;
1465
1466 QString code;
1467 QTextStream tmpStream(&code);
1468 snip.formattedCode(tmpStream, INDENT);
1469 ArgumentMap map = snip.argumentMap;
1470 ArgumentMap::iterator it = map.begin();
1471 for (;it!=map.end();++it) {
1472 int pos = it.key() - 1;
1473 QString meta_name = it.value();
1474
1475 if (pos >= 0 && pos < java_function->arguments().count()) {
1476 code = code.replace(meta_name, java_function->arguments().at(pos)->indexedName());
1477 } else {
1478 QString debug = QString("argument map specifies invalid argument index %1"
1479 "for function '%2'")
1480 .arg(pos + 1).arg(java_function->name());
1481 ReportHandler::warning(debug);
1482 }
1483
1484 }
1485 s << code;
1486 if (position == CodeSnip::Beginning)
1487 s << endl;
1488 }
1489 }
1490 }
1491
1492 static QString function_call_for_ownership(TypeSystem::Ownership owner, const QString &var_name)
1493 {
1494 if (owner == TypeSystem::CppOwnership) {
1495 return "setCppOwnership(__jni_env, " + var_name + ")";
1496 } else if (owner == TypeSystem::TargetLangOwnership) {
1497 return "setJavaOwnership(__jni_env, " + var_name + ")";
1498 } else if (owner == TypeSystem::DefaultOwnership) {
1499 return "setDefaultOwnership(__jni_env, " + var_name + ")";
1500 } else {
1501 Q_ASSERT(false);
1502 return "bogus()";
1503 }
1504 }
1505
1506 void CppImplGenerator::writeOwnership(QTextStream &s,
1507 const AbstractMetaFunction *java_function,
1508 const QString &var_name,
1509 int var_index,
1510 const AbstractMetaClass *implementor)
1511 {
1512 TypeSystem::Ownership owner = TypeSystem::InvalidOwnership;
1513 const AbstractMetaClass *cls = implementor;
1514 while (cls != 0 && owner == TypeSystem::InvalidOwnership) {
1515 owner = java_function->ownership(cls, TypeSystem::ShellCode, var_index);
1516 cls = cls->baseClass();
1517 }
1518
1519 if (owner == TypeSystem::InvalidOwnership)
1520 return;
1521
1522 if (var_index != -1) {
1523 s << INDENT << "if (" << var_name << " != 0) {" << endl;
1524 {
1525 Indentation indent(INDENT);
1526 s << INDENT << "QtJambiLink *__link = QtJambiLink::findLink(__jni_env, "
1527 << var_name << ");" << endl
1528 << INDENT << "Q_ASSERT(__link != 0);" << endl;
1529
1530 s << INDENT << "__link->" << function_call_for_ownership(owner, var_name) << ";" << endl;
1531 }
1532 s << INDENT << "}" << endl;
1533 } else {
1534 s << INDENT << "if (m_link) {" << endl;
1535 {
1536 Indentation indent(INDENT);
1537 s << INDENT << "m_link->" << function_call_for_ownership(owner, "m_link->javaObject(__jni_env)") << ";" << endl;
1538 }
1539 s << INDENT << "}" << endl;
1540 }
1541
1542 }
1543
1544 void CppImplGenerator::writeShellFunction(QTextStream &s, const AbstractMetaFunction *java_function,
1545 const AbstractMetaClass *implementor, int id)
1546 {
1547 writeFunctionSignature(s, java_function, implementor, QString(), OriginalName);
1548
1549 s << endl
1550 << "{" << endl;
1551
1552 Indentation indent(INDENT);
1553
1554 QString java_function_signature = java_function->signature();
1555 /* s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) entering: " << implementor->name() << "::"
1556 << java_function_signature << "\");" << endl;
1557 */
1558 writeCodeInjections(s, java_function, implementor, CodeSnip::Beginning, TypeSystem::ShellCode);
1559
1560 // s << " printf(\"%s : %s\\n\", \"" << java_function->enclosingClass()->name() << "\""
1561 // << ", \"" << java_function->name() << "\");" << endl;
1562
1563 if (!java_function->isFinalInCpp() || java_function->isVirtualSlot()) {
1564 s << INDENT << "jmethodID method_id = m_vtable->method(" << id << ");" << endl;
1565 s << INDENT << "if (method_id) {" << endl;
1566
1567 {
1568 Indentation indent(INDENT);
1569 s << INDENT << "JNIEnv *__jni_env = qtjambi_current_environment();" << endl;
1570
1571 // This nasty case comes up when we're shutting down while receiving virtual
1572 // calls.. With these checks we safly abort...
1573 s << INDENT << "if (!__jni_env) {" << endl
1574 << " ";
1575 writeBaseClassFunctionCall(s, java_function, implementor);
1576 if (!java_function->type()) {
1577 s << INDENT << " return;" << endl;
1578 }
1579 s << INDENT << "}" << endl;
1580
1581 // otherwise, continue with the function call...
1582 s << INDENT << "__jni_env->PushLocalFrame(100);" << endl;
1583
1584 AbstractMetaArgumentList arguments = java_function->arguments();
1585 AbstractMetaArgumentList argumentsToReset;
1586 foreach (AbstractMetaArgument *argument, arguments) {
1587 if (!java_function->argumentRemoved(argument->argumentIndex()+1)) {
1588 if (!argument->type()->isPrimitive()
1589 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) {
1590 writeQtToJava(s,
1591 argument->type(),
1592 argument->indexedName(),
1593 "__java_" + argument->indexedName(),
1594 java_function,
1595 argument->argumentIndex() + 1);
1596 }
1597
1598 if (java_function->resetObjectAfterUse(argument->argumentIndex()+1))
1599 argumentsToReset.append(argument);
1600 }
1601 }
1602
1603 for (int i=0; i<arguments.size(); ++i)
1604 writeOwnership(s, java_function, "__java_" + arguments.at(i)->indexedName(), i+1, implementor);
1605
1606
1607 AbstractMetaType *function_type = java_function->type();
1608 QString new_return_type = java_function->typeReplaced(0);
1609 bool has_function_type = ((function_type != 0
1610 || !new_return_type.isEmpty())
1611 && new_return_type != "void");
1612
1613 s << INDENT;
1614 if (has_function_type) {
1615 if (new_return_type.isEmpty()) {
1616 s << translateType(function_type);
1617 } else {
1618 s << jniName(new_return_type);
1619 }
1620 s << " " << "__d_return_value = ";
1621 }
1622
1623 s << "__jni_env->";
1624 if (new_return_type.isEmpty()) {
1625 s << callXxxMethod(java_function->type());
1626 } else if (!has_function_type) {
1627 s << "CallVoidMethod";
1628 } else {
1629 s << callXxxMethod(new_return_type);
1630 }
1631
1632 s << "(m_link->javaObject(__jni_env), method_id";
1633 if (arguments.size() > 0)
1634 s << ", ";
1635 writeFunctionCallArguments(s, java_function, "__java_", Option(NoCasts | SkipRemovedArguments));
1636 s << ");" << endl
1637 << INDENT << "qtjambi_exception_check(__jni_env);" << endl;
1638
1639 if (has_function_type) {
1640 writeJavaToQt(s, function_type, "__qt_return_value", "__d_return_value",
1641 java_function, 0, GlobalRefJObject);
1642
1643 if (java_function->nullPointersDisabled()) {
1644 s << INDENT << "if (__d_return_value == 0) {" << endl;
1645 {
1646 Indentation indent(INDENT);
1647 s << INDENT << "fprintf(stderr, \"QtJambi: Unexpected null pointer returned from override of '" << java_function->name() << "' in class '%s'\\n\"," << endl
1648 << INDENT << " qPrintable(qtjambi_object_class_name(__jni_env, m_link->javaObject(__jni_env))));" << endl;
1649 s << INDENT << "__qt_return_value = ";
1650 QString defaultValue = java_function->nullPointerDefaultValue();
1651 if (!defaultValue.isEmpty())
1652 s << defaultValue << ";";
1653 else
1654 writeBaseClassFunctionCall(s, java_function, implementor, NoReturnStatement);
1655 s << endl;
1656 }
1657
1658 s << INDENT << "}" << endl;
1659 }
1660 } else if (!java_function->conversionRule(TypeSystem::ShellCode, 0).isEmpty()) {
1661 writeConversionRule(s, TypeSystem::ShellCode, java_function, 0, "<invalid>", "<invalid>");
1662 }
1663
1664 writeOwnership(s, java_function, "this", -1, implementor);
1665 writeOwnership(s, java_function, "__d_return_value", 0, implementor);
1666
1667 foreach (AbstractMetaArgument *argumentToReset, argumentsToReset) {
1668
1669 QString argumentName = "__java_" + argumentToReset->indexedName();
1670
1671 s << INDENT;
1672 if (argumentToReset->type()->isContainer())
1673 s << "qtjambi_invalidate_collection(";
1674 else
1675 s << "qtjambi_invalidate_object(";
1676
1677 s << "__jni_env, " << argumentName << ");" << endl;
1678 }
1679
1680 s << INDENT << "__jni_env->PopLocalFrame(0);" << endl;
1681
1682 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> leaving: " << implementor->name()
1683 << "::" << java_function_signature << "\");" << endl;
1684
1685 if (function_type)
1686 s << INDENT << "return __qt_return_value;" << endl;
1687
1688 }
1689
1690 s << INDENT << "} else {" << endl;
1691
1692 {
1693 Indentation indent(INDENT);
1694 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> super() and leaving: "
1695 << implementor->name() << "::" << java_function_signature << "\");" << endl;
1696 writeBaseClassFunctionCall(s, java_function, implementor);
1697 }
1698
1699
1700 s << INDENT << "}" << endl;
1701
1702 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode);
1703
1704 // A little trick to close open painters on a widget
1705 if (java_function->name() == "paintEvent") {
1706 s << INDENT << "JNIEnv *env = qtjambi_current_environment();" << endl
1707 << INDENT << "qtjambi_end_paint(env, m_link->javaObject(env));" << endl;
1708 }
1709
1710 } else {
1711 if(java_function->isRemovedFrom(implementor, TypeSystem::TargetLangCode)){
1712 // Avoid compiler warnings for unused parameters
1713 AbstractMetaArgumentList arguments = java_function->arguments();
1714
1715 foreach (const AbstractMetaArgument *argument, arguments) {
1716 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl;
1717 }
1718 }
1719 writeBaseClassFunctionCall(s, java_function, implementor);
1720 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode);
1721 }
1722
1723 s << "}" << endl << endl;
1724
1725 }
1726
1727 // ### kill implementor
1728
1729 void CppImplGenerator::writePublicFunctionOverride(QTextStream &s,
1730 const AbstractMetaFunction *java_function,
1731 const AbstractMetaClass *implementor)
1732 {
1733 Q_ASSERT(java_function->originalAttributes()
1734 & (AbstractMetaAttributes::Protected
1735 | AbstractMetaAttributes::Final));
1736
1737 // The write a public override version of this function to be used by native functions
1738 writeFunctionSignature(s, java_function, implementor, "__public_",
1739 Option(EnumAsInts | UnderscoreSpaces
1740 | (java_function->isAbstract() ? SkipName : NoOption)));
1741 s << endl
1742 << "{" << endl;
1743 Indentation indent(INDENT);
1744 writeBaseClassFunctionCall(s, java_function, implementor);
1745 s << "}" << endl << endl;
1746 }
1747
1748
1749 void CppImplGenerator::writeVirtualFunctionOverride(QTextStream &s,
1750 const AbstractMetaFunction *java_function,
1751 const AbstractMetaClass *implementor)
1752 {
1753 Q_ASSERT(!java_function->isFinalInCpp());
1754
1755 Option options = Option(EnumAsInts | UnderscoreSpaces);
1756
1757 // The write a public override version of this function to be used by native functions
1758 writeFunctionSignature(s, java_function, implementor, "__override_",
1759 options,
1760 QString());
1761 s << endl
1762 << "{" << endl;
1763 Indentation indent(INDENT);
1764 /* qtd s << INDENT << "if (static_call) {" << endl;
1765 {
1766 Indentation indent(INDENT); */
1767 writeBaseClassFunctionCall(s, java_function, implementor);
1768 /* qtd }
1769 s << INDENT << "} else {" << endl;
1770 {
1771 Indentation indent(INDENT);
1772 writeBaseClassFunctionCall(s, java_function, implementor, VirtualCall);
1773 }
1774
1775 s << INDENT << "}" << endl */
1776 s << "}" << endl << endl;
1777 }
1778
1779
1780 void CppImplGenerator::writeBaseClassFunctionCall(QTextStream &s,
1781 const AbstractMetaFunction *java_function,
1782 const AbstractMetaClass *,
1783 Option options)
1784 {
1785 bool static_call = !(options & VirtualCall);
1786 if ((options & NoReturnStatement) == 0)
1787 s << INDENT;
1788 if (java_function->isAbstract() && static_call) {
1789 s << default_return_statement_qt(java_function->type(), options) << ";" << endl;
1790 } else {
1791 if (java_function->type() && (options & NoReturnStatement) == 0)
1792 s << "return ";
1793 if (static_call) {
1794 const AbstractMetaClass *implementor = java_function->implementingClass();
1795 if (java_function->isInterfaceFunction())
1796 implementor = java_function->interfaceClass()->primaryInterfaceImplementor();
1797 s << implementor->qualifiedCppName() << "::";
1798 }
1799 s << java_function->originalName() << "(";
1800 writeFunctionCallArguments(s, java_function, QString(), Option(options | ForceEnumCast));
1801 s << ");" << endl;
1802 }
1803 }
1804
1805
1806 void CppImplGenerator::writeFunctionName(QTextStream &s,
1807 const AbstractMetaFunction *java_function,
1808 const AbstractMetaClass *java_class,
1809 uint options)
1810 {
1811 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass();
1812 AbstractMetaArgumentList arguments = java_function->arguments();
1813
1814 // Function signature
1815 QString return_type = jniReturnName(java_function, options);
1816
1817 QString function_name;
1818
1819 bool callThrough = java_function->needsCallThrough();
1820 /* qtd if (!callThrough)
1821 function_name = java_function->name();
1822 else */
1823 function_name = java_function->marshalledName();
1824
1825 QString args = "__";
1826
1827 if (callThrough && !java_function->isStatic() && !java_function->isConstructor())
1828 args += "J";
1829
1830 if (!arguments.isEmpty()) {
1831 foreach (const AbstractMetaArgument *argument, arguments) {
1832 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) {
1833 if (!argument->type()->hasNativeId()) {
1834 QString modified_type = java_function->typeReplaced(argument->argumentIndex()+1);
1835 if (modified_type.isEmpty())
1836 args += jni_signature(argument->type(), Underscores);
1837 else
1838 args += jni_signature(modified_type, Underscores);
1839 } else {
1840 args += "J";
1841 }
1842 }
1843 }
1844 }
1845
1846 s << jni_function_signature(cls->package(), cls->name(), function_name,
1847 return_type, args, options);
1848
1849 }
1850
1851 void CppImplGenerator::writeFinalFunctionArguments(QTextStream &s, const AbstractMetaFunction *java_function, bool d_export)
1852 {
1853 bool callThrough = java_function->needsCallThrough();
1854 s << "(";
1855 /* qtd
1856 << "JNIEnv *__jni_env," << endl;
1857 if (!java_function->isConstructor()) {
1858 if (java_function->isStatic())
1859 s << " jclass";
1860 else
1861 s << " jobject";
1862 } else
1863 s << " jobject " << java_object_name;
1864 */
1865 uint nativeArgCount = 0;
1866 const AbstractMetaClass *cls = java_function->ownerClass();
1867 if (java_function->isConstructor() &&
1868 ( cls->hasVirtualFunctions()
1869 || cls->typeEntry()->isObject() ) )
1870 {
1871 s << "void *d_ptr";
1872 nativeArgCount++;
1873 }
1874
1875 // passing pointer to C++ object
1876 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor());
1877 if (hasNativeId) {
1878 if (nativeArgCount > 0)
1879 s << "," << endl << " ";
1880 /* qtd << "," << endl */ s << "void* __this_nativeId";
1881 nativeArgCount++;
1882 }
1883
1884 AbstractMetaType *f_type = java_function->type();
1885
1886 // return values as strings, arrays or QModelIndex'es we return as arguments
1887 bool return_arg = false;
1888 if (f_type) {
1889 if (f_type->isTargetLangString() ||
1890 f_type->isContainer() ||
1891 f_type->name() == "QModelIndex")
1892 return_arg = true;
1893
1894 if (return_arg && nativeArgCount > 0)
1895 s << "," << endl << " ";
1896
1897 if (f_type->isTargetLangString() || f_type->isContainer())
1898 s << "void*";
1899 else if (f_type->name() == "QModelIndex")
1900 s << "QModelIndexAccessor*";
1901
1902 if(return_arg) {
1903 s << " __d_return_value";
1904 nativeArgCount++;
1905 }
1906 }
1907
1908 // the function arguments
1909 AbstractMetaArgumentList arguments = java_function->arguments();
1910 foreach (const AbstractMetaArgument *argument, arguments) {
1911 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) {
1912 AbstractMetaType *d_type = argument->type();
1913 const TypeEntry *te = d_type->typeEntry();
1914
1915 QString arg_name = argument->indexedName();
1916
1917 if (nativeArgCount > 0)
1918 s << "," << endl << " ";
1919 // if has QString argument we have to pass char* and str.length to QString constructor
1920 if (argument->type()->isTargetLangString()
1921 || (argument->type()->typeEntry() && argument->type()->typeEntry()->qualifiedCppName() == "QString")) {
1922 s << QString("char* %1, uint %1_size").arg(arg_name);
1923 } else if (d_type->isContainer()) {
1924 const ContainerTypeEntry *cte =
1925 static_cast<const ContainerTypeEntry *>(te);
1926 if(isLinearContainer(cte))
1927 s << QString("void *%1, size_t %1_size").arg(arg_name);
1928 } else {
1929 if (!d_type->hasNativeId()) {
1930 if(d_type->isVariant()) {
1931 if (d_export) s << "void*";
1932 else s << "QVariant*";
1933 } else
1934 s << translateType(argument->type(), EnumAsInts, d_export);
1935 }
1936 else if (d_type->name() == "QModelIndex")
1937 s << "QModelIndexAccessor";
1938 else if (te->isStructInD())
1939 s << te->qualifiedCppName();
1940 else
1941 s << "void*";
1942
1943 s << " " << arg_name;
1944 }
1945 nativeArgCount++;
1946 }
1947 }
1948 s << ")";
1949 }
1950
1951
1952 /*!
1953 Generates type conversion from Java -> Qt for all the arguments
1954 that are to be to be passed to the function
1955 */
1956 void CppImplGenerator::writeFinalFunctionSetup(QTextStream &s, const AbstractMetaFunction *java_function,
1957 const QString &qt_object_name,
1958 const AbstractMetaClass *cls)
1959 {
1960
1961 // Translate each of the function arguments into qt types
1962 AbstractMetaArgumentList arguments = java_function->arguments();
1963 foreach (const AbstractMetaArgument *argument, arguments) {
1964 if (!argument->type()->isPrimitive()
1965 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex() + 1).isEmpty()) {
1966 writeJavaToQt(s,
1967 argument->type(),
1968 "__qt_" + argument->indexedName(),
1969 argument->indexedName(),
1970 java_function,
1971 argument->argumentIndex() + 1,
1972 Option(UseNativeIds | EnumAsInts));
1973 }
1974 }
1975
1976 // Extract the qt equivalent to the this pointer and name it "qt_object_name"
1977 if (!java_function->isStatic() && !java_function->isConstructor()) {
1978 // qtd2 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls);
1979 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls);
1980 s << INDENT
1981 << className << " *" << qt_object_name
1982 << " = (" << className << " *) __this_nativeId;"
1983 << endl;
1984 // << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl
1985 // qtd << INDENT << "Q_ASSERT(" << qt_object_name << ");" << endl;
1986 }
1987 }
1988
1989 void CppImplGenerator::writeFinalFunction(QTextStream &s, const AbstractMetaFunction *java_function,
1990 const AbstractMetaClass *java_class)
1991 {
1992 Q_ASSERT(java_class);
1993
1994 if (java_function->isModifiedRemoved(TypeSystem::NativeCode))
1995 return;
1996
1997 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass();
1998
1999 QString java_function_signature = cls->name() + "::" + java_function->signature();
2000
2001 s << "// " << java_function_signature << endl;
2002
2003 const AbstractMetaType *function_type = java_function->type();
2004 QString new_return_type = java_function->typeReplaced(0);
2005 bool has_function_type = new_return_type != "void"
2006 && (!new_return_type.isEmpty() || function_type != 0)
2007 && java_function->argumentReplaced(0).isEmpty();
2008
2009 const QString qt_object_name = java_function->isStatic() ? shellClassName(cls) : "__qt_this";
2010 const QString java_object_name = java_function->isStatic() ? "__jni_class" : "__jni_object";
2011
2012 // we are not wrapping some functions depending on arguments because API is not yet full
2013 if (notWrappedYet(java_function))
2014 return;
2015
2016 // function signature...
2017 bool callThrough = java_function->needsCallThrough();
2018 uint options = m_native_jump_table ? ReturnType | ExternC : StandardJNISignature;
2019 writeFunctionName(s, java_function, cls, options);
2020 s << endl;
2021 writeFinalFunctionArguments(s, java_function);
2022 s << endl << "{" << endl;
2023 Indentation indent(INDENT);
2024
2025 // qtd2 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) entering: " << java_function_signature << "\");" << endl;
2026 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor());
2027 /* if (hasNativeId)
2028 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl;
2029 // Avoid compiler warnings when the variables are unused
2030 {
2031 s << INDENT << "Q_UNUSED(__jni_env)" << endl;
2032
2033 if (java_function->isConstructor())
2034 s << INDENT << "Q_UNUSED(" << java_object_name << ")" << endl;
2035
2036 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor());
2037 if (hasNativeId)
2038 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl;
2039 }
2040 */
2041
2042 if (cls->isFinal() && (!java_function->isAbstract() || !java_function->isFinalInTargetLang()) && !java_function->wasPublic()) {
2043 QString debug = QString("protected function '%1' in final class '%2'")
2044 .arg(java_function->signature()).arg(java_class->name());
2045 ReportHandler::warning(debug);
2046 // Avoid compiler warnings for unused parameters
2047 AbstractMetaArgumentList arguments = java_function->arguments();
2048
2049 foreach (const AbstractMetaArgument *argument, arguments) {
2050 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl;
2051 }
2052 s << INDENT << default_return_statement_qt(java_function->type()) << "";
2053 } else {
2054 writeFinalFunctionSetup(s, java_function, qt_object_name, cls);
2055
2056 writeCodeInjections(s, java_function, java_function->implementingClass(), CodeSnip::Beginning, TypeSystem::NativeCode);
2057
2058 if (java_function->isConstructor()) {
2059 writeFinalConstructor(s, java_function, qt_object_name, java_object_name);
2060 } else {
2061
2062 QString function_prefix = "";
2063 QStringList extra_param;
2064 Option option = NoOption;
2065
2066 bool hasShell = cls->generateShellClass();
2067
2068 if (java_function->isFinalOverload()) {
2069 // no prefix
2070 } else if (java_function->isFinalInCpp() && !java_function->wasPublic() && hasShell) {
2071 function_prefix = "__public_";
2072 } else if (!java_function->isFinalInCpp() && !java_function->isStatic() && hasShell) {
2073 function_prefix = "__override_";
2074 /* qtd extra_param.append("__do_static_call");
2075 s << INDENT
2076 << "bool __do_static_call = __this_nativeId ? ((QtJambiLink *) "
2077 << "__this_nativeId)->createdByJava() : false;" << endl;
2078 */ } else {
2079 option = OriginalName;
2080 }
2081
2082 // Call the Qt function on the java object
2083 s << " ";
2084 if (has_function_type) {
2085 const QString qt_return_value = "__qt_return_value";
2086 const QString java_return_value = "__d_return_value";
2087 if (function_type) {
2088 writeTypeInfo(s, function_type, EnumAsInts);
2089 s << " " << qt_return_value
2090 << " = ";
2091 }
2092
2093 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option,
2094 extra_param);
2095 s << endl;
2096
2097 writeQtToJava(s, function_type, qt_return_value, java_return_value,
2098 java_function, 0, EnumAsInts);
2099
2100 // qtd s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) -> leaving: "
2101 // << java_function_signature << "\");";
2102
2103 if (function_type && function_type->name() != "QModelIndex") {
2104 if(function_type->typeEntry()->isStructInD())
2105 s << endl << INDENT << "return " << qt_return_value << ";";
2106 else if (!function_type->isTargetLangString() && !function_type->isContainer())
2107 s << endl << INDENT << "return " << java_return_value << ";";
2108 }
2109
2110 } else {
2111 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option,
2112 extra_param);
2113 /* qtd
2114 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) -> leaving: "
2115 << java_function_signature << "\");" << endl;
2116 */
2117 }
2118 }
2119 }
2120 if(!java_function->argumentReplaced(0).isEmpty()) {
2121 s << INDENT << "return 0;" << endl;
2122 }
2123
2124 s << endl << "}";
2125 s << endl << endl;
2126 }
2127
2128 void CppImplGenerator::writeAssignment(QTextStream &s, const QString &destName, const QString &srcName,
2129 const AbstractMetaType *java_type)
2130 {
2131 if (java_type->isArray()) {
2132 for (int i=0; i<java_type->arrayElementCount(); ++i) {
2133 writeAssignment(s, destName + "[" + QString::number(i) + "]",
2134 srcName + "[" + QString::number(i) + "]", java_type->arrayElementType());
2135 }
2136 } else {
2137 s << INDENT << destName << " = " << srcName << ";" << endl;
2138 }
2139 }
2140
2141 void CppImplGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *java_field)
2142 {
2143 Q_ASSERT(java_field);
2144 Q_ASSERT(java_field->isPublic() || java_field->isProtected());
2145
2146 const AbstractMetaFunction *setter = java_field->setter();
2147 const AbstractMetaFunction *getter = java_field->getter();
2148
2149 const AbstractMetaClass *cls = java_field->enclosingClass();
2150 FieldModification mod = cls->typeEntry()->fieldModification(java_field->name());
2151
2152 if(notWrappedYet(getter))
2153 return;
2154
2155 if (mod.isReadable()) {
2156 // Getter
2157 if (getter->wasProtected()) {
2158 writeFunctionSignature(s, getter, getter->ownerClass());
2159 s << endl
2160 << "{" << endl;
2161 {
2162 Indentation indent(INDENT);
2163 s << INDENT << "return " << java_field->name() << ";" << endl;
2164 }
2165 s << "}" << endl << endl;
2166 }
2167
2168
2169 writeFunctionName(s, getter, getter->ownerClass());
2170 s << endl;
2171 writeFinalFunctionArguments(s, getter);
2172 s << "{" << endl;
2173 {
2174 Indentation indent(INDENT);
2175
2176
2177 writeFinalFunctionSetup(s, getter, "__qt_object", getter->ownerClass());
2178
2179 const QString java_return_value = "__d_return_value";
2180 QString qt_return_value;
2181 if (setter->isStatic())
2182 qt_return_value = shellClassName(setter->ownerClass()) + "::";
2183 else
2184 qt_return_value = "__qt_object->";
2185
2186
2187 // To avoid "taking address of tmp"
2188 s << INDENT;
2189 writeTypeInfo(s, getter->type(), Option(ArrayAsPointer));
2190 QString tmp_name = "__tmp_" + getter->name();
2191 s << tmp_name << " = ";
2192
2193 if (getter->wasPublic())
2194 qt_return_value += java_field->name();
2195 else
2196 qt_return_value += getter->name() + "_getter()";
2197 s << qt_return_value << ";" << endl;
2198
2199 writeQtToJava(s, getter->type(), tmp_name, java_return_value, 0, -1, EnumAsInts);
2200 if (getter->type()->isTargetLangString())
2201 ;
2202 else if(getter->type()->typeEntry()->isStructInD())
2203 s << INDENT << "return " << tmp_name << ";" << endl;
2204 else
2205 s << INDENT << "return " << java_return_value << ";" << endl;
2206 }
2207 s << "}" << endl << endl;
2208 }
2209
2210 if(notWrappedYet(setter))
2211 return;
2212
2213 // Setter
2214 if (mod.isWritable() && !java_field->type()->isConstant()) {
2215 // Write public override for protected fields
2216 if (setter->wasProtected()) {
2217 writeFunctionSignature(s, setter, setter->ownerClass());
2218 s << endl
2219 << "{" << endl;
2220 {
2221 Indentation indent(INDENT);
2222
2223 Q_ASSERT(setter->arguments().count() > 0);
2224 const AbstractMetaArgument *argument = setter->arguments().at(0);
2225
2226 QString thisRef = java_field->isStatic()
2227 ? setter->ownerClass()->qualifiedCppName() + QString("::")
2228 : QString("this->");
2229 writeAssignment(s, thisRef + java_field->name(), argument->indexedName(), argument->type());
2230 }
2231 s << "}" << endl << endl;
2232 }
2233
2234 writeFunctionName(s, setter, setter->ownerClass());
2235 s << endl;
2236 writeFinalFunctionArguments(s, setter);
2237 s << "{" << endl;
2238
2239 {
2240 Indentation indent(INDENT);
2241
2242 writeFinalFunctionSetup(s, setter, "__qt_object", setter->ownerClass());
2243
2244 Q_ASSERT(setter->arguments().count() == 1);
2245 const AbstractMetaArgument *argument = setter->arguments().at(0);
2246
2247 QString dest;
2248 if (setter->isStatic())
2249 dest = shellClassName(setter->ownerClass()) + "::";
2250 else
2251 dest = "__qt_object->";
2252
2253 QString src;
2254 if (!argument->type()->isPrimitive() && !argument->type()->typeEntry()->isStructInD())
2255 src = "__qt_" + argument->indexedName();
2256 else
2257 src = argument->indexedName();
2258
2259 if (setter->wasPublic())
2260 writeAssignment(s, dest + java_field->name(), src, argument->type());
2261 else
2262 s << INDENT << dest << setter->name() << "_setter(" << src << ");" << endl;
2263 }
2264 s << "}" << endl << endl;
2265 }
2266 }
2267
2268 void CppImplGenerator::writeFinalDestructor(QTextStream &s, const AbstractMetaClass *cls)
2269 {
2270 if (cls->hasConstructors()) {
2271 s << INDENT << "extern \"C\" DLL_PUBLIC void qtd_" << cls->name() << "_destructor(void *ptr)" << endl
2272 << INDENT << "{" << endl;
2273 {
2274 Indentation indent(INDENT);
2275 /* qtd
2276 if (!cls->isQObject() && !cls->generateShellClass()) {
2277 s << INDENT << "QtJambiLink *link = QtJambiLink::findLinkForUserObject(ptr);" << endl
2278 << INDENT << "if (link) link->resetObject(qtjambi_current_environment());" << endl;
2279 }
2280
2281 // Code injectsions...
2282 foreach (const CodeSnip &snip, cls->typeEntry()->codeSnips()) {
2283 if (snip.language == TypeSystem::DestructorFunction) {
2284 s << snip.code();
2285 }
2286 }
2287 */
2288 s << INDENT << "delete (" << shellClassName(cls) << " *)ptr;" << endl;
2289
2290 #if defined(QTJAMBI_DEBUG_TOOLS)
2291 s << INDENT << "qtjambi_increase_destructorFunctionCalledCount(QString::fromLatin1(\"" << cls->name() << "\"));" << endl;
2292 #endif
2293 }
2294
2295 s << INDENT << "}" << endl << endl;
2296 }
2297 }
2298
2299 void CppImplGenerator::writeFinalConstructor(QTextStream &s,
2300 const AbstractMetaFunction *java_function,
2301 const QString &qt_object_name,
2302 const QString &java_object_name)
2303 {
2304 const AbstractMetaClass *cls = java_function->ownerClass();
2305 AbstractMetaArgumentList arguments = java_function->arguments();
2306 QString className = cls->typeEntry()->qualifiedCppName();
2307
2308 bool hasShellClass = cls->generateShellClass();
2309
2310 s << INDENT << shellClassName(cls) << " *" << qt_object_name
2311 << " = new " << shellClassName(cls)
2312 << "(";
2313 writeFunctionCallArguments(s, java_function, "__qt_");
2314 s << ");" << endl;
2315 s << INDENT << "return (void *) " << qt_object_name << ";" << endl; // qtd
2316
2317 /* qtd s << INDENT << "QtJambiLink *__qt_java_link = ";
2318 if (cls->isQObject()) {
2319 s << "qtjambi_construct_qobject(__jni_env, " << java_object_name << ", "
2320 << qt_object_name << ")";
2321 } else {
2322 s << "qtjambi_construct_object(__jni_env, " << java_object_name << ", " << qt_object_name;
2323 if (cls->typeEntry()->isValue())
2324 s << ", \"" << className << "\")";
2325 else // non-QObject, object type
2326 s << ", QMetaType::Void, QLatin1String(\"" << cls->fullName().replace(".", "/") << "\"), true)";
2327 }
2328 s << ";" << endl
2329 << INDENT << "if (!__qt_java_link) {" << endl;
2330 {
2331 Indentation indent(INDENT);
2332 s << INDENT << "qWarning(\"object construction failed for type: "
2333 << className << "\");" << endl
2334 << INDENT << "return;" << endl;
2335 }
2336 s << INDENT << "}" << endl;
2337
2338
2339 if (cls->isQObject()) {
2340 // Make sure all qobjects created by Java are owned by java only if
2341 // parent object has not been set.
2342 // All other objects will default to split ownership.
2343 s << INDENT << "if(!__qt_this->QObject::parent()){" << endl;
2344 s << INDENT << " __qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl;
2345 s << INDENT << "}" << endl;
2346 } else {
2347 // All non-qobjects created by Java are owned by java
2348 s << INDENT << "__qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl;
2349 }
2350
2351 if (hasCustomDestructor(cls)) {
2352 s << INDENT << "__qt_java_link->setDestructorFunction(qtjambi_destructor);" << endl;
2353 }
2354
2355 if (cls->typeEntry()->typeFlags() & ComplexTypeEntry::DeleteInMainThread)
2356 s << INDENT << "__qt_java_link->setDeleteInMainThread(true);" << endl;
2357
2358 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions() && !cls->typeEntry()->isObject())
2359 return;
2360
2361 if (hasShellClass) {
2362 // Set up the link object
2363 s << INDENT << qt_object_name << "->m_link = __qt_java_link;" << endl;
2364
2365 // Make sure the user data in the QObject has bindings to the qobject's meta object
2366 // (this has to be done after the link is set, so that the fake meta object
2367 // can access the java object, for which it gets a reference in the link)
2368 if (cls->isQObject())
2369 s << INDENT << qt_object_name << "->m_link->setMetaObject(" << qt_object_name << "->metaObject());" << endl;
2370
2371 s << INDENT << qt_object_name << "->m_link->setCreatedByJava(true);" << endl;
2372
2373
2374
2375 AbstractMetaClassList interfaces = cls->interfaces();
2376 if (interfaces.size() + (cls->baseClass() != 0 ? 1 : 0) > 1) {
2377 if (cls->baseClass() != 0)
2378 interfaces += cls->baseClass();
2379 foreach (AbstractMetaClass *iface, interfaces) {
2380 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface;
2381 s << INDENT << qt_object_name << "->m_link->registerSubObject((" << impl->qualifiedCppName() << " *) " << qt_object_name << ");" << endl;
2382 }
2383 }
2384 }
2385
2386 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions())
2387 return;
2388
2389 // Set up the vtable
2390 s << INDENT;
2391 QString space(24, ' ');
2392 if (hasShellClass)
2393 s << qt_object_name << "->m_vtable = ";
2394 s << "qtjambi_setup_vtable(__jni_env, " << endl << space << "__jni_object, " << endl;
2395
2396 if (cls->hasInconsistentFunctions()) {
2397 s << space << "qtjambi_inconsistent_count, " << endl
2398 << space << "qtjambi_inconsistent_names, " << endl
2399 << space << "qtjambi_inconsistent_signatures, " << endl;
2400 } else {
2401 s << space << "0, 0, 0, // no inconsistent functions" << endl;
2402 }
2403
2404 if (cls->hasVirtualFunctions()) {
2405 s << space << "qtjambi_method_count, " << endl
2406 << space << "qtjambi_method_names, " << endl
2407 << space << "qtjambi_method_signatures" << endl;
2408 } else {
2409 s << space << "0, 0, 0 // no virtual functions" << endl;
2410 }
2411
2412 s << space << ");" << endl; */
2413 }
2414
2415 void CppImplGenerator::writeSignalInitialization(QTextStream &s, const AbstractMetaClass *java_class)
2416 {
2417 if (!java_class->isQObject()
2418 || java_class->queryFunctions(AbstractMetaClass::Signals | AbstractMetaClass::Visible | AbstractMetaClass::NotRemovedFromTargetLang).size() == 0) {
2419 return ;
2420 }
2421
2422 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_signalInitialization", "jboolean")
2423 << endl << "(JNIEnv *__jni_env, jobject java_object, jlong ptr, jstring java_signal_name)" << endl
2424 << "{" << endl
2425 << " QtJambiLink *link = (QtJambiLink *) ptr;" << endl
2426 << " if (link == 0)" << endl
2427 << " return true;" << endl << endl
2428 << " QObject *qt_this = link->qobject();" << endl
2429 << " Q_ASSERT(qt_this);" << endl << endl
2430 << " QtJambi_SignalWrapper_" << java_class->name() << " *qt_wrapper = "
2431 << " (QtJambi_SignalWrapper_" << java_class->name() << " *) link->signalWrapper();" << endl
2432 << " if (qt_wrapper == 0) {" << endl
2433 << " qt_wrapper = new QtJambi_SignalWrapper_" << java_class->name() << ";" << endl
2434 << " link->setSignalWrapper(qt_wrapper);" << endl
2435 << " qt_wrapper->link = link;" << endl << endl
2436 << " qtjambi_resolve_signals(__jni_env," << endl
2437 << " java_object," << endl
2438 << " qt_wrapper->m_signals," << endl
2439 << " qtjambi_signal_count," << endl
2440 << " (char **) qtjambi_signal_names," << endl
2441 << " (int *) qtjambi_signal_argumentcounts);" << endl
2442 << " }" << endl
2443 << " QString signal_name = qtjambi_to_qstring(__jni_env, java_signal_name);" << endl
2444 << " return qtjambi_connect_cpp_to_java(__jni_env," << endl
2445 << " signal_name," << endl
2446 << " qt_this," << endl
2447 << " qt_wrapper," << endl
2448 << " QLatin1String(\"" << java_class->fullName() << "\")," << endl
2449 << " QLatin1String(\"" << signalWrapperPrefix() << "\"));" << endl
2450 << "}";
2451 }
2452
2453 QString CppImplGenerator::fromObject(const TypeEntry *entry,
2454 const QString &var_name)
2455 {
2456 QString returned;
2457 QString package = entry->javaPackage();
2458 const ComplexTypeEntry *centry = entry->isComplex()
2459 ? static_cast<const ComplexTypeEntry *>(entry)
2460 : 0;
2461
2462 if (centry == 0 || centry->polymorphicIdValue().isEmpty()) {
2463 /* qtd returned = "qtjambi_from_object(__jni_env, " + var_name + ", \""
2464 + entry->lookupName()
2465 + "\", \"" + QString(package).replace(".", "/") + "/\", true);";
2466 */
2467 if(entry->isObject())
2468 returned = var_name + ";";
2469 else
2470 returned = "new " + entry->lookupName() + "(" + var_name +");";
2471 } else {
2472 AbstractMetaClass *cls = classes().findClass(centry->qualifiedCppName());
2473 if (!cls) {
2474 qFatal("CppImplGenerator::fromObject(): class '%s' could not be resolved...",
2475 qPrintable(centry->qualifiedCppName()));
2476 }
2477
2478 while (cls != 0 && !cls->typeEntry()->isPolymorphicBase())
2479 cls = cls->baseClass();
2480
2481 QString full_name;
2482 if (cls != 0) {
2483 full_name = cls->fullName();
2484 } else {
2485 ReportHandler::warning(QString("class '%1' has polymorphic id but does not inherit a polymorphic class")
2486 .arg(centry->qualifiedCppName()));
2487 }
2488 /* qtd
2489 returned = "qtjambi_from_object(__jni_env, " + var_name + ", \""
2490 + centry->lookupName()
2491 + "\", \"" + QString(package).replace(".", "/") + "/\","
2492 + "\"" + jni_signature(full_name, Underscores) + "\", true); // fucking complex";
2493 */
2494 if(entry->isObject())
2495 returned = var_name + "; // complex entry";
2496 else
2497 returned = "new " + centry->lookupName() + "(" + var_name +"); // complex entry";
2498 }
2499
2500 return returned;
2501 }
2502
2503 void CppImplGenerator::writeOriginalMetaObjectFunction(QTextStream &s, const AbstractMetaClass *java_class)
2504 {
2505 Q_ASSERT(java_class->isQObject());
2506
2507 s << jni_function_signature(java_class->package(),
2508 java_class->name(),
2509 "originalMetaObject",
2510 "jlong");
2511
2512 s << endl
2513 << "(JNIEnv *," << endl
2514 << " jclass)" << endl
2515 << "{" << endl;
2516 {
2517 Indentation indent(INDENT);
2518 s << INDENT << "return reinterpret_cast<jlong>(&" << java_class->qualifiedCppName() << "::staticMetaObject);" << endl;
2519 }
2520 s << "}" << endl << endl;
2521 }
2522
2523 void CppImplGenerator::writeFromNativeFunction(QTextStream &s, const AbstractMetaClass *java_class)
2524 {
2525 s << jni_function_signature(java_class->package(),
2526 java_class->name(),
2527 "fromNativePointer",
2528 "jobject");
2529 s << endl
2530 << "(JNIEnv *__jni_env," << endl
2531 << " jclass," << endl
2532 << " jobject nativePointer)" << endl
2533 << "{" << endl;
2534 {
2535 Indentation indent(INDENT);
2536 s << INDENT << "void *ptr = qtjambi_to_cpointer(__jni_env, nativePointer, 1);" << endl
2537 << INDENT << "return " << fromObject(java_class->typeEntry(), "ptr") << endl
2538 << "}" << endl;
2539 }
2540 }
2541
2542 void CppImplGenerator::writeFromArrayFunction(QTextStream &s, const AbstractMetaClass *java_class)
2543 {
2544 s << jni_function_signature(java_class->package(),
2545 java_class->name(),
2546 "nativePointerArray",
2547 "jobject");
2548 s << endl
2549 << "(JNIEnv *__jni_env," << endl
2550 << " jclass," << endl
2551 << " jobjectArray array)" << endl
2552 << "{" << endl;
2553 {
2554 Indentation indent(INDENT);
2555 s << INDENT << "return qtjambi_array_to_nativepointer(__jni_env, " << endl
2556 << INDENT << " array, " << endl
2557 << INDENT << " sizeof("
2558 << java_class->qualifiedCppName() << "));" << endl;
2559 }
2560 s << "}" << endl;
2561 }
2562
2563
2564 void CppImplGenerator::writeInterfaceCastFunction(QTextStream &s,
2565 const AbstractMetaClass *java_class,
2566 const AbstractMetaClass *interface)
2567 {
2568 Q_ASSERT(interface->isInterface());
2569 const InterfaceTypeEntry *ie = static_cast<const InterfaceTypeEntry *>(interface->typeEntry());
2570 QString interface_name = ie->origin()->targetLangName();
2571
2572 s << endl
2573 << jni_function_signature(java_class->package(),
2574 java_class->name(),
2575 QString("__qt_cast_to_%1").arg(interface_name),
2576 "jlong",
2577 "__J");
2578
2579 s << endl
2580 << "(JNIEnv *," << endl
2581 << " jobject," << endl
2582 << " jlong ptr)" << endl
2583 << "{" << endl
2584 << " return (jlong) (" << interface->primaryInterfaceImplementor()->qualifiedCppName() << " *) "
2585 << "(" << java_class->qualifiedCppName() << " *) ptr;" << endl
2586 << "}" << endl;
2587 }
2588
2589 bool CppImplGenerator::writeConversionRule(QTextStream &s,
2590 TypeSystem::Language target_language,
2591 const AbstractMetaFunction *java_function,
2592 int argument_index,
2593 const QString &qt_name,
2594 const QString &java_name)
2595 {
2596 if (argument_index < 0 || java_function == 0)
2597 return false;
2598
2599 QString conversion_rule = java_function->conversionRule(target_language, argument_index);
2600
2601 if (!conversion_rule.isEmpty()) {
2602 QString qt_name_var;
2603 QString java_name_var;
2604
2605 if ((argument_index == 0 && target_language == TypeSystem::NativeCode)
2606 || (argument_index != 0 && target_language == TypeSystem::ShellCode)) {
2607 qt_name_var = "%in";
2608 java_name_var = "%out";
2609 } else {
2610 qt_name_var = "%out";
2611 java_name_var = "%in";
2612 }
2613
2614 conversion_rule = conversion_rule.replace(qt_name_var, qt_name)
2615 .replace(java_name_var, java_name);
2616
2617 AbstractMetaArgumentList arguments = java_function->arguments();
2618 for (int i=0; i<arguments.size(); ++i) {
2619 conversion_rule = conversion_rule.replace("%" + QString::number(i+1),
2620 arguments.at(i)->indexedName());
2621 }
2622
2623 QStringList lines = conversion_rule.split("\n");
2624 foreach (QString line, lines) {
2625 s << INDENT << line.trimmed() << endl;
2626 }
2627
2628 return true;
2629 } else {
2630 return false;
2631 }
2632 }
2633
2634
2635 void CppImplGenerator::writeJavaToQt(QTextStream &s,
2636 const AbstractMetaClass *java_class,
2637 const AbstractMetaType *function_return_type,
2638 const QString &qt_name,
2639 const QString &java_name,
2640 const AbstractMetaFunction *java_function,
2641 int argument_index)
2642 {
2643 // Conversion to C++: Shell code for return values, native code for arguments
2644 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode;
2645 if (writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name))
2646 return;
2647
2648 s << INDENT << shellClassName(java_class) << " *" << qt_name << " = ("
2649 << shellClassName(java_class) << " *) ";
2650 if (java_class->isQObject())
2651 s << "qtjambi_to_qobject";
2652 else
2653 s << "qtjambi_to_object";
2654 s << "(__jni_env, " << java_name << ");" << endl;
2655 if (java_class->isQObject()) {
2656 // ### throw exceptions when objects are null...
2657 s << INDENT << "if (!" << qt_name << ") "
2658 << default_return_statement_java(function_return_type) << ";" << endl << endl;
2659 }
2660 }
2661
2662
2663 void CppImplGenerator::writeJavaToQt(QTextStream &s,
2664 const AbstractMetaType *java_type,
2665 const QString &qt_name,
2666 const QString &java_name,
2667 const AbstractMetaFunction *java_function,
2668 int argument_index,
2669 Option options)
2670 {
2671 // Conversion to C++: Shell code for return values, native code for arguments
2672 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode;
2673 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name))
2674 return;
2675
2676 if (java_type == 0) {
2677 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for "
2678 "language '%4'")
2679 .arg(argument_index)
2680 .arg(java_function->implementingClass()->name())
2681 .arg(java_function->name())
2682 .arg(int(lang));
2683 ReportHandler::warning(warn);
2684 return;
2685 }
2686 if (java_type->name() == "QModelIndex") {
2687 s << INDENT << "QModelIndex " << qt_name << " = qtd_to_QModelIndex("
2688 << java_name << ");" << endl;
2689 } else if (java_type->typeEntry()->isStructInD()) {
2690 // empty
2691 } else if (java_type->typeEntry() && java_type->typeEntry()->qualifiedCppName() == "QString") {
2692 s << INDENT << "QString " << qt_name
2693 << " = " << "QString::fromUtf8(" << java_name << ", " << java_name << "_size);" << endl;
2694 } else if (java_type->isJObjectWrapper()) {
2695 s << INDENT << "JObjectWrapper " << qt_name
2696 << " = qtjambi_to_jobjectwrapper(__jni_env, " << java_name << ");" << endl;
2697 } else if (java_type->isVariant()) {
2698 s << INDENT << "QVariant " << qt_name
2699 << " = " << java_name << " == NULL ? QVariant() : QVariant(*" << java_name << ");" << endl;
2700 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) {
2701 AbstractMetaType *elementType = java_type->arrayElementType();
2702
2703 // ### Don't assert on wrong array lengths
2704 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl;
2705 s << INDENT;
2706 writeTypeInfo(s, elementType);
2707 s << " " << qt_name << "[" << java_type->arrayElementCount() << "];" << endl;
2708
2709 s << INDENT << "__jni_env->" << getXxxArrayRegion(elementType) << "( (" << translateType(java_type, options)
2710 << ")" << java_name << ", 0, " << java_type->arrayElementCount() << ", "
2711 << "(" << translateType(elementType, options) << " *" << ")"
2712 << qt_name << ");" << endl;
2713
2714 } else if (java_type->isArray()) {
2715 AbstractMetaType *elementType = java_type->arrayElementType();
2716
2717 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl;
2718 writeTypeInfo(s, elementType);
2719 s << "[" << java_type->arrayElementCount() << "]" << qt_name << ";" << endl;
2720
2721 for (int i=0; i<java_type->arrayElementCount(); ++i) {
2722 writeJavaToQt(s, elementType, qt_name + "[" + QString::number(i) + "]",
2723 "__jni_env->GetObjectArrayElement(" + java_name + ", " + QString::number(i) + ")", 0, -1, options);
2724 }
2725
2726 } else if (java_type->isTargetLangString()) {
2727 s << INDENT << "QString " << qt_name
2728 << " = " << "QString::fromUtf8(" << java_name << ", " << java_name << "_size);" << endl;
2729 // qtd << " = qtjambi_to_qstring(__jni_env, (jstring) " << java_name << ");" << endl;
2730
2731 } else if (java_type->isTargetLangChar()) {
2732 s << INDENT << "QChar " << qt_name
2733 << " = (ushort)" << java_name << ";" << endl;
2734
2735 } else if (java_type->isEnum() || java_type->isFlags()) {
2736
2737 bool written = false;
2738 if (java_type->isEnum()) {
2739 AbstractMetaEnum *java_enum =
2740 m_classes.findEnum(static_cast<const EnumTypeEntry *>(java_type->typeEntry()));
2741 if (java_enum && !java_enum->isPublic()) {
2742
2743 s << INDENT << "int " << qt_name << " = ";
2744 written = true;
2745 }
2746 }
2747
2748 if (!written) {
2749 QString qualified_name = java_type->typeEntry()->qualifiedCppName();
2750 s << INDENT << qualified_name << " " << qt_name
2751 << " = (" << qualified_name << ") ";
2752 }
2753
2754 if ((options & EnumAsInts) == 0 && (java_type->isTargetLangEnum() || java_type->isTargetLangFlags())) {
2755 s << "qtjambi_to_enumerator(__jni_env, " << java_name << ");" << endl;
2756
2757 } else if (options & BoxedPrimitive) {
2758 const PrimitiveTypeEntry *pentry = TypeDatabase::instance()->findTargetLangPrimitiveType("int");
2759 Q_ASSERT(pentry);
2760
2761 s << java_name << ";" << endl;
2762
2763 } else {
2764 s << java_name << ';' << endl;
2765 }
2766
2767 } else if (java_type->isContainer()) {
2768 writeJavaToQtContainer(s, java_type, qt_name, java_name, 0, -1);
2769
2770 } else if (java_type->isThread()) {
2771 s << INDENT << "QThread *" << qt_name << " = qtjambi_to_thread(__jni_env, " << java_name
2772 << ");" << endl;
2773
2774 } else if (java_type->typeEntry()->isCustom()) {
2775 const CustomTypeEntry *custom_type =
2776 static_cast<const CustomTypeEntry *>(java_type->typeEntry());
2777 s << INDENT;
2778 custom_type->generateCppJavaToQt(s, java_type, "__jni_env", qt_name, java_name);
2779 s << ";" << endl;
2780
2781 } else {
2782
2783 const TypeEntry *type = java_type->typeEntry();
2784 QString class_name = type->name();
2785 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName());
2786
2787 // Declaration and the c-cast
2788 s << INDENT;
2789 writeTypeInfo(s, java_type);
2790 s << ' ' << qt_name << " = (";
2791 writeTypeInfo(s, java_type);
2792 s << ") ";
2793
2794 if (java_type->isPrimitive()) {
2795 if (options & BoxedPrimitive) {
2796 const PrimitiveTypeEntry *pentry = static_cast<const PrimitiveTypeEntry *>(type);
2797 //std::cout << "---error_here " << type->targetLangName().toStdString() << " \n";
2798 //std::cout << "----func_here " << java_function->marshalledName().toStdString() << " \n";
2799
2800 if (!pentry->preferredConversion())
2801 pentry = TypeDatabase::instance()->findTargetLangPrimitiveType(pentry->targetLangName());
2802 Q_ASSERT(pentry);
2803
2804 s << java_name << ";" << endl;
2805
2806 } else if ((options & GlobalRefJObject) && type->jniName() == QLatin1String("jobject")) {
2807 s << "__jni_env->NewGlobalRef(" << java_name << ");" << endl;
2808 } else {
2809 s << java_name << ';' << endl;
2810 }
2811
2812 #if 0
2813 } else if (java_type->isEnum()) {
2814 s << "qtjambi_to_enum(__jni_env, " << java_name << ");" << endl;
2815 #endif
2816
2817 } else if ((java_type->isQObject() || java_type->isObject())
2818 && static_cast<const ObjectTypeEntry *>(type)->designatedInterface()) {
2819 /* qtd const InterfaceTypeEntry *ie =
2820 static_cast<const ObjectTypeEntry *>(type)->designatedInterface();
2821 s << "qtjambi_to_interface(__jni_env, ";
2822
2823 // This cast is only valid if we're dealing with a native id
2824 if ((options & UseNativeIds) == UseNativeIds)
2825 s << "(QtJambiLink *)";
2826 */
2827 s << java_name << ";" << endl;
2828 /* qtd
2829 s << "\"" << ie->targetLangName() << "\", \""
2830 << ie->javaPackage().replace(".", "/") << "/\", "
2831 << "\"__qt_cast_to_" << type->targetLangName() << "\");" << endl;
2832 */
2833 } else if (java_type->isObject() || java_type->isQObject() || java_type->isNativePointer()) {
2834 if (java_type->isReference()) {
2835 s << "* (" << qualified_class_name << " "
2836 << QString(java_type->actualIndirections(), '*') << ") ";
2837 }
2838
2839 if (java_type->isNativePointer()) {
2840 /* qtd s << "qtjambi_to_cpointer("
2841 << "__jni_env, "
2842 << java_name << ", "
2843 << java_type->actualIndirections() << ");" << endl; */
2844 s << java_name << ";" << endl; // qtd
2845 }/* qtd else if (java_type->isQObject()) {
2846 if ((options & UseNativeIds) == 0)
2847 s << "qtjambi_to_qobject(__jni_env, ";
2848 else
2849 s << "qtjambi_from_jlong(";
2850 s << java_name;
2851 s << ");" << endl;
2852 }*/ else {
2853 /* qtd if ((options & UseNativeIds) == 0)
2854 s << "qtjambi_to_object(__jni_env, ";
2855 else
2856 s << "qtjambi_from_jlong(";
2857 */ s << java_name;
2858 s << ";" << endl; // +
2859 // qtd s << ");" << endl;
2860 }
2861
2862 } else {
2863 // Return values...
2864 if (argument_index == 0) {
2865 s << "(" << java_name << " != 0 ? *(" << qualified_class_name << " *)";
2866 /* qtd if ((options & UseNativeIds) == 0)
2867 s << "qtjambi_to_object(__jni_env, ";
2868 else
2869 s << "qtjambi_from_jlong(";
2870 */ s << java_name;
2871 s << " : " << qualified_class_name << "());" << endl;
2872 } else {
2873 s << "*"
2874 << "(" << qualified_class_name << " *)";
2875 bool null_check = false;
2876 /* qtd if ((options & UseNativeIds) == 0) {
2877 s << "qtjambi_to_object(__jni_env, ";
2878 } else if (hasDefaultConstructor(java_type)) {
2879 null_check = true;
2880 s << "(" << java_name << " != 0 ? qtjambi_from_jlong(";
2881 } else {
2882 s << "qtjambi_from_jlong(";
2883 }
2884 */ s << java_name;
2885 // qtd s << ")";
2886
2887 if (null_check)
2888 s << " : default_" << QString(qualified_class_name).replace("::", "_") << "())";
2889 s << ";" << endl;
2890 }
2891
2892 }
2893 }
2894 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl;
2895 }
2896
2897 static int nativePointerType(const AbstractMetaType *java_type)
2898 {
2899 Q_ASSERT(java_type);
2900 Q_ASSERT(java_type->isNativePointer());
2901
2902 if (!java_type->typeEntry()->isPrimitive())
2903 return PointerType;
2904
2905 if (java_type->indirections() > 1)
2906 return PointerType;
2907
2908 static QHash<QString, int> types;
2909 if (types.isEmpty()) {
2910 types["boolean"] = BooleanType;
2911 types["byte"] = ByteType;
2912 types["char"] = CharType;
2913 types["short"] = ShortType;
2914 types["int"] = IntType;
2915 types["long"] = LongType;
2916 types["float"] = FloatType;
2917 types["double"] = DoubleType;
2918 }
2919
2920 QString targetLangName = java_type->typeEntry()->targetLangName();
2921 if (!types.contains(targetLangName))
2922 return PointerType;
2923
2924 return types[targetLangName];
2925 }
2926
2927 void CppImplGenerator::writeQtToJava(QTextStream &s,
2928 const AbstractMetaType *java_type,
2929 const QString &qt_name,
2930 const QString &java_name,
2931 const AbstractMetaFunction *java_function,
2932 int argument_index,
2933 Option option)
2934 {
2935
2936 // Conversion to Java: Native code for return values, shell code for arguments
2937 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode;
2938 /* qtd if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name))
2939 return;
2940 */
2941 if (java_type == 0) {
2942 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for "
2943 "language '%4'")
2944 .arg(argument_index)
2945 .arg(java_function->implementingClass()->name())
2946 .arg(java_function->name())
2947 .arg(int(lang));
2948 ReportHandler::warning(warn);
2949 return;
2950 }
2951
2952 if (java_type->name() == "QModelIndex") {
2953 QString prefix = "*";
2954 if (option & BoxedPrimitive)
2955 s << INDENT << "QModelIndexAccessor tmp_index = qtd_from_QModelIndex(" << qt_name << ");" << endl
2956 << INDENT << "QModelIndexAccessor *" << java_name << " = &tmp_index;" << endl;
2957 else
2958 s << INDENT << "*" << java_name << " = qtd_from_QModelIndex(" << qt_name << ");" << endl;
2959
2960 } else if(java_type->typeEntry()->isStructInD()) {
2961 s << INDENT << java_type->typeEntry()->name() << " *" << java_name << " = (" << java_type->typeEntry()->name() << " *) &"
2962 << qt_name << ";" << endl; // do nothing
2963 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) {
2964 AbstractMetaType *elementType = java_type->arrayElementType();
2965
2966 s << INDENT << translateType(java_type, option) << " " << java_name << " = __jni_env->" << newXxxArray(elementType)
2967 << "(" << java_type->arrayElementCount() << ");" << endl;
2968
2969 s << INDENT << "__jni_env->" << setXxxArrayRegion(elementType) << "("
2970 << "(" << translateType(java_type, option) << ")" << java_name
2971 << ", 0, " << java_type->arrayElementCount() << ", "
2972 << "(" << translateType(elementType, option) << " *" << ")"
2973 << qt_name << ");" << endl;
2974
2975 } else if (java_type->isArray()) {
2976 AbstractMetaType *elementType = java_type->arrayElementType();
2977
2978 s << INDENT << "jobject " << java_name << " = __jni_env->NewObjectArray("
2979 << java_type->arrayElementCount() << ");" << endl;
2980
2981 s << "jobject __qt_element = 0;";
2982
2983 for (int i=0; i<java_type->arrayElementCount(); ++i) {
2984 writeQtToJava(s, elementType, qt_name + "[" + QString::number(i) + "]",
2985 "__qt_element", 0, -1, option);
2986 s << "__jni_env->SetObjectArrayElement((jobjectArray) " << java_name << ", "
2987 << i << ", __qt_element);" << endl;
2988 }
2989
2990 } else if (java_type->isPrimitive()) {
2991 const PrimitiveTypeEntry *type =
2992 static_cast<const PrimitiveTypeEntry *>(java_type->typeEntry());
2993
2994 Q_ASSERT(type);
2995 QString ret_val;
2996 if (java_function)
2997 ret_val = jniReturnName(java_function);
2998 else
2999 ret_val = fixCppTypeName(java_type->typeEntry()->qualifiedCppName());
3000 s << INDENT << ret_val << " " << java_name << " = " << qt_name << ";" << endl;
3001 } else if (java_type->isJObjectWrapper()) {
3002 s << INDENT << "jobject " << java_name << " = qtjambi_from_jobjectwrapper(__jni_env, "
3003 << qt_name << ");" << endl;
3004 } else if (java_type->isVariant()) {
3005 s << INDENT << "QVariant *" << java_name
3006 << " = new QVariant(" << qt_name << ");" << endl;
3007
3008 } else if (java_type->isTargetLangString()) {
3009
3010 // if (option & BoxedPrimitive)
3011 s << INDENT << "_d_toUtf8(" << qt_name << ".utf16(), "
3012 << qt_name << ".size(), " << java_name << ");" << endl;
3013
3014 } else if (java_type->isTargetLangChar()) {
3015 s << INDENT << "jchar " << java_name << " = " << qt_name << ".unicode();" << endl;
3016
3017 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags()
3018 || ((option & EnumAsInts) && (java_type->isEnum() || java_type->isFlags()))) {
3019 // } else if (java_type->isEnum() || java_type->isFlags()) {
3020
3021 // if (option & EnumAsInts) {
3022 // qDebug() << java_type->name() << "should be int...";
3023 // }
3024
3025 /* if (option & BoxedPrimitive) {
3026 s << INDENT << "jobject " << java_name << " = qtjambi_from_int(__jni_env, "
3027 << qt_name << ");" << endl;
3028 } else */{
3029 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl;
3030 }
3031
3032 } else if (java_type->isTargetLangEnum()) {
3033 Q_ASSERT((option & EnumAsInts) == 0);
3034 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry());
3035 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl;
3036
3037 } else if (java_type->isTargetLangFlags()) {
3038 Q_ASSERT((option & EnumAsInts) == 0);
3039 const FlagsTypeEntry *ft = static_cast<const FlagsTypeEntry *>(java_type->typeEntry());
3040 s << INDENT << "jobject " << java_name << " = qtjambi_from_flags(__jni_env, "
3041 << qt_name << ", \"" << ft->javaPackage().replace('.', '/') << '/'
3042 << ft->originator()->javaQualifier() << '$' << ft->targetLangName() << "\");" << endl;
3043
3044 } else if (java_type->isContainer()) {
3045 writeQtToJavaContainer(s, java_type, qt_name, java_name, 0, -1);
3046
3047 } else if (java_type->isThread()) {
3048 s << INDENT << "jobject " << java_name << " = qtjambi_from_thread(__jni_env, " << qt_name
3049 << ");" << endl;
3050
3051 } else if (!java_type->isNativePointer() && java_type->typeEntry()->isCustom()) {
3052 s << INDENT;
3053 static_cast<const CustomTypeEntry *>(java_type->typeEntry())
3054 ->generateCppQtToJava(s, java_type, "__jni_env", qt_name, java_name);
3055 s << ";" << endl;
3056
3057 } else {
3058 QString return_type;
3059 if (java_function)
3060 return_type = jniReturnName(java_function);
3061 else {
3062 return_type = jniReturnType(java_type);
3063 return_type = fixCppTypeName(return_type);
3064 // return_type = fixCppTypeName(java_type->typeEntry()->qualifiedCppName());
3065 }
3066 /* if( (java_type->isValue() && !java_type->typeEntry()->isStructInD())
3067 || java_type->isObject() )
3068 s << INDENT << return_type << " *" << java_name << " = (" << return_type << "*) ";
3069 else*/
3070 s << INDENT << return_type << " " << java_name << " = (" << return_type << ") ";
3071
3072 if (java_type->isQObject()) {
3073 /* qtd s << "qtjambi_from_qobject(__jni_env, " << "(QObject *) ";
3074
3075 if (java_type->isReference() && java_type->indirections() == 0)
3076 s << "&";
3077
3078 s << qt_name
3079 << ", \"" << java_type->typeEntry()->lookupName() << "\""
3080 << ", \"" << java_type->package().replace(".", "/") << "/\""
3081 << ");" << endl;
3082 */
3083 s << qt_name << ";" << endl;
3084
3085 #if 0
3086 } else if (java_type->isEnum()) {
3087
3088 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry());
3089 s << "qtjambi_from_enum(__jni_env, " << qt_name << ", \""
3090 << et->javaQualifier() << "$" << et->targetLangName() << "\");" << endl;
3091 #endif
3092 } else if (java_type->isNativePointer()) {
3093 /* qtd s << "qtjambi_from_cpointer(__jni_env, ";
3094 if (java_type->isReference())
3095 s << "&";
3096 s << qt_name << ", " << nativePointerType(java_type) << ", "
3097 << java_type->actualIndirections() << ");" << endl;
3098 */
3099 if (java_type->isReference())
3100 s << "&";
3101 s << qt_name << ";" << "// qtjambi_from_cpointer" << endl;
3102 } else if (java_type->isValue()) {
3103 // qtd s << fromObject(java_type->typeEntry(), "&" + qt_name) << endl;
3104 s << "new " << java_type->typeEntry()->qualifiedCppName() << "(" << qt_name << ");" << endl;
3105 } else {
3106 // qtd s << fromObject(java_type->typeEntry(),
3107 // qtd (java_type->isReference() ? "&" : "") + qt_name) << endl;
3108 s << qt_name << ";" << endl;
3109 }
3110 }
3111
3112 }
3113
3114 QString CppImplGenerator::getTypeName(const TypeEntry *entry, Option option)
3115 {
3116 if(entry->isEnum() && (option & EnumAsInts))
3117 return "int";
3118
3119 return entry->lookupName();
3120 }
3121
3122 void CppImplGenerator::writeQtToJavaContainer(QTextStream &s,
3123 const AbstractMetaType *java_type,
3124 const QString &qt_name,
3125 const QString &java_name,
3126 const AbstractMetaFunction *java_function,
3127 int argument_index)
3128 {
3129 // Language for conversion to Java: Native code for return values and Shell code for arguments
3130 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode;
3131 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name))
3132 return;
3133
3134 if (java_type == 0) {
3135 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for "
3136 "language '%4'")
3137 .arg(argument_index)
3138 .arg(java_function->implementingClass()->name())
3139 .arg(java_function->name())
3140 .arg(int(lang));
3141 ReportHandler::warning(warn);
3142 return;
3143 }
3144
3145 Q_ASSERT(java_type->isContainer());
3146 const ContainerTypeEntry *type =
3147 static_cast<const ContainerTypeEntry *>(java_type->typeEntry());
3148
3149 if (type->type() == ContainerTypeEntry::ListContainer
3150 || type->type() == ContainerTypeEntry::VectorContainer
3151 || type->type() == ContainerTypeEntry::StringListContainer
3152 || type->type() == ContainerTypeEntry::LinkedListContainer
3153 || type->type() == ContainerTypeEntry::StackContainer
3154 || type->type() == ContainerTypeEntry::SetContainer
3155 || type->type() == ContainerTypeEntry::QueueContainer) {
3156
3157 Q_ASSERT(java_type->instantiations().size() == 1);
3158 AbstractMetaType *targ = java_type->instantiations().first();
3159
3160 QString cls_name = getTypeName(targ->typeEntry(), EnumAsInts);
3161 cls_name.remove("_ConcreteWrapper");
3162
3163 s << endl
3164 << INDENT;
3165
3166 switch (type->type()) {
3167 case ContainerTypeEntry::LinkedListContainer:
3168 case ContainerTypeEntry::QueueContainer:
3169 s << "qtjambi_linkedlist_new(__jni_env)";
3170 break;
3171 case ContainerTypeEntry::StackContainer:
3172 s << "qtjambi_stack_new(__jni_env)";
3173 break;
3174 case ContainerTypeEntry::SetContainer:
3175 s << "qtjambi_hashset_new(__jni_env)";
3176 break;
3177 default:
3178 s << "qtd_allocate_" << cls_name
3179 << "_array(" << java_name << ", " << qt_name << ".size())";
3180 break;
3181 }
3182
3183 s << ";" << endl
3184 << INDENT;
3185
3186
3187 writeTypeInfo(s, java_type, ForceValueType);
3188 s << "::const_iterator " << qt_name << "_end_it = " << qt_name << ".constEnd();" << endl
3189 << INDENT << "int i = 0;" << endl
3190 << INDENT;
3191 s << "for (";
3192 writeTypeInfo(s, java_type, ForceValueType);
3193 s << "::const_iterator " << qt_name << "_it = " << qt_name << ".constBegin(); "
3194 << qt_name << "_it != " << qt_name << "_end_it; ++" << qt_name << "_it) {" << endl;
3195 {
3196 Indentation indent(INDENT);
3197 s << INDENT;
3198 writeTypeInfo(s, targ);
3199 s << " __qt_tmp = *" << qt_name << "_it;" << endl;
3200
3201 if(targ->isTargetLangString())
3202 s << INDENT << "void *__java_tmp = qtd_string_from_array(" << java_name << ", i);" << endl;
3203
3204 writeQtToJava(s, targ, "__qt_tmp", "__java_tmp", 0, -1, BoxedPrimitive);
3205
3206 s << INDENT << "qtd_assign_" << cls_name << "_array_element(" << java_name << ", i, __java_tmp);" << endl;
3207 s << INDENT << "++i;" << endl;
3208 }
3209 s << INDENT << "}" << endl;
3210
3211 } else if (type->type() == ContainerTypeEntry::PairContainer) {
3212 QList<AbstractMetaType *> args = java_type->instantiations();
3213 Q_ASSERT(args.size() == 2);
3214
3215 s << INDENT << "jobject " << java_name << ";" << endl
3216 << INDENT << "{" << endl;
3217 {
3218 Indentation indent(INDENT);
3219 writeQtToJava(s, args.at(0), qt_name + ".first", "__java_tmp_first", 0, -1, BoxedPrimitive);
3220 writeQtToJava(s, args.at(1), qt_name + ".second", "__java_tmp_second", 0, -1, BoxedPrimitive);
3221 s << INDENT << java_name << " = qtjambi_pair_new(__jni_env, "
3222 << "__java_tmp_first, __java_tmp_second);" << endl;
3223 }
3224
3225 s << INDENT << "}" << endl;
3226
3227 } else if (type->type() == ContainerTypeEntry::MultiMapContainer) {
3228
3229 Q_ASSERT(java_type->instantiations().size() == 2);
3230 AbstractMetaType *targ_key = java_type->instantiations().at(0);
3231 AbstractMetaType *targ_val = java_type->instantiations().at(1);
3232
3233 s << endl
3234 << INDENT << "jobject " << java_name << " = qtjambi_treemap_new(__jni_env, " << qt_name << ".keys().size());" << endl
3235 << INDENT << "QList<";
3236 writeTypeInfo(s, targ_key);
3237 s << "> __qt_keys = " << qt_name << ".keys();" << endl
3238 << INDENT << "for (int i=0; i<__qt_keys.size(); ++i) {" << endl;
3239 {
3240 Indentation indent(INDENT);
3241
3242 s << INDENT;
3243 writeTypeInfo(s, targ_key);
3244 s << " __qt_tmp_key = __qt_keys.at(i);" << endl;
3245 writeQtToJava(s, targ_key, "__qt_tmp_key", "__java_tmp_key", 0, -1, BoxedPrimitive);
3246
3247 s << INDENT << "QList<";
3248 writeTypeInfo(s, targ_val);
3249 s << "> __qt_values = " << qt_name << ".values(__qt_tmp_key);" << endl
3250 << INDENT << "jobject __java_value_list = qtjambi_arraylist_new(__jni_env, __qt_values.size());" << endl
3251 << INDENT << "for (int j=0; j<__qt_values.size(); ++j) {" << endl;
3252 {
3253 Indentation indent(INDENT);
3254
3255 s << INDENT;
3256 writeTypeInfo(s, targ_val);
3257 s << " __qt_tmp_val = __qt_values.at(j);" << endl;
3258 writeQtToJava(s, targ_val, "__qt_tmp_val", "__java_tmp_val", 0, -1, BoxedPrimitive);
3259
3260 s << INDENT << "qtjambi_collection_add(__jni_env, __java_value_list, __java_tmp_val);" << endl;
3261 }
3262 s << INDENT << "}" << endl
3263 << INDENT << "qtjambi_map_put(__jni_env, " << java_name << ", __java_tmp_key, __java_value_list);" << endl;
3264 }
3265 s << INDENT << "}" << endl;
3266
3267 } else if (type->type() == ContainerTypeEntry::MapContainer
3268 || type->type() == ContainerTypeEntry::HashContainer) {
3269 QString constructor = type->type() == ContainerTypeEntry::MapContainer
3270 ? "qtjambi_treemap_new"
3271 : "qtjambi_hashmap_new";
3272
3273 Q_ASSERT(java_type->instantiations().size() == 2);
3274 AbstractMetaType *targ_key = java_type->instantiations().at(0);
3275 AbstractMetaType *targ_val = java_type->instantiations().at(1);
3276
3277 s << endl
3278 << INDENT << "jobject " << java_name << " = " << constructor << "(__jni_env, " << qt_name
3279 << ".size());" << endl
3280 << INDENT;
3281 writeTypeInfo(s, java_type, Option(ExcludeReference | ExcludeConst));
3282 s << "::const_iterator it;" << endl
3283 << INDENT << "for (it=" << qt_name << ".constBegin(); it!=" << qt_name << ".constEnd(); ++it) {" << endl;
3284 {
3285 Indentation indent(INDENT);
3286 s << INDENT;
3287 writeTypeInfo(s, targ_key);
3288 s << " __qt_tmp_key = it.key();" << endl
3289 << INDENT;
3290 writeTypeInfo(s, targ_val);
3291 s << " __qt_tmp_val = it.value();" << endl;
3292 writeQtToJava(s, targ_key, "__qt_tmp_key", "__java_tmp_key", 0, -1, BoxedPrimitive);
3293 writeQtToJava(s, targ_val, "__qt_tmp_val", "__java_tmp_val", 0, -1, BoxedPrimitive);
3294 s << INDENT << "qtjambi_map_put(__jni_env, " << java_name
3295 << ", __java_tmp_key, __java_tmp_val);" << endl;
3296 }
3297 s << INDENT << "}" << endl;
3298
3299 } else {
3300 ReportHandler::warning(QString("unable to generate container type %1, type=%2")
3301 .arg(java_type->name()).arg(type->type()));
3302 }
3303
3304 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl;
3305 }
3306
3307
3308 void CppImplGenerator::writeJavaToQtContainer(QTextStream &s,
3309 const AbstractMetaType *java_type,
3310 const QString &qt_name,
3311 const QString &java_name,
3312 const AbstractMetaFunction *java_function,
3313 int argument_index)
3314 {
3315 // Conversion to C++: Shell code for return value, native code for arguments
3316 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode;
3317 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name))
3318 return;
3319
3320 if (java_type == 0) {
3321 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for "
3322 "language '%4'")
3323 .arg(argument_index)
3324 .arg(java_function->implementingClass()->name())
3325 .arg(java_function->name())
3326 .arg(int(lang));
3327 ReportHandler::warning(warn);
3328 return;
3329 }
3330
3331
3332 Q_ASSERT(java_type->isContainer());
3333 const ContainerTypeEntry *type =
3334 static_cast<const ContainerTypeEntry *>(java_type->typeEntry());
3335
3336 if (type->type() == ContainerTypeEntry::ListContainer
3337 || type->type() == ContainerTypeEntry::VectorContainer
3338 || type->type() == ContainerTypeEntry::StringListContainer
3339 || type->type() == ContainerTypeEntry::LinkedListContainer
3340 || type->type() == ContainerTypeEntry::StackContainer
3341 || type->type() == ContainerTypeEntry::SetContainer
3342 || type->type() == ContainerTypeEntry::QueueContainer) {
3343 Q_ASSERT(java_type->instantiations().size() == 1);
3344 AbstractMetaType *targ = java_type->instantiations().first();
3345 QString elem_type = getTypeName(targ->typeEntry(), EnumAsInts);
3346 elem_type.remove("_ConcreteWrapper");
3347
3348 s << INDENT;
3349 writeTypeInfo(s, java_type, ForceValueType);
3350 s << qt_name << ";" << endl;
3351
3352 // qtd s << INDENT << "if (" << java_name << " != 0) {" << endl;
3353 {
3354 /* qtd Indentation indent(INDENT);
3355 s << INDENT << "jobjectArray __qt__array = qtjambi_collection_toArray(__jni_env, "
3356 << java_name << ");" << endl
3357 << INDENT << "jsize __qt__size = __jni_env->GetArrayLength(__qt__array);" << endl;
3358 */
3359 if (type->type() == ContainerTypeEntry::VectorContainer
3360 || type->type() == ContainerTypeEntry::StackContainer)
3361 s << INDENT << qt_name << ".reserve(" << java_name << "_size);" << endl;
3362
3363 s << INDENT << "for (int i=0; i<" << java_name << "_size; ++i) {" << endl;
3364 {
3365 Indentation indent(INDENT);
3366 if(targ->isTargetLangString())
3367 s << INDENT << "char* __d_element;" << endl
3368 << INDENT << "size_t __d_element_size;" << endl
3369 << INDENT << "qtd_get_string_from_array(" << java_name << ", i, &__d_element, &__d_element_size);" << endl;
3370 else {
3371 s << INDENT;
3372 writeTypeInfo(s, targ, Option(VirtualDispatch | ForcePointer));
3373 QString cast_string = "";
3374 const TypeEntry* centry = targ->typeEntry();
3375 if (centry->isComplex() && (centry->isObject() || centry->isValue() || centry->isInterface()))
3376 cast_string = "(void**)";
3377 s << "__d_element;" << endl
3378 << INDENT << "qtd_get_" << elem_type << "_from_array(" << java_name << ", i, &__d_element);" << endl;
3379 }
3380 /* qtd s << INDENT << "jobject __d_element = "
3381 << "__jni_env->GetObjectArrayElement(__qt__array, i);" << endl;*/
3382 writeJavaToQt(s, targ, "__qt_element", "__d_element", 0, -1, BoxedPrimitive);
3383 QString cont_element = "__qt_element";
3384 if(targ->typeEntry()->isStructInD() && targ->name() != "QModelIndex")
3385 cont_element = "__d_element";
3386 s << INDENT << qt_name << " << " << cont_element << ";" << endl;
3387 }
3388 // qtd s << INDENT << "}" << endl;
3389 }
3390 s << INDENT << "}" << endl;
3391 } else if (type->type() == ContainerTypeEntry::PairContainer) {
3392 QList<AbstractMetaType *> targs = java_type->instantiations();
3393 Q_ASSERT(targs.size() == 2);
3394
3395 s << INDENT;
3396 writeTypeInfo(s, java_type, ForceValueType);
3397 s << " " << qt_name << ";" << endl
3398 << INDENT << "if (" << java_name << " != 0) {" << endl;
3399 {
3400 // separate scope required just in case function takes two QPair's.
3401 Indentation indent(INDENT);
3402 s << INDENT << "jobject __java_first = qtjambi_pair_get(__jni_env, "
3403 << java_name << ", 0);" << endl;
3404 writeJavaToQt(s, targs.at(0), "__qt_first", "__java_first", 0, -1, BoxedPrimitive);
3405
3406 s << INDENT << "jobject __java_second = qtjambi_pair_get(__jni_env, "
3407 << java_name << ", 1);" << endl;
3408 writeJavaToQt(s, targs.at(1), "__qt_second", "__java_second", 0, -1, BoxedPrimitive);
3409
3410 s << INDENT << qt_name << ".first = __qt_first;" << endl
3411 << INDENT << qt_name << ".second = __qt_second;" << endl;
3412 }
3413 s << INDENT << "}" << endl;
3414 } else if (type->type() == ContainerTypeEntry::MapContainer
3415 || type->type() == ContainerTypeEntry::HashContainer) {
3416 Q_ASSERT(java_type->instantiations().size() == 2);
3417 AbstractMetaType *targ_key = java_type->instantiations().at(0);
3418 AbstractMetaType *targ_val = java_type->instantiations().at(1);
3419
3420 s << INDENT;
3421 writeTypeInfo(s, java_type, ForceValueType);
3422 s << qt_name << ";" << endl;
3423 s << INDENT << "if (" << java_name << " != 0) {" << endl;
3424 {
3425 Indentation indent(INDENT);
3426 s << INDENT << "int __qt_list_size = qtjambi_map_size(__jni_env, " << java_name
3427 << ");" << endl
3428 << INDENT
3429 << "jobjectArray __java_entry_set = qtjambi_map_entryset_array(__jni_env, " << java_name
3430 << ");" << endl;
3431
3432 s << INDENT << "for (int i=0; i<__qt_list_size; ++i) {" << endl;
3433 {
3434 Indentation indent(INDENT);
3435 s << INDENT
3436 << "QPair<jobject, jobject> __java_entry = "
3437 << "qtjambi_entryset_array_get(__jni_env, __java_entry_set, i);"
3438 << endl
3439 << INDENT << "jobject __java_key = __java_entry.first;" << endl
3440 << INDENT << "jobject __java_val = __java_entry.second;" << endl;
3441 writeJavaToQt(s, targ_key, "__qt_key", "__java_key", 0, -1, BoxedPrimitive);
3442 writeJavaToQt(s, targ_val, "__qt_val", "__java_val", 0, -1, BoxedPrimitive);
3443 s << INDENT << qt_name << ".insert(__qt_key, __qt_val);" << endl;
3444 }
3445 s << INDENT << "}" << endl;
3446 }
3447 s << INDENT << "}" << endl;
3448
3449 } else {
3450 ReportHandler::warning(QString("unable to generate container type %1, %2")
3451 .arg(java_type->name()).arg(type->type()));
3452 }
3453
3454 }
3455
3456
3457 void CppImplGenerator::writeFunctionCall(QTextStream &s, const QString &object_name,
3458 const AbstractMetaFunction *java_function,
3459 const QString &prefix,
3460 Option option,
3461 const QStringList &extra_arguments)
3462 {
3463 QString function_name = option & OriginalName ? java_function->originalName() : java_function->name();
3464
3465 AbstractMetaClassList interfaces = java_function->implementingClass()->interfaces();
3466
3467 QString classPrefix;
3468 if (prefix.isEmpty()
3469 && !java_function->implementingClass()->interfaces().isEmpty()
3470 && !java_function->implementingClass()->inheritsFrom(java_function->declaringClass())) {
3471 classPrefix = java_function->declaringClass()->qualifiedCppName() + "::";
3472 }
3473
3474 if (java_function->isInGlobalScope()) {
3475
3476 // Global scope stream operators need the arguments to be reordered (this ref at end)
3477 // so we special case them in order to simplify this code
3478 bool stream_operator = java_function->originalName() == "operator<<"
3479 || java_function->originalName() == "operator>>";
3480
3481 if (java_function->type() == 0)
3482 s << "if (" << object_name << " != 0) ";
3483 else
3484 s << "(" << object_name << " != 0) ? ";
3485 s << "::" << prefix << function_name << "(";
3486 if (!stream_operator)
3487 s << "*" << object_name << ", ";
3488 writeFunctionCallArguments(s, java_function, "__qt_");
3489 if (stream_operator)
3490 s << ", *" << object_name;
3491 s << ")";
3492 if (java_function->type() != 0)
3493 s << " : " << default_return_statement_qt(java_function->type(), Generator::Option(option | Generator::NoReturnStatement));
3494 s << ";";
3495 } else {
3496 s << object_name << (java_function->isStatic() ? QLatin1String("::") : QLatin1String("->") + classPrefix)
3497 << prefix << function_name << "(";
3498 writeFunctionCallArguments(s, java_function, "__qt_");
3499
3500 // The extra arguments...
3501 for (int i=0; i<extra_arguments.size(); ++i) {
3502 if (i > 0 || java_function->arguments().size() != 0)
3503 s << ", ";
3504 s << extra_arguments.at(i);
3505 }
3506
3507 s << ");";
3508 }
3509
3510 s << endl;
3511
3512 }
3513
3514
3515 void CppImplGenerator::writeFunctionCallArguments(QTextStream &s,
3516 const AbstractMetaFunction *java_function,
3517 const QString &prefix,
3518 Option options)
3519 {
3520 AbstractMetaArgumentList arguments = java_function->arguments();
3521
3522 int written_arguments = 0;
3523 const AbstractMetaClass *cls = java_function->ownerClass();
3524 if (java_function->isConstructor() && cls->hasVirtualFunctions()) {
3525 s << "d_ptr";
3526 written_arguments++;
3527 }
3528 for (int i=0; i<arguments.size(); ++i) {
3529 const AbstractMetaArgument *argument = arguments.at(i);
3530 AbstractMetaType *a_type = argument->type();
3531
3532 if ((options & SkipRemovedArguments) == SkipRemovedArguments
3533 && java_function->argumentRemoved(i+1)) {
3534 continue;
3535 }
3536
3537 if (written_arguments++ > 0) {
3538 s << ", ";
3539 }
3540
3541 bool enum_as_int = (options & EnumAsInts) && (argument->type()->typeEntry()->isEnum()
3542 || argument->type()->typeEntry()->isFlags());
3543 if (a_type->isEnum()) {
3544 AbstractMetaEnum *java_enum =
3545 m_classes.findEnum(static_cast<const EnumTypeEntry *>(argument->type()->typeEntry()));
3546 if (java_enum == 0) {
3547 ReportHandler::warning(QString("enum not found: '%1'")
3548 .arg(argument->type()->typeEntry()->qualifiedCppName()));
3549 } else {
3550 enum_as_int |= !java_enum->isPublic();
3551 }
3552 }
3553
3554 if ( (options & VirtualDispatch)
3555 && a_type->isContainer()) {
3556 s << "__d_" << argument->indexedName();
3557 continue;
3558 }
3559
3560 if ((!(options & NoCasts) && !enum_as_int) || ((options & ForceEnumCast) && a_type->isEnum())) {
3561
3562 // If the type in the signature is specified without template instantiation, but the
3563 // class is actually a template class, then we have troubles.
3564 AbstractMetaClass *cls = classes().findClass(argument->type()->typeEntry()->qualifiedCppName());
3565
3566 if ( (options & VirtualDispatch) && !a_type->typeEntry()->isStructInD()
3567 && ( a_type->isValue()
3568 || (a_type->isReference() && (a_type->typeEntry()->isValue() || a_type->typeEntry()->isObject() || a_type->typeEntry()->isPrimitive()) && !a_type->isTargetLangString())
3569 ) )
3570 s << "&";
3571
3572 if( (options & VirtualDispatch) && a_type->typeEntry()->isStructInD() && a_type->isReference())
3573 s << "&";
3574
3575 if ( (options & VirtualDispatch)
3576 && ( a_type->isTargetLangString() || a_type->name() == "QModelIndex" ) )
3577 ;
3578 else if (cls == 0 || cls->templateArguments().size() == a_type->instantiations().size()) {
3579 s << "(";
3580 writeTypeInfo(s, a_type, options);
3581 s << ")";
3582 }
3583 }
3584
3585 if (a_type->isNativePointer() && a_type->typeEntry()->name() == "QString" && !a_type->isReference())
3586 s << "&";
3587
3588 if ( ( !a_type->isPrimitive()
3589 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty() )
3590 && (!a_type->typeEntry()->isStructInD() || a_type->name() == "QModelIndex") ) {
3591 s << prefix;
3592 }
3593
3594 if (options & VirtualDispatch) {
3595 if( argument->type()->isTargetLangString())
3596 s << argument->indexedName() << ".utf16(), " << argument->indexedName() << ".size()";
3597 else if (argument->type()->name() == "QModelIndex")
3598 s << "qtd_from_QModelIndex(" << argument->indexedName() << ")";
3599 else
3600 s << argument->indexedName();
3601 } else
3602 s << argument->indexedName();
3603 }
3604 }
3605
3606
3607 QString CppImplGenerator::translateType(const AbstractMetaType *java_type, Option option, bool d_export)
3608 {
3609 if (!java_type)
3610 return "void";
3611
3612 const TypeEntry *type = java_type->typeEntry();
3613 QString class_name = type->name();
3614 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName());
3615 QString d_name = qualified_class_name;
3616 if (d_export)
3617 d_name = type->lookupName();
3618
3619 if (java_type->isPrimitive()
3620 || java_type->isTargetLangString()
3621 || java_type->isVariant()
3622 || java_type->isJObjectWrapper()
3623 || java_type->isTargetLangChar()
3624 || java_type->isArray()) {
3625 return d_name;
3626 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags()
3627 || ((option & EnumAsInts) && (java_type->isEnum() || java_type->isFlags()))) {
3628 return "int";
3629 } else if (java_type->isReference()) {
3630 if (java_type->typeEntry()->isValue())
3631 return "void*";
3632 else
3633 return d_name + " "+ QString(java_type->actualIndirections(), '*');
3634 } else if (java_type->isNativePointer()) {
3635 if (java_type->typeEntry()->isValue())
3636 return "void*";
3637 else if (java_type->typeEntry()->isEnum() && d_export)
3638 return "int" + QString(java_type->indirections(), '*');
3639 else
3640 return d_name + QString(java_type->indirections(), '*');
3641 } else {
3642 return d_name + QString(java_type->indirections(), '*');
3643 }
3644 }
3645
3646 void CppImplGenerator::writeExtraIncludes(QTextStream &s, const AbstractMetaClass *java_class)
3647 {
3648 IncludeList includes = java_class->typeEntry()->extraIncludes();
3649 qSort(includes.begin(), includes.end());
3650
3651 int used = 0;
3652 foreach (const Include &i, includes) {
3653 if (i.type != Include::TargetLangImport) {
3654 s << i.toString() << endl;
3655 ++used;
3656 }
3657 }
3658
3659
3660 if (used)
3661 s << endl;
3662
3663 }
3664
3665
3666 void CppImplGenerator::writeDefaultConstructedValues_helper(QSet<QString> &values,
3667 const AbstractMetaFunction *func)
3668 {
3669 foreach (AbstractMetaArgument *arg, func->arguments()) {
3670 AbstractMetaType *type = arg->type();
3671 if (func->typeReplaced(arg->argumentIndex()+1).isEmpty() && type->isValue() && hasDefaultConstructor(type))
3672 values << type->typeEntry()->qualifiedCppName();
3673 }
3674 }
3675
3676
3677 void CppImplGenerator::writeDefaultConstructedValues(QTextStream &s, const AbstractMetaClass *java_class) {
3678
3679 QSet<QString> values;
3680
3681 // Class functions, more or less copied from the logic in write(Class) above...
3682 AbstractMetaFunctionList class_funcs;
3683
3684 // Add normal final functions
3685 foreach (AbstractMetaFunction *function, java_class->functionsInTargetLang()) {
3686 if (!function->isEmptyFunction())
3687 class_funcs << function;
3688 }
3689
3690 // Add abstract functions, I think...
3691 foreach (AbstractMetaFunction *function, java_class->queryFunctions(AbstractMetaClass::NormalFunctions
3692 | AbstractMetaClass::AbstractFunctions
3693 | AbstractMetaClass::NotRemovedFromTargetLang)) {
3694 if (function->implementingClass() != java_class)
3695 class_funcs << function;
3696 }
3697
3698 // Signals (their c++ wrapper calls actually...)
3699 class_funcs += java_class->queryFunctions(AbstractMetaClass::Signals);
3700
3701 //
3702 foreach (AbstractMetaFunction *f, class_funcs) {
3703 writeDefaultConstructedValues_helper(values, f);
3704 }
3705
3706 foreach (AbstractMetaField *field, java_class->fields()) {
3707 writeDefaultConstructedValues_helper(values, field->setter());
3708 }
3709
3710 if (!values.isEmpty()) {
3711 s << endl << endl
3712 << "// Default constructed values used throughout final functions..." << endl;
3713 for (QSet<QString>::const_iterator it = values.constBegin(); it != values.constEnd(); ++it) {
3714 s << "Q_GLOBAL_STATIC(" << *it << ", default_" << QString(*it).replace("::", "_")
3715 << ");" << endl;
3716 }
3717 s << endl << endl;
3718 }
3719 }