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