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