Mercurial > projects > qtd
annotate generator/cppimplgenerator.cpp @ 310:5bcfe9e7db7f lifetime
Committing local changes
author | maxter <spambox@d-coding.com> |
---|---|
date | Wed, 23 Dec 2009 16:10:46 +0200 |
parents | 90131f64c9c9 |
children | 80b52f5e97b6 |
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 */ | |
390 if (options & CppImplGenerator::ReturnType) { | |
391 s += return_type; | |
392 s += " "; | |
393 } | |
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) | |
257 | 544 { |
545 s << "#include \"" << java_class->name() << "_shell" << ".h\"" << endl | |
546 << "#include <typeinfo>" << endl; | |
547 } | |
548 | |
1 | 549 /* qtd |
550 if (java_class->isQObject()) | |
551 s << "#include <qtdynamicmetaobject.h>" << endl; | |
552 */ | |
553 s << "#include <iostream>" << endl; | |
253 | 554 |
1 | 555 Include inc = java_class->typeEntry()->include(); |
556 if (!inc.name.isEmpty()) { | |
557 s << "#include "; | |
558 if (inc.type == Include::IncludePath) | |
559 s << "<"; | |
560 else | |
561 s << "\""; | |
562 s << inc.name; | |
563 if (inc.type == Include::IncludePath) | |
564 s << ">"; | |
565 else | |
566 s << "\""; | |
567 s << endl; | |
568 } | |
569 s << endl; // qtd | |
570 s << "#include \"qtd_core.h\"" << endl | |
571 << "#include \"ArrayOpsPrimitive.h\"" << endl | |
572 << "#include \"ArrayOps_qt_core.h\"" << endl; | |
573 | |
574 // qtd2 hack!! | |
37 | 575 if (java_class->package() == "qt.svg") |
1 | 576 s << "#include \"ArrayOps_qt_gui.h\"" << endl; |
577 | |
35 | 578 QString pkg_name = QString(java_class->package()).replace(".", "_"); |
579 s << QString("#include \"ArrayOps_%1.h\"").arg(pkg_name) << endl; | |
580 | |
1 | 581 s << endl; |
582 | |
583 writeInterfaceCasts(s, java_class); | |
584 | |
585 /* qtd2 | |
586 writeShellSignatures(s, java_class); | |
587 | |
588 writeDefaultConstructedValues(s, java_class); | |
589 | |
590 if (hasCustomDestructor(java_class)) */ | |
257 | 591 |
310 | 592 if (java_class->hasPublicDestructor() && !java_class->hasVirtualDestructor()) |
257 | 593 writeDestructor(s, java_class); |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
594 |
107 | 595 if (java_class->isQObject()) |
596 writeSignalsHandling(s, java_class); | |
597 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
598 if (shellClass) { |
1 | 599 foreach (AbstractMetaFunction *function, java_class->functions()) { |
257 | 600 if (function->isConstructor()) |
1 | 601 writeShellConstructor(s, function); |
602 } | |
257 | 603 |
310 | 604 if (java_class->isPolymorphic()) |
257 | 605 writeShellDestructor(s, java_class); |
1 | 606 |
607 if (java_class->isQObject()) | |
608 writeQObjectFunctions(s, java_class); | |
257 | 609 else if (java_class->typeEntry()->isObject()) |
610 writeObjectFunctions(s, java_class); | |
611 | |
1 | 612 |
613 // Virtual overrides | |
614 s << "// Virtual overrides" << endl; | |
615 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions(); | |
616 for (int pos = 0; pos<virtualFunctions.size(); ++pos) { | |
617 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
|
618 // qtd writeShellFunction(s, function, java_class, pos); |
1 | 619 writeShellVirtualFunction(s, function, java_class, pos); |
620 } | |
310 | 621 s << "// end ------------------------" << endl << endl; |
1 | 622 |
623 if (cpp_shared) | |
624 writeInitCallbacks(s, java_class); | |
625 | |
626 // Functions in shell class | |
627 s << "// Functions in shell class" << endl; | |
628 AbstractMetaFunctionList shellFunctions = java_class->nonVirtualShellFunctions(); | |
629 for (int i=0; i<shellFunctions.size(); ++i) { | |
630 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
|
631 writeShellFunction(s, function, java_class, -1); |
1 | 632 } |
310 | 633 s << "// end ------------------------" << endl << endl; |
1 | 634 |
635 // Write public overrides for functions that are protected in the base class | |
636 // so they can be accessed from the native callback | |
637 s << "// public overrides for functions that are protected in the base class" << endl; | |
638 AbstractMetaFunctionList public_override_functions = java_class->publicOverrideFunctions(); | |
639 foreach (AbstractMetaFunction *function, public_override_functions) { | |
640 if(notWrappedYet(function)) | |
641 continue; | |
642 writePublicFunctionOverride(s, function, java_class); | |
643 } | |
310 | 644 s << "// end ------------------------" << endl << endl; |
1 | 645 |
646 // Write virtual function overries used to decide on static/virtual calls | |
647 s << "// Write virtual function overries used to decide on static/virtual calls" << endl; | |
648 AbstractMetaFunctionList virtual_functions = java_class->virtualOverrideFunctions(); | |
649 foreach (const AbstractMetaFunction *function, virtual_functions) { | |
650 if(notWrappedYet(function)) | |
651 continue; | |
652 writeVirtualFunctionOverride(s, function, java_class); | |
653 } | |
310 | 654 s << "// end ------------------------" << endl << endl;; |
1 | 655 } |
656 | |
657 writeExtraFunctions(s, java_class); | |
658 /* qtd2 | |
659 writeToStringFunction(s, java_class); | |
660 | |
661 if (java_class->hasCloneOperator()) { | |
662 writeCloneFunction(s, java_class); | |
663 } | |
664 | |
665 // Signals | |
666 AbstractMetaFunctionList signal_functions = signalFunctions(java_class); | |
667 for (int i=0; i<signal_functions.size(); ++i) | |
668 writeSignalFunction(s, signal_functions.at(i), java_class, i); | |
669 */ | |
310 | 670 s << "// extern C" << endl; |
1 | 671 // Native callbacks (all java functions require native callbacks) |
672 AbstractMetaFunctionList class_funcs = java_class->functionsInTargetLang(); | |
673 foreach (AbstractMetaFunction *function, class_funcs) { | |
674 if (!function->isEmptyFunction()) | |
675 writeFinalFunction(s, function, java_class); | |
676 } | |
310 | 677 s << "// end ------------------------" << endl << endl; |
1 | 678 |
679 | |
680 class_funcs = java_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NotRemovedFromTargetLang); | |
681 foreach (AbstractMetaFunction *function, class_funcs) { | |
682 if (function->implementingClass() != java_class) { | |
683 writeFinalFunction(s, function, java_class); | |
684 } | |
685 } | |
686 | |
687 // Field accessors | |
688 s << "// Field accessors" << endl; | |
689 foreach (AbstractMetaField *field, java_class->fields()) { | |
690 if (field->wasPublic() || (field->wasProtected() && !java_class->isFinal())) | |
691 writeFieldAccessors(s, field); | |
692 } | |
310 | 693 s << "// end ------------------------" << endl << endl; |
694 | |
1 | 695 /* |
696 s << "// writeFromNativeFunction" << endl; | |
697 writeFromNativeFunction(s, java_class); | |
698 | |
699 if (java_class->isQObject()) | |
700 writeOriginalMetaObjectFunction(s, java_class); | |
701 | |
702 if (java_class->typeEntry()->isValue()) | |
703 writeFromArrayFunction(s, java_class); | |
704 | |
705 // generate the __qt_cast_to_Xxx functions | |
706 if (!java_class->isNamespace() && !java_class->isInterface()) { | |
707 AbstractMetaClassList interfaces = java_class->interfaces(); | |
708 foreach (AbstractMetaClass *iface, interfaces) | |
709 writeInterfaceCastFunction(s, java_class, iface); | |
710 } | |
711 | |
712 writeSignalInitialization(s, java_class); | |
713 */ | |
714 // qtd writeJavaLangObjectOverrideFunctions(s, java_class); | |
715 | |
253 | 716 if (java_class->isQObject()) |
717 { | |
718 s << endl << endl | |
310 | 719 << "extern \"C\" DLL_PUBLIC void* qtd_" << java_class->name() << "_staticTypeId() {" << endl |
253 | 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 | |
729 void CppImplGenerator::writeVirtualDispatchFunction(QTextStream &s, const AbstractMetaFunction *function, bool d_export) | |
730 { | |
731 uint options2 = ReturnType | ExternC; | |
732 QString return_type = jniReturnName(function, options2); | |
733 QString f_name = function->marshalledName() + "_dispatch"; | |
734 | |
735 if(!d_export) | |
736 s << "extern \"C\" "; | |
737 | |
738 if (!cpp_shared || d_export) { | |
739 s << return_type << " " << f_name; | |
740 writeVirtualDispatchArguments(s, function, d_export); | |
741 if(!d_export) | |
742 s << ";"; | |
743 } else if (cpp_shared) { | |
744 s << "typedef " << return_type << " " << "(*pf" << f_name << ")"; | |
745 writeVirtualDispatchArguments(s, function, false); | |
746 s << ";" << endl | |
148
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
132
diff
changeset
|
747 << "pf" << f_name << " " << f_name << ";"; |
1 | 748 } |
749 | |
750 s << endl; | |
751 } | |
752 | |
753 void CppImplGenerator::writeShellVirtualFunction(QTextStream &s, const AbstractMetaFunction *function, | |
754 const AbstractMetaClass *implementor, int id) | |
755 { | |
756 // ---------------------------- | |
757 if(notWrappedYet(function)) | |
758 return; | |
759 | |
760 AbstractMetaType *f_type = function->type(); | |
761 QString new_return_type = function->typeReplaced(0); | |
762 bool has_function_type = ((f_type != 0 | |
763 || !new_return_type.isEmpty()) | |
764 && new_return_type != "void"); | |
765 | |
766 writeVirtualDispatchFunction(s, function); | |
767 | |
768 writeFunctionSignature(s, function, implementor, QString(), OriginalName); | |
769 | |
770 s << endl | |
771 << "{" << endl; | |
772 | |
773 Option options = NoOption; | |
774 | |
775 Indentation indent(INDENT); | |
776 //bool static_call = !(options & VirtualCall); | |
777 //s << "std::cout << \"entering " << function->marshalledName() << " \\n\"; " << endl; | |
778 | |
779 if (f_type) { | |
780 if (f_type->isTargetLangString()) | |
104 | 781 s << INDENT << "DArray ret_str;" << endl; |
81 | 782 |
1 | 783 if (f_type->name() == "QModelIndex") |
784 s << INDENT << "QModelIndexAccessor __d_return_value;" << endl; | |
81 | 785 else if (f_type->typeEntry()->isStructInD()) |
786 s << INDENT << f_type->typeEntry()->qualifiedCppName() << " __d_return_value;" << endl; | |
787 | |
1 | 788 if (f_type->isContainer()) |
789 s << INDENT << "void* __d_return_value;" << endl | |
790 << INDENT << "size_t __d_return_value_size;" << endl; | |
791 } | |
792 | |
793 AbstractMetaArgumentList arguments = function->arguments(); | |
794 foreach (AbstractMetaArgument *argument, arguments) { | |
795 if (!function->argumentRemoved(argument->argumentIndex()+1)) { | |
796 if (!argument->type()->isPrimitive() | |
797 || !function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) { | |
798 if(argument->type()->isContainer()) { | |
799 QString arg_name = argument->indexedName(); | |
800 s << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl | |
801 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name); | |
802 | |
803 writeQtToJava(s, | |
804 argument->type(), | |
805 arg_name, | |
806 "__d_" + arg_name, | |
807 function, | |
808 argument->argumentIndex() + 1, | |
809 Option(VirtualDispatch)); | |
810 } | |
811 } | |
812 } | |
813 } | |
814 | |
815 if ((options & NoReturnStatement) == 0) | |
816 s << INDENT; | |
817 /* qtd if (function->isAbstract() && static_call) { | |
818 s << default_return_statement_qt(function->type(), options) << ";" << endl; | |
819 } else */ | |
820 { | |
821 if (f_type) { | |
822 if (f_type->isTargetLangString() || f_type->isContainer()) | |
823 ; | |
824 else if ((f_type->isValue() && !f_type->typeEntry()->isStructInD()) || | |
825 f_type->isVariant() ) | |
826 s << f_type->name() << " *__qt_return_value = (" << f_type->name() << "*) "; | |
827 else if (f_type->isObject() || f_type->isQObject()) | |
828 s << "return (" << f_type->name() <<"*) "; | |
81 | 829 else if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 830 ; |
831 else if ((options & NoReturnStatement) == 0) | |
832 s << "return "; | |
833 | |
834 if (f_type->isEnum() || f_type->isFlags()) | |
835 s << "(" << f_type->typeEntry()->qualifiedCppName() <<") "; | |
836 } | |
837 | |
838 s << function->marshalledName() << "_dispatch(" | |
253 | 839 << "this->dId"; |
1 | 840 |
841 if (f_type) { | |
842 if (f_type->isTargetLangString()) | |
104 | 843 s << ", &ret_str"; |
81 | 844 if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 845 s << ", &__d_return_value"; |
846 if (f_type->isContainer()) | |
847 s << ", &__d_return_value, &__d_return_value_size"; | |
848 } | |
849 | |
850 if (function->arguments().size() > 0) | |
851 s << ", "; | |
852 writeFunctionCallArguments(s, function, QString(), Option(options | ForceEnumCast | SkipRemovedArguments | ExcludeConst | VirtualDispatch)); | |
853 | |
854 s << ");" << endl; | |
855 | |
856 //s << "std::cout << \"leaving " << function->marshalledName() << " \\n\"; " << endl; | |
857 | |
858 if (f_type) { | |
859 if (f_type->name() == "QModelIndex") { | |
860 s << INDENT << "QModelIndex __qt_return_value = qtd_to_QModelIndex( __d_return_value );" << endl; | |
861 #ifdef Q_OS_WIN32 | |
253 | 862 s << "qtd_dummy();" << endl; // hack!!! |
1 | 863 #endif |
864 s << INDENT << "return __qt_return_value;" << endl; | |
81 | 865 } else if (f_type->typeEntry()->isStructInD()) |
866 s << INDENT << "return __d_return_value;" << endl; | |
1 | 867 |
868 if (f_type->isContainer()) { | |
869 writeJavaToQt(s, f_type, "__qt_return_value", "__d_return_value", | |
870 function, 0, GlobalRefJObject); | |
871 s << INDENT << "return __qt_return_value;" << endl; | |
872 } | |
873 | |
874 if (f_type->isTargetLangString()) | |
104 | 875 s << INDENT << "return " << "QString::fromUtf8((const char*) ret_str.ptr, ret_str.length);" << endl; |
1 | 876 |
877 if ( ( f_type->isValue() && !f_type->typeEntry()->isStructInD() ) || f_type->isVariant() ) | |
878 s << INDENT << "return " << f_type->name() << "(*__qt_return_value);" << endl; //" __qt_return_value = "; | |
879 } | |
880 } | |
881 | |
882 s << "}" << endl << endl; | |
883 // ---------------------------- | |
884 } | |
885 | |
886 void CppImplGenerator::writeVirtualDispatchArguments(QTextStream &s, const AbstractMetaFunction *d_function, bool d_export) | |
887 { | |
888 uint nativeArgCount = 0; | |
889 AbstractMetaType *ret_type = d_function->type(); | |
890 | |
891 s << "(void *d_entity"; | |
892 | |
893 if (ret_type) { | |
104 | 894 if (ret_type->isTargetLangString()) { |
895 if (d_export) | |
896 s << ", string* ret_str"; | |
897 else | |
898 s << ", DArray* ret_str"; | |
899 } | |
1 | 900 if (ret_type->name() == "QModelIndex") |
901 s << ", QModelIndexAccessor *__d_return_value"; | |
81 | 902 else if(ret_type->typeEntry()->isStructInD()) |
903 s << ", " << ret_type->typeEntry()->qualifiedCppName() << " *__d_return_value"; | |
904 | |
1 | 905 if (ret_type->isContainer()) |
906 s << ", void** __d_arr_ptr, size_t* __d_arr_size"; | |
907 } | |
908 | |
909 if (d_function->arguments().size() > 0) | |
910 s << ", "; | |
911 | |
912 | |
913 // the function arguments | |
914 AbstractMetaArgumentList arguments = d_function->arguments(); | |
915 foreach (const AbstractMetaArgument *argument, arguments) { | |
916 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
917 if (nativeArgCount > 0) | |
918 s << "," << " "; | |
919 | |
920 AbstractMetaType *d_type = argument->type(); | |
921 QString arg_name = argument->indexedName(); | |
922 | |
923 if (d_type->name() == "QModelIndex") | |
924 s << "QModelIndexAccessor" << QString(d_type->actualIndirections(), '*') << " " << arg_name; | |
925 else if (d_type->isContainer()) { | |
926 if (d_export) { | |
927 s << DGenerator::translateType(d_type, d_function->ownerClass(), NoOption) << "* "; | |
928 } else | |
929 s << "DArray* "; | |
930 s << arg_name; | |
931 } else if (d_type->typeEntry()->isStructInD()) | |
932 s << d_type->typeEntry()->qualifiedCppName() << QString(d_type->actualIndirections(), '*') | |
933 << " " << arg_name; | |
934 else if (d_type->isTargetLangString()) { | |
935 if (d_export) | |
936 s << "wchar* "; | |
937 else | |
938 s << "const unsigned short* "; | |
939 s << QString("%1, int %1_size").arg(arg_name); | |
940 } else { | |
941 if(d_type->isVariant()) | |
942 s << "void*"; | |
943 else if (!d_type->hasNativeId()) | |
944 { | |
945 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_type->typeEntry()); | |
946 if(d_type->typeEntry()->isObject() && ctype->isAbstract() && d_export) | |
947 { | |
948 QString d_name = d_type->typeEntry()->qualifiedCppName(); | |
949 s << d_name + "_ConcreteWrapper" + QString(d_type->indirections(),'*'); | |
950 } | |
951 else | |
39 | 952 s << translateType(d_type, EnumAsInts, d_export); |
1 | 953 } |
954 else | |
955 s << "void*"; | |
956 s << " " << argument->indexedName(); | |
957 } | |
958 nativeArgCount++; | |
959 } | |
960 } | |
961 s << ")"; | |
962 } | |
963 | |
964 void CppImplGenerator::writeJavaLangObjectOverrideFunctions(QTextStream &s, const AbstractMetaClass *cls) | |
965 { | |
966 if (cls->hasHashFunction()) { | |
967 AbstractMetaFunctionList hashcode_functions = cls->queryFunctionsByName("hashCode"); | |
968 bool found = false; | |
969 foreach (const AbstractMetaFunction *function, hashcode_functions) { | |
970 if (function->actualMinimumArgumentCount() == 0) { | |
971 found = true; | |
972 break; | |
973 } | |
974 } | |
975 | |
976 if (!found) { | |
977 s << endl | |
978 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_hashCode", "jint") | |
979 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
980 << INDENT << "{" << endl; | |
981 { | |
982 Indentation indent(INDENT); | |
983 s << INDENT << "Q_UNUSED(__jni_env);" << endl | |
984 << INDENT << cls->qualifiedCppName() << " *__qt_this = (" | |
985 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
986 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
987 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
988 << INDENT << "return qHash(*__qt_this);" << endl; | |
989 } | |
990 s << INDENT << "}" << endl; | |
991 } | |
992 } | |
993 | |
994 // Qt has a standard toString() conversion in QVariant? | |
995 QVariant::Type type = QVariant::nameToType(cls->qualifiedCppName().toLatin1()); | |
996 if (QVariant(type).canConvert(QVariant::String) && !cls->hasToStringCapability()) { | |
997 AbstractMetaFunctionList tostring_functions = cls->queryFunctionsByName("toString"); | |
998 bool found = false; | |
999 foreach (const AbstractMetaFunction *function, tostring_functions) { | |
1000 if (function->actualMinimumArgumentCount() == 0) { | |
1001 found = true; | |
1002 break; | |
1003 } | |
1004 } | |
1005 | |
1006 if (!found) { | |
1007 s << endl | |
1008 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_toString", "jstring") | |
1009 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1010 << INDENT << "{" << endl; | |
1011 { | |
1012 Indentation indent(INDENT); | |
1013 s << INDENT << cls->qualifiedCppName() << " *__qt_this = (" | |
1014 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1015 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1016 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1017 << INDENT << "return qtjambi_from_qstring(__jni_env, QVariant(*__qt_this).toString());" << endl; | |
1018 } | |
1019 s << INDENT << "}" << endl; | |
1020 } | |
1021 } | |
1022 } | |
1023 | |
1024 void CppImplGenerator::writeExtraFunctions(QTextStream &s, const AbstractMetaClass *java_class) | |
1025 { | |
1026 const ComplexTypeEntry *class_type = java_class->typeEntry(); | |
1027 Q_ASSERT(class_type); | |
1028 | |
1029 CodeSnipList code_snips = class_type->codeSnips(); | |
1030 foreach (const CodeSnip &snip, code_snips) { | |
1031 if (snip.language == TypeSystem::ShellCode || snip.language == TypeSystem::NativeCode) { | |
1032 snip.formattedCode(s, INDENT) << endl; | |
1033 } | |
1034 } | |
1035 } | |
1036 | |
1037 void CppImplGenerator::writeToStringFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
1038 { | |
1039 FunctionModelItem fun = java_class->hasToStringCapability(); | |
1040 bool core = java_class->package() == QLatin1String("qt.core"); | |
1041 bool qevent = false; | |
1042 | |
1043 const AbstractMetaClass *cls = java_class; | |
1044 while (cls) { | |
1045 if (cls->name() == "QEvent") { | |
1046 qevent = true; | |
1047 fun = cls->hasToStringCapability(); | |
1048 break; | |
1049 } | |
1050 cls = cls->baseClass(); | |
1051 } | |
1052 | |
1053 if (!java_class->hasDefaultToStringFunction() && fun && !(qevent && core)) { | |
1054 | |
1055 int indirections = fun->arguments().at(1)->type().indirections(); | |
1056 QString deref = QLatin1String(indirections == 0 ? "*" : ""); | |
1057 | |
1058 s << endl; | |
1059 s << "#include <QDebug>" << endl; | |
1060 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_toString", "jstring") | |
1061 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1062 << INDENT << "{" << endl; | |
1063 { | |
1064 Indentation indent(INDENT); | |
1065 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = (" | |
1066 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1067 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1068 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1069 | |
1070 << INDENT << "QString res;" << endl | |
1071 << INDENT << "QDebug d(&res);" << endl | |
1072 << INDENT << "d << " << deref << "__qt_this;" << endl; | |
1073 s << INDENT << "return qtjambi_from_qstring(__jni_env, res);" << endl; | |
1074 } | |
1075 s << INDENT << "}" << endl << endl; | |
1076 } | |
1077 } | |
1078 | |
1079 void CppImplGenerator::writeCloneFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
1080 { | |
1081 s << endl | |
1082 << jni_function_signature(java_class->package(), java_class->name(), "__qt_clone", "jobject") << endl | |
1083 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1084 << INDENT << "{" << endl; | |
1085 { | |
1086 Indentation indent(INDENT); | |
1087 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = (" | |
1088 | |
1089 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1090 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1091 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1092 << INDENT << java_class->qualifiedCppName() << " *res = __qt_this;" << endl | |
1093 << INDENT << "return qtjambi_from_object(__jni_env, res, \"" << java_class->name() << "\", \"" << java_class->package().replace(".", "/") << "/\", true);" << endl; | |
1094 } | |
1095 s << INDENT << "}" << endl << endl; | |
1096 } | |
1097 | |
1098 void CppImplGenerator::writeShellSignatures(QTextStream &s, const AbstractMetaClass *java_class) | |
1099 { | |
1100 bool has_constructors = java_class->hasConstructors(); | |
1101 | |
1102 // Write the function names... | |
1103 if (has_constructors && java_class->hasVirtualFunctions()) { | |
1104 AbstractMetaFunctionList virtual_functions = java_class->virtualFunctions(); | |
1105 { | |
1106 Indentation indent(INDENT); | |
1107 | |
1108 int pos = -1; | |
1109 foreach (AbstractMetaFunction *function, virtual_functions) { | |
1110 ++pos; | |
1111 | |
1112 if (pos == 0) | |
1113 s << "static const char *qtjambi_method_names[] = {"; | |
1114 else | |
1115 s << ","; | |
1116 s << endl | |
1117 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ " | |
1118 << "\"" << function->name() << "\""; | |
1119 } | |
1120 if (pos >= 0) | |
1121 s << endl << "};" << endl << endl; | |
1122 else | |
1123 s << "static const char **qtjambi_method_names = 0;" << endl; | |
1124 } | |
1125 | |
1126 // Write the function signatures | |
1127 { | |
1128 Indentation indent(INDENT); | |
1129 | |
1130 int pos = -1; | |
1131 foreach (AbstractMetaFunction *function, virtual_functions) { | |
1132 ++pos; | |
1133 | |
1134 if (pos == 0) | |
1135 s << "static const char *qtjambi_method_signatures[] = {"; | |
1136 else | |
1137 s << ","; | |
1138 s << endl | |
1139 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ " | |
1140 << "\"" | |
1141 << jni_signature(function, SlashesAndStuff) | |
1142 << "\""; | |
1143 } | |
1144 if (pos >= 0) | |
1145 s << endl << "};" << endl; | |
1146 else | |
1147 s << "static const char **qtjambi_method_signatures = 0;" << endl; | |
1148 s << "static const int qtjambi_method_count = " << QString::number(pos + 1) << ";" << endl | |
1149 << endl; | |
1150 } | |
1151 } | |
1152 | |
1153 if (has_constructors && java_class->hasInconsistentFunctions()) { | |
1154 AbstractMetaFunctionList inconsistents = java_class->cppInconsistentFunctions(); | |
1155 // Write the inconsistent function names... | |
1156 { | |
1157 Indentation indent(INDENT); | |
1158 s << "static const char *qtjambi_inconsistent_names[] = {"; | |
1159 for (int i=0; i<inconsistents.size(); ++i) { | |
1160 if (i != 0) | |
1161 s << ","; | |
1162 s << endl << INDENT << "\"" << inconsistents.at(i)->name() << "\""; | |
1163 } | |
1164 s << endl << "};" << endl << endl; | |
1165 } | |
1166 | |
1167 // Write the function signatures | |
1168 { | |
1169 Indentation indent(INDENT); | |
1170 s << "static const char *qtjambi_inconsistent_signatures[] = {"; | |
1171 for (int i=0; i<inconsistents.size(); ++i) { | |
1172 const AbstractMetaFunction *function = inconsistents.at(i); | |
1173 | |
1174 if (i != 0) | |
1175 s << ","; | |
1176 s << endl << INDENT << "\"" | |
1177 << jni_signature(function, SlashesAndStuff) | |
1178 << "\""; | |
1179 } | |
1180 s << endl << "};" << endl << endl; | |
1181 } | |
1182 s << "static const int qtjambi_inconsistent_count = " << inconsistents.size() << ";" << endl | |
1183 << endl; | |
1184 } | |
1185 | |
1186 | |
1187 AbstractMetaFunctionList signal_functions = java_class->cppSignalFunctions(); | |
1188 if (signal_functions.size()) { | |
1189 Indentation indent(INDENT); | |
1190 s << "static const char *qtjambi_signal_names[] = {"; | |
1191 for (int i=0; i<signal_functions.size(); ++i) { | |
1192 if (i != 0) | |
1193 s << ","; | |
1194 | |
1195 const AbstractMetaFunction *f = signal_functions.at(i); | |
1196 | |
1197 QString signalName = f->name(); | |
1198 | |
1199 s << endl << INDENT << "\"" << signalName << "\""; | |
1200 } | |
1201 s << endl << "};" << endl << endl; | |
1202 | |
1203 s << "static const int qtjambi_signal_argumentcounts[] = {"; | |
1204 for (int i=0; i<signal_functions.size(); ++i) { | |
1205 if (i != 0) | |
1206 s << ","; | |
1207 s << endl << INDENT << signal_functions.at(i)->arguments().count(); | |
1208 } | |
1209 s << endl << "};" << endl << endl; | |
1210 s << "static const int qtjambi_signal_count = " << signal_functions.size() << ";" << endl | |
1211 << endl; | |
1212 } | |
1213 } | |
1214 | |
253 | 1215 void CppImplGenerator::writeQObjectEntity(QTextStream &s, const AbstractMetaClass *java_class) |
1216 { | |
1217 QString entityName = java_class->name() + "Entity"; | |
1218 QString className = java_class->name(); | |
1219 | |
1220 s << "class " << entityName << " : public QObject, public QtD_QObjectEntity" << endl | |
1221 << "{" << endl | |
1222 << "public:" << endl | |
1223 << " Q_OBJECT_CHECK" << endl | |
1224 << " virtual int qt_metacall(QMetaObject::Call, int, void **);" << endl << endl | |
1225 | |
1226 << " " << entityName << "(QObject *qObject, void *dId) : QObject(), QtD_QObjectEntity(qObject, dId) {}" << endl | |
1227 << "};" << endl << endl; | |
1228 | |
1229 // QObject_Link::qt_metacall() | |
1230 s << "int " << entityName << "::qt_metacall(QMetaObject::Call _c, int _id, void **_a)" << endl | |
1231 << "{" << endl | |
1232 << " _id = QObject::qt_metacall(_c, _id, _a);" << endl | |
1233 << " if (_id < 0 || _c != QMetaObject::InvokeMetaMethod)" << endl | |
1234 << " return _id;" << endl | |
1235 // << " Q_ASSERT(_id < 2);" << endl | |
1236 << " emit_callbacks_" << java_class->name() << "[_id](dId, _a);" << endl | |
1237 << " return -1;" << endl | |
1238 << "}" << endl << endl; | |
1239 | |
1240 s << "extern \"C\" DLL_PUBLIC void qtd_" << className << "_createEntity(void *nativeId, void* dId)" << endl | |
1241 << "{" << endl | |
1242 << " new " << entityName << "((QObject*)nativeId, dId);" << endl | |
1243 << "}" << endl << endl; | |
1244 } | |
1245 | |
1 | 1246 void CppImplGenerator::writeQObjectFunctions(QTextStream &s, const AbstractMetaClass *java_class) |
1247 { | |
1248 // QObject::metaObject() | |
1249 /* s << "const QMetaObject *" << shellClassName(java_class) << "::metaObject() const" << endl | |
1250 << "{" << endl | |
1251 << " if (m_meta_object == 0) {" << endl | |
1252 << " JNIEnv *__jni_env = qtjambi_current_environment();" << endl | |
1253 << " jobject __obj = m_link != 0 ? m_link->javaObject(__jni_env) : 0;" << endl | |
1254 << " if (__obj == 0) return " << java_class->qualifiedCppName() << "::metaObject();" << endl | |
1255 << " else m_meta_object = qtjambi_metaobject_for_class(__jni_env, __jni_env->GetObjectClass(__obj), " << java_class->qualifiedCppName() << "::metaObject());" << endl; | |
1256 | |
1257 | |
1258 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions(); | |
1259 for (int pos=0; pos<virtualFunctions.size(); ++pos) { | |
1260 const AbstractMetaFunction *virtualFunction = virtualFunctions.at(pos); | |
1261 if (virtualFunction->isVirtualSlot()) { | |
1262 QStringList introspectionCompatibleSignatures = virtualFunction->introspectionCompatibleSignatures(); | |
1263 foreach (QString introspectionCompatibleSignature, introspectionCompatibleSignatures) { | |
1264 s << " {" << endl | |
1265 << " int idx = " | |
1266 << java_class->qualifiedCppName() << "::metaObject()->indexOfMethod(\"" | |
1267 << introspectionCompatibleSignature << "\");" << endl; | |
1268 | |
1269 s << " if (idx >= 0) m_map.insert(idx, " << pos << ");" << endl | |
1270 << " }" << endl; | |
1271 } | |
1272 } | |
1273 | |
1274 } | |
1275 | |
1276 s << " }" << endl | |
1277 << " return m_meta_object;" << endl | |
1278 << "}" << endl << endl; | |
1279 | |
1280 // QObject::qt_metacast() | |
1281 s << "void *" << shellClassName(java_class) << "::qt_metacast(const char *_clname)" << endl | |
1282 << "{" << endl | |
1283 << " if (!_clname) return 0;" << endl | |
1284 << " if (!strcmp(_clname, \"" << shellClassName(java_class) << "\"))" << endl | |
1285 << " return static_cast<void*>(const_cast<" << shellClassName(java_class) << "*>(this));" << endl | |
1286 << " return " << java_class->qualifiedCppName() << "::qt_metacast(_clname);" << endl | |
1287 << "}" << endl << endl; | |
1288 */ | |
253 | 1289 |
1 | 1290 s << "int " << shellClassName(java_class) << "::qt_metacall(QMetaObject::Call _c, int _id, void **_a)" << endl |
1291 << "{" << endl; | |
1292 | |
253 | 1293 s << " _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
|
1294 << " if (_id < 0 || _c != QMetaObject::InvokeMetaMethod)" << endl |
253 | 1295 << " return _id;" << endl |
1296 << " 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
|
1297 << " return -1;" << endl |
1 | 1298 << "}" << endl << endl; |
1299 } | |
1300 | |
1301 void CppImplGenerator::writeSignalHandler(QTextStream &s, const AbstractMetaClass *d_class, AbstractMetaFunction *function) | |
1302 { | |
1303 QString extra_args, extra_call_args, conversion_code; | |
1304 QTextStream s2(&conversion_code); | |
1305 | |
1306 Indentation indent(INDENT); | |
1307 | |
1308 AbstractMetaArgumentList arguments = function->arguments(); | |
1309 foreach (AbstractMetaArgument *argument, arguments) { | |
1310 if(argument->type()->isContainer()) { | |
1311 QString arg_name = argument->indexedName(); | |
1312 const AbstractMetaType *arg_type = argument->type(); | |
1313 extra_args += ", DArray " + arg_name; | |
1314 extra_call_args += ", " + arg_name + "_arr"; | |
1315 | |
1316 s2 << INDENT; | |
1317 writeTypeInfo(s2, arg_type, NoOption); | |
1318 s2 << arg_name << " = (*reinterpret_cast< "; | |
1319 writeTypeInfo(s2, arg_type, ExcludeReference); | |
1320 s2 << "(*)>(args[" << argument->argumentIndex() + 1 << "]));" << endl | |
1321 << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl | |
1322 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name); | |
1323 | |
1324 writeQtToJava(s2, | |
1325 arg_type, | |
1326 arg_name, | |
1327 "__d_" + arg_name, | |
1328 function, | |
1329 argument->argumentIndex() + 1, | |
1330 Option(VirtualDispatch)); | |
1331 } | |
1332 } | |
1333 QString sig_name = signalExternName(d_class, function); | |
1334 s << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle_in_d(void* d_entity, void** args" << extra_args <<");" << endl | |
1335 << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle(void* d_entity, void** args)" << endl | |
1336 << "{" << endl | |
1337 << conversion_code << endl | |
1338 << INDENT << sig_name << "_handle_in_d(d_entity, args" << extra_call_args << ");" << endl | |
1339 << "}" << endl; | |
1340 | |
1341 } | |
1342 | |
1343 void CppImplGenerator::writeSignalsHandling(QTextStream &s, const AbstractMetaClass *java_class) | |
1344 { | |
1345 s << "extern \"C\" typedef void (*EmitCallback)(void*, void**);" << endl; | |
1346 AbstractMetaFunctionList signal_funcs = signalFunctions(java_class); | |
1347 | |
1348 if (cpp_shared) | |
132 | 1349 if(signal_funcs.size() > 0) |
1350 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "];" << endl; | |
1351 else | |
1352 s << "EmitCallback emit_callbacks_" << java_class->name() << "[1];" << endl; // Hack for msvc. | |
1 | 1353 else { |
1354 // D-side signal callbacks | |
1355 for(int i = 0; i < signal_funcs.size(); i++) { | |
1356 AbstractMetaFunction *signal = signal_funcs.at(i); | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
1357 s << "extern \"C\" DLL_PUBLIC void " << signalExternName(java_class, signal) << "_handle(void* d_entity, void** args);" << endl; |
1 | 1358 } |
1359 | |
132 | 1360 if(signal_funcs.size() > 0) |
1361 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "] = {" << endl; | |
1362 else | |
1363 s << "EmitCallback emit_callbacks_" << java_class->name() << "[1] = {" << endl; // Hack for msvc. | |
1 | 1364 for(int i = 0; i < signal_funcs.size(); i++) { |
1365 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1366 s << endl; | |
1367 if (i != 0) | |
1368 s << ", "; | |
1369 s << "&" << signalExternName(java_class, signal) << "_handle"; | |
1370 } | |
1371 s << endl << "};" << endl << endl; | |
1372 } | |
1373 | |
253 | 1374 writeQObjectEntity(s, java_class); |
1 | 1375 } |
1376 | |
1377 | |
1378 void CppImplGenerator::writeShellConstructor(QTextStream &s, const AbstractMetaFunction *java_function) | |
1379 { | |
1380 if (java_function->isModifiedRemoved(TypeSystem::ShellCode)) | |
1381 return; | |
1382 | |
1383 const AbstractMetaClass *cls = java_function->ownerClass(); | |
1384 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1385 | |
1386 writeFunctionSignature(s, java_function, cls); | |
1387 | |
1388 s << endl; | |
1389 s << " : " << cls->qualifiedCppName() << "("; | |
1390 for (int i=0; i<arguments.size(); ++i) { | |
1391 s << arguments.at(i)->indexedName(); | |
1392 if (i != arguments.size() - 1) | |
1393 s << ", "; | |
1394 } | |
1395 s << ")"; | |
257 | 1396 |
253 | 1397 if (cls->isQObject()) |
1398 s << "," << endl << " QtD_QObjectEntity(this, d_ptr)"; | |
310 | 1399 else if (cls->isPolymorphic()) |
253 | 1400 s << "," << endl << " QtD_Entity(d_ptr)"; |
1 | 1401 /* qtd s << " m_meta_object(0)," << endl; |
1402 s << " m_vtable(0)," << endl | |
1403 << " m_link(0)" << endl; | |
1404 */ | |
1405 s << endl; | |
1406 s << "{" << endl; | |
1407 { | |
1408 Indentation indent(INDENT); | |
253 | 1409 |
1 | 1410 writeCodeInjections(s, java_function, cls, CodeSnip::Beginning, TypeSystem::ShellCode); |
1411 writeCodeInjections(s, java_function, cls, CodeSnip::End, TypeSystem::ShellCode); | |
1412 } | |
257 | 1413 s << "}" << endl << endl; |
1 | 1414 } |
1415 | |
1416 void CppImplGenerator::writeShellDestructor(QTextStream &s, const AbstractMetaClass *java_class) | |
1417 { | |
1418 s << shellClassName(java_class) << "::~" | |
1419 << shellClassName(java_class) << "()" << endl | |
1420 << "{" << endl; | |
1421 { | |
257 | 1422 s << " std::cout << \"In shell destructor of " << java_class->name() << ", nativeId: \" << this << std::endl;"; |
253 | 1423 if (java_class->isQObject()) |
257 | 1424 s << " destroyEntity(this);" << endl; |
1425 else | |
1426 s << " qtd_delete_d_object(dId);" << endl; | |
1 | 1427 } |
1428 s << "}" << endl << endl; | |
1429 } | |
1430 | |
1431 void CppImplGenerator::writeCodeInjections(QTextStream &s, const AbstractMetaFunction *java_function, | |
1432 const AbstractMetaClass *implementor, CodeSnip::Position position, | |
1433 TypeSystem::Language language) | |
1434 { | |
1435 | |
1436 FunctionModificationList mods; | |
1437 const AbstractMetaClass *cls = implementor; | |
1438 while (cls != 0) { | |
1439 mods += java_function->modifications(cls); | |
1440 | |
1441 if (cls == cls->baseClass()) | |
1442 break; | |
1443 cls = cls->baseClass(); | |
1444 } | |
1445 | |
1446 foreach (FunctionModification mod, mods) { | |
1447 if (mod.snips.count() <= 0) | |
1448 continue ; | |
1449 | |
1450 foreach (CodeSnip snip, mod.snips) { | |
1451 if (snip.position != position) | |
1452 continue ; | |
1453 | |
1454 if ((snip.language & language) == false) | |
1455 continue ; | |
1456 | |
1457 if (position == CodeSnip::End) | |
1458 s << endl; | |
1459 | |
1460 QString code; | |
1461 QTextStream tmpStream(&code); | |
1462 snip.formattedCode(tmpStream, INDENT); | |
1463 ArgumentMap map = snip.argumentMap; | |
1464 ArgumentMap::iterator it = map.begin(); | |
1465 for (;it!=map.end();++it) { | |
1466 int pos = it.key() - 1; | |
1467 QString meta_name = it.value(); | |
1468 | |
1469 if (pos >= 0 && pos < java_function->arguments().count()) { | |
1470 code = code.replace(meta_name, java_function->arguments().at(pos)->indexedName()); | |
1471 } else { | |
1472 QString debug = QString("argument map specifies invalid argument index %1" | |
1473 "for function '%2'") | |
1474 .arg(pos + 1).arg(java_function->name()); | |
1475 ReportHandler::warning(debug); | |
1476 } | |
1477 | |
1478 } | |
1479 s << code; | |
1480 if (position == CodeSnip::Beginning) | |
1481 s << endl; | |
1482 } | |
1483 } | |
1484 } | |
1485 | |
1486 static QString function_call_for_ownership(TypeSystem::Ownership owner, const QString &var_name) | |
1487 { | |
1488 if (owner == TypeSystem::CppOwnership) { | |
1489 return "setCppOwnership(__jni_env, " + var_name + ")"; | |
1490 } else if (owner == TypeSystem::TargetLangOwnership) { | |
1491 return "setJavaOwnership(__jni_env, " + var_name + ")"; | |
1492 } else if (owner == TypeSystem::DefaultOwnership) { | |
1493 return "setDefaultOwnership(__jni_env, " + var_name + ")"; | |
1494 } else { | |
1495 Q_ASSERT(false); | |
1496 return "bogus()"; | |
1497 } | |
1498 } | |
1499 | |
1500 void CppImplGenerator::writeOwnership(QTextStream &s, | |
1501 const AbstractMetaFunction *java_function, | |
1502 const QString &var_name, | |
1503 int var_index, | |
1504 const AbstractMetaClass *implementor) | |
1505 { | |
1506 TypeSystem::Ownership owner = TypeSystem::InvalidOwnership; | |
1507 const AbstractMetaClass *cls = implementor; | |
1508 while (cls != 0 && owner == TypeSystem::InvalidOwnership) { | |
1509 owner = java_function->ownership(cls, TypeSystem::ShellCode, var_index); | |
1510 cls = cls->baseClass(); | |
1511 } | |
1512 | |
1513 if (owner == TypeSystem::InvalidOwnership) | |
1514 return; | |
1515 | |
1516 if (var_index != -1) { | |
1517 s << INDENT << "if (" << var_name << " != 0) {" << endl; | |
1518 { | |
1519 Indentation indent(INDENT); | |
1520 s << INDENT << "QtJambiLink *__link = QtJambiLink::findLink(__jni_env, " | |
1521 << var_name << ");" << endl | |
1522 << INDENT << "Q_ASSERT(__link != 0);" << endl; | |
1523 | |
1524 s << INDENT << "__link->" << function_call_for_ownership(owner, var_name) << ";" << endl; | |
1525 } | |
1526 s << INDENT << "}" << endl; | |
1527 } else { | |
1528 s << INDENT << "if (m_link) {" << endl; | |
1529 { | |
1530 Indentation indent(INDENT); | |
1531 s << INDENT << "m_link->" << function_call_for_ownership(owner, "m_link->javaObject(__jni_env)") << ";" << endl; | |
1532 } | |
1533 s << INDENT << "}" << endl; | |
1534 } | |
1535 | |
1536 } | |
1537 | |
1538 void CppImplGenerator::writeShellFunction(QTextStream &s, const AbstractMetaFunction *java_function, | |
1539 const AbstractMetaClass *implementor, int id) | |
1540 { | |
1541 writeFunctionSignature(s, java_function, implementor, QString(), OriginalName); | |
1542 | |
1543 s << endl | |
1544 << "{" << endl; | |
1545 | |
1546 Indentation indent(INDENT); | |
1547 | |
1548 QString java_function_signature = java_function->signature(); | |
1549 /* s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) entering: " << implementor->name() << "::" | |
1550 << java_function_signature << "\");" << endl; | |
1551 */ | |
1552 writeCodeInjections(s, java_function, implementor, CodeSnip::Beginning, TypeSystem::ShellCode); | |
1553 | |
1554 // s << " printf(\"%s : %s\\n\", \"" << java_function->enclosingClass()->name() << "\"" | |
1555 // << ", \"" << java_function->name() << "\");" << endl; | |
1556 | |
1557 if (!java_function->isFinalInCpp() || java_function->isVirtualSlot()) { | |
1558 s << INDENT << "jmethodID method_id = m_vtable->method(" << id << ");" << endl; | |
1559 s << INDENT << "if (method_id) {" << endl; | |
1560 | |
1561 { | |
1562 Indentation indent(INDENT); | |
1563 s << INDENT << "JNIEnv *__jni_env = qtjambi_current_environment();" << endl; | |
1564 | |
1565 // This nasty case comes up when we're shutting down while receiving virtual | |
1566 // calls.. With these checks we safly abort... | |
1567 s << INDENT << "if (!__jni_env) {" << endl | |
1568 << " "; | |
1569 writeBaseClassFunctionCall(s, java_function, implementor); | |
1570 if (!java_function->type()) { | |
1571 s << INDENT << " return;" << endl; | |
1572 } | |
1573 s << INDENT << "}" << endl; | |
1574 | |
1575 // otherwise, continue with the function call... | |
1576 s << INDENT << "__jni_env->PushLocalFrame(100);" << endl; | |
1577 | |
1578 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1579 AbstractMetaArgumentList argumentsToReset; | |
1580 foreach (AbstractMetaArgument *argument, arguments) { | |
1581 if (!java_function->argumentRemoved(argument->argumentIndex()+1)) { | |
1582 if (!argument->type()->isPrimitive() | |
1583 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) { | |
1584 writeQtToJava(s, | |
1585 argument->type(), | |
1586 argument->indexedName(), | |
1587 "__java_" + argument->indexedName(), | |
1588 java_function, | |
1589 argument->argumentIndex() + 1); | |
1590 } | |
1591 | |
1592 if (java_function->resetObjectAfterUse(argument->argumentIndex()+1)) | |
1593 argumentsToReset.append(argument); | |
1594 } | |
1595 } | |
1596 | |
1597 for (int i=0; i<arguments.size(); ++i) | |
1598 writeOwnership(s, java_function, "__java_" + arguments.at(i)->indexedName(), i+1, implementor); | |
1599 | |
1600 | |
1601 AbstractMetaType *function_type = java_function->type(); | |
1602 QString new_return_type = java_function->typeReplaced(0); | |
1603 bool has_function_type = ((function_type != 0 | |
1604 || !new_return_type.isEmpty()) | |
1605 && new_return_type != "void"); | |
1606 | |
1607 s << INDENT; | |
1608 if (has_function_type) { | |
1609 if (new_return_type.isEmpty()) { | |
1610 s << translateType(function_type); | |
1611 } else { | |
1612 s << jniName(new_return_type); | |
1613 } | |
1614 s << " " << "__d_return_value = "; | |
1615 } | |
1616 | |
1617 s << "__jni_env->"; | |
1618 if (new_return_type.isEmpty()) { | |
1619 s << callXxxMethod(java_function->type()); | |
1620 } else if (!has_function_type) { | |
1621 s << "CallVoidMethod"; | |
1622 } else { | |
1623 s << callXxxMethod(new_return_type); | |
1624 } | |
1625 | |
1626 s << "(m_link->javaObject(__jni_env), method_id"; | |
1627 if (arguments.size() > 0) | |
1628 s << ", "; | |
1629 writeFunctionCallArguments(s, java_function, "__java_", Option(NoCasts | SkipRemovedArguments)); | |
1630 s << ");" << endl | |
1631 << INDENT << "qtjambi_exception_check(__jni_env);" << endl; | |
1632 | |
1633 if (has_function_type) { | |
1634 writeJavaToQt(s, function_type, "__qt_return_value", "__d_return_value", | |
1635 java_function, 0, GlobalRefJObject); | |
1636 | |
1637 if (java_function->nullPointersDisabled()) { | |
1638 s << INDENT << "if (__d_return_value == 0) {" << endl; | |
1639 { | |
1640 Indentation indent(INDENT); | |
1641 s << INDENT << "fprintf(stderr, \"QtJambi: Unexpected null pointer returned from override of '" << java_function->name() << "' in class '%s'\\n\"," << endl | |
1642 << INDENT << " qPrintable(qtjambi_object_class_name(__jni_env, m_link->javaObject(__jni_env))));" << endl; | |
1643 s << INDENT << "__qt_return_value = "; | |
1644 QString defaultValue = java_function->nullPointerDefaultValue(); | |
1645 if (!defaultValue.isEmpty()) | |
1646 s << defaultValue << ";"; | |
1647 else | |
1648 writeBaseClassFunctionCall(s, java_function, implementor, NoReturnStatement); | |
1649 s << endl; | |
1650 } | |
1651 | |
1652 s << INDENT << "}" << endl; | |
1653 } | |
1654 } else if (!java_function->conversionRule(TypeSystem::ShellCode, 0).isEmpty()) { | |
1655 writeConversionRule(s, TypeSystem::ShellCode, java_function, 0, "<invalid>", "<invalid>"); | |
1656 } | |
1657 | |
1658 writeOwnership(s, java_function, "this", -1, implementor); | |
1659 writeOwnership(s, java_function, "__d_return_value", 0, implementor); | |
1660 | |
1661 foreach (AbstractMetaArgument *argumentToReset, argumentsToReset) { | |
1662 | |
1663 QString argumentName = "__java_" + argumentToReset->indexedName(); | |
1664 | |
1665 s << INDENT; | |
1666 if (argumentToReset->type()->isContainer()) | |
1667 s << "qtjambi_invalidate_collection("; | |
1668 else | |
1669 s << "qtjambi_invalidate_object("; | |
1670 | |
1671 s << "__jni_env, " << argumentName << ");" << endl; | |
1672 } | |
1673 | |
1674 s << INDENT << "__jni_env->PopLocalFrame(0);" << endl; | |
1675 | |
1676 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> leaving: " << implementor->name() | |
1677 << "::" << java_function_signature << "\");" << endl; | |
1678 | |
1679 if (function_type) | |
1680 s << INDENT << "return __qt_return_value;" << endl; | |
1681 | |
1682 } | |
1683 | |
1684 s << INDENT << "} else {" << endl; | |
1685 | |
1686 { | |
1687 Indentation indent(INDENT); | |
1688 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> super() and leaving: " | |
1689 << implementor->name() << "::" << java_function_signature << "\");" << endl; | |
1690 writeBaseClassFunctionCall(s, java_function, implementor); | |
1691 } | |
1692 | |
1693 | |
1694 s << INDENT << "}" << endl; | |
1695 | |
1696 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1697 | |
1698 // A little trick to close open painters on a widget | |
1699 if (java_function->name() == "paintEvent") { | |
1700 s << INDENT << "JNIEnv *env = qtjambi_current_environment();" << endl | |
1701 << INDENT << "qtjambi_end_paint(env, m_link->javaObject(env));" << endl; | |
1702 } | |
1703 | |
1704 } else { | |
1705 if(java_function->isRemovedFrom(implementor, TypeSystem::TargetLangCode)){ | |
1706 // Avoid compiler warnings for unused parameters | |
1707 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1708 | |
1709 foreach (const AbstractMetaArgument *argument, arguments) { | |
1710 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl; | |
1711 } | |
1712 } | |
1713 writeBaseClassFunctionCall(s, java_function, implementor); | |
1714 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1715 } | |
1716 | |
1717 s << "}" << endl << endl; | |
1718 | |
1719 } | |
1720 | |
1721 // ### kill implementor | |
1722 | |
1723 void CppImplGenerator::writePublicFunctionOverride(QTextStream &s, | |
1724 const AbstractMetaFunction *java_function, | |
1725 const AbstractMetaClass *implementor) | |
1726 { | |
1727 Q_ASSERT(java_function->originalAttributes() | |
1728 & (AbstractMetaAttributes::Protected | |
1729 | AbstractMetaAttributes::Final)); | |
1730 | |
1731 // The write a public override version of this function to be used by native functions | |
1732 writeFunctionSignature(s, java_function, implementor, "__public_", | |
1733 Option(EnumAsInts | UnderscoreSpaces | |
1734 | (java_function->isAbstract() ? SkipName : NoOption))); | |
1735 s << endl | |
1736 << "{" << endl; | |
1737 Indentation indent(INDENT); | |
1738 writeBaseClassFunctionCall(s, java_function, implementor); | |
1739 s << "}" << endl << endl; | |
1740 } | |
1741 | |
257 | 1742 void CppImplGenerator::writeObjectFunctions(QTextStream &s, const AbstractMetaClass *java_class) |
253 | 1743 { |
310 | 1744 if (java_class == java_class->polymorphicBase()) |
1745 { | |
1746 if (java_class->isPolymorphic()) | |
1747 { | |
1748 s << "extern \"C\" DLL_PUBLIC void* qtd_" << java_class->name() << "_dId(void *nativeId)" << endl | |
1749 << "{" << endl | |
1750 << " QtD_Entity *e = dynamic_cast<QtD_Entity*>((" << java_class->qualifiedCppName() << "*)nativeId);" << endl | |
1751 << " return e ? e->dId : NULL;" << endl | |
1752 << "}" << endl << endl; | |
1753 } | |
1754 | |
1755 if (!java_class->isQObject()) | |
1756 { | |
1757 s << "extern \"C\" DLL_PUBLIC const void* qtd_" << java_class->name() << "_typeId(void *nativeId)" << endl | |
1758 << "{" << endl | |
1759 << " return &typeid((" << java_class->qualifiedCppName() << "*)nativeId);" << endl | |
1760 << "}" << endl << endl; | |
1761 } | |
1762 } | |
1763 | |
1764 if (!java_class->isQObject()) | |
253 | 1765 { |
259 | 1766 s << "extern \"C\" DLL_PUBLIC const void* qtd_" << java_class->name() << "_staticTypeId()" << endl; |
1767 s << "{" << endl; | |
1768 { | |
1769 Indentation indent(INDENT); | |
1770 s << INDENT << "return &typeid(" << java_class->qualifiedCppName() << ");" << endl; | |
1771 } | |
1772 s << "}" << endl << endl; | |
257 | 1773 } |
253 | 1774 } |
1 | 1775 |
1776 void CppImplGenerator::writeVirtualFunctionOverride(QTextStream &s, | |
1777 const AbstractMetaFunction *java_function, | |
1778 const AbstractMetaClass *implementor) | |
1779 { | |
1780 Q_ASSERT(!java_function->isFinalInCpp()); | |
1781 | |
1782 Option options = Option(EnumAsInts | UnderscoreSpaces); | |
1783 | |
1784 // The write a public override version of this function to be used by native functions | |
1785 writeFunctionSignature(s, java_function, implementor, "__override_", | |
1786 options, | |
21 | 1787 QString(), // the class prefix |
1788 QStringList() << "bool static_call"); | |
1 | 1789 s << endl |
1790 << "{" << endl; | |
1791 Indentation indent(INDENT); | |
21 | 1792 s << INDENT << "if (static_call) {" << endl; |
1 | 1793 { |
21 | 1794 Indentation indent(INDENT); |
1 | 1795 writeBaseClassFunctionCall(s, java_function, implementor); |
21 | 1796 } |
1 | 1797 s << INDENT << "} else {" << endl; |
1798 { | |
1799 Indentation indent(INDENT); | |
1800 writeBaseClassFunctionCall(s, java_function, implementor, VirtualCall); | |
1801 } | |
1802 | |
21 | 1803 s << INDENT << "}" << endl; |
1 | 1804 s << "}" << endl << endl; |
1805 } | |
1806 | |
1807 | |
1808 void CppImplGenerator::writeBaseClassFunctionCall(QTextStream &s, | |
1809 const AbstractMetaFunction *java_function, | |
1810 const AbstractMetaClass *, | |
1811 Option options) | |
1812 { | |
1813 bool static_call = !(options & VirtualCall); | |
1814 if ((options & NoReturnStatement) == 0) | |
1815 s << INDENT; | |
1816 if (java_function->isAbstract() && static_call) { | |
1817 s << default_return_statement_qt(java_function->type(), options) << ";" << endl; | |
1818 } else { | |
1819 if (java_function->type() && (options & NoReturnStatement) == 0) | |
1820 s << "return "; | |
1821 if (static_call) { | |
1822 const AbstractMetaClass *implementor = java_function->implementingClass(); | |
1823 if (java_function->isInterfaceFunction()) | |
1824 implementor = java_function->interfaceClass()->primaryInterfaceImplementor(); | |
1825 s << implementor->qualifiedCppName() << "::"; | |
1826 } | |
1827 s << java_function->originalName() << "("; | |
1828 writeFunctionCallArguments(s, java_function, QString(), Option(options | ForceEnumCast)); | |
1829 s << ");" << endl; | |
1830 } | |
1831 } | |
1832 | |
1833 | |
1834 void CppImplGenerator::writeFunctionName(QTextStream &s, | |
1835 const AbstractMetaFunction *java_function, | |
1836 const AbstractMetaClass *java_class, | |
1837 uint options) | |
1838 { | |
1839 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass(); | |
1840 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1841 | |
1842 // Function signature | |
1843 QString return_type = jniReturnName(java_function, options); | |
1844 | |
1845 QString function_name; | |
1846 | |
1847 bool callThrough = java_function->needsCallThrough(); | |
1848 /* qtd if (!callThrough) | |
1849 function_name = java_function->name(); | |
1850 else */ | |
1851 function_name = java_function->marshalledName(); | |
1852 | |
1853 QString args = "__"; | |
1854 | |
1855 if (callThrough && !java_function->isStatic() && !java_function->isConstructor()) | |
1856 args += "J"; | |
1857 | |
1858 if (!arguments.isEmpty()) { | |
1859 foreach (const AbstractMetaArgument *argument, arguments) { | |
1860 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
1861 if (!argument->type()->hasNativeId()) { | |
1862 QString modified_type = java_function->typeReplaced(argument->argumentIndex()+1); | |
1863 if (modified_type.isEmpty()) | |
1864 args += jni_signature(argument->type(), Underscores); | |
1865 else | |
1866 args += jni_signature(modified_type, Underscores); | |
1867 } else { | |
1868 args += "J"; | |
1869 } | |
1870 } | |
1871 } | |
1872 } | |
1873 | |
1874 s << jni_function_signature(cls->package(), cls->name(), function_name, | |
1875 return_type, args, options); | |
1876 | |
1877 } | |
1878 | |
1879 void CppImplGenerator::writeFinalFunctionArguments(QTextStream &s, const AbstractMetaFunction *java_function, bool d_export) | |
1880 { | |
1881 bool callThrough = java_function->needsCallThrough(); | |
1882 s << "("; | |
1883 /* qtd | |
1884 << "JNIEnv *__jni_env," << endl; | |
1885 if (!java_function->isConstructor()) { | |
1886 if (java_function->isStatic()) | |
1887 s << " jclass"; | |
1888 else | |
1889 s << " jobject"; | |
1890 } else | |
1891 s << " jobject " << java_object_name; | |
1892 */ | |
1893 uint nativeArgCount = 0; | |
1894 const AbstractMetaClass *cls = java_function->ownerClass(); | |
257 | 1895 |
1 | 1896 if (java_function->isConstructor() && |
310 | 1897 cls->isPolymorphic()) |
1 | 1898 { |
1899 s << "void *d_ptr"; | |
1900 nativeArgCount++; | |
1901 } | |
1902 | |
1903 // passing pointer to C++ object | |
1904 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
1905 if (hasNativeId) { | |
1906 if (nativeArgCount > 0) | |
1907 s << "," << endl << " "; | |
1908 /* qtd << "," << endl */ s << "void* __this_nativeId"; | |
1909 nativeArgCount++; | |
1910 } | |
1911 | |
1912 AbstractMetaType *f_type = java_function->type(); | |
1913 | |
1914 // return values as strings, arrays or QModelIndex'es we return as arguments | |
1915 bool return_arg = false; | |
1916 if (f_type) { | |
1917 if (f_type->isTargetLangString() || | |
1918 f_type->isContainer() || | |
81 | 1919 f_type->name() == "QModelIndex" || |
1920 f_type->typeEntry()->isStructInD()) | |
1 | 1921 return_arg = true; |
1922 | |
1923 if (return_arg && nativeArgCount > 0) | |
1924 s << "," << endl << " "; | |
1925 | |
1926 if (f_type->isTargetLangString() || f_type->isContainer()) | |
1927 s << "void*"; | |
1928 else if (f_type->name() == "QModelIndex") | |
1929 s << "QModelIndexAccessor*"; | |
81 | 1930 else if (f_type->typeEntry()->isStructInD()) |
1931 s << f_type->typeEntry()->qualifiedCppName() << " *"; | |
1 | 1932 |
1933 if(return_arg) { | |
1934 s << " __d_return_value"; | |
1935 nativeArgCount++; | |
1936 } | |
1937 } | |
1938 | |
1939 // the function arguments | |
1940 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1941 foreach (const AbstractMetaArgument *argument, arguments) { | |
1942 if (!java_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
1943 AbstractMetaType *d_type = argument->type(); | |
1944 const TypeEntry *te = d_type->typeEntry(); | |
1945 | |
1946 QString arg_name = argument->indexedName(); | |
1947 | |
1948 if (nativeArgCount > 0) | |
1949 s << "," << endl << " "; | |
81 | 1950 |
1951 // if QString argument we have to pass DArray | |
35 | 1952 if ((te && te->qualifiedCppName() == "QString") || d_type->isTargetLangString()) { |
1953 if (d_export) | |
1954 s << "string " << arg_name; | |
1955 else | |
1956 s << "DArray " << arg_name; | |
1957 } else if (d_type->isContainer()) { | |
1 | 1958 const ContainerTypeEntry *cte = |
1959 static_cast<const ContainerTypeEntry *>(te); | |
1960 if(isLinearContainer(cte)) | |
1961 s << QString("void *%1, size_t %1_size").arg(arg_name); | |
1962 } else { | |
1963 if (!d_type->hasNativeId()) { | |
1964 if(d_type->isVariant()) { | |
1965 if (d_export) s << "void*"; | |
1966 else s << "QVariant*"; | |
1967 } else | |
1968 s << translateType(argument->type(), EnumAsInts, d_export); | |
1969 } | |
188 | 1970 else if (d_type->name() == "QModelIndex") { |
1971 if(d_export && dVersion == 2 && d_type->isConstant()) | |
1972 s << "const(QModelIndexAccessor)"; | |
1973 else | |
1974 s << "QModelIndexAccessor"; | |
1975 } else if (te->isStructInD()) | |
1 | 1976 s << te->qualifiedCppName(); |
1977 else | |
1978 s << "void*"; | |
1979 | |
1980 s << " " << arg_name; | |
1981 } | |
1982 nativeArgCount++; | |
1983 } | |
1984 } | |
1985 s << ")"; | |
1986 } | |
1987 | |
1988 | |
1989 /*! | |
1990 Generates type conversion from Java -> Qt for all the arguments | |
1991 that are to be to be passed to the function | |
1992 */ | |
1993 void CppImplGenerator::writeFinalFunctionSetup(QTextStream &s, const AbstractMetaFunction *java_function, | |
1994 const QString &qt_object_name, | |
1995 const AbstractMetaClass *cls) | |
1996 { | |
1997 | |
1998 // Translate each of the function arguments into qt types | |
1999 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2000 foreach (const AbstractMetaArgument *argument, arguments) { | |
2001 if (!argument->type()->isPrimitive() | |
2002 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex() + 1).isEmpty()) { | |
2003 writeJavaToQt(s, | |
2004 argument->type(), | |
2005 "__qt_" + argument->indexedName(), | |
2006 argument->indexedName(), | |
2007 java_function, | |
2008 argument->argumentIndex() + 1, | |
2009 Option(UseNativeIds | EnumAsInts)); | |
2010 } | |
2011 } | |
2012 | |
2013 // Extract the qt equivalent to the this pointer and name it "qt_object_name" | |
2014 if (!java_function->isStatic() && !java_function->isConstructor()) { | |
2015 // qtd2 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls); | |
2016 QString className = java_function->isFinalOverload() ? cls->name() : shellClassName(cls); | |
2017 s << INDENT | |
2018 << className << " *" << qt_object_name | |
2019 << " = (" << className << " *) __this_nativeId;" | |
2020 << endl; | |
2021 // << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
2022 // qtd << INDENT << "Q_ASSERT(" << qt_object_name << ");" << endl; | |
2023 } | |
2024 } | |
2025 | |
2026 void CppImplGenerator::writeFinalFunction(QTextStream &s, const AbstractMetaFunction *java_function, | |
2027 const AbstractMetaClass *java_class) | |
2028 { | |
2029 Q_ASSERT(java_class); | |
2030 | |
2031 if (java_function->isModifiedRemoved(TypeSystem::NativeCode)) | |
2032 return; | |
2033 | |
2034 const AbstractMetaClass *cls = java_class ? java_class : java_function->ownerClass(); | |
2035 | |
2036 QString java_function_signature = cls->name() + "::" + java_function->signature(); | |
2037 | |
2038 s << "// " << java_function_signature << endl; | |
2039 | |
2040 const AbstractMetaType *function_type = java_function->type(); | |
2041 QString new_return_type = java_function->typeReplaced(0); | |
2042 bool has_function_type = new_return_type != "void" | |
2043 && (!new_return_type.isEmpty() || function_type != 0) | |
2044 && java_function->argumentReplaced(0).isEmpty(); | |
2045 | |
2046 const QString qt_object_name = java_function->isStatic() ? shellClassName(cls) : "__qt_this"; | |
2047 const QString java_object_name = java_function->isStatic() ? "__jni_class" : "__jni_object"; | |
2048 | |
2049 // we are not wrapping some functions depending on arguments because API is not yet full | |
2050 if (notWrappedYet(java_function)) | |
2051 return; | |
2052 | |
2053 // function signature... | |
2054 bool callThrough = java_function->needsCallThrough(); | |
2055 uint options = m_native_jump_table ? ReturnType | ExternC : StandardJNISignature; | |
2056 writeFunctionName(s, java_function, cls, options); | |
2057 s << endl; | |
2058 writeFinalFunctionArguments(s, java_function); | |
2059 s << endl << "{" << endl; | |
2060 Indentation indent(INDENT); | |
2061 | |
2062 // qtd2 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) entering: " << java_function_signature << "\");" << endl; | |
2063 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
2064 /* if (hasNativeId) | |
2065 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl; | |
2066 // Avoid compiler warnings when the variables are unused | |
2067 { | |
2068 s << INDENT << "Q_UNUSED(__jni_env)" << endl; | |
2069 | |
2070 if (java_function->isConstructor()) | |
2071 s << INDENT << "Q_UNUSED(" << java_object_name << ")" << endl; | |
2072 | |
2073 bool hasNativeId = (callThrough && !java_function->isStatic() && !java_function->isConstructor()); | |
2074 if (hasNativeId) | |
2075 s << INDENT << "Q_UNUSED(__this_nativeId)" << endl; | |
2076 } | |
2077 */ | |
2078 | |
2079 if (cls->isFinal() && (!java_function->isAbstract() || !java_function->isFinalInTargetLang()) && !java_function->wasPublic()) { | |
2080 QString debug = QString("protected function '%1' in final class '%2'") | |
2081 .arg(java_function->signature()).arg(java_class->name()); | |
2082 ReportHandler::warning(debug); | |
2083 // Avoid compiler warnings for unused parameters | |
2084 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2085 | |
2086 foreach (const AbstractMetaArgument *argument, arguments) { | |
2087 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl; | |
2088 } | |
257 | 2089 s << INDENT << default_return_statement_qt(java_function->type()) << ""; |
2090 | |
1 | 2091 } else { |
2092 writeFinalFunctionSetup(s, java_function, qt_object_name, cls); | |
2093 | |
2094 writeCodeInjections(s, java_function, java_function->implementingClass(), CodeSnip::Beginning, TypeSystem::NativeCode); | |
2095 | |
2096 if (java_function->isConstructor()) { | |
2097 writeFinalConstructor(s, java_function, qt_object_name, java_object_name); | |
2098 } else { | |
2099 | |
2100 QString function_prefix = ""; | |
2101 QStringList extra_param; | |
2102 Option option = NoOption; | |
2103 | |
2104 bool hasShell = cls->generateShellClass(); | |
2105 | |
2106 if (java_function->isFinalOverload()) { | |
2107 // no prefix | |
2108 } else if (java_function->isFinalInCpp() && !java_function->wasPublic() && hasShell) { | |
2109 function_prefix = "__public_"; | |
2110 } else if (!java_function->isFinalInCpp() && !java_function->isStatic() && hasShell) { | |
2111 function_prefix = "__override_"; | |
21 | 2112 extra_param.append("__do_static_call"); |
1 | 2113 s << INDENT |
253 | 2114 << "bool __do_static_call = __this_nativeId ? "; |
2115 if (java_class->isQObject()) | |
2116 s << "QtD_QObjectEntity::getQObjectEntity((QObject*)__this_nativeId) : false;" << endl; | |
2117 else | |
310 | 2118 s << "qtd_" << java_class->polymorphicBase()->name() << "_dId(__this_nativeId) : false;" << endl; |
21 | 2119 } else { |
1 | 2120 option = OriginalName; |
2121 } | |
2122 | |
2123 // Call the Qt function on the java object | |
2124 s << " "; | |
2125 if (has_function_type) { | |
2126 const QString qt_return_value = "__qt_return_value"; | |
2127 const QString java_return_value = "__d_return_value"; | |
2128 if (function_type) { | |
2129 writeTypeInfo(s, function_type, EnumAsInts); | |
2130 s << " " << qt_return_value | |
2131 << " = "; | |
2132 } | |
2133 | |
2134 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option, | |
2135 extra_param); | |
2136 s << endl; | |
2137 | |
104 | 2138 writeRefArguments(s, java_function); |
2139 | |
1 | 2140 writeQtToJava(s, function_type, qt_return_value, java_return_value, |
2141 java_function, 0, EnumAsInts); | |
2142 | |
2143 // qtd s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(native) -> leaving: " | |
2144 // << java_function_signature << "\");"; | |
2145 | |
2146 if (function_type && function_type->name() != "QModelIndex") { | |
2147 if(function_type->typeEntry()->isStructInD()) | |
81 | 2148 ; //s << endl << INDENT << "return " << qt_return_value << ";"; |
1 | 2149 else if (!function_type->isTargetLangString() && !function_type->isContainer()) |
2150 s << endl << INDENT << "return " << java_return_value << ";"; | |
2151 } | |
2152 | |
2153 } else { | |
2154 writeFunctionCall(s, qt_object_name, java_function, function_prefix, option, | |
2155 extra_param); | |
104 | 2156 writeRefArguments(s, java_function); |
1 | 2157 } |
2158 } | |
2159 } | |
2160 if(!java_function->argumentReplaced(0).isEmpty()) { | |
2161 s << INDENT << "return 0;" << endl; | |
2162 } | |
2163 | |
2164 s << endl << "}"; | |
2165 s << endl << endl; | |
2166 } | |
2167 | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2168 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
|
2169 { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2170 AbstractMetaArgumentList arguments = java_function->arguments(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2171 foreach (const AbstractMetaArgument *argument, arguments) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2172 AbstractMetaType *d_type = argument->type(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2173 const TypeEntry *te = d_type->typeEntry(); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2174 if ((te && d_type->isNativePointer() && te->name() == "QString")) |
253 | 2175 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
|
2176 } |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2177 } |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2178 |
1 | 2179 void CppImplGenerator::writeAssignment(QTextStream &s, const QString &destName, const QString &srcName, |
2180 const AbstractMetaType *java_type) | |
2181 { | |
2182 if (java_type->isArray()) { | |
2183 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2184 writeAssignment(s, destName + "[" + QString::number(i) + "]", | |
2185 srcName + "[" + QString::number(i) + "]", java_type->arrayElementType()); | |
2186 } | |
2187 } else { | |
2188 s << INDENT << destName << " = " << srcName << ";" << endl; | |
2189 } | |
2190 } | |
2191 | |
2192 void CppImplGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *java_field) | |
2193 { | |
2194 Q_ASSERT(java_field); | |
2195 Q_ASSERT(java_field->isPublic() || java_field->isProtected()); | |
2196 | |
2197 const AbstractMetaFunction *setter = java_field->setter(); | |
2198 const AbstractMetaFunction *getter = java_field->getter(); | |
2199 | |
2200 const AbstractMetaClass *cls = java_field->enclosingClass(); | |
2201 FieldModification mod = cls->typeEntry()->fieldModification(java_field->name()); | |
2202 | |
2203 if(notWrappedYet(getter)) | |
2204 return; | |
2205 | |
2206 if (mod.isReadable()) { | |
2207 // Getter | |
2208 if (getter->wasProtected()) { | |
2209 writeFunctionSignature(s, getter, getter->ownerClass()); | |
2210 s << endl | |
2211 << "{" << endl; | |
2212 { | |
2213 Indentation indent(INDENT); | |
2214 s << INDENT << "return " << java_field->name() << ";" << endl; | |
2215 } | |
2216 s << "}" << endl << endl; | |
2217 } | |
2218 | |
2219 | |
2220 writeFunctionName(s, getter, getter->ownerClass()); | |
2221 s << endl; | |
2222 writeFinalFunctionArguments(s, getter); | |
2223 s << "{" << endl; | |
2224 { | |
2225 Indentation indent(INDENT); | |
2226 | |
2227 | |
2228 writeFinalFunctionSetup(s, getter, "__qt_object", getter->ownerClass()); | |
2229 | |
2230 const QString java_return_value = "__d_return_value"; | |
2231 QString qt_return_value; | |
2232 if (setter->isStatic()) | |
2233 qt_return_value = shellClassName(setter->ownerClass()) + "::"; | |
2234 else | |
2235 qt_return_value = "__qt_object->"; | |
2236 | |
2237 | |
2238 // To avoid "taking address of tmp" | |
2239 s << INDENT; | |
2240 writeTypeInfo(s, getter->type(), Option(ArrayAsPointer)); | |
2241 QString tmp_name = "__tmp_" + getter->name(); | |
2242 s << tmp_name << " = "; | |
2243 | |
2244 if (getter->wasPublic()) | |
2245 qt_return_value += java_field->name(); | |
2246 else | |
2247 qt_return_value += getter->name() + "_getter()"; | |
2248 s << qt_return_value << ";" << endl; | |
2249 | |
2250 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
|
2251 if (getter->type()->isTargetLangString() || getter->type()->name() == "QModelIndex") |
1 | 2252 ; |
2253 else if(getter->type()->typeEntry()->isStructInD()) | |
81 | 2254 ; //s << INDENT << "return " << tmp_name << ";" << endl; |
1 | 2255 else |
2256 s << INDENT << "return " << java_return_value << ";" << endl; | |
2257 } | |
2258 s << "}" << endl << endl; | |
2259 } | |
2260 | |
2261 if(notWrappedYet(setter)) | |
2262 return; | |
2263 | |
2264 // Setter | |
2265 if (mod.isWritable() && !java_field->type()->isConstant()) { | |
2266 // Write public override for protected fields | |
2267 if (setter->wasProtected()) { | |
2268 writeFunctionSignature(s, setter, setter->ownerClass()); | |
2269 s << endl | |
2270 << "{" << endl; | |
2271 { | |
2272 Indentation indent(INDENT); | |
2273 | |
2274 Q_ASSERT(setter->arguments().count() > 0); | |
2275 const AbstractMetaArgument *argument = setter->arguments().at(0); | |
2276 | |
2277 QString thisRef = java_field->isStatic() | |
2278 ? setter->ownerClass()->qualifiedCppName() + QString("::") | |
2279 : QString("this->"); | |
2280 writeAssignment(s, thisRef + java_field->name(), argument->indexedName(), argument->type()); | |
2281 } | |
2282 s << "}" << endl << endl; | |
2283 } | |
2284 | |
2285 writeFunctionName(s, setter, setter->ownerClass()); | |
2286 s << endl; | |
2287 writeFinalFunctionArguments(s, setter); | |
2288 s << "{" << endl; | |
2289 | |
2290 { | |
2291 Indentation indent(INDENT); | |
2292 | |
2293 writeFinalFunctionSetup(s, setter, "__qt_object", setter->ownerClass()); | |
2294 | |
2295 Q_ASSERT(setter->arguments().count() == 1); | |
2296 const AbstractMetaArgument *argument = setter->arguments().at(0); | |
2297 | |
2298 QString dest; | |
2299 if (setter->isStatic()) | |
2300 dest = shellClassName(setter->ownerClass()) + "::"; | |
2301 else | |
2302 dest = "__qt_object->"; | |
2303 | |
2304 QString src; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2305 if (!argument->type()->isPrimitive() && !argument->type()->typeEntry()->isStructInD()) { |
1 | 2306 src = "__qt_" + argument->indexedName(); |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2307 } else if (argument->type()->name() == "QModelIndex") { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2308 src = "qtd_to_QModelIndex(" + argument->indexedName() + ")"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2309 } else |
1 | 2310 src = argument->indexedName(); |
2311 | |
2312 if (setter->wasPublic()) | |
2313 writeAssignment(s, dest + java_field->name(), src, argument->type()); | |
2314 else | |
2315 s << INDENT << dest << setter->name() << "_setter(" << src << ");" << endl; | |
2316 } | |
2317 s << "}" << endl << endl; | |
2318 } | |
2319 } | |
2320 | |
257 | 2321 void CppImplGenerator::writeDestructor(QTextStream &s, const AbstractMetaClass *cls) |
2322 { | |
2323 if (!cls->hasConstructors()) | |
2324 return; | |
2325 | |
310 | 2326 if (cls->polymorphicBase() == cls) |
257 | 2327 { |
310 | 2328 s << "extern \"C\" DLL_PUBLIC void qtd_" << cls->name() << "_delete(void *ptr){" << endl; |
2329 QString className = cls->generateShellClass() ? shellClassName(cls) : cls->typeEntry()->name(); | |
2330 s << " delete (" << className << " *)ptr;" << endl | |
2331 << "}" << endl << endl; | |
1 | 2332 } |
2333 } | |
2334 | |
2335 void CppImplGenerator::writeFinalConstructor(QTextStream &s, | |
2336 const AbstractMetaFunction *java_function, | |
2337 const QString &qt_object_name, | |
2338 const QString &java_object_name) | |
2339 { | |
2340 const AbstractMetaClass *cls = java_function->ownerClass(); | |
2341 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2342 QString className = cls->typeEntry()->qualifiedCppName(); | |
2343 | |
2344 bool hasShellClass = cls->generateShellClass(); | |
2345 | |
2346 s << INDENT << shellClassName(cls) << " *" << qt_object_name | |
2347 << " = new " << shellClassName(cls) | |
2348 << "("; | |
2349 writeFunctionCallArguments(s, java_function, "__qt_"); | |
2350 s << ");" << endl; | |
2351 s << INDENT << "return (void *) " << qt_object_name << ";" << endl; // qtd | |
2352 | |
2353 /* qtd s << INDENT << "QtJambiLink *__qt_java_link = "; | |
2354 if (cls->isQObject()) { | |
2355 s << "qtjambi_construct_qobject(__jni_env, " << java_object_name << ", " | |
2356 << qt_object_name << ")"; | |
2357 } else { | |
2358 s << "qtjambi_construct_object(__jni_env, " << java_object_name << ", " << qt_object_name; | |
2359 if (cls->typeEntry()->isValue()) | |
2360 s << ", \"" << className << "\")"; | |
2361 else // non-QObject, object type | |
2362 s << ", QMetaType::Void, QLatin1String(\"" << cls->fullName().replace(".", "/") << "\"), true)"; | |
2363 } | |
2364 s << ";" << endl | |
2365 << INDENT << "if (!__qt_java_link) {" << endl; | |
2366 { | |
2367 Indentation indent(INDENT); | |
2368 s << INDENT << "qWarning(\"object construction failed for type: " | |
2369 << className << "\");" << endl | |
2370 << INDENT << "return;" << endl; | |
2371 } | |
2372 s << INDENT << "}" << endl; | |
2373 | |
2374 | |
2375 if (cls->isQObject()) { | |
2376 // Make sure all qobjects created by Java are owned by java only if | |
2377 // parent object has not been set. | |
2378 // All other objects will default to split ownership. | |
2379 s << INDENT << "if(!__qt_this->QObject::parent()){" << endl; | |
2380 s << INDENT << " __qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2381 s << INDENT << "}" << endl; | |
2382 } else { | |
2383 // All non-qobjects created by Java are owned by java | |
2384 s << INDENT << "__qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2385 } | |
2386 | |
2387 if (hasCustomDestructor(cls)) { | |
2388 s << INDENT << "__qt_java_link->setDestructorFunction(qtjambi_destructor);" << endl; | |
2389 } | |
2390 | |
2391 if (cls->typeEntry()->typeFlags() & ComplexTypeEntry::DeleteInMainThread) | |
2392 s << INDENT << "__qt_java_link->setDeleteInMainThread(true);" << endl; | |
2393 | |
2394 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions() && !cls->typeEntry()->isObject()) | |
2395 return; | |
2396 | |
2397 if (hasShellClass) { | |
2398 // Set up the link object | |
2399 s << INDENT << qt_object_name << "->m_link = __qt_java_link;" << endl; | |
2400 | |
2401 // Make sure the user data in the QObject has bindings to the qobject's meta object | |
2402 // (this has to be done after the link is set, so that the fake meta object | |
2403 // can access the java object, for which it gets a reference in the link) | |
2404 if (cls->isQObject()) | |
2405 s << INDENT << qt_object_name << "->m_link->setMetaObject(" << qt_object_name << "->metaObject());" << endl; | |
2406 | |
2407 s << INDENT << qt_object_name << "->m_link->setCreatedByJava(true);" << endl; | |
2408 | |
2409 | |
2410 | |
2411 AbstractMetaClassList interfaces = cls->interfaces(); | |
2412 if (interfaces.size() + (cls->baseClass() != 0 ? 1 : 0) > 1) { | |
2413 if (cls->baseClass() != 0) | |
2414 interfaces += cls->baseClass(); | |
2415 foreach (AbstractMetaClass *iface, interfaces) { | |
2416 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface; | |
2417 s << INDENT << qt_object_name << "->m_link->registerSubObject((" << impl->qualifiedCppName() << " *) " << qt_object_name << ");" << endl; | |
2418 } | |
2419 } | |
2420 } | |
2421 | |
2422 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions()) | |
2423 return; | |
2424 | |
2425 // Set up the vtable | |
2426 s << INDENT; | |
2427 QString space(24, ' '); | |
2428 if (hasShellClass) | |
2429 s << qt_object_name << "->m_vtable = "; | |
2430 s << "qtjambi_setup_vtable(__jni_env, " << endl << space << "__jni_object, " << endl; | |
2431 | |
2432 if (cls->hasInconsistentFunctions()) { | |
2433 s << space << "qtjambi_inconsistent_count, " << endl | |
2434 << space << "qtjambi_inconsistent_names, " << endl | |
2435 << space << "qtjambi_inconsistent_signatures, " << endl; | |
2436 } else { | |
2437 s << space << "0, 0, 0, // no inconsistent functions" << endl; | |
2438 } | |
2439 | |
2440 if (cls->hasVirtualFunctions()) { | |
2441 s << space << "qtjambi_method_count, " << endl | |
2442 << space << "qtjambi_method_names, " << endl | |
2443 << space << "qtjambi_method_signatures" << endl; | |
2444 } else { | |
2445 s << space << "0, 0, 0 // no virtual functions" << endl; | |
2446 } | |
2447 | |
2448 s << space << ");" << endl; */ | |
2449 } | |
2450 | |
2451 void CppImplGenerator::writeSignalInitialization(QTextStream &s, const AbstractMetaClass *java_class) | |
2452 { | |
2453 if (!java_class->isQObject() | |
2454 || java_class->queryFunctions(AbstractMetaClass::Signals | AbstractMetaClass::Visible | AbstractMetaClass::NotRemovedFromTargetLang).size() == 0) { | |
2455 return ; | |
2456 } | |
2457 | |
2458 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_signalInitialization", "jboolean") | |
2459 << endl << "(JNIEnv *__jni_env, jobject java_object, jlong ptr, jstring java_signal_name)" << endl | |
2460 << "{" << endl | |
2461 << " QtJambiLink *link = (QtJambiLink *) ptr;" << endl | |
2462 << " if (link == 0)" << endl | |
2463 << " return true;" << endl << endl | |
2464 << " QObject *qt_this = link->qobject();" << endl | |
2465 << " Q_ASSERT(qt_this);" << endl << endl | |
2466 << " QtJambi_SignalWrapper_" << java_class->name() << " *qt_wrapper = " | |
2467 << " (QtJambi_SignalWrapper_" << java_class->name() << " *) link->signalWrapper();" << endl | |
2468 << " if (qt_wrapper == 0) {" << endl | |
2469 << " qt_wrapper = new QtJambi_SignalWrapper_" << java_class->name() << ";" << endl | |
2470 << " link->setSignalWrapper(qt_wrapper);" << endl | |
2471 << " qt_wrapper->link = link;" << endl << endl | |
2472 << " qtjambi_resolve_signals(__jni_env," << endl | |
2473 << " java_object," << endl | |
2474 << " qt_wrapper->m_signals," << endl | |
2475 << " qtjambi_signal_count," << endl | |
2476 << " (char **) qtjambi_signal_names," << endl | |
2477 << " (int *) qtjambi_signal_argumentcounts);" << endl | |
2478 << " }" << endl | |
2479 << " QString signal_name = qtjambi_to_qstring(__jni_env, java_signal_name);" << endl | |
2480 << " return qtjambi_connect_cpp_to_java(__jni_env," << endl | |
2481 << " signal_name," << endl | |
2482 << " qt_this," << endl | |
2483 << " qt_wrapper," << endl | |
2484 << " QLatin1String(\"" << java_class->fullName() << "\")," << endl | |
2485 << " QLatin1String(\"" << signalWrapperPrefix() << "\"));" << endl | |
2486 << "}"; | |
2487 } | |
2488 | |
2489 QString CppImplGenerator::fromObject(const TypeEntry *entry, | |
2490 const QString &var_name) | |
2491 { | |
2492 QString returned; | |
2493 QString package = entry->javaPackage(); | |
2494 const ComplexTypeEntry *centry = entry->isComplex() | |
2495 ? static_cast<const ComplexTypeEntry *>(entry) | |
2496 : 0; | |
2497 | |
2498 if (centry == 0 || centry->polymorphicIdValue().isEmpty()) { | |
2499 /* qtd returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2500 + entry->lookupName() | |
2501 + "\", \"" + QString(package).replace(".", "/") + "/\", true);"; | |
2502 */ | |
2503 if(entry->isObject()) | |
2504 returned = var_name + ";"; | |
2505 else | |
2506 returned = "new " + entry->lookupName() + "(" + var_name +");"; | |
2507 } else { | |
2508 AbstractMetaClass *cls = classes().findClass(centry->qualifiedCppName()); | |
2509 if (!cls) { | |
2510 qFatal("CppImplGenerator::fromObject(): class '%s' could not be resolved...", | |
2511 qPrintable(centry->qualifiedCppName())); | |
2512 } | |
2513 | |
2514 while (cls != 0 && !cls->typeEntry()->isPolymorphicBase()) | |
2515 cls = cls->baseClass(); | |
2516 | |
2517 QString full_name; | |
2518 if (cls != 0) { | |
2519 full_name = cls->fullName(); | |
2520 } else { | |
2521 ReportHandler::warning(QString("class '%1' has polymorphic id but does not inherit a polymorphic class") | |
2522 .arg(centry->qualifiedCppName())); | |
2523 } | |
2524 /* qtd | |
2525 returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2526 + centry->lookupName() | |
2527 + "\", \"" + QString(package).replace(".", "/") + "/\"," | |
2528 + "\"" + jni_signature(full_name, Underscores) + "\", true); // fucking complex"; | |
2529 */ | |
2530 if(entry->isObject()) | |
2531 returned = var_name + "; // complex entry"; | |
2532 else | |
2533 returned = "new " + centry->lookupName() + "(" + var_name +"); // complex entry"; | |
2534 } | |
2535 | |
2536 return returned; | |
2537 } | |
2538 | |
2539 void CppImplGenerator::writeOriginalMetaObjectFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2540 { | |
2541 Q_ASSERT(java_class->isQObject()); | |
2542 | |
2543 s << jni_function_signature(java_class->package(), | |
2544 java_class->name(), | |
2545 "originalMetaObject", | |
2546 "jlong"); | |
2547 | |
2548 s << endl | |
2549 << "(JNIEnv *," << endl | |
2550 << " jclass)" << endl | |
2551 << "{" << endl; | |
2552 { | |
2553 Indentation indent(INDENT); | |
2554 s << INDENT << "return reinterpret_cast<jlong>(&" << java_class->qualifiedCppName() << "::staticMetaObject);" << endl; | |
2555 } | |
2556 s << "}" << endl << endl; | |
2557 } | |
2558 | |
2559 void CppImplGenerator::writeFromNativeFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2560 { | |
2561 s << jni_function_signature(java_class->package(), | |
2562 java_class->name(), | |
2563 "fromNativePointer", | |
2564 "jobject"); | |
2565 s << endl | |
2566 << "(JNIEnv *__jni_env," << endl | |
2567 << " jclass," << endl | |
2568 << " jobject nativePointer)" << endl | |
2569 << "{" << endl; | |
2570 { | |
2571 Indentation indent(INDENT); | |
2572 s << INDENT << "void *ptr = qtjambi_to_cpointer(__jni_env, nativePointer, 1);" << endl | |
2573 << INDENT << "return " << fromObject(java_class->typeEntry(), "ptr") << endl | |
2574 << "}" << endl; | |
2575 } | |
2576 } | |
2577 | |
2578 void CppImplGenerator::writeFromArrayFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2579 { | |
2580 s << jni_function_signature(java_class->package(), | |
2581 java_class->name(), | |
2582 "nativePointerArray", | |
2583 "jobject"); | |
2584 s << endl | |
2585 << "(JNIEnv *__jni_env," << endl | |
2586 << " jclass," << endl | |
2587 << " jobjectArray array)" << endl | |
2588 << "{" << endl; | |
2589 { | |
2590 Indentation indent(INDENT); | |
2591 s << INDENT << "return qtjambi_array_to_nativepointer(__jni_env, " << endl | |
2592 << INDENT << " array, " << endl | |
2593 << INDENT << " sizeof(" | |
2594 << java_class->qualifiedCppName() << "));" << endl; | |
2595 } | |
2596 s << "}" << endl; | |
2597 } | |
2598 | |
2599 | |
2600 void CppImplGenerator::writeInterfaceCastFunction(QTextStream &s, | |
2601 const AbstractMetaClass *java_class, | |
2602 const AbstractMetaClass *interface) | |
2603 { | |
2604 Q_ASSERT(interface->isInterface()); | |
2605 const InterfaceTypeEntry *ie = static_cast<const InterfaceTypeEntry *>(interface->typeEntry()); | |
2606 QString interface_name = ie->origin()->targetLangName(); | |
2607 | |
2608 s << endl | |
2609 << jni_function_signature(java_class->package(), | |
2610 java_class->name(), | |
2611 QString("__qt_cast_to_%1").arg(interface_name), | |
2612 "jlong", | |
2613 "__J"); | |
2614 | |
2615 s << endl | |
2616 << "(JNIEnv *," << endl | |
2617 << " jobject," << endl | |
2618 << " jlong ptr)" << endl | |
2619 << "{" << endl | |
2620 << " return (jlong) (" << interface->primaryInterfaceImplementor()->qualifiedCppName() << " *) " | |
2621 << "(" << java_class->qualifiedCppName() << " *) ptr;" << endl | |
2622 << "}" << endl; | |
2623 } | |
2624 | |
2625 bool CppImplGenerator::writeConversionRule(QTextStream &s, | |
2626 TypeSystem::Language target_language, | |
2627 const AbstractMetaFunction *java_function, | |
2628 int argument_index, | |
2629 const QString &qt_name, | |
2630 const QString &java_name) | |
2631 { | |
2632 if (argument_index < 0 || java_function == 0) | |
2633 return false; | |
2634 | |
2635 QString conversion_rule = java_function->conversionRule(target_language, argument_index); | |
2636 | |
2637 if (!conversion_rule.isEmpty()) { | |
2638 QString qt_name_var; | |
2639 QString java_name_var; | |
2640 | |
2641 if ((argument_index == 0 && target_language == TypeSystem::NativeCode) | |
2642 || (argument_index != 0 && target_language == TypeSystem::ShellCode)) { | |
2643 qt_name_var = "%in"; | |
2644 java_name_var = "%out"; | |
2645 } else { | |
2646 qt_name_var = "%out"; | |
2647 java_name_var = "%in"; | |
2648 } | |
2649 | |
2650 conversion_rule = conversion_rule.replace(qt_name_var, qt_name) | |
2651 .replace(java_name_var, java_name); | |
2652 | |
2653 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2654 for (int i=0; i<arguments.size(); ++i) { | |
2655 conversion_rule = conversion_rule.replace("%" + QString::number(i+1), | |
2656 arguments.at(i)->indexedName()); | |
2657 } | |
2658 | |
2659 QStringList lines = conversion_rule.split("\n"); | |
2660 foreach (QString line, lines) { | |
2661 s << INDENT << line.trimmed() << endl; | |
2662 } | |
2663 | |
2664 return true; | |
2665 } else { | |
2666 return false; | |
2667 } | |
2668 } | |
2669 | |
2670 | |
2671 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2672 const AbstractMetaClass *java_class, | |
2673 const AbstractMetaType *function_return_type, | |
2674 const QString &qt_name, | |
2675 const QString &java_name, | |
2676 const AbstractMetaFunction *java_function, | |
2677 int argument_index) | |
2678 { | |
310 | 2679 s << "//writeJavaToQt" << endl; // TMP |
2680 | |
1 | 2681 // Conversion to C++: Shell code for return values, native code for arguments |
2682 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2683 if (writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2684 return; | |
2685 | |
2686 s << INDENT << shellClassName(java_class) << " *" << qt_name << " = (" | |
2687 << shellClassName(java_class) << " *) "; | |
2688 if (java_class->isQObject()) | |
2689 s << "qtjambi_to_qobject"; | |
2690 else | |
2691 s << "qtjambi_to_object"; | |
2692 s << "(__jni_env, " << java_name << ");" << endl; | |
2693 if (java_class->isQObject()) { | |
2694 // ### throw exceptions when objects are null... | |
2695 s << INDENT << "if (!" << qt_name << ") " | |
2696 << default_return_statement_java(function_return_type) << ";" << endl << endl; | |
2697 } | |
2698 } | |
2699 | |
2700 | |
2701 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2702 const AbstractMetaType *java_type, | |
2703 const QString &qt_name, | |
2704 const QString &java_name, | |
2705 const AbstractMetaFunction *java_function, | |
2706 int argument_index, | |
2707 Option options) | |
2708 { | |
310 | 2709 s << "//writeJavaToQt 2" << endl; // TMP |
2710 | |
1 | 2711 // Conversion to C++: Shell code for return values, native code for arguments |
2712 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2713 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2714 return; | |
2715 | |
2716 if (java_type == 0) { | |
2717 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
2718 "language '%4'") | |
2719 .arg(argument_index) | |
2720 .arg(java_function->implementingClass()->name()) | |
2721 .arg(java_function->name()) | |
2722 .arg(int(lang)); | |
2723 ReportHandler::warning(warn); | |
2724 return; | |
2725 } | |
2726 if (java_type->name() == "QModelIndex") { | |
2727 s << INDENT << "QModelIndex " << qt_name << " = qtd_to_QModelIndex(" | |
2728 << java_name << ");" << endl; | |
2729 } else if (java_type->typeEntry()->isStructInD()) { | |
2730 // empty | |
2731 } else if (java_type->typeEntry() && java_type->typeEntry()->qualifiedCppName() == "QString") { | |
2732 s << INDENT << "QString " << qt_name | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2733 << " = " << QString("QString::fromUtf8((const char *)%1.ptr, %1.length);").arg(java_name) << endl; |
1 | 2734 } else if (java_type->isJObjectWrapper()) { |
2735 s << INDENT << "JObjectWrapper " << qt_name | |
2736 << " = qtjambi_to_jobjectwrapper(__jni_env, " << java_name << ");" << endl; | |
2737 } else if (java_type->isVariant()) { | |
2738 s << INDENT << "QVariant " << qt_name | |
2739 << " = " << java_name << " == NULL ? QVariant() : QVariant(*" << java_name << ");" << endl; | |
2740 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { | |
2741 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2742 | |
2743 // ### 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
|
2744 // s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; |
1 | 2745 s << INDENT; |
2746 writeTypeInfo(s, elementType); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2747 s << " *" << qt_name << " = ("; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2748 writeTypeInfo(s, elementType); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2749 s << "*) " << java_name << ";" << endl; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2750 /* |
1 | 2751 s << INDENT << "__jni_env->" << getXxxArrayRegion(elementType) << "( (" << translateType(java_type, options) |
2752 << ")" << java_name << ", 0, " << java_type->arrayElementCount() << ", " | |
2753 << "(" << translateType(elementType, options) << " *" << ")" | |
2754 << qt_name << ");" << endl; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2755 */ |
1 | 2756 } else if (java_type->isArray()) { |
2757 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2758 | |
2759 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; | |
2760 writeTypeInfo(s, elementType); | |
2761 s << "[" << java_type->arrayElementCount() << "]" << qt_name << ";" << endl; | |
2762 | |
2763 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2764 writeJavaToQt(s, elementType, qt_name + "[" + QString::number(i) + "]", | |
2765 "__jni_env->GetObjectArrayElement(" + java_name + ", " + QString::number(i) + ")", 0, -1, options); | |
2766 } | |
2767 | |
2768 } else if (java_type->isTargetLangString()) { | |
2769 s << INDENT << "QString " << qt_name | |
2770 << " = " << "QString::fromUtf8(" << java_name << ", " << java_name << "_size);" << endl; | |
2771 // qtd << " = qtjambi_to_qstring(__jni_env, (jstring) " << java_name << ");" << endl; | |
2772 | |
2773 } else if (java_type->isTargetLangChar()) { | |
2774 s << INDENT << "QChar " << qt_name | |
2775 << " = (ushort)" << java_name << ";" << endl; | |
2776 | |
2777 } else if (java_type->isEnum() || java_type->isFlags()) { | |
2778 | |
2779 bool written = false; | |
2780 if (java_type->isEnum()) { | |
2781 AbstractMetaEnum *java_enum = | |
2782 m_classes.findEnum(static_cast<const EnumTypeEntry *>(java_type->typeEntry())); | |
2783 if (java_enum && !java_enum->isPublic()) { | |
2784 | |
2785 s << INDENT << "int " << qt_name << " = "; | |
2786 written = true; | |
2787 } | |
2788 } | |
2789 | |
2790 if (!written) { | |
2791 QString qualified_name = java_type->typeEntry()->qualifiedCppName(); | |
2792 s << INDENT << qualified_name << " " << qt_name | |
2793 << " = (" << qualified_name << ") "; | |
2794 } | |
2795 | |
2796 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
|
2797 s << java_name << ";" << endl; |
1 | 2798 |
2799 } else if (options & BoxedPrimitive) { | |
2800 const PrimitiveTypeEntry *pentry = TypeDatabase::instance()->findTargetLangPrimitiveType("int"); | |
2801 Q_ASSERT(pentry); | |
2802 | |
2803 s << java_name << ";" << endl; | |
2804 | |
2805 } else { | |
2806 s << java_name << ';' << endl; | |
2807 } | |
2808 | |
2809 } else if (java_type->isContainer()) { | |
2810 writeJavaToQtContainer(s, java_type, qt_name, java_name, 0, -1); | |
2811 | |
2812 } else if (java_type->isThread()) { | |
2813 s << INDENT << "QThread *" << qt_name << " = qtjambi_to_thread(__jni_env, " << java_name | |
2814 << ");" << endl; | |
2815 | |
2816 } else if (java_type->typeEntry()->isCustom()) { | |
2817 const CustomTypeEntry *custom_type = | |
2818 static_cast<const CustomTypeEntry *>(java_type->typeEntry()); | |
2819 s << INDENT; | |
2820 custom_type->generateCppJavaToQt(s, java_type, "__jni_env", qt_name, java_name); | |
2821 s << ";" << endl; | |
2822 | |
2823 } else { | |
2824 | |
2825 const TypeEntry *type = java_type->typeEntry(); | |
2826 QString class_name = type->name(); | |
2827 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName()); | |
2828 | |
2829 // Declaration and the c-cast | |
2830 s << INDENT; | |
2831 writeTypeInfo(s, java_type); | |
2832 s << ' ' << qt_name << " = ("; | |
2833 writeTypeInfo(s, java_type); | |
2834 s << ") "; | |
2835 | |
2836 if (java_type->isPrimitive()) { | |
2837 if (options & BoxedPrimitive) { | |
2838 const PrimitiveTypeEntry *pentry = static_cast<const PrimitiveTypeEntry *>(type); | |
2839 //std::cout << "---error_here " << type->targetLangName().toStdString() << " \n"; | |
2840 //std::cout << "----func_here " << java_function->marshalledName().toStdString() << " \n"; | |
2841 | |
2842 if (!pentry->preferredConversion()) | |
2843 pentry = TypeDatabase::instance()->findTargetLangPrimitiveType(pentry->targetLangName()); | |
2844 Q_ASSERT(pentry); | |
2845 | |
2846 s << java_name << ";" << endl; | |
2847 | |
2848 } else if ((options & GlobalRefJObject) && type->jniName() == QLatin1String("jobject")) { | |
2849 s << "__jni_env->NewGlobalRef(" << java_name << ");" << endl; | |
2850 } else { | |
2851 s << java_name << ';' << endl; | |
2852 } | |
2853 | |
2854 #if 0 | |
2855 } else if (java_type->isEnum()) { | |
2856 s << "qtjambi_to_enum(__jni_env, " << java_name << ");" << endl; | |
2857 #endif | |
2858 | |
2859 } else if ((java_type->isQObject() || java_type->isObject()) | |
2860 && static_cast<const ObjectTypeEntry *>(type)->designatedInterface()) { | |
2861 /* qtd const InterfaceTypeEntry *ie = | |
2862 static_cast<const ObjectTypeEntry *>(type)->designatedInterface(); | |
2863 s << "qtjambi_to_interface(__jni_env, "; | |
2864 | |
2865 // This cast is only valid if we're dealing with a native id | |
2866 if ((options & UseNativeIds) == UseNativeIds) | |
2867 s << "(QtJambiLink *)"; | |
2868 */ | |
2869 s << java_name << ";" << endl; | |
2870 /* qtd | |
2871 s << "\"" << ie->targetLangName() << "\", \"" | |
2872 << ie->javaPackage().replace(".", "/") << "/\", " | |
2873 << "\"__qt_cast_to_" << type->targetLangName() << "\");" << endl; | |
2874 */ | |
2875 } else if (java_type->isObject() || java_type->isQObject() || java_type->isNativePointer()) { | |
2876 if (java_type->isReference()) { | |
2877 s << "* (" << qualified_class_name << " " | |
2878 << QString(java_type->actualIndirections(), '*') << ") "; | |
2879 } | |
2880 | |
2881 if (java_type->isNativePointer()) { | |
2882 /* qtd s << "qtjambi_to_cpointer(" | |
2883 << "__jni_env, " | |
2884 << java_name << ", " | |
2885 << java_type->actualIndirections() << ");" << endl; */ | |
2886 s << java_name << ";" << endl; // qtd | |
2887 }/* qtd else if (java_type->isQObject()) { | |
2888 if ((options & UseNativeIds) == 0) | |
2889 s << "qtjambi_to_qobject(__jni_env, "; | |
2890 else | |
2891 s << "qtjambi_from_jlong("; | |
2892 s << java_name; | |
2893 s << ");" << endl; | |
2894 }*/ else { | |
2895 /* qtd if ((options & UseNativeIds) == 0) | |
2896 s << "qtjambi_to_object(__jni_env, "; | |
2897 else | |
2898 s << "qtjambi_from_jlong("; | |
2899 */ s << java_name; | |
2900 s << ";" << endl; // + | |
2901 // qtd s << ");" << endl; | |
2902 } | |
2903 | |
2904 } else { | |
2905 // Return values... | |
2906 if (argument_index == 0) { | |
2907 s << "(" << java_name << " != 0 ? *(" << qualified_class_name << " *)"; | |
2908 /* qtd if ((options & UseNativeIds) == 0) | |
2909 s << "qtjambi_to_object(__jni_env, "; | |
2910 else | |
2911 s << "qtjambi_from_jlong("; | |
2912 */ s << java_name; | |
2913 s << " : " << qualified_class_name << "());" << endl; | |
2914 } else { | |
2915 s << "*" | |
2916 << "(" << qualified_class_name << " *)"; | |
2917 bool null_check = false; | |
2918 /* qtd if ((options & UseNativeIds) == 0) { | |
2919 s << "qtjambi_to_object(__jni_env, "; | |
2920 } else if (hasDefaultConstructor(java_type)) { | |
2921 null_check = true; | |
2922 s << "(" << java_name << " != 0 ? qtjambi_from_jlong("; | |
2923 } else { | |
2924 s << "qtjambi_from_jlong("; | |
2925 } | |
2926 */ s << java_name; | |
2927 // qtd s << ")"; | |
2928 | |
2929 if (null_check) | |
2930 s << " : default_" << QString(qualified_class_name).replace("::", "_") << "())"; | |
2931 s << ";" << endl; | |
2932 } | |
2933 | |
2934 } | |
2935 } | |
2936 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl; | |
2937 } | |
2938 | |
2939 void CppImplGenerator::writeQtToJava(QTextStream &s, | |
2940 const AbstractMetaType *java_type, | |
2941 const QString &qt_name, | |
2942 const QString &java_name, | |
2943 const AbstractMetaFunction *java_function, | |
2944 int argument_index, | |
2945 Option option) | |
2946 { | |
310 | 2947 s << "writeQtToJava" << endl; |
1 | 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()) | |
257 | 3090 // || java_type->isObject() ) |
1 | 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(); | |
310 | 3549 if (java_function->isConstructor() && cls->isPolymorphic()) { |
1 | 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 } |