Mercurial > projects > qtd
annotate generator/cppimplgenerator.cpp @ 257:17b5e13364b7 lifetime
(none)
author | maxter |
---|---|
date | Wed, 16 Sep 2009 13:56:02 +0000 |
parents | 073b9153ed8a |
children | 515d6e1c7b10 |
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 |
592 if (java_class->hasPublicDestructor() && (!java_class->baseClass() || !java_class->hasVirtualFunctions())) | |
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 |
604 if (java_class->typeEntry()->isObject()) | |
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 } | |
621 | |
622 if (cpp_shared) | |
623 writeInitCallbacks(s, java_class); | |
624 | |
625 // Functions in shell class | |
626 s << "// Functions in shell class" << endl; | |
627 AbstractMetaFunctionList shellFunctions = java_class->nonVirtualShellFunctions(); | |
628 for (int i=0; i<shellFunctions.size(); ++i) { | |
629 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
|
630 writeShellFunction(s, function, java_class, -1); |
1 | 631 } |
632 | |
633 // Write public overrides for functions that are protected in the base class | |
634 // so they can be accessed from the native callback | |
635 s << "// public overrides for functions that are protected in the base class" << endl; | |
636 AbstractMetaFunctionList public_override_functions = java_class->publicOverrideFunctions(); | |
637 foreach (AbstractMetaFunction *function, public_override_functions) { | |
638 if(notWrappedYet(function)) | |
639 continue; | |
640 writePublicFunctionOverride(s, function, java_class); | |
641 } | |
642 | |
643 // Write virtual function overries used to decide on static/virtual calls | |
644 s << "// Write virtual function overries used to decide on static/virtual calls" << endl; | |
645 AbstractMetaFunctionList virtual_functions = java_class->virtualOverrideFunctions(); | |
646 foreach (const AbstractMetaFunction *function, virtual_functions) { | |
647 if(notWrappedYet(function)) | |
648 continue; | |
649 writeVirtualFunctionOverride(s, function, java_class); | |
650 } | |
651 } | |
652 | |
653 writeExtraFunctions(s, java_class); | |
654 /* qtd2 | |
655 writeToStringFunction(s, java_class); | |
656 | |
657 if (java_class->hasCloneOperator()) { | |
658 writeCloneFunction(s, java_class); | |
659 } | |
660 | |
661 // Signals | |
662 AbstractMetaFunctionList signal_functions = signalFunctions(java_class); | |
663 for (int i=0; i<signal_functions.size(); ++i) | |
664 writeSignalFunction(s, signal_functions.at(i), java_class, i); | |
665 */ | |
666 s << "// ---externC---" << endl; | |
667 | |
668 // Native callbacks (all java functions require native callbacks) | |
669 AbstractMetaFunctionList class_funcs = java_class->functionsInTargetLang(); | |
670 foreach (AbstractMetaFunction *function, class_funcs) { | |
671 if (!function->isEmptyFunction()) | |
672 writeFinalFunction(s, function, java_class); | |
673 } | |
674 s << "// ---externC---end" << endl; | |
675 | |
676 | |
677 class_funcs = java_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NotRemovedFromTargetLang); | |
678 foreach (AbstractMetaFunction *function, class_funcs) { | |
679 if (function->implementingClass() != java_class) { | |
680 writeFinalFunction(s, function, java_class); | |
681 } | |
682 } | |
683 | |
684 // Field accessors | |
685 s << "// Field accessors" << endl; | |
686 foreach (AbstractMetaField *field, java_class->fields()) { | |
687 if (field->wasPublic() || (field->wasProtected() && !java_class->isFinal())) | |
688 writeFieldAccessors(s, field); | |
689 } | |
690 /* | |
691 s << "// writeFromNativeFunction" << endl; | |
692 writeFromNativeFunction(s, java_class); | |
693 | |
694 if (java_class->isQObject()) | |
695 writeOriginalMetaObjectFunction(s, java_class); | |
696 | |
697 if (java_class->typeEntry()->isValue()) | |
698 writeFromArrayFunction(s, java_class); | |
699 | |
700 // generate the __qt_cast_to_Xxx functions | |
701 if (!java_class->isNamespace() && !java_class->isInterface()) { | |
702 AbstractMetaClassList interfaces = java_class->interfaces(); | |
703 foreach (AbstractMetaClass *iface, interfaces) | |
704 writeInterfaceCastFunction(s, java_class, iface); | |
705 } | |
706 | |
707 writeSignalInitialization(s, java_class); | |
708 */ | |
709 // qtd writeJavaLangObjectOverrideFunctions(s, java_class); | |
710 | |
253 | 711 if (java_class->isQObject()) |
712 { | |
713 s << endl << endl | |
714 << "extern \"C\" DLL_PUBLIC void* qtd_" << java_class->name() << "_staticMetaObject() {" << endl | |
715 << " return (void*)&" << java_class->name() << "::staticMetaObject;" << endl | |
716 << "}" << endl; | |
717 } | |
718 | |
1 | 719 s << endl << endl; |
720 | |
238 | 721 priGenerator->addSource(java_class->package(), fileNameForClass(java_class)); |
1 | 722 } |
723 | |
724 void CppImplGenerator::writeVirtualDispatchFunction(QTextStream &s, const AbstractMetaFunction *function, bool d_export) | |
725 { | |
726 uint options2 = ReturnType | ExternC; | |
727 QString return_type = jniReturnName(function, options2); | |
728 QString f_name = function->marshalledName() + "_dispatch"; | |
729 | |
730 if(!d_export) | |
731 s << "extern \"C\" "; | |
732 | |
733 if (!cpp_shared || d_export) { | |
734 s << return_type << " " << f_name; | |
735 writeVirtualDispatchArguments(s, function, d_export); | |
736 if(!d_export) | |
737 s << ";"; | |
738 } else if (cpp_shared) { | |
739 s << "typedef " << return_type << " " << "(*pf" << f_name << ")"; | |
740 writeVirtualDispatchArguments(s, function, false); | |
741 s << ";" << endl | |
148
ae34188ddd84
private signals of QAbstractItemModel are now accessible
eldar
parents:
132
diff
changeset
|
742 << "pf" << f_name << " " << f_name << ";"; |
1 | 743 } |
744 | |
745 s << endl; | |
746 } | |
747 | |
748 void CppImplGenerator::writeShellVirtualFunction(QTextStream &s, const AbstractMetaFunction *function, | |
749 const AbstractMetaClass *implementor, int id) | |
750 { | |
751 // ---------------------------- | |
752 if(notWrappedYet(function)) | |
753 return; | |
754 | |
755 AbstractMetaType *f_type = function->type(); | |
756 QString new_return_type = function->typeReplaced(0); | |
757 bool has_function_type = ((f_type != 0 | |
758 || !new_return_type.isEmpty()) | |
759 && new_return_type != "void"); | |
760 | |
761 writeVirtualDispatchFunction(s, function); | |
762 | |
763 writeFunctionSignature(s, function, implementor, QString(), OriginalName); | |
764 | |
765 s << endl | |
766 << "{" << endl; | |
767 | |
768 Option options = NoOption; | |
769 | |
770 Indentation indent(INDENT); | |
771 //bool static_call = !(options & VirtualCall); | |
772 //s << "std::cout << \"entering " << function->marshalledName() << " \\n\"; " << endl; | |
773 | |
774 if (f_type) { | |
775 if (f_type->isTargetLangString()) | |
104 | 776 s << INDENT << "DArray ret_str;" << endl; |
81 | 777 |
1 | 778 if (f_type->name() == "QModelIndex") |
779 s << INDENT << "QModelIndexAccessor __d_return_value;" << endl; | |
81 | 780 else if (f_type->typeEntry()->isStructInD()) |
781 s << INDENT << f_type->typeEntry()->qualifiedCppName() << " __d_return_value;" << endl; | |
782 | |
1 | 783 if (f_type->isContainer()) |
784 s << INDENT << "void* __d_return_value;" << endl | |
785 << INDENT << "size_t __d_return_value_size;" << endl; | |
786 } | |
787 | |
788 AbstractMetaArgumentList arguments = function->arguments(); | |
789 foreach (AbstractMetaArgument *argument, arguments) { | |
790 if (!function->argumentRemoved(argument->argumentIndex()+1)) { | |
791 if (!argument->type()->isPrimitive() | |
792 || !function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) { | |
793 if(argument->type()->isContainer()) { | |
794 QString arg_name = argument->indexedName(); | |
795 s << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl | |
796 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name); | |
797 | |
798 writeQtToJava(s, | |
799 argument->type(), | |
800 arg_name, | |
801 "__d_" + arg_name, | |
802 function, | |
803 argument->argumentIndex() + 1, | |
804 Option(VirtualDispatch)); | |
805 } | |
806 } | |
807 } | |
808 } | |
809 | |
810 if ((options & NoReturnStatement) == 0) | |
811 s << INDENT; | |
812 /* qtd if (function->isAbstract() && static_call) { | |
813 s << default_return_statement_qt(function->type(), options) << ";" << endl; | |
814 } else */ | |
815 { | |
816 if (f_type) { | |
817 if (f_type->isTargetLangString() || f_type->isContainer()) | |
818 ; | |
819 else if ((f_type->isValue() && !f_type->typeEntry()->isStructInD()) || | |
820 f_type->isVariant() ) | |
821 s << f_type->name() << " *__qt_return_value = (" << f_type->name() << "*) "; | |
822 else if (f_type->isObject() || f_type->isQObject()) | |
823 s << "return (" << f_type->name() <<"*) "; | |
81 | 824 else if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 825 ; |
826 else if ((options & NoReturnStatement) == 0) | |
827 s << "return "; | |
828 | |
829 if (f_type->isEnum() || f_type->isFlags()) | |
830 s << "(" << f_type->typeEntry()->qualifiedCppName() <<") "; | |
831 } | |
832 | |
833 s << function->marshalledName() << "_dispatch(" | |
253 | 834 << "this->dId"; |
1 | 835 |
836 if (f_type) { | |
837 if (f_type->isTargetLangString()) | |
104 | 838 s << ", &ret_str"; |
81 | 839 if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 840 s << ", &__d_return_value"; |
841 if (f_type->isContainer()) | |
842 s << ", &__d_return_value, &__d_return_value_size"; | |
843 } | |
844 | |
845 if (function->arguments().size() > 0) | |
846 s << ", "; | |
847 writeFunctionCallArguments(s, function, QString(), Option(options | ForceEnumCast | SkipRemovedArguments | ExcludeConst | VirtualDispatch)); | |
848 | |
849 s << ");" << endl; | |
850 | |
851 //s << "std::cout << \"leaving " << function->marshalledName() << " \\n\"; " << endl; | |
852 | |
853 if (f_type) { | |
854 if (f_type->name() == "QModelIndex") { | |
855 s << INDENT << "QModelIndex __qt_return_value = qtd_to_QModelIndex( __d_return_value );" << endl; | |
856 #ifdef Q_OS_WIN32 | |
253 | 857 s << "qtd_dummy();" << endl; // hack!!! |
1 | 858 #endif |
859 s << INDENT << "return __qt_return_value;" << endl; | |
81 | 860 } else if (f_type->typeEntry()->isStructInD()) |
861 s << INDENT << "return __d_return_value;" << endl; | |
1 | 862 |
863 if (f_type->isContainer()) { | |
864 writeJavaToQt(s, f_type, "__qt_return_value", "__d_return_value", | |
865 function, 0, GlobalRefJObject); | |
866 s << INDENT << "return __qt_return_value;" << endl; | |
867 } | |
868 | |
869 if (f_type->isTargetLangString()) | |
104 | 870 s << INDENT << "return " << "QString::fromUtf8((const char*) ret_str.ptr, ret_str.length);" << endl; |
1 | 871 |
872 if ( ( f_type->isValue() && !f_type->typeEntry()->isStructInD() ) || f_type->isVariant() ) | |
873 s << INDENT << "return " << f_type->name() << "(*__qt_return_value);" << endl; //" __qt_return_value = "; | |
874 } | |
875 } | |
876 | |
877 s << "}" << endl << endl; | |
878 // ---------------------------- | |
879 } | |
880 | |
881 void CppImplGenerator::writeVirtualDispatchArguments(QTextStream &s, const AbstractMetaFunction *d_function, bool d_export) | |
882 { | |
883 uint nativeArgCount = 0; | |
884 AbstractMetaType *ret_type = d_function->type(); | |
885 | |
886 s << "(void *d_entity"; | |
887 | |
888 if (ret_type) { | |
104 | 889 if (ret_type->isTargetLangString()) { |
890 if (d_export) | |
891 s << ", string* ret_str"; | |
892 else | |
893 s << ", DArray* ret_str"; | |
894 } | |
1 | 895 if (ret_type->name() == "QModelIndex") |
896 s << ", QModelIndexAccessor *__d_return_value"; | |
81 | 897 else if(ret_type->typeEntry()->isStructInD()) |
898 s << ", " << ret_type->typeEntry()->qualifiedCppName() << " *__d_return_value"; | |
899 | |
1 | 900 if (ret_type->isContainer()) |
901 s << ", void** __d_arr_ptr, size_t* __d_arr_size"; | |
902 } | |
903 | |
904 if (d_function->arguments().size() > 0) | |
905 s << ", "; | |
906 | |
907 | |
908 // the function arguments | |
909 AbstractMetaArgumentList arguments = d_function->arguments(); | |
910 foreach (const AbstractMetaArgument *argument, arguments) { | |
911 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
912 if (nativeArgCount > 0) | |
913 s << "," << " "; | |
914 | |
915 AbstractMetaType *d_type = argument->type(); | |
916 QString arg_name = argument->indexedName(); | |
917 | |
918 if (d_type->name() == "QModelIndex") | |
919 s << "QModelIndexAccessor" << QString(d_type->actualIndirections(), '*') << " " << arg_name; | |
920 else if (d_type->isContainer()) { | |
921 if (d_export) { | |
922 s << DGenerator::translateType(d_type, d_function->ownerClass(), NoOption) << "* "; | |
923 } else | |
924 s << "DArray* "; | |
925 s << arg_name; | |
926 } else if (d_type->typeEntry()->isStructInD()) | |
927 s << d_type->typeEntry()->qualifiedCppName() << QString(d_type->actualIndirections(), '*') | |
928 << " " << arg_name; | |
929 else if (d_type->isTargetLangString()) { | |
930 if (d_export) | |
931 s << "wchar* "; | |
932 else | |
933 s << "const unsigned short* "; | |
934 s << QString("%1, int %1_size").arg(arg_name); | |
935 } else { | |
936 if(d_type->isVariant()) | |
937 s << "void*"; | |
938 else if (!d_type->hasNativeId()) | |
939 { | |
940 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_type->typeEntry()); | |
941 if(d_type->typeEntry()->isObject() && ctype->isAbstract() && d_export) | |
942 { | |
943 QString d_name = d_type->typeEntry()->qualifiedCppName(); | |
944 s << d_name + "_ConcreteWrapper" + QString(d_type->indirections(),'*'); | |
945 } | |
946 else | |
39 | 947 s << translateType(d_type, EnumAsInts, d_export); |
1 | 948 } |
949 else | |
950 s << "void*"; | |
951 s << " " << argument->indexedName(); | |
952 } | |
953 nativeArgCount++; | |
954 } | |
955 } | |
956 s << ")"; | |
957 } | |
958 | |
959 void CppImplGenerator::writeJavaLangObjectOverrideFunctions(QTextStream &s, const AbstractMetaClass *cls) | |
960 { | |
961 if (cls->hasHashFunction()) { | |
962 AbstractMetaFunctionList hashcode_functions = cls->queryFunctionsByName("hashCode"); | |
963 bool found = false; | |
964 foreach (const AbstractMetaFunction *function, hashcode_functions) { | |
965 if (function->actualMinimumArgumentCount() == 0) { | |
966 found = true; | |
967 break; | |
968 } | |
969 } | |
970 | |
971 if (!found) { | |
972 s << endl | |
973 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_hashCode", "jint") | |
974 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
975 << INDENT << "{" << endl; | |
976 { | |
977 Indentation indent(INDENT); | |
978 s << INDENT << "Q_UNUSED(__jni_env);" << endl | |
979 << INDENT << cls->qualifiedCppName() << " *__qt_this = (" | |
980 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
981 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
982 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
983 << INDENT << "return qHash(*__qt_this);" << endl; | |
984 } | |
985 s << INDENT << "}" << endl; | |
986 } | |
987 } | |
988 | |
989 // Qt has a standard toString() conversion in QVariant? | |
990 QVariant::Type type = QVariant::nameToType(cls->qualifiedCppName().toLatin1()); | |
991 if (QVariant(type).canConvert(QVariant::String) && !cls->hasToStringCapability()) { | |
992 AbstractMetaFunctionList tostring_functions = cls->queryFunctionsByName("toString"); | |
993 bool found = false; | |
994 foreach (const AbstractMetaFunction *function, tostring_functions) { | |
995 if (function->actualMinimumArgumentCount() == 0) { | |
996 found = true; | |
997 break; | |
998 } | |
999 } | |
1000 | |
1001 if (!found) { | |
1002 s << endl | |
1003 << INDENT << jni_function_signature(cls->package(), cls->name(), "__qt_toString", "jstring") | |
1004 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1005 << INDENT << "{" << endl; | |
1006 { | |
1007 Indentation indent(INDENT); | |
1008 s << INDENT << cls->qualifiedCppName() << " *__qt_this = (" | |
1009 << cls->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1010 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1011 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1012 << INDENT << "return qtjambi_from_qstring(__jni_env, QVariant(*__qt_this).toString());" << endl; | |
1013 } | |
1014 s << INDENT << "}" << endl; | |
1015 } | |
1016 } | |
1017 } | |
1018 | |
1019 void CppImplGenerator::writeExtraFunctions(QTextStream &s, const AbstractMetaClass *java_class) | |
1020 { | |
1021 const ComplexTypeEntry *class_type = java_class->typeEntry(); | |
1022 Q_ASSERT(class_type); | |
1023 | |
1024 CodeSnipList code_snips = class_type->codeSnips(); | |
1025 foreach (const CodeSnip &snip, code_snips) { | |
1026 if (snip.language == TypeSystem::ShellCode || snip.language == TypeSystem::NativeCode) { | |
1027 snip.formattedCode(s, INDENT) << endl; | |
1028 } | |
1029 } | |
1030 } | |
1031 | |
1032 void CppImplGenerator::writeToStringFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
1033 { | |
1034 FunctionModelItem fun = java_class->hasToStringCapability(); | |
1035 bool core = java_class->package() == QLatin1String("qt.core"); | |
1036 bool qevent = false; | |
1037 | |
1038 const AbstractMetaClass *cls = java_class; | |
1039 while (cls) { | |
1040 if (cls->name() == "QEvent") { | |
1041 qevent = true; | |
1042 fun = cls->hasToStringCapability(); | |
1043 break; | |
1044 } | |
1045 cls = cls->baseClass(); | |
1046 } | |
1047 | |
1048 if (!java_class->hasDefaultToStringFunction() && fun && !(qevent && core)) { | |
1049 | |
1050 int indirections = fun->arguments().at(1)->type().indirections(); | |
1051 QString deref = QLatin1String(indirections == 0 ? "*" : ""); | |
1052 | |
1053 s << endl; | |
1054 s << "#include <QDebug>" << endl; | |
1055 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_toString", "jstring") | |
1056 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1057 << INDENT << "{" << endl; | |
1058 { | |
1059 Indentation indent(INDENT); | |
1060 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = (" | |
1061 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1062 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1063 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1064 | |
1065 << INDENT << "QString res;" << endl | |
1066 << INDENT << "QDebug d(&res);" << endl | |
1067 << INDENT << "d << " << deref << "__qt_this;" << endl; | |
1068 s << INDENT << "return qtjambi_from_qstring(__jni_env, res);" << endl; | |
1069 } | |
1070 s << INDENT << "}" << endl << endl; | |
1071 } | |
1072 } | |
1073 | |
1074 void CppImplGenerator::writeCloneFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
1075 { | |
1076 s << endl | |
1077 << jni_function_signature(java_class->package(), java_class->name(), "__qt_clone", "jobject") << endl | |
1078 << "(JNIEnv *__jni_env, jobject, jlong __this_nativeId)" << endl | |
1079 << INDENT << "{" << endl; | |
1080 { | |
1081 Indentation indent(INDENT); | |
1082 s << INDENT << java_class->qualifiedCppName() << " *__qt_this = (" | |
1083 | |
1084 << java_class->qualifiedCppName() << " *) qtjambi_from_jlong(__this_nativeId);" << endl | |
1085 << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl | |
1086 << INDENT << "Q_ASSERT(__qt_this);" << endl | |
1087 << INDENT << java_class->qualifiedCppName() << " *res = __qt_this;" << endl | |
1088 << INDENT << "return qtjambi_from_object(__jni_env, res, \"" << java_class->name() << "\", \"" << java_class->package().replace(".", "/") << "/\", true);" << endl; | |
1089 } | |
1090 s << INDENT << "}" << endl << endl; | |
1091 } | |
1092 | |
1093 void CppImplGenerator::writeShellSignatures(QTextStream &s, const AbstractMetaClass *java_class) | |
1094 { | |
1095 bool has_constructors = java_class->hasConstructors(); | |
1096 | |
1097 // Write the function names... | |
1098 if (has_constructors && java_class->hasVirtualFunctions()) { | |
1099 AbstractMetaFunctionList virtual_functions = java_class->virtualFunctions(); | |
1100 { | |
1101 Indentation indent(INDENT); | |
1102 | |
1103 int pos = -1; | |
1104 foreach (AbstractMetaFunction *function, virtual_functions) { | |
1105 ++pos; | |
1106 | |
1107 if (pos == 0) | |
1108 s << "static const char *qtjambi_method_names[] = {"; | |
1109 else | |
1110 s << ","; | |
1111 s << endl | |
1112 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ " | |
1113 << "\"" << function->name() << "\""; | |
1114 } | |
1115 if (pos >= 0) | |
1116 s << endl << "};" << endl << endl; | |
1117 else | |
1118 s << "static const char **qtjambi_method_names = 0;" << endl; | |
1119 } | |
1120 | |
1121 // Write the function signatures | |
1122 { | |
1123 Indentation indent(INDENT); | |
1124 | |
1125 int pos = -1; | |
1126 foreach (AbstractMetaFunction *function, virtual_functions) { | |
1127 ++pos; | |
1128 | |
1129 if (pos == 0) | |
1130 s << "static const char *qtjambi_method_signatures[] = {"; | |
1131 else | |
1132 s << ","; | |
1133 s << endl | |
1134 << "/* " << QString("%1").arg(QString::number(pos), 3) << " */ " | |
1135 << "\"" | |
1136 << jni_signature(function, SlashesAndStuff) | |
1137 << "\""; | |
1138 } | |
1139 if (pos >= 0) | |
1140 s << endl << "};" << endl; | |
1141 else | |
1142 s << "static const char **qtjambi_method_signatures = 0;" << endl; | |
1143 s << "static const int qtjambi_method_count = " << QString::number(pos + 1) << ";" << endl | |
1144 << endl; | |
1145 } | |
1146 } | |
1147 | |
1148 if (has_constructors && java_class->hasInconsistentFunctions()) { | |
1149 AbstractMetaFunctionList inconsistents = java_class->cppInconsistentFunctions(); | |
1150 // Write the inconsistent function names... | |
1151 { | |
1152 Indentation indent(INDENT); | |
1153 s << "static const char *qtjambi_inconsistent_names[] = {"; | |
1154 for (int i=0; i<inconsistents.size(); ++i) { | |
1155 if (i != 0) | |
1156 s << ","; | |
1157 s << endl << INDENT << "\"" << inconsistents.at(i)->name() << "\""; | |
1158 } | |
1159 s << endl << "};" << endl << endl; | |
1160 } | |
1161 | |
1162 // Write the function signatures | |
1163 { | |
1164 Indentation indent(INDENT); | |
1165 s << "static const char *qtjambi_inconsistent_signatures[] = {"; | |
1166 for (int i=0; i<inconsistents.size(); ++i) { | |
1167 const AbstractMetaFunction *function = inconsistents.at(i); | |
1168 | |
1169 if (i != 0) | |
1170 s << ","; | |
1171 s << endl << INDENT << "\"" | |
1172 << jni_signature(function, SlashesAndStuff) | |
1173 << "\""; | |
1174 } | |
1175 s << endl << "};" << endl << endl; | |
1176 } | |
1177 s << "static const int qtjambi_inconsistent_count = " << inconsistents.size() << ";" << endl | |
1178 << endl; | |
1179 } | |
1180 | |
1181 | |
1182 AbstractMetaFunctionList signal_functions = java_class->cppSignalFunctions(); | |
1183 if (signal_functions.size()) { | |
1184 Indentation indent(INDENT); | |
1185 s << "static const char *qtjambi_signal_names[] = {"; | |
1186 for (int i=0; i<signal_functions.size(); ++i) { | |
1187 if (i != 0) | |
1188 s << ","; | |
1189 | |
1190 const AbstractMetaFunction *f = signal_functions.at(i); | |
1191 | |
1192 QString signalName = f->name(); | |
1193 | |
1194 s << endl << INDENT << "\"" << signalName << "\""; | |
1195 } | |
1196 s << endl << "};" << endl << endl; | |
1197 | |
1198 s << "static const int qtjambi_signal_argumentcounts[] = {"; | |
1199 for (int i=0; i<signal_functions.size(); ++i) { | |
1200 if (i != 0) | |
1201 s << ","; | |
1202 s << endl << INDENT << signal_functions.at(i)->arguments().count(); | |
1203 } | |
1204 s << endl << "};" << endl << endl; | |
1205 s << "static const int qtjambi_signal_count = " << signal_functions.size() << ";" << endl | |
1206 << endl; | |
1207 } | |
1208 } | |
1209 | |
253 | 1210 void CppImplGenerator::writeQObjectEntity(QTextStream &s, const AbstractMetaClass *java_class) |
1211 { | |
1212 QString entityName = java_class->name() + "Entity"; | |
1213 QString className = java_class->name(); | |
1214 | |
1215 s << "class " << entityName << " : public QObject, public QtD_QObjectEntity" << endl | |
1216 << "{" << endl | |
1217 << "public:" << endl | |
1218 << " Q_OBJECT_CHECK" << endl | |
1219 << " virtual int qt_metacall(QMetaObject::Call, int, void **);" << endl << endl | |
1220 | |
1221 << " " << entityName << "(QObject *qObject, void *dId) : QObject(), QtD_QObjectEntity(qObject, dId) {}" << endl | |
1222 << "};" << endl << endl; | |
1223 | |
1224 // QObject_Link::qt_metacall() | |
1225 s << "int " << entityName << "::qt_metacall(QMetaObject::Call _c, int _id, void **_a)" << endl | |
1226 << "{" << endl | |
1227 << " _id = QObject::qt_metacall(_c, _id, _a);" << endl | |
1228 << " if (_id < 0 || _c != QMetaObject::InvokeMetaMethod)" << endl | |
1229 << " return _id;" << endl | |
1230 // << " Q_ASSERT(_id < 2);" << endl | |
1231 << " emit_callbacks_" << java_class->name() << "[_id](dId, _a);" << endl | |
1232 << " return -1;" << endl | |
1233 << "}" << endl << endl; | |
1234 | |
1235 s << "extern \"C\" DLL_PUBLIC void qtd_" << className << "_createEntity(void *nativeId, void* dId)" << endl | |
1236 << "{" << endl | |
1237 << " new " << entityName << "((QObject*)nativeId, dId);" << endl | |
1238 << "}" << endl << endl; | |
1239 } | |
1240 | |
1 | 1241 void CppImplGenerator::writeQObjectFunctions(QTextStream &s, const AbstractMetaClass *java_class) |
1242 { | |
1243 // QObject::metaObject() | |
1244 /* s << "const QMetaObject *" << shellClassName(java_class) << "::metaObject() const" << endl | |
1245 << "{" << endl | |
1246 << " if (m_meta_object == 0) {" << endl | |
1247 << " JNIEnv *__jni_env = qtjambi_current_environment();" << endl | |
1248 << " jobject __obj = m_link != 0 ? m_link->javaObject(__jni_env) : 0;" << endl | |
1249 << " if (__obj == 0) return " << java_class->qualifiedCppName() << "::metaObject();" << endl | |
1250 << " else m_meta_object = qtjambi_metaobject_for_class(__jni_env, __jni_env->GetObjectClass(__obj), " << java_class->qualifiedCppName() << "::metaObject());" << endl; | |
1251 | |
1252 | |
1253 AbstractMetaFunctionList virtualFunctions = java_class->virtualFunctions(); | |
1254 for (int pos=0; pos<virtualFunctions.size(); ++pos) { | |
1255 const AbstractMetaFunction *virtualFunction = virtualFunctions.at(pos); | |
1256 if (virtualFunction->isVirtualSlot()) { | |
1257 QStringList introspectionCompatibleSignatures = virtualFunction->introspectionCompatibleSignatures(); | |
1258 foreach (QString introspectionCompatibleSignature, introspectionCompatibleSignatures) { | |
1259 s << " {" << endl | |
1260 << " int idx = " | |
1261 << java_class->qualifiedCppName() << "::metaObject()->indexOfMethod(\"" | |
1262 << introspectionCompatibleSignature << "\");" << endl; | |
1263 | |
1264 s << " if (idx >= 0) m_map.insert(idx, " << pos << ");" << endl | |
1265 << " }" << endl; | |
1266 } | |
1267 } | |
1268 | |
1269 } | |
1270 | |
1271 s << " }" << endl | |
1272 << " return m_meta_object;" << endl | |
1273 << "}" << endl << endl; | |
1274 | |
1275 // QObject::qt_metacast() | |
1276 s << "void *" << shellClassName(java_class) << "::qt_metacast(const char *_clname)" << endl | |
1277 << "{" << endl | |
1278 << " if (!_clname) return 0;" << endl | |
1279 << " if (!strcmp(_clname, \"" << shellClassName(java_class) << "\"))" << endl | |
1280 << " return static_cast<void*>(const_cast<" << shellClassName(java_class) << "*>(this));" << endl | |
1281 << " return " << java_class->qualifiedCppName() << "::qt_metacast(_clname);" << endl | |
1282 << "}" << endl << endl; | |
1283 */ | |
253 | 1284 |
1 | 1285 s << "int " << shellClassName(java_class) << "::qt_metacall(QMetaObject::Call _c, int _id, void **_a)" << endl |
1286 << "{" << endl; | |
1287 | |
253 | 1288 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
|
1289 << " if (_id < 0 || _c != QMetaObject::InvokeMetaMethod)" << endl |
253 | 1290 << " return _id;" << endl |
1291 << " 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
|
1292 << " return -1;" << endl |
1 | 1293 << "}" << endl << endl; |
1294 } | |
1295 | |
1296 void CppImplGenerator::writeSignalHandler(QTextStream &s, const AbstractMetaClass *d_class, AbstractMetaFunction *function) | |
1297 { | |
1298 QString extra_args, extra_call_args, conversion_code; | |
1299 QTextStream s2(&conversion_code); | |
1300 | |
1301 Indentation indent(INDENT); | |
1302 | |
1303 AbstractMetaArgumentList arguments = function->arguments(); | |
1304 foreach (AbstractMetaArgument *argument, arguments) { | |
1305 if(argument->type()->isContainer()) { | |
1306 QString arg_name = argument->indexedName(); | |
1307 const AbstractMetaType *arg_type = argument->type(); | |
1308 extra_args += ", DArray " + arg_name; | |
1309 extra_call_args += ", " + arg_name + "_arr"; | |
1310 | |
1311 s2 << INDENT; | |
1312 writeTypeInfo(s2, arg_type, NoOption); | |
1313 s2 << arg_name << " = (*reinterpret_cast< "; | |
1314 writeTypeInfo(s2, arg_type, ExcludeReference); | |
1315 s2 << "(*)>(args[" << argument->argumentIndex() + 1 << "]));" << endl | |
1316 << INDENT << QString("DArray %1_arr;").arg(arg_name) << endl | |
1317 << INDENT << QString("DArray *__d_%1 = &%1_arr;").arg(arg_name); | |
1318 | |
1319 writeQtToJava(s2, | |
1320 arg_type, | |
1321 arg_name, | |
1322 "__d_" + arg_name, | |
1323 function, | |
1324 argument->argumentIndex() + 1, | |
1325 Option(VirtualDispatch)); | |
1326 } | |
1327 } | |
1328 QString sig_name = signalExternName(d_class, function); | |
1329 s << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle_in_d(void* d_entity, void** args" << extra_args <<");" << endl | |
1330 << "extern \"C\" DLL_PUBLIC void " << sig_name << "_handle(void* d_entity, void** args)" << endl | |
1331 << "{" << endl | |
1332 << conversion_code << endl | |
1333 << INDENT << sig_name << "_handle_in_d(d_entity, args" << extra_call_args << ");" << endl | |
1334 << "}" << endl; | |
1335 | |
1336 } | |
1337 | |
1338 void CppImplGenerator::writeSignalsHandling(QTextStream &s, const AbstractMetaClass *java_class) | |
1339 { | |
1340 s << "extern \"C\" typedef void (*EmitCallback)(void*, void**);" << endl; | |
1341 AbstractMetaFunctionList signal_funcs = signalFunctions(java_class); | |
1342 | |
1343 if (cpp_shared) | |
132 | 1344 if(signal_funcs.size() > 0) |
1345 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "];" << endl; | |
1346 else | |
1347 s << "EmitCallback emit_callbacks_" << java_class->name() << "[1];" << endl; // Hack for msvc. | |
1 | 1348 else { |
1349 // D-side signal callbacks | |
1350 for(int i = 0; i < signal_funcs.size(); i++) { | |
1351 AbstractMetaFunction *signal = signal_funcs.at(i); | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
1352 s << "extern \"C\" DLL_PUBLIC void " << signalExternName(java_class, signal) << "_handle(void* d_entity, void** args);" << endl; |
1 | 1353 } |
1354 | |
132 | 1355 if(signal_funcs.size() > 0) |
1356 s << "EmitCallback emit_callbacks_" << java_class->name() << "[" << signal_funcs.size() << "] = {" << endl; | |
1357 else | |
1358 s << "EmitCallback emit_callbacks_" << java_class->name() << "[1] = {" << endl; // Hack for msvc. | |
1 | 1359 for(int i = 0; i < signal_funcs.size(); i++) { |
1360 AbstractMetaFunction *signal = signal_funcs.at(i); | |
1361 s << endl; | |
1362 if (i != 0) | |
1363 s << ", "; | |
1364 s << "&" << signalExternName(java_class, signal) << "_handle"; | |
1365 } | |
1366 s << endl << "};" << endl << endl; | |
1367 } | |
1368 | |
253 | 1369 writeQObjectEntity(s, java_class); |
1 | 1370 } |
1371 | |
1372 | |
1373 void CppImplGenerator::writeShellConstructor(QTextStream &s, const AbstractMetaFunction *java_function) | |
1374 { | |
1375 if (java_function->isModifiedRemoved(TypeSystem::ShellCode)) | |
1376 return; | |
1377 | |
1378 const AbstractMetaClass *cls = java_function->ownerClass(); | |
1379 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1380 | |
1381 writeFunctionSignature(s, java_function, cls); | |
1382 | |
1383 s << endl; | |
1384 s << " : " << cls->qualifiedCppName() << "("; | |
1385 for (int i=0; i<arguments.size(); ++i) { | |
1386 s << arguments.at(i)->indexedName(); | |
1387 if (i != arguments.size() - 1) | |
1388 s << ", "; | |
1389 } | |
1390 s << ")"; | |
257 | 1391 |
253 | 1392 if (cls->isQObject()) |
1393 s << "," << endl << " QtD_QObjectEntity(this, d_ptr)"; | |
257 | 1394 else if (cls->typeEntry()->isObject() && cls->hasVirtualFunctions()) |
253 | 1395 s << "," << endl << " QtD_Entity(d_ptr)"; |
1 | 1396 /* qtd s << " m_meta_object(0)," << endl; |
1397 s << " m_vtable(0)," << endl | |
1398 << " m_link(0)" << endl; | |
1399 */ | |
1400 s << endl; | |
1401 s << "{" << endl; | |
1402 { | |
1403 Indentation indent(INDENT); | |
253 | 1404 |
1 | 1405 writeCodeInjections(s, java_function, cls, CodeSnip::Beginning, TypeSystem::ShellCode); |
1406 writeCodeInjections(s, java_function, cls, CodeSnip::End, TypeSystem::ShellCode); | |
1407 } | |
257 | 1408 s << "}" << endl << endl; |
1 | 1409 } |
1410 | |
1411 void CppImplGenerator::writeShellDestructor(QTextStream &s, const AbstractMetaClass *java_class) | |
1412 { | |
1413 s << shellClassName(java_class) << "::~" | |
1414 << shellClassName(java_class) << "()" << endl | |
1415 << "{" << endl; | |
1416 { | |
257 | 1417 s << " std::cout << \"In shell destructor of " << java_class->name() << ", nativeId: \" << this << std::endl;"; |
253 | 1418 if (java_class->isQObject()) |
257 | 1419 s << " destroyEntity(this);" << endl; |
1420 else | |
1421 s << " qtd_delete_d_object(dId);" << endl; | |
1 | 1422 } |
1423 s << "}" << endl << endl; | |
1424 } | |
1425 | |
1426 void CppImplGenerator::writeCodeInjections(QTextStream &s, const AbstractMetaFunction *java_function, | |
1427 const AbstractMetaClass *implementor, CodeSnip::Position position, | |
1428 TypeSystem::Language language) | |
1429 { | |
1430 | |
1431 FunctionModificationList mods; | |
1432 const AbstractMetaClass *cls = implementor; | |
1433 while (cls != 0) { | |
1434 mods += java_function->modifications(cls); | |
1435 | |
1436 if (cls == cls->baseClass()) | |
1437 break; | |
1438 cls = cls->baseClass(); | |
1439 } | |
1440 | |
1441 foreach (FunctionModification mod, mods) { | |
1442 if (mod.snips.count() <= 0) | |
1443 continue ; | |
1444 | |
1445 foreach (CodeSnip snip, mod.snips) { | |
1446 if (snip.position != position) | |
1447 continue ; | |
1448 | |
1449 if ((snip.language & language) == false) | |
1450 continue ; | |
1451 | |
1452 if (position == CodeSnip::End) | |
1453 s << endl; | |
1454 | |
1455 QString code; | |
1456 QTextStream tmpStream(&code); | |
1457 snip.formattedCode(tmpStream, INDENT); | |
1458 ArgumentMap map = snip.argumentMap; | |
1459 ArgumentMap::iterator it = map.begin(); | |
1460 for (;it!=map.end();++it) { | |
1461 int pos = it.key() - 1; | |
1462 QString meta_name = it.value(); | |
1463 | |
1464 if (pos >= 0 && pos < java_function->arguments().count()) { | |
1465 code = code.replace(meta_name, java_function->arguments().at(pos)->indexedName()); | |
1466 } else { | |
1467 QString debug = QString("argument map specifies invalid argument index %1" | |
1468 "for function '%2'") | |
1469 .arg(pos + 1).arg(java_function->name()); | |
1470 ReportHandler::warning(debug); | |
1471 } | |
1472 | |
1473 } | |
1474 s << code; | |
1475 if (position == CodeSnip::Beginning) | |
1476 s << endl; | |
1477 } | |
1478 } | |
1479 } | |
1480 | |
1481 static QString function_call_for_ownership(TypeSystem::Ownership owner, const QString &var_name) | |
1482 { | |
1483 if (owner == TypeSystem::CppOwnership) { | |
1484 return "setCppOwnership(__jni_env, " + var_name + ")"; | |
1485 } else if (owner == TypeSystem::TargetLangOwnership) { | |
1486 return "setJavaOwnership(__jni_env, " + var_name + ")"; | |
1487 } else if (owner == TypeSystem::DefaultOwnership) { | |
1488 return "setDefaultOwnership(__jni_env, " + var_name + ")"; | |
1489 } else { | |
1490 Q_ASSERT(false); | |
1491 return "bogus()"; | |
1492 } | |
1493 } | |
1494 | |
1495 void CppImplGenerator::writeOwnership(QTextStream &s, | |
1496 const AbstractMetaFunction *java_function, | |
1497 const QString &var_name, | |
1498 int var_index, | |
1499 const AbstractMetaClass *implementor) | |
1500 { | |
1501 TypeSystem::Ownership owner = TypeSystem::InvalidOwnership; | |
1502 const AbstractMetaClass *cls = implementor; | |
1503 while (cls != 0 && owner == TypeSystem::InvalidOwnership) { | |
1504 owner = java_function->ownership(cls, TypeSystem::ShellCode, var_index); | |
1505 cls = cls->baseClass(); | |
1506 } | |
1507 | |
1508 if (owner == TypeSystem::InvalidOwnership) | |
1509 return; | |
1510 | |
1511 if (var_index != -1) { | |
1512 s << INDENT << "if (" << var_name << " != 0) {" << endl; | |
1513 { | |
1514 Indentation indent(INDENT); | |
1515 s << INDENT << "QtJambiLink *__link = QtJambiLink::findLink(__jni_env, " | |
1516 << var_name << ");" << endl | |
1517 << INDENT << "Q_ASSERT(__link != 0);" << endl; | |
1518 | |
1519 s << INDENT << "__link->" << function_call_for_ownership(owner, var_name) << ";" << endl; | |
1520 } | |
1521 s << INDENT << "}" << endl; | |
1522 } else { | |
1523 s << INDENT << "if (m_link) {" << endl; | |
1524 { | |
1525 Indentation indent(INDENT); | |
1526 s << INDENT << "m_link->" << function_call_for_ownership(owner, "m_link->javaObject(__jni_env)") << ";" << endl; | |
1527 } | |
1528 s << INDENT << "}" << endl; | |
1529 } | |
1530 | |
1531 } | |
1532 | |
1533 void CppImplGenerator::writeShellFunction(QTextStream &s, const AbstractMetaFunction *java_function, | |
1534 const AbstractMetaClass *implementor, int id) | |
1535 { | |
1536 writeFunctionSignature(s, java_function, implementor, QString(), OriginalName); | |
1537 | |
1538 s << endl | |
1539 << "{" << endl; | |
1540 | |
1541 Indentation indent(INDENT); | |
1542 | |
1543 QString java_function_signature = java_function->signature(); | |
1544 /* s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) entering: " << implementor->name() << "::" | |
1545 << java_function_signature << "\");" << endl; | |
1546 */ | |
1547 writeCodeInjections(s, java_function, implementor, CodeSnip::Beginning, TypeSystem::ShellCode); | |
1548 | |
1549 // s << " printf(\"%s : %s\\n\", \"" << java_function->enclosingClass()->name() << "\"" | |
1550 // << ", \"" << java_function->name() << "\");" << endl; | |
1551 | |
1552 if (!java_function->isFinalInCpp() || java_function->isVirtualSlot()) { | |
1553 s << INDENT << "jmethodID method_id = m_vtable->method(" << id << ");" << endl; | |
1554 s << INDENT << "if (method_id) {" << endl; | |
1555 | |
1556 { | |
1557 Indentation indent(INDENT); | |
1558 s << INDENT << "JNIEnv *__jni_env = qtjambi_current_environment();" << endl; | |
1559 | |
1560 // This nasty case comes up when we're shutting down while receiving virtual | |
1561 // calls.. With these checks we safly abort... | |
1562 s << INDENT << "if (!__jni_env) {" << endl | |
1563 << " "; | |
1564 writeBaseClassFunctionCall(s, java_function, implementor); | |
1565 if (!java_function->type()) { | |
1566 s << INDENT << " return;" << endl; | |
1567 } | |
1568 s << INDENT << "}" << endl; | |
1569 | |
1570 // otherwise, continue with the function call... | |
1571 s << INDENT << "__jni_env->PushLocalFrame(100);" << endl; | |
1572 | |
1573 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1574 AbstractMetaArgumentList argumentsToReset; | |
1575 foreach (AbstractMetaArgument *argument, arguments) { | |
1576 if (!java_function->argumentRemoved(argument->argumentIndex()+1)) { | |
1577 if (!argument->type()->isPrimitive() | |
1578 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty()) { | |
1579 writeQtToJava(s, | |
1580 argument->type(), | |
1581 argument->indexedName(), | |
1582 "__java_" + argument->indexedName(), | |
1583 java_function, | |
1584 argument->argumentIndex() + 1); | |
1585 } | |
1586 | |
1587 if (java_function->resetObjectAfterUse(argument->argumentIndex()+1)) | |
1588 argumentsToReset.append(argument); | |
1589 } | |
1590 } | |
1591 | |
1592 for (int i=0; i<arguments.size(); ++i) | |
1593 writeOwnership(s, java_function, "__java_" + arguments.at(i)->indexedName(), i+1, implementor); | |
1594 | |
1595 | |
1596 AbstractMetaType *function_type = java_function->type(); | |
1597 QString new_return_type = java_function->typeReplaced(0); | |
1598 bool has_function_type = ((function_type != 0 | |
1599 || !new_return_type.isEmpty()) | |
1600 && new_return_type != "void"); | |
1601 | |
1602 s << INDENT; | |
1603 if (has_function_type) { | |
1604 if (new_return_type.isEmpty()) { | |
1605 s << translateType(function_type); | |
1606 } else { | |
1607 s << jniName(new_return_type); | |
1608 } | |
1609 s << " " << "__d_return_value = "; | |
1610 } | |
1611 | |
1612 s << "__jni_env->"; | |
1613 if (new_return_type.isEmpty()) { | |
1614 s << callXxxMethod(java_function->type()); | |
1615 } else if (!has_function_type) { | |
1616 s << "CallVoidMethod"; | |
1617 } else { | |
1618 s << callXxxMethod(new_return_type); | |
1619 } | |
1620 | |
1621 s << "(m_link->javaObject(__jni_env), method_id"; | |
1622 if (arguments.size() > 0) | |
1623 s << ", "; | |
1624 writeFunctionCallArguments(s, java_function, "__java_", Option(NoCasts | SkipRemovedArguments)); | |
1625 s << ");" << endl | |
1626 << INDENT << "qtjambi_exception_check(__jni_env);" << endl; | |
1627 | |
1628 if (has_function_type) { | |
1629 writeJavaToQt(s, function_type, "__qt_return_value", "__d_return_value", | |
1630 java_function, 0, GlobalRefJObject); | |
1631 | |
1632 if (java_function->nullPointersDisabled()) { | |
1633 s << INDENT << "if (__d_return_value == 0) {" << endl; | |
1634 { | |
1635 Indentation indent(INDENT); | |
1636 s << INDENT << "fprintf(stderr, \"QtJambi: Unexpected null pointer returned from override of '" << java_function->name() << "' in class '%s'\\n\"," << endl | |
1637 << INDENT << " qPrintable(qtjambi_object_class_name(__jni_env, m_link->javaObject(__jni_env))));" << endl; | |
1638 s << INDENT << "__qt_return_value = "; | |
1639 QString defaultValue = java_function->nullPointerDefaultValue(); | |
1640 if (!defaultValue.isEmpty()) | |
1641 s << defaultValue << ";"; | |
1642 else | |
1643 writeBaseClassFunctionCall(s, java_function, implementor, NoReturnStatement); | |
1644 s << endl; | |
1645 } | |
1646 | |
1647 s << INDENT << "}" << endl; | |
1648 } | |
1649 } else if (!java_function->conversionRule(TypeSystem::ShellCode, 0).isEmpty()) { | |
1650 writeConversionRule(s, TypeSystem::ShellCode, java_function, 0, "<invalid>", "<invalid>"); | |
1651 } | |
1652 | |
1653 writeOwnership(s, java_function, "this", -1, implementor); | |
1654 writeOwnership(s, java_function, "__d_return_value", 0, implementor); | |
1655 | |
1656 foreach (AbstractMetaArgument *argumentToReset, argumentsToReset) { | |
1657 | |
1658 QString argumentName = "__java_" + argumentToReset->indexedName(); | |
1659 | |
1660 s << INDENT; | |
1661 if (argumentToReset->type()->isContainer()) | |
1662 s << "qtjambi_invalidate_collection("; | |
1663 else | |
1664 s << "qtjambi_invalidate_object("; | |
1665 | |
1666 s << "__jni_env, " << argumentName << ");" << endl; | |
1667 } | |
1668 | |
1669 s << INDENT << "__jni_env->PopLocalFrame(0);" << endl; | |
1670 | |
1671 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> leaving: " << implementor->name() | |
1672 << "::" << java_function_signature << "\");" << endl; | |
1673 | |
1674 if (function_type) | |
1675 s << INDENT << "return __qt_return_value;" << endl; | |
1676 | |
1677 } | |
1678 | |
1679 s << INDENT << "} else {" << endl; | |
1680 | |
1681 { | |
1682 Indentation indent(INDENT); | |
1683 s << INDENT << "QTJAMBI_DEBUG_TRACE(\"(shell) -> super() and leaving: " | |
1684 << implementor->name() << "::" << java_function_signature << "\");" << endl; | |
1685 writeBaseClassFunctionCall(s, java_function, implementor); | |
1686 } | |
1687 | |
1688 | |
1689 s << INDENT << "}" << endl; | |
1690 | |
1691 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1692 | |
1693 // A little trick to close open painters on a widget | |
1694 if (java_function->name() == "paintEvent") { | |
1695 s << INDENT << "JNIEnv *env = qtjambi_current_environment();" << endl | |
1696 << INDENT << "qtjambi_end_paint(env, m_link->javaObject(env));" << endl; | |
1697 } | |
1698 | |
1699 } else { | |
1700 if(java_function->isRemovedFrom(implementor, TypeSystem::TargetLangCode)){ | |
1701 // Avoid compiler warnings for unused parameters | |
1702 AbstractMetaArgumentList arguments = java_function->arguments(); | |
1703 | |
1704 foreach (const AbstractMetaArgument *argument, arguments) { | |
1705 s << INDENT << "Q_UNUSED(" << argument->indexedName() << ")" << endl; | |
1706 } | |
1707 } | |
1708 writeBaseClassFunctionCall(s, java_function, implementor); | |
1709 writeCodeInjections(s, java_function, implementor, CodeSnip::End, TypeSystem::ShellCode); | |
1710 } | |
1711 | |
1712 s << "}" << endl << endl; | |
1713 | |
1714 } | |
1715 | |
1716 // ### kill implementor | |
1717 | |
1718 void CppImplGenerator::writePublicFunctionOverride(QTextStream &s, | |
1719 const AbstractMetaFunction *java_function, | |
1720 const AbstractMetaClass *implementor) | |
1721 { | |
1722 Q_ASSERT(java_function->originalAttributes() | |
1723 & (AbstractMetaAttributes::Protected | |
1724 | AbstractMetaAttributes::Final)); | |
1725 | |
1726 // The write a public override version of this function to be used by native functions | |
1727 writeFunctionSignature(s, java_function, implementor, "__public_", | |
1728 Option(EnumAsInts | UnderscoreSpaces | |
1729 | (java_function->isAbstract() ? SkipName : NoOption))); | |
1730 s << endl | |
1731 << "{" << endl; | |
1732 Indentation indent(INDENT); | |
1733 writeBaseClassFunctionCall(s, java_function, implementor); | |
1734 s << "}" << endl << endl; | |
1735 } | |
1736 | |
257 | 1737 void CppImplGenerator::writeObjectFunctions(QTextStream &s, const AbstractMetaClass *java_class) |
253 | 1738 { |
257 | 1739 s << "extern \"C\" DLL_PUBLIC const void* qtd_" << java_class->name() << "_staticTypeId()" << endl; |
253 | 1740 s << "{" << endl; |
1741 { | |
1742 Indentation indent(INDENT); | |
257 | 1743 s << INDENT << "return &typeid(" << java_class->qualifiedCppName() << ");" << endl; |
253 | 1744 } |
1745 s << "}" << endl << endl; | |
257 | 1746 |
1747 if (java_class->hasVirtualFunctions()) | |
1748 { | |
1749 if (!java_class->baseClass()) | |
1750 { | |
1751 s << "extern \"C\" DLL_PUBLIC void* qtd_" << java_class->name() << "_dId(void *q_ptr)" << endl; | |
1752 s << "{" << endl; | |
1753 { | |
1754 Indentation indent(INDENT); | |
1755 s << INDENT << "QtD_Entity* a = dynamic_cast<QtD_Entity*>((" << java_class->qualifiedCppName() << "*)q_ptr);" << endl | |
1756 << INDENT << "if (a != NULL)" << endl | |
1757 << INDENT << " return a->dId;" << endl | |
1758 << INDENT << "else" << endl | |
1759 << INDENT << " return NULL;" << endl; | |
1760 } | |
1761 s << "}" << endl << endl; | |
1762 | |
1763 s << "extern \"C\" DLL_PUBLIC const void* qtd_" << java_class->name() << "_typeId(void *nativeId)" << endl; | |
1764 s << "{" << endl; | |
1765 { | |
1766 Indentation indent(INDENT); | |
1767 s << INDENT << "return &typeid((" << java_class->qualifiedCppName() << "*)nativeId);" << endl; | |
1768 } | |
1769 s << "}" << endl << endl; | |
1770 } | |
1771 else | |
1772 s << "extern \"C\" DLL_PUBLIC void* qtd_" << java_class->rootClass()->name() << "_dId(void *q_ptr);" << endl; | |
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() && |
257 | 1897 cls->typeEntry()->isObject()) |
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 | |
257 | 2118 s << "qtd_" << java_class->rootClass()->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 | |
2326 s << INDENT << "extern \"C\" DLL_PUBLIC void qtd_" << cls->name() << "_destructor(void *ptr)" << endl | |
2327 << INDENT << "{" << endl; | |
2328 { | |
2329 QString className = cls->hasVirtualFunctions() ? cls->typeEntry()->name() : shellClassName(cls); | |
2330 s << INDENT << "delete (" << className << " *)ptr;" << endl; | |
1 | 2331 } |
257 | 2332 |
2333 s << INDENT << "}" << endl << endl; | |
1 | 2334 } |
2335 | |
2336 void CppImplGenerator::writeFinalConstructor(QTextStream &s, | |
2337 const AbstractMetaFunction *java_function, | |
2338 const QString &qt_object_name, | |
2339 const QString &java_object_name) | |
2340 { | |
2341 const AbstractMetaClass *cls = java_function->ownerClass(); | |
2342 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2343 QString className = cls->typeEntry()->qualifiedCppName(); | |
2344 | |
2345 bool hasShellClass = cls->generateShellClass(); | |
2346 | |
2347 s << INDENT << shellClassName(cls) << " *" << qt_object_name | |
2348 << " = new " << shellClassName(cls) | |
2349 << "("; | |
2350 writeFunctionCallArguments(s, java_function, "__qt_"); | |
2351 s << ");" << endl; | |
2352 s << INDENT << "return (void *) " << qt_object_name << ";" << endl; // qtd | |
2353 | |
2354 /* qtd s << INDENT << "QtJambiLink *__qt_java_link = "; | |
2355 if (cls->isQObject()) { | |
2356 s << "qtjambi_construct_qobject(__jni_env, " << java_object_name << ", " | |
2357 << qt_object_name << ")"; | |
2358 } else { | |
2359 s << "qtjambi_construct_object(__jni_env, " << java_object_name << ", " << qt_object_name; | |
2360 if (cls->typeEntry()->isValue()) | |
2361 s << ", \"" << className << "\")"; | |
2362 else // non-QObject, object type | |
2363 s << ", QMetaType::Void, QLatin1String(\"" << cls->fullName().replace(".", "/") << "\"), true)"; | |
2364 } | |
2365 s << ";" << endl | |
2366 << INDENT << "if (!__qt_java_link) {" << endl; | |
2367 { | |
2368 Indentation indent(INDENT); | |
2369 s << INDENT << "qWarning(\"object construction failed for type: " | |
2370 << className << "\");" << endl | |
2371 << INDENT << "return;" << endl; | |
2372 } | |
2373 s << INDENT << "}" << endl; | |
2374 | |
2375 | |
2376 if (cls->isQObject()) { | |
2377 // Make sure all qobjects created by Java are owned by java only if | |
2378 // parent object has not been set. | |
2379 // All other objects will default to split ownership. | |
2380 s << INDENT << "if(!__qt_this->QObject::parent()){" << endl; | |
2381 s << INDENT << " __qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2382 s << INDENT << "}" << endl; | |
2383 } else { | |
2384 // All non-qobjects created by Java are owned by java | |
2385 s << INDENT << "__qt_java_link->setJavaOwnership(__jni_env, " << java_object_name << ");" << endl; | |
2386 } | |
2387 | |
2388 if (hasCustomDestructor(cls)) { | |
2389 s << INDENT << "__qt_java_link->setDestructorFunction(qtjambi_destructor);" << endl; | |
2390 } | |
2391 | |
2392 if (cls->typeEntry()->typeFlags() & ComplexTypeEntry::DeleteInMainThread) | |
2393 s << INDENT << "__qt_java_link->setDeleteInMainThread(true);" << endl; | |
2394 | |
2395 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions() && !cls->typeEntry()->isObject()) | |
2396 return; | |
2397 | |
2398 if (hasShellClass) { | |
2399 // Set up the link object | |
2400 s << INDENT << qt_object_name << "->m_link = __qt_java_link;" << endl; | |
2401 | |
2402 // Make sure the user data in the QObject has bindings to the qobject's meta object | |
2403 // (this has to be done after the link is set, so that the fake meta object | |
2404 // can access the java object, for which it gets a reference in the link) | |
2405 if (cls->isQObject()) | |
2406 s << INDENT << qt_object_name << "->m_link->setMetaObject(" << qt_object_name << "->metaObject());" << endl; | |
2407 | |
2408 s << INDENT << qt_object_name << "->m_link->setCreatedByJava(true);" << endl; | |
2409 | |
2410 | |
2411 | |
2412 AbstractMetaClassList interfaces = cls->interfaces(); | |
2413 if (interfaces.size() + (cls->baseClass() != 0 ? 1 : 0) > 1) { | |
2414 if (cls->baseClass() != 0) | |
2415 interfaces += cls->baseClass(); | |
2416 foreach (AbstractMetaClass *iface, interfaces) { | |
2417 AbstractMetaClass *impl = iface->isInterface() ? iface->primaryInterfaceImplementor() : iface; | |
2418 s << INDENT << qt_object_name << "->m_link->registerSubObject((" << impl->qualifiedCppName() << " *) " << qt_object_name << ");" << endl; | |
2419 } | |
2420 } | |
2421 } | |
2422 | |
2423 if (!cls->hasVirtualFunctions() && !cls->hasInconsistentFunctions()) | |
2424 return; | |
2425 | |
2426 // Set up the vtable | |
2427 s << INDENT; | |
2428 QString space(24, ' '); | |
2429 if (hasShellClass) | |
2430 s << qt_object_name << "->m_vtable = "; | |
2431 s << "qtjambi_setup_vtable(__jni_env, " << endl << space << "__jni_object, " << endl; | |
2432 | |
2433 if (cls->hasInconsistentFunctions()) { | |
2434 s << space << "qtjambi_inconsistent_count, " << endl | |
2435 << space << "qtjambi_inconsistent_names, " << endl | |
2436 << space << "qtjambi_inconsistent_signatures, " << endl; | |
2437 } else { | |
2438 s << space << "0, 0, 0, // no inconsistent functions" << endl; | |
2439 } | |
2440 | |
2441 if (cls->hasVirtualFunctions()) { | |
2442 s << space << "qtjambi_method_count, " << endl | |
2443 << space << "qtjambi_method_names, " << endl | |
2444 << space << "qtjambi_method_signatures" << endl; | |
2445 } else { | |
2446 s << space << "0, 0, 0 // no virtual functions" << endl; | |
2447 } | |
2448 | |
2449 s << space << ");" << endl; */ | |
2450 } | |
2451 | |
2452 void CppImplGenerator::writeSignalInitialization(QTextStream &s, const AbstractMetaClass *java_class) | |
2453 { | |
2454 if (!java_class->isQObject() | |
2455 || java_class->queryFunctions(AbstractMetaClass::Signals | AbstractMetaClass::Visible | AbstractMetaClass::NotRemovedFromTargetLang).size() == 0) { | |
2456 return ; | |
2457 } | |
2458 | |
2459 s << jni_function_signature(java_class->package(), java_class->name(), "__qt_signalInitialization", "jboolean") | |
2460 << endl << "(JNIEnv *__jni_env, jobject java_object, jlong ptr, jstring java_signal_name)" << endl | |
2461 << "{" << endl | |
2462 << " QtJambiLink *link = (QtJambiLink *) ptr;" << endl | |
2463 << " if (link == 0)" << endl | |
2464 << " return true;" << endl << endl | |
2465 << " QObject *qt_this = link->qobject();" << endl | |
2466 << " Q_ASSERT(qt_this);" << endl << endl | |
2467 << " QtJambi_SignalWrapper_" << java_class->name() << " *qt_wrapper = " | |
2468 << " (QtJambi_SignalWrapper_" << java_class->name() << " *) link->signalWrapper();" << endl | |
2469 << " if (qt_wrapper == 0) {" << endl | |
2470 << " qt_wrapper = new QtJambi_SignalWrapper_" << java_class->name() << ";" << endl | |
2471 << " link->setSignalWrapper(qt_wrapper);" << endl | |
2472 << " qt_wrapper->link = link;" << endl << endl | |
2473 << " qtjambi_resolve_signals(__jni_env," << endl | |
2474 << " java_object," << endl | |
2475 << " qt_wrapper->m_signals," << endl | |
2476 << " qtjambi_signal_count," << endl | |
2477 << " (char **) qtjambi_signal_names," << endl | |
2478 << " (int *) qtjambi_signal_argumentcounts);" << endl | |
2479 << " }" << endl | |
2480 << " QString signal_name = qtjambi_to_qstring(__jni_env, java_signal_name);" << endl | |
2481 << " return qtjambi_connect_cpp_to_java(__jni_env," << endl | |
2482 << " signal_name," << endl | |
2483 << " qt_this," << endl | |
2484 << " qt_wrapper," << endl | |
2485 << " QLatin1String(\"" << java_class->fullName() << "\")," << endl | |
2486 << " QLatin1String(\"" << signalWrapperPrefix() << "\"));" << endl | |
2487 << "}"; | |
2488 } | |
2489 | |
2490 QString CppImplGenerator::fromObject(const TypeEntry *entry, | |
2491 const QString &var_name) | |
2492 { | |
2493 QString returned; | |
2494 QString package = entry->javaPackage(); | |
2495 const ComplexTypeEntry *centry = entry->isComplex() | |
2496 ? static_cast<const ComplexTypeEntry *>(entry) | |
2497 : 0; | |
2498 | |
2499 if (centry == 0 || centry->polymorphicIdValue().isEmpty()) { | |
2500 /* qtd returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2501 + entry->lookupName() | |
2502 + "\", \"" + QString(package).replace(".", "/") + "/\", true);"; | |
2503 */ | |
2504 if(entry->isObject()) | |
2505 returned = var_name + ";"; | |
2506 else | |
2507 returned = "new " + entry->lookupName() + "(" + var_name +");"; | |
2508 } else { | |
2509 AbstractMetaClass *cls = classes().findClass(centry->qualifiedCppName()); | |
2510 if (!cls) { | |
2511 qFatal("CppImplGenerator::fromObject(): class '%s' could not be resolved...", | |
2512 qPrintable(centry->qualifiedCppName())); | |
2513 } | |
2514 | |
2515 while (cls != 0 && !cls->typeEntry()->isPolymorphicBase()) | |
2516 cls = cls->baseClass(); | |
2517 | |
2518 QString full_name; | |
2519 if (cls != 0) { | |
2520 full_name = cls->fullName(); | |
2521 } else { | |
2522 ReportHandler::warning(QString("class '%1' has polymorphic id but does not inherit a polymorphic class") | |
2523 .arg(centry->qualifiedCppName())); | |
2524 } | |
2525 /* qtd | |
2526 returned = "qtjambi_from_object(__jni_env, " + var_name + ", \"" | |
2527 + centry->lookupName() | |
2528 + "\", \"" + QString(package).replace(".", "/") + "/\"," | |
2529 + "\"" + jni_signature(full_name, Underscores) + "\", true); // fucking complex"; | |
2530 */ | |
2531 if(entry->isObject()) | |
2532 returned = var_name + "; // complex entry"; | |
2533 else | |
2534 returned = "new " + centry->lookupName() + "(" + var_name +"); // complex entry"; | |
2535 } | |
2536 | |
2537 return returned; | |
2538 } | |
2539 | |
2540 void CppImplGenerator::writeOriginalMetaObjectFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2541 { | |
2542 Q_ASSERT(java_class->isQObject()); | |
2543 | |
2544 s << jni_function_signature(java_class->package(), | |
2545 java_class->name(), | |
2546 "originalMetaObject", | |
2547 "jlong"); | |
2548 | |
2549 s << endl | |
2550 << "(JNIEnv *," << endl | |
2551 << " jclass)" << endl | |
2552 << "{" << endl; | |
2553 { | |
2554 Indentation indent(INDENT); | |
2555 s << INDENT << "return reinterpret_cast<jlong>(&" << java_class->qualifiedCppName() << "::staticMetaObject);" << endl; | |
2556 } | |
2557 s << "}" << endl << endl; | |
2558 } | |
2559 | |
2560 void CppImplGenerator::writeFromNativeFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2561 { | |
2562 s << jni_function_signature(java_class->package(), | |
2563 java_class->name(), | |
2564 "fromNativePointer", | |
2565 "jobject"); | |
2566 s << endl | |
2567 << "(JNIEnv *__jni_env," << endl | |
2568 << " jclass," << endl | |
2569 << " jobject nativePointer)" << endl | |
2570 << "{" << endl; | |
2571 { | |
2572 Indentation indent(INDENT); | |
2573 s << INDENT << "void *ptr = qtjambi_to_cpointer(__jni_env, nativePointer, 1);" << endl | |
2574 << INDENT << "return " << fromObject(java_class->typeEntry(), "ptr") << endl | |
2575 << "}" << endl; | |
2576 } | |
2577 } | |
2578 | |
2579 void CppImplGenerator::writeFromArrayFunction(QTextStream &s, const AbstractMetaClass *java_class) | |
2580 { | |
2581 s << jni_function_signature(java_class->package(), | |
2582 java_class->name(), | |
2583 "nativePointerArray", | |
2584 "jobject"); | |
2585 s << endl | |
2586 << "(JNIEnv *__jni_env," << endl | |
2587 << " jclass," << endl | |
2588 << " jobjectArray array)" << endl | |
2589 << "{" << endl; | |
2590 { | |
2591 Indentation indent(INDENT); | |
2592 s << INDENT << "return qtjambi_array_to_nativepointer(__jni_env, " << endl | |
2593 << INDENT << " array, " << endl | |
2594 << INDENT << " sizeof(" | |
2595 << java_class->qualifiedCppName() << "));" << endl; | |
2596 } | |
2597 s << "}" << endl; | |
2598 } | |
2599 | |
2600 | |
2601 void CppImplGenerator::writeInterfaceCastFunction(QTextStream &s, | |
2602 const AbstractMetaClass *java_class, | |
2603 const AbstractMetaClass *interface) | |
2604 { | |
2605 Q_ASSERT(interface->isInterface()); | |
2606 const InterfaceTypeEntry *ie = static_cast<const InterfaceTypeEntry *>(interface->typeEntry()); | |
2607 QString interface_name = ie->origin()->targetLangName(); | |
2608 | |
2609 s << endl | |
2610 << jni_function_signature(java_class->package(), | |
2611 java_class->name(), | |
2612 QString("__qt_cast_to_%1").arg(interface_name), | |
2613 "jlong", | |
2614 "__J"); | |
2615 | |
2616 s << endl | |
2617 << "(JNIEnv *," << endl | |
2618 << " jobject," << endl | |
2619 << " jlong ptr)" << endl | |
2620 << "{" << endl | |
2621 << " return (jlong) (" << interface->primaryInterfaceImplementor()->qualifiedCppName() << " *) " | |
2622 << "(" << java_class->qualifiedCppName() << " *) ptr;" << endl | |
2623 << "}" << endl; | |
2624 } | |
2625 | |
2626 bool CppImplGenerator::writeConversionRule(QTextStream &s, | |
2627 TypeSystem::Language target_language, | |
2628 const AbstractMetaFunction *java_function, | |
2629 int argument_index, | |
2630 const QString &qt_name, | |
2631 const QString &java_name) | |
2632 { | |
2633 if (argument_index < 0 || java_function == 0) | |
2634 return false; | |
2635 | |
2636 QString conversion_rule = java_function->conversionRule(target_language, argument_index); | |
2637 | |
2638 if (!conversion_rule.isEmpty()) { | |
2639 QString qt_name_var; | |
2640 QString java_name_var; | |
2641 | |
2642 if ((argument_index == 0 && target_language == TypeSystem::NativeCode) | |
2643 || (argument_index != 0 && target_language == TypeSystem::ShellCode)) { | |
2644 qt_name_var = "%in"; | |
2645 java_name_var = "%out"; | |
2646 } else { | |
2647 qt_name_var = "%out"; | |
2648 java_name_var = "%in"; | |
2649 } | |
2650 | |
2651 conversion_rule = conversion_rule.replace(qt_name_var, qt_name) | |
2652 .replace(java_name_var, java_name); | |
2653 | |
2654 AbstractMetaArgumentList arguments = java_function->arguments(); | |
2655 for (int i=0; i<arguments.size(); ++i) { | |
2656 conversion_rule = conversion_rule.replace("%" + QString::number(i+1), | |
2657 arguments.at(i)->indexedName()); | |
2658 } | |
2659 | |
2660 QStringList lines = conversion_rule.split("\n"); | |
2661 foreach (QString line, lines) { | |
2662 s << INDENT << line.trimmed() << endl; | |
2663 } | |
2664 | |
2665 return true; | |
2666 } else { | |
2667 return false; | |
2668 } | |
2669 } | |
2670 | |
2671 | |
2672 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2673 const AbstractMetaClass *java_class, | |
2674 const AbstractMetaType *function_return_type, | |
2675 const QString &qt_name, | |
2676 const QString &java_name, | |
2677 const AbstractMetaFunction *java_function, | |
2678 int argument_index) | |
2679 { | |
2680 // Conversion to C++: Shell code for return values, native code for arguments | |
2681 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2682 if (writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2683 return; | |
2684 | |
2685 s << INDENT << shellClassName(java_class) << " *" << qt_name << " = (" | |
2686 << shellClassName(java_class) << " *) "; | |
2687 if (java_class->isQObject()) | |
2688 s << "qtjambi_to_qobject"; | |
2689 else | |
2690 s << "qtjambi_to_object"; | |
2691 s << "(__jni_env, " << java_name << ");" << endl; | |
2692 if (java_class->isQObject()) { | |
2693 // ### throw exceptions when objects are null... | |
2694 s << INDENT << "if (!" << qt_name << ") " | |
2695 << default_return_statement_java(function_return_type) << ";" << endl << endl; | |
2696 } | |
2697 } | |
2698 | |
2699 | |
2700 void CppImplGenerator::writeJavaToQt(QTextStream &s, | |
2701 const AbstractMetaType *java_type, | |
2702 const QString &qt_name, | |
2703 const QString &java_name, | |
2704 const AbstractMetaFunction *java_function, | |
2705 int argument_index, | |
2706 Option options) | |
2707 { | |
2708 // Conversion to C++: Shell code for return values, native code for arguments | |
2709 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
2710 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2711 return; | |
2712 | |
2713 if (java_type == 0) { | |
2714 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
2715 "language '%4'") | |
2716 .arg(argument_index) | |
2717 .arg(java_function->implementingClass()->name()) | |
2718 .arg(java_function->name()) | |
2719 .arg(int(lang)); | |
2720 ReportHandler::warning(warn); | |
2721 return; | |
2722 } | |
2723 if (java_type->name() == "QModelIndex") { | |
2724 s << INDENT << "QModelIndex " << qt_name << " = qtd_to_QModelIndex(" | |
2725 << java_name << ");" << endl; | |
2726 } else if (java_type->typeEntry()->isStructInD()) { | |
2727 // empty | |
2728 } else if (java_type->typeEntry() && java_type->typeEntry()->qualifiedCppName() == "QString") { | |
2729 s << INDENT << "QString " << qt_name | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2730 << " = " << QString("QString::fromUtf8((const char *)%1.ptr, %1.length);").arg(java_name) << endl; |
1 | 2731 } else if (java_type->isJObjectWrapper()) { |
2732 s << INDENT << "JObjectWrapper " << qt_name | |
2733 << " = qtjambi_to_jobjectwrapper(__jni_env, " << java_name << ");" << endl; | |
2734 } else if (java_type->isVariant()) { | |
2735 s << INDENT << "QVariant " << qt_name | |
2736 << " = " << java_name << " == NULL ? QVariant() : QVariant(*" << java_name << ");" << endl; | |
2737 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { | |
2738 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2739 | |
2740 // ### 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
|
2741 // s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; |
1 | 2742 s << INDENT; |
2743 writeTypeInfo(s, elementType); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2744 s << " *" << qt_name << " = ("; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2745 writeTypeInfo(s, elementType); |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2746 s << "*) " << java_name << ";" << endl; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2747 /* |
1 | 2748 s << INDENT << "__jni_env->" << getXxxArrayRegion(elementType) << "( (" << translateType(java_type, options) |
2749 << ")" << java_name << ", 0, " << java_type->arrayElementCount() << ", " | |
2750 << "(" << translateType(elementType, options) << " *" << ")" | |
2751 << qt_name << ");" << endl; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2752 */ |
1 | 2753 } else if (java_type->isArray()) { |
2754 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2755 | |
2756 s << INDENT << "Q_ASSERT(__jni_env->GetArrayLength((jarray) " << java_name << ") == " << java_type->arrayElementCount() << ");" << endl; | |
2757 writeTypeInfo(s, elementType); | |
2758 s << "[" << java_type->arrayElementCount() << "]" << qt_name << ";" << endl; | |
2759 | |
2760 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2761 writeJavaToQt(s, elementType, qt_name + "[" + QString::number(i) + "]", | |
2762 "__jni_env->GetObjectArrayElement(" + java_name + ", " + QString::number(i) + ")", 0, -1, options); | |
2763 } | |
2764 | |
2765 } else if (java_type->isTargetLangString()) { | |
2766 s << INDENT << "QString " << qt_name | |
2767 << " = " << "QString::fromUtf8(" << java_name << ", " << java_name << "_size);" << endl; | |
2768 // qtd << " = qtjambi_to_qstring(__jni_env, (jstring) " << java_name << ");" << endl; | |
2769 | |
2770 } else if (java_type->isTargetLangChar()) { | |
2771 s << INDENT << "QChar " << qt_name | |
2772 << " = (ushort)" << java_name << ";" << endl; | |
2773 | |
2774 } else if (java_type->isEnum() || java_type->isFlags()) { | |
2775 | |
2776 bool written = false; | |
2777 if (java_type->isEnum()) { | |
2778 AbstractMetaEnum *java_enum = | |
2779 m_classes.findEnum(static_cast<const EnumTypeEntry *>(java_type->typeEntry())); | |
2780 if (java_enum && !java_enum->isPublic()) { | |
2781 | |
2782 s << INDENT << "int " << qt_name << " = "; | |
2783 written = true; | |
2784 } | |
2785 } | |
2786 | |
2787 if (!written) { | |
2788 QString qualified_name = java_type->typeEntry()->qualifiedCppName(); | |
2789 s << INDENT << qualified_name << " " << qt_name | |
2790 << " = (" << qualified_name << ") "; | |
2791 } | |
2792 | |
2793 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
|
2794 s << java_name << ";" << endl; |
1 | 2795 |
2796 } else if (options & BoxedPrimitive) { | |
2797 const PrimitiveTypeEntry *pentry = TypeDatabase::instance()->findTargetLangPrimitiveType("int"); | |
2798 Q_ASSERT(pentry); | |
2799 | |
2800 s << java_name << ";" << endl; | |
2801 | |
2802 } else { | |
2803 s << java_name << ';' << endl; | |
2804 } | |
2805 | |
2806 } else if (java_type->isContainer()) { | |
2807 writeJavaToQtContainer(s, java_type, qt_name, java_name, 0, -1); | |
2808 | |
2809 } else if (java_type->isThread()) { | |
2810 s << INDENT << "QThread *" << qt_name << " = qtjambi_to_thread(__jni_env, " << java_name | |
2811 << ");" << endl; | |
2812 | |
2813 } else if (java_type->typeEntry()->isCustom()) { | |
2814 const CustomTypeEntry *custom_type = | |
2815 static_cast<const CustomTypeEntry *>(java_type->typeEntry()); | |
2816 s << INDENT; | |
2817 custom_type->generateCppJavaToQt(s, java_type, "__jni_env", qt_name, java_name); | |
2818 s << ";" << endl; | |
2819 | |
2820 } else { | |
2821 | |
2822 const TypeEntry *type = java_type->typeEntry(); | |
2823 QString class_name = type->name(); | |
2824 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName()); | |
2825 | |
2826 // Declaration and the c-cast | |
2827 s << INDENT; | |
2828 writeTypeInfo(s, java_type); | |
2829 s << ' ' << qt_name << " = ("; | |
2830 writeTypeInfo(s, java_type); | |
2831 s << ") "; | |
2832 | |
2833 if (java_type->isPrimitive()) { | |
2834 if (options & BoxedPrimitive) { | |
2835 const PrimitiveTypeEntry *pentry = static_cast<const PrimitiveTypeEntry *>(type); | |
2836 //std::cout << "---error_here " << type->targetLangName().toStdString() << " \n"; | |
2837 //std::cout << "----func_here " << java_function->marshalledName().toStdString() << " \n"; | |
2838 | |
2839 if (!pentry->preferredConversion()) | |
2840 pentry = TypeDatabase::instance()->findTargetLangPrimitiveType(pentry->targetLangName()); | |
2841 Q_ASSERT(pentry); | |
2842 | |
2843 s << java_name << ";" << endl; | |
2844 | |
2845 } else if ((options & GlobalRefJObject) && type->jniName() == QLatin1String("jobject")) { | |
2846 s << "__jni_env->NewGlobalRef(" << java_name << ");" << endl; | |
2847 } else { | |
2848 s << java_name << ';' << endl; | |
2849 } | |
2850 | |
2851 #if 0 | |
2852 } else if (java_type->isEnum()) { | |
2853 s << "qtjambi_to_enum(__jni_env, " << java_name << ");" << endl; | |
2854 #endif | |
2855 | |
2856 } else if ((java_type->isQObject() || java_type->isObject()) | |
2857 && static_cast<const ObjectTypeEntry *>(type)->designatedInterface()) { | |
2858 /* qtd const InterfaceTypeEntry *ie = | |
2859 static_cast<const ObjectTypeEntry *>(type)->designatedInterface(); | |
2860 s << "qtjambi_to_interface(__jni_env, "; | |
2861 | |
2862 // This cast is only valid if we're dealing with a native id | |
2863 if ((options & UseNativeIds) == UseNativeIds) | |
2864 s << "(QtJambiLink *)"; | |
2865 */ | |
2866 s << java_name << ";" << endl; | |
2867 /* qtd | |
2868 s << "\"" << ie->targetLangName() << "\", \"" | |
2869 << ie->javaPackage().replace(".", "/") << "/\", " | |
2870 << "\"__qt_cast_to_" << type->targetLangName() << "\");" << endl; | |
2871 */ | |
2872 } else if (java_type->isObject() || java_type->isQObject() || java_type->isNativePointer()) { | |
2873 if (java_type->isReference()) { | |
2874 s << "* (" << qualified_class_name << " " | |
2875 << QString(java_type->actualIndirections(), '*') << ") "; | |
2876 } | |
2877 | |
2878 if (java_type->isNativePointer()) { | |
2879 /* qtd s << "qtjambi_to_cpointer(" | |
2880 << "__jni_env, " | |
2881 << java_name << ", " | |
2882 << java_type->actualIndirections() << ");" << endl; */ | |
2883 s << java_name << ";" << endl; // qtd | |
2884 }/* qtd else if (java_type->isQObject()) { | |
2885 if ((options & UseNativeIds) == 0) | |
2886 s << "qtjambi_to_qobject(__jni_env, "; | |
2887 else | |
2888 s << "qtjambi_from_jlong("; | |
2889 s << java_name; | |
2890 s << ");" << endl; | |
2891 }*/ else { | |
2892 /* qtd if ((options & UseNativeIds) == 0) | |
2893 s << "qtjambi_to_object(__jni_env, "; | |
2894 else | |
2895 s << "qtjambi_from_jlong("; | |
2896 */ s << java_name; | |
2897 s << ";" << endl; // + | |
2898 // qtd s << ");" << endl; | |
2899 } | |
2900 | |
2901 } else { | |
2902 // Return values... | |
2903 if (argument_index == 0) { | |
2904 s << "(" << java_name << " != 0 ? *(" << qualified_class_name << " *)"; | |
2905 /* qtd if ((options & UseNativeIds) == 0) | |
2906 s << "qtjambi_to_object(__jni_env, "; | |
2907 else | |
2908 s << "qtjambi_from_jlong("; | |
2909 */ s << java_name; | |
2910 s << " : " << qualified_class_name << "());" << endl; | |
2911 } else { | |
2912 s << "*" | |
2913 << "(" << qualified_class_name << " *)"; | |
2914 bool null_check = false; | |
2915 /* qtd if ((options & UseNativeIds) == 0) { | |
2916 s << "qtjambi_to_object(__jni_env, "; | |
2917 } else if (hasDefaultConstructor(java_type)) { | |
2918 null_check = true; | |
2919 s << "(" << java_name << " != 0 ? qtjambi_from_jlong("; | |
2920 } else { | |
2921 s << "qtjambi_from_jlong("; | |
2922 } | |
2923 */ s << java_name; | |
2924 // qtd s << ")"; | |
2925 | |
2926 if (null_check) | |
2927 s << " : default_" << QString(qualified_class_name).replace("::", "_") << "())"; | |
2928 s << ";" << endl; | |
2929 } | |
2930 | |
2931 } | |
2932 } | |
2933 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl; | |
2934 } | |
2935 | |
2936 void CppImplGenerator::writeQtToJava(QTextStream &s, | |
2937 const AbstractMetaType *java_type, | |
2938 const QString &qt_name, | |
2939 const QString &java_name, | |
2940 const AbstractMetaFunction *java_function, | |
2941 int argument_index, | |
2942 Option option) | |
2943 { | |
2944 | |
2945 // Conversion to Java: Native code for return values, shell code for arguments | |
2946 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode; | |
2947 /* qtd if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
2948 return; | |
2949 */ | |
2950 if (java_type == 0) { | |
2951 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
2952 "language '%4'") | |
2953 .arg(argument_index) | |
2954 .arg(java_function->implementingClass()->name()) | |
2955 .arg(java_function->name()) | |
2956 .arg(int(lang)); | |
2957 ReportHandler::warning(warn); | |
2958 return; | |
2959 } | |
2960 | |
2961 if (java_type->name() == "QModelIndex") { | |
2962 QString prefix = "*"; | |
2963 if (option & BoxedPrimitive) | |
2964 s << INDENT << "QModelIndexAccessor tmp_index = qtd_from_QModelIndex(" << qt_name << ");" << endl | |
2965 << INDENT << "QModelIndexAccessor *" << java_name << " = &tmp_index;" << endl; | |
2966 else | |
2967 s << INDENT << "*" << java_name << " = qtd_from_QModelIndex(" << qt_name << ");" << endl; | |
2968 | |
81 | 2969 } else if (java_type->typeEntry()->isStructInD()) { |
2970 if (option & BoxedPrimitive) { | |
2971 s << INDENT << java_type->typeEntry()->name() << " *" << java_name << " = (" | |
2972 << java_type->typeEntry()->name() << " *) &" << qt_name << ";" << endl; | |
2973 } else { | |
2974 s << INDENT << "*" << java_name << " = " << qt_name << ";" << endl; | |
2975 } | |
1 | 2976 } else if (java_type->isArray() && java_type->arrayElementType()->isPrimitive()) { |
2977 AbstractMetaType *elementType = java_type->arrayElementType(); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2978 /* qtd |
1 | 2979 s << INDENT << translateType(java_type, option) << " " << java_name << " = __jni_env->" << newXxxArray(elementType) |
2980 << "(" << java_type->arrayElementCount() << ");" << endl; | |
2981 | |
2982 s << INDENT << "__jni_env->" << setXxxArrayRegion(elementType) << "(" | |
2983 << "(" << translateType(java_type, option) << ")" << java_name | |
2984 << ", 0, " << java_type->arrayElementCount() << ", " | |
2985 << "(" << translateType(elementType, option) << " *" << ")" | |
2986 << qt_name << ");" << endl; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2987 */ |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
2988 s << INDENT << translateType(java_type, option) << " " << java_name << " = " << qt_name << ";" <<endl; |
1 | 2989 } else if (java_type->isArray()) { |
2990 AbstractMetaType *elementType = java_type->arrayElementType(); | |
2991 | |
2992 s << INDENT << "jobject " << java_name << " = __jni_env->NewObjectArray(" | |
2993 << java_type->arrayElementCount() << ");" << endl; | |
2994 | |
2995 s << "jobject __qt_element = 0;"; | |
2996 | |
2997 for (int i=0; i<java_type->arrayElementCount(); ++i) { | |
2998 writeQtToJava(s, elementType, qt_name + "[" + QString::number(i) + "]", | |
2999 "__qt_element", 0, -1, option); | |
3000 s << "__jni_env->SetObjectArrayElement((jobjectArray) " << java_name << ", " | |
3001 << i << ", __qt_element);" << endl; | |
3002 } | |
3003 | |
3004 } else if (java_type->isPrimitive()) { | |
3005 const PrimitiveTypeEntry *type = | |
3006 static_cast<const PrimitiveTypeEntry *>(java_type->typeEntry()); | |
3007 | |
3008 Q_ASSERT(type); | |
3009 QString ret_val; | |
3010 if (java_function) | |
3011 ret_val = jniReturnName(java_function); | |
3012 else | |
3013 ret_val = fixCppTypeName(java_type->typeEntry()->qualifiedCppName()); | |
3014 s << INDENT << ret_val << " " << java_name << " = " << qt_name << ";" << endl; | |
3015 } else if (java_type->isJObjectWrapper()) { | |
3016 s << INDENT << "jobject " << java_name << " = qtjambi_from_jobjectwrapper(__jni_env, " | |
3017 << qt_name << ");" << endl; | |
3018 } else if (java_type->isVariant()) { | |
3019 s << INDENT << "QVariant *" << java_name | |
3020 << " = new QVariant(" << qt_name << ");" << endl; | |
3021 } else if (java_type->isTargetLangString()) { | |
35 | 3022 if(java_type->typeEntry()->qualifiedCppName() == "QStringRef") { |
3023 s << INDENT << "const QString *str_ref = " << qt_name << ".string();" << endl | |
3024 << INDENT << "if(str_ref)" << endl | |
253 | 3025 << INDENT << " qtd_toUtf8(str_ref->utf16(), str_ref->size(), " << java_name << ");" << endl |
35 | 3026 << INDENT << "else {" << endl |
3027 << INDENT << " QString empty_str;" << endl | |
253 | 3028 << INDENT << " qtd_toUtf8(empty_str.utf16(), empty_str.size(), " << java_name << ");" << endl |
35 | 3029 << INDENT << "}" << endl; |
3030 } else { | |
253 | 3031 s << INDENT << QString("qtd_toUtf8(%1.utf16(), %1.size(), %2);").arg(qt_name, java_name) << endl; |
35 | 3032 } |
1 | 3033 } else if (java_type->isTargetLangChar()) { |
3034 s << INDENT << "jchar " << java_name << " = " << qt_name << ".unicode();" << endl; | |
3035 | |
3036 } else if (java_type->isIntegerEnum() || java_type->isIntegerFlags() | |
3037 || ((option & EnumAsInts) && (java_type->isEnum() || java_type->isFlags()))) { | |
3038 // } else if (java_type->isEnum() || java_type->isFlags()) { | |
3039 | |
3040 // if (option & EnumAsInts) { | |
3041 // qDebug() << java_type->name() << "should be int..."; | |
3042 // } | |
3043 | |
3044 /* if (option & BoxedPrimitive) { | |
3045 s << INDENT << "jobject " << java_name << " = qtjambi_from_int(__jni_env, " | |
3046 << qt_name << ");" << endl; | |
3047 } else */{ | |
3048 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl; | |
3049 } | |
3050 | |
3051 } else if (java_type->isTargetLangEnum()) { | |
3052 Q_ASSERT((option & EnumAsInts) == 0); | |
3053 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry()); | |
3054 s << INDENT << "int " << java_name << " = " << qt_name << ";" << endl; | |
3055 | |
3056 } else if (java_type->isTargetLangFlags()) { | |
3057 Q_ASSERT((option & EnumAsInts) == 0); | |
3058 const FlagsTypeEntry *ft = static_cast<const FlagsTypeEntry *>(java_type->typeEntry()); | |
3059 s << INDENT << "jobject " << java_name << " = qtjambi_from_flags(__jni_env, " | |
3060 << qt_name << ", \"" << ft->javaPackage().replace('.', '/') << '/' | |
3061 << ft->originator()->javaQualifier() << '$' << ft->targetLangName() << "\");" << endl; | |
3062 | |
3063 } else if (java_type->isContainer()) { | |
3064 writeQtToJavaContainer(s, java_type, qt_name, java_name, 0, -1); | |
3065 | |
3066 } else if (java_type->isThread()) { | |
3067 s << INDENT << "jobject " << java_name << " = qtjambi_from_thread(__jni_env, " << qt_name | |
3068 << ");" << endl; | |
3069 | |
3070 } else if (!java_type->isNativePointer() && java_type->typeEntry()->isCustom()) { | |
3071 s << INDENT; | |
3072 static_cast<const CustomTypeEntry *>(java_type->typeEntry()) | |
3073 ->generateCppQtToJava(s, java_type, "__jni_env", qt_name, java_name); | |
3074 s << ";" << endl; | |
3075 | |
3076 } else { | |
3077 QString return_type; | |
3078 if (java_function) | |
3079 return_type = jniReturnName(java_function); | |
3080 else { | |
3081 return_type = jniReturnType(java_type); | |
3082 return_type = fixCppTypeName(return_type); | |
3083 // return_type = fixCppTypeName(java_type->typeEntry()->qualifiedCppName()); | |
3084 } | |
3085 /* if( (java_type->isValue() && !java_type->typeEntry()->isStructInD()) | |
257 | 3086 // || java_type->isObject() ) |
1 | 3087 s << INDENT << return_type << " *" << java_name << " = (" << return_type << "*) "; |
3088 else*/ | |
3089 s << INDENT << return_type << " " << java_name << " = (" << return_type << ") "; | |
3090 | |
3091 if (java_type->isQObject()) { | |
3092 /* qtd s << "qtjambi_from_qobject(__jni_env, " << "(QObject *) "; | |
3093 | |
3094 if (java_type->isReference() && java_type->indirections() == 0) | |
3095 s << "&"; | |
3096 | |
3097 s << qt_name | |
3098 << ", \"" << java_type->typeEntry()->lookupName() << "\"" | |
3099 << ", \"" << java_type->package().replace(".", "/") << "/\"" | |
3100 << ");" << endl; | |
3101 */ | |
3102 s << qt_name << ";" << endl; | |
3103 | |
3104 #if 0 | |
3105 } else if (java_type->isEnum()) { | |
3106 | |
3107 const EnumTypeEntry *et = static_cast<const EnumTypeEntry *>(java_type->typeEntry()); | |
3108 s << "qtjambi_from_enum(__jni_env, " << qt_name << ", \"" | |
3109 << et->javaQualifier() << "$" << et->targetLangName() << "\");" << endl; | |
3110 #endif | |
3111 } else if (java_type->isNativePointer()) { | |
3112 /* qtd s << "qtjambi_from_cpointer(__jni_env, "; | |
3113 if (java_type->isReference()) | |
3114 s << "&"; | |
3115 s << qt_name << ", " << nativePointerType(java_type) << ", " | |
3116 << java_type->actualIndirections() << ");" << endl; | |
3117 */ | |
3118 if (java_type->isReference()) | |
3119 s << "&"; | |
3120 s << qt_name << ";" << "// qtjambi_from_cpointer" << endl; | |
3121 } else if (java_type->isValue()) { | |
3122 // qtd s << fromObject(java_type->typeEntry(), "&" + qt_name) << endl; | |
3123 s << "new " << java_type->typeEntry()->qualifiedCppName() << "(" << qt_name << ");" << endl; | |
3124 } else { | |
3125 // qtd s << fromObject(java_type->typeEntry(), | |
3126 // qtd (java_type->isReference() ? "&" : "") + qt_name) << endl; | |
3127 s << qt_name << ";" << endl; | |
3128 } | |
3129 } | |
3130 | |
3131 } | |
3132 | |
3133 QString CppImplGenerator::getTypeName(const TypeEntry *entry, Option option) | |
3134 { | |
3135 if(entry->isEnum() && (option & EnumAsInts)) | |
3136 return "int"; | |
3137 | |
3138 return entry->lookupName(); | |
3139 } | |
3140 | |
3141 void CppImplGenerator::writeQtToJavaContainer(QTextStream &s, | |
3142 const AbstractMetaType *java_type, | |
3143 const QString &qt_name, | |
3144 const QString &java_name, | |
3145 const AbstractMetaFunction *java_function, | |
3146 int argument_index) | |
3147 { | |
3148 // Language for conversion to Java: Native code for return values and Shell code for arguments | |
3149 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::NativeCode : TypeSystem::ShellCode; | |
3150 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3151 return; | |
3152 | |
3153 if (java_type == 0) { | |
3154 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3155 "language '%4'") | |
3156 .arg(argument_index) | |
3157 .arg(java_function->implementingClass()->name()) | |
3158 .arg(java_function->name()) | |
3159 .arg(int(lang)); | |
3160 ReportHandler::warning(warn); | |
3161 return; | |
3162 } | |
3163 | |
3164 Q_ASSERT(java_type->isContainer()); | |
3165 const ContainerTypeEntry *type = | |
3166 static_cast<const ContainerTypeEntry *>(java_type->typeEntry()); | |
3167 | |
3168 if (type->type() == ContainerTypeEntry::ListContainer | |
3169 || type->type() == ContainerTypeEntry::VectorContainer | |
3170 || type->type() == ContainerTypeEntry::StringListContainer | |
3171 || type->type() == ContainerTypeEntry::LinkedListContainer | |
3172 || type->type() == ContainerTypeEntry::StackContainer | |
3173 || type->type() == ContainerTypeEntry::SetContainer | |
3174 || type->type() == ContainerTypeEntry::QueueContainer) { | |
3175 | |
3176 Q_ASSERT(java_type->instantiations().size() == 1); | |
3177 AbstractMetaType *targ = java_type->instantiations().first(); | |
3178 | |
3179 QString cls_name = getTypeName(targ->typeEntry(), EnumAsInts); | |
3180 cls_name.remove("_ConcreteWrapper"); | |
3181 | |
3182 s << endl | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3183 // << INDENT << "{" << endl // qtd2 hack, additional scope for avoiding duplicating of "i" |
1 | 3184 << INDENT; |
3185 | |
3186 switch (type->type()) { | |
3187 case ContainerTypeEntry::LinkedListContainer: | |
3188 case ContainerTypeEntry::QueueContainer: | |
3189 s << "qtjambi_linkedlist_new(__jni_env)"; | |
3190 break; | |
3191 case ContainerTypeEntry::StackContainer: | |
3192 s << "qtjambi_stack_new(__jni_env)"; | |
3193 break; | |
3194 case ContainerTypeEntry::SetContainer: | |
3195 s << "qtjambi_hashset_new(__jni_env)"; | |
3196 break; | |
3197 default: | |
3198 s << "qtd_allocate_" << cls_name | |
3199 << "_array(" << java_name << ", " << qt_name << ".size())"; | |
3200 break; | |
3201 } | |
3202 | |
3203 s << ";" << endl | |
3204 << INDENT; | |
3205 | |
3206 | |
3207 writeTypeInfo(s, java_type, ForceValueType); | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3208 QString index = "i_" + qt_name; |
1 | 3209 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
|
3210 << INDENT << QString("int %0 = 0;").arg(index) << endl |
1 | 3211 << INDENT; |
3212 s << "for ("; | |
3213 writeTypeInfo(s, java_type, ForceValueType); | |
3214 s << "::const_iterator " << qt_name << "_it = " << qt_name << ".constBegin(); " | |
3215 << qt_name << "_it != " << qt_name << "_end_it; ++" << qt_name << "_it) {" << endl; | |
3216 { | |
3217 Indentation indent(INDENT); | |
3218 s << INDENT; | |
3219 writeTypeInfo(s, targ); | |
3220 s << " __qt_tmp = *" << qt_name << "_it;" << endl; | |
3221 | |
3222 if(targ->isTargetLangString()) | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3223 s << INDENT << "void *__java_tmp = qtd_string_from_array(" << java_name << ", " << index << ");" << endl; |
1 | 3224 |
3225 writeQtToJava(s, targ, "__qt_tmp", "__java_tmp", 0, -1, BoxedPrimitive); | |
3226 | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3227 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
|
3228 s << INDENT << "++" << index << ";" << endl; |
1 | 3229 } |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3230 s << INDENT << "}" << endl; |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
148
diff
changeset
|
3231 // << INDENT << "}" << endl; |
1 | 3232 |
3233 } else if (type->type() == ContainerTypeEntry::PairContainer) { | |
3234 QList<AbstractMetaType *> args = java_type->instantiations(); | |
3235 Q_ASSERT(args.size() == 2); | |
3236 | |
3237 s << INDENT << "jobject " << java_name << ";" << endl | |
3238 << INDENT << "{" << endl; | |
3239 { | |
3240 Indentation indent(INDENT); | |
3241 writeQtToJava(s, args.at(0), qt_name + ".first", "__java_tmp_first", 0, -1, BoxedPrimitive); | |
3242 writeQtToJava(s, args.at(1), qt_name + ".second", "__java_tmp_second", 0, -1, BoxedPrimitive); | |
3243 s << INDENT << java_name << " = qtjambi_pair_new(__jni_env, " | |
3244 << "__java_tmp_first, __java_tmp_second);" << endl; | |
3245 } | |
3246 | |
3247 s << INDENT << "}" << endl; | |
3248 | |
3249 } else if (type->type() == ContainerTypeEntry::MultiMapContainer) { | |
3250 | |
3251 Q_ASSERT(java_type->instantiations().size() == 2); | |
3252 AbstractMetaType *targ_key = java_type->instantiations().at(0); | |
3253 AbstractMetaType *targ_val = java_type->instantiations().at(1); | |
3254 | |
3255 s << endl | |
3256 << INDENT << "jobject " << java_name << " = qtjambi_treemap_new(__jni_env, " << qt_name << ".keys().size());" << endl | |
3257 << INDENT << "QList<"; | |
3258 writeTypeInfo(s, targ_key); | |
3259 s << "> __qt_keys = " << qt_name << ".keys();" << endl | |
3260 << INDENT << "for (int i=0; i<__qt_keys.size(); ++i) {" << endl; | |
3261 { | |
3262 Indentation indent(INDENT); | |
3263 | |
3264 s << INDENT; | |
3265 writeTypeInfo(s, targ_key); | |
3266 s << " __qt_tmp_key = __qt_keys.at(i);" << endl; | |
3267 writeQtToJava(s, targ_key, "__qt_tmp_key", "__java_tmp_key", 0, -1, BoxedPrimitive); | |
3268 | |
3269 s << INDENT << "QList<"; | |
3270 writeTypeInfo(s, targ_val); | |
3271 s << "> __qt_values = " << qt_name << ".values(__qt_tmp_key);" << endl | |
3272 << INDENT << "jobject __java_value_list = qtjambi_arraylist_new(__jni_env, __qt_values.size());" << endl | |
3273 << INDENT << "for (int j=0; j<__qt_values.size(); ++j) {" << endl; | |
3274 { | |
3275 Indentation indent(INDENT); | |
3276 | |
3277 s << INDENT; | |
3278 writeTypeInfo(s, targ_val); | |
3279 s << " __qt_tmp_val = __qt_values.at(j);" << endl; | |
3280 writeQtToJava(s, targ_val, "__qt_tmp_val", "__java_tmp_val", 0, -1, BoxedPrimitive); | |
3281 | |
3282 s << INDENT << "qtjambi_collection_add(__jni_env, __java_value_list, __java_tmp_val);" << endl; | |
3283 } | |
3284 s << INDENT << "}" << endl | |
3285 << INDENT << "qtjambi_map_put(__jni_env, " << java_name << ", __java_tmp_key, __java_value_list);" << endl; | |
3286 } | |
3287 s << INDENT << "}" << endl; | |
3288 | |
3289 } else if (type->type() == ContainerTypeEntry::MapContainer | |
3290 || type->type() == ContainerTypeEntry::HashContainer) { | |
3291 QString constructor = type->type() == ContainerTypeEntry::MapContainer | |
3292 ? "qtjambi_treemap_new" | |
3293 : "qtjambi_hashmap_new"; | |
3294 | |
3295 Q_ASSERT(java_type->instantiations().size() == 2); | |
3296 AbstractMetaType *targ_key = java_type->instantiations().at(0); | |
3297 AbstractMetaType *targ_val = java_type->instantiations().at(1); | |
3298 | |
3299 s << endl | |
3300 << INDENT << "jobject " << java_name << " = " << constructor << "(__jni_env, " << qt_name | |
3301 << ".size());" << endl | |
3302 << INDENT; | |
3303 writeTypeInfo(s, java_type, Option(ExcludeReference | ExcludeConst)); | |
3304 s << "::const_iterator it;" << endl | |
3305 << INDENT << "for (it=" << qt_name << ".constBegin(); it!=" << qt_name << ".constEnd(); ++it) {" << endl; | |
3306 { | |
3307 Indentation indent(INDENT); | |
3308 s << INDENT; | |
3309 writeTypeInfo(s, targ_key); | |
3310 s << " __qt_tmp_key = it.key();" << endl | |
3311 << INDENT; | |
3312 writeTypeInfo(s, targ_val); | |
3313 s << " __qt_tmp_val = it.value();" << endl; | |
3314 writeQtToJava(s, targ_key, "__qt_tmp_key", "__java_tmp_key", 0, -1, BoxedPrimitive); | |
3315 writeQtToJava(s, targ_val, "__qt_tmp_val", "__java_tmp_val", 0, -1, BoxedPrimitive); | |
3316 s << INDENT << "qtjambi_map_put(__jni_env, " << java_name | |
3317 << ", __java_tmp_key, __java_tmp_val);" << endl; | |
3318 } | |
3319 s << INDENT << "}" << endl; | |
3320 | |
3321 } else { | |
3322 ReportHandler::warning(QString("unable to generate container type %1, type=%2") | |
3323 .arg(java_type->name()).arg(type->type())); | |
3324 } | |
3325 | |
3326 // qtd s << INDENT << "QTJAMBI_EXCEPTION_CHECK(__jni_env);" << endl; | |
3327 } | |
3328 | |
3329 | |
3330 void CppImplGenerator::writeJavaToQtContainer(QTextStream &s, | |
3331 const AbstractMetaType *java_type, | |
3332 const QString &qt_name, | |
3333 const QString &java_name, | |
3334 const AbstractMetaFunction *java_function, | |
3335 int argument_index) | |
3336 { | |
3337 // Conversion to C++: Shell code for return value, native code for arguments | |
3338 TypeSystem::Language lang = argument_index == 0 ? TypeSystem::ShellCode : TypeSystem::NativeCode; | |
3339 if (java_function && writeConversionRule(s, lang, java_function, argument_index, qt_name, java_name)) | |
3340 return; | |
3341 | |
3342 if (java_type == 0) { | |
3343 QString warn = QString("no conversion possible for argument '%1' in function '%2::%3' for " | |
3344 "language '%4'") | |
3345 .arg(argument_index) | |
3346 .arg(java_function->implementingClass()->name()) | |
3347 .arg(java_function->name()) | |
3348 .arg(int(lang)); | |
3349 ReportHandler::warning(warn); | |
3350 return; | |
3351 } | |
3352 | |
3353 | |
3354 Q_ASSERT(java_type->isContainer()); | |
3355 const ContainerTypeEntry *type = | |
3356 static_cast<const ContainerTypeEntry *>(java_type->typeEntry()); | |
3357 | |
3358 if (type->type() == ContainerTypeEntry::ListContainer | |
3359 || type->type() == ContainerTypeEntry::VectorContainer | |
3360 || type->type() == ContainerTypeEntry::StringListContainer | |
3361 || type->type() == ContainerTypeEntry::LinkedListContainer | |
3362 || type->type() == ContainerTypeEntry::StackContainer | |
3363 || type->type() == ContainerTypeEntry::SetContainer | |
3364 || type->type() == ContainerTypeEntry::QueueContainer) { | |
3365 Q_ASSERT(java_type->instantiations().size() == 1); | |
3366 AbstractMetaType *targ = java_type->instantiations().first(); | |
3367 QString elem_type = getTypeName(targ->typeEntry(), EnumAsInts); | |
3368 elem_type.remove("_ConcreteWrapper"); | |
3369 | |
3370 s << INDENT; | |
3371 writeTypeInfo(s, java_type, ForceValueType); | |
3372 s << qt_name << ";" << endl; | |
3373 | |
3374 // qtd s << INDENT << "if (" << java_name << " != 0) {" << endl; | |
3375 { | |
3376 /* qtd Indentation indent(INDENT); | |
3377 s << INDENT << "jobjectArray __qt__array = qtjambi_collection_toArray(__jni_env, " | |
3378 << java_name << ");" << endl | |
3379 << INDENT << "jsize __qt__size = __jni_env->GetArrayLength(__qt__array);" << endl; | |
3380 */ | |
3381 if (type->type() == ContainerTypeEntry::VectorContainer | |
3382 || type->type() == ContainerTypeEntry::StackContainer) | |
3383 s << INDENT << qt_name << ".reserve(" << java_name << "_size);" << endl; | |
3384 | |
3385 s << INDENT << "for (int i=0; i<" << java_name << "_size; ++i) {" << endl; | |
3386 { | |
3387 Indentation indent(INDENT); | |
3388 if(targ->isTargetLangString()) | |
35 | 3389 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
|
3390 << INDENT << "qtd_get_string_from_array(" << java_name << ", i, &__d_element);" << endl; |
1 | 3391 else { |
3392 s << INDENT; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3393 writeTypeInfo(s, targ, Option(VirtualDispatch | ForcePointer | EnumAsInts)); |
1 | 3394 QString cast_string = ""; |
3395 const TypeEntry* centry = targ->typeEntry(); | |
3396 if (centry->isComplex() && (centry->isObject() || centry->isValue() || centry->isInterface())) | |
3397 cast_string = "(void**)"; | |
3398 s << "__d_element;" << endl | |
3399 << INDENT << "qtd_get_" << elem_type << "_from_array(" << java_name << ", i, &__d_element);" << endl; | |
3400 } | |
3401 /* qtd s << INDENT << "jobject __d_element = " | |
3402 << "__jni_env->GetObjectArrayElement(__qt__array, i);" << endl;*/ | |
3403 writeJavaToQt(s, targ, "__qt_element", "__d_element", 0, -1, BoxedPrimitive); | |
3404 QString cont_element = "__qt_element"; | |
3405 if(targ->typeEntry()->isStructInD() && targ->name() != "QModelIndex") | |
3406 cont_element = "__d_element"; | |
3407 s << INDENT << qt_name << " << " << cont_element << ";" << endl; | |
3408 } | |
3409 // qtd s << INDENT << "}" << endl; | |
3410 } | |
3411 s << INDENT << "}" << endl; | |
3412 } else if (type->type() == ContainerTypeEntry::PairContainer) { | |
3413 QList<AbstractMetaType *> targs = java_type->instantiations(); | |
3414 Q_ASSERT(targs.size() == 2); | |
3415 | |
3416 s << INDENT; | |
3417 writeTypeInfo(s, java_type, ForceValueType); | |
3418 s << " " << qt_name << ";" << endl | |
3419 << INDENT << "if (" << java_name << " != 0) {" << endl; | |
3420 { | |
3421 // separate scope required just in case function takes two QPair's. | |
3422 Indentation indent(INDENT); | |
3423 s << INDENT << "jobject __java_first = qtjambi_pair_get(__jni_env, " | |
3424 << java_name << ", 0);" << endl; | |
3425 writeJavaToQt(s, targs.at(0), "__qt_first", "__java_first", 0, -1, BoxedPrimitive); | |
3426 | |
3427 s << INDENT << "jobject __java_second = qtjambi_pair_get(__jni_env, " | |
3428 << java_name << ", 1);" << endl; | |
3429 writeJavaToQt(s, targs.at(1), "__qt_second", "__java_second", 0, -1, BoxedPrimitive); | |
3430 | |
3431 s << INDENT << qt_name << ".first = __qt_first;" << endl | |
3432 << INDENT << qt_name << ".second = __qt_second;" << endl; | |
3433 } | |
3434 s << INDENT << "}" << endl; | |
3435 } else if (type->type() == ContainerTypeEntry::MapContainer | |
3436 || type->type() == ContainerTypeEntry::HashContainer) { | |
3437 Q_ASSERT(java_type->instantiations().size() == 2); | |
3438 AbstractMetaType *targ_key = java_type->instantiations().at(0); | |
3439 AbstractMetaType *targ_val = java_type->instantiations().at(1); | |
3440 | |
3441 s << INDENT; | |
3442 writeTypeInfo(s, java_type, ForceValueType); | |
3443 s << qt_name << ";" << endl; | |
3444 s << INDENT << "if (" << java_name << " != 0) {" << endl; | |
3445 { | |
3446 Indentation indent(INDENT); | |
3447 s << INDENT << "int __qt_list_size = qtjambi_map_size(__jni_env, " << java_name | |
3448 << ");" << endl | |
3449 << INDENT | |
3450 << "jobjectArray __java_entry_set = qtjambi_map_entryset_array(__jni_env, " << java_name | |
3451 << ");" << endl; | |
3452 | |
3453 s << INDENT << "for (int i=0; i<__qt_list_size; ++i) {" << endl; | |
3454 { | |
3455 Indentation indent(INDENT); | |
3456 s << INDENT | |
3457 << "QPair<jobject, jobject> __java_entry = " | |
3458 << "qtjambi_entryset_array_get(__jni_env, __java_entry_set, i);" | |
3459 << endl | |
3460 << INDENT << "jobject __java_key = __java_entry.first;" << endl | |
3461 << INDENT << "jobject __java_val = __java_entry.second;" << endl; | |
3462 writeJavaToQt(s, targ_key, "__qt_key", "__java_key", 0, -1, BoxedPrimitive); | |
3463 writeJavaToQt(s, targ_val, "__qt_val", "__java_val", 0, -1, BoxedPrimitive); | |
3464 s << INDENT << qt_name << ".insert(__qt_key, __qt_val);" << endl; | |
3465 } | |
3466 s << INDENT << "}" << endl; | |
3467 } | |
3468 s << INDENT << "}" << endl; | |
3469 | |
3470 } else { | |
3471 ReportHandler::warning(QString("unable to generate container type %1, %2") | |
3472 .arg(java_type->name()).arg(type->type())); | |
3473 } | |
3474 | |
3475 } | |
3476 | |
3477 | |
3478 void CppImplGenerator::writeFunctionCall(QTextStream &s, const QString &object_name, | |
3479 const AbstractMetaFunction *java_function, | |
3480 const QString &prefix, | |
3481 Option option, | |
3482 const QStringList &extra_arguments) | |
3483 { | |
3484 QString function_name = option & OriginalName ? java_function->originalName() : java_function->name(); | |
3485 | |
3486 AbstractMetaClassList interfaces = java_function->implementingClass()->interfaces(); | |
3487 | |
3488 QString classPrefix; | |
3489 if (prefix.isEmpty() | |
3490 && !java_function->implementingClass()->interfaces().isEmpty() | |
3491 && !java_function->implementingClass()->inheritsFrom(java_function->declaringClass())) { | |
3492 classPrefix = java_function->declaringClass()->qualifiedCppName() + "::"; | |
3493 } | |
3494 | |
3495 if (java_function->isInGlobalScope()) { | |
3496 | |
3497 // Global scope stream operators need the arguments to be reordered (this ref at end) | |
3498 // so we special case them in order to simplify this code | |
3499 bool stream_operator = java_function->originalName() == "operator<<" | |
3500 || java_function->originalName() == "operator>>"; | |
3501 | |
3502 if (java_function->type() == 0) | |
3503 s << "if (" << object_name << " != 0) "; | |
3504 else | |
3505 s << "(" << object_name << " != 0) ? "; | |
3506 s << "::" << prefix << function_name << "("; | |
3507 if (!stream_operator) | |
3508 s << "*" << object_name << ", "; | |
3509 writeFunctionCallArguments(s, java_function, "__qt_"); | |
3510 if (stream_operator) | |
3511 s << ", *" << object_name; | |
3512 s << ")"; | |
3513 if (java_function->type() != 0) | |
3514 s << " : " << default_return_statement_qt(java_function->type(), Generator::Option(option | Generator::NoReturnStatement)); | |
3515 s << ";"; | |
3516 } else { | |
3517 s << object_name << (java_function->isStatic() ? QLatin1String("::") : QLatin1String("->") + classPrefix) | |
3518 << prefix << function_name << "("; | |
3519 writeFunctionCallArguments(s, java_function, "__qt_"); | |
3520 | |
3521 // The extra arguments... | |
3522 for (int i=0; i<extra_arguments.size(); ++i) { | |
3523 if (i > 0 || java_function->arguments().size() != 0) | |
3524 s << ", "; | |
3525 s << extra_arguments.at(i); | |
3526 } | |
3527 | |
3528 s << ");"; | |
3529 } | |
3530 | |
3531 s << endl; | |
3532 | |
3533 } | |
3534 | |
3535 | |
3536 void CppImplGenerator::writeFunctionCallArguments(QTextStream &s, | |
3537 const AbstractMetaFunction *java_function, | |
3538 const QString &prefix, | |
3539 Option options) | |
3540 { | |
3541 AbstractMetaArgumentList arguments = java_function->arguments(); | |
3542 | |
3543 int written_arguments = 0; | |
3544 const AbstractMetaClass *cls = java_function->ownerClass(); | |
257 | 3545 if (java_function->isConstructor() && cls->typeEntry()->isObject()) { |
1 | 3546 s << "d_ptr"; |
3547 written_arguments++; | |
3548 } | |
3549 for (int i=0; i<arguments.size(); ++i) { | |
3550 const AbstractMetaArgument *argument = arguments.at(i); | |
3551 AbstractMetaType *a_type = argument->type(); | |
3552 | |
3553 if ((options & SkipRemovedArguments) == SkipRemovedArguments | |
3554 && java_function->argumentRemoved(i+1)) { | |
3555 continue; | |
3556 } | |
3557 | |
3558 if (written_arguments++ > 0) { | |
3559 s << ", "; | |
3560 } | |
3561 | |
3562 bool enum_as_int = (options & EnumAsInts) && (argument->type()->typeEntry()->isEnum() | |
3563 || argument->type()->typeEntry()->isFlags()); | |
3564 if (a_type->isEnum()) { | |
3565 AbstractMetaEnum *java_enum = | |
3566 m_classes.findEnum(static_cast<const EnumTypeEntry *>(argument->type()->typeEntry())); | |
3567 if (java_enum == 0) { | |
3568 ReportHandler::warning(QString("enum not found: '%1'") | |
3569 .arg(argument->type()->typeEntry()->qualifiedCppName())); | |
3570 } else { | |
3571 enum_as_int |= !java_enum->isPublic(); | |
3572 } | |
3573 } | |
3574 | |
3575 if ( (options & VirtualDispatch) | |
3576 && a_type->isContainer()) { | |
3577 s << "__d_" << argument->indexedName(); | |
3578 continue; | |
3579 } | |
3580 | |
3581 if ((!(options & NoCasts) && !enum_as_int) || ((options & ForceEnumCast) && a_type->isEnum())) { | |
3582 | |
3583 // If the type in the signature is specified without template instantiation, but the | |
3584 // class is actually a template class, then we have troubles. | |
3585 AbstractMetaClass *cls = classes().findClass(argument->type()->typeEntry()->qualifiedCppName()); | |
3586 | |
3587 if ( (options & VirtualDispatch) && !a_type->typeEntry()->isStructInD() | |
3588 && ( a_type->isValue() | |
3589 || (a_type->isReference() && (a_type->typeEntry()->isValue() || a_type->typeEntry()->isObject() || a_type->typeEntry()->isPrimitive()) && !a_type->isTargetLangString()) | |
3590 ) ) | |
3591 s << "&"; | |
3592 | |
3593 if( (options & VirtualDispatch) && a_type->typeEntry()->isStructInD() && a_type->isReference()) | |
3594 s << "&"; | |
3595 | |
3596 if ( (options & VirtualDispatch) | |
3597 && ( a_type->isTargetLangString() || a_type->name() == "QModelIndex" ) ) | |
3598 ; | |
3599 else if (cls == 0 || cls->templateArguments().size() == a_type->instantiations().size()) { | |
3600 s << "("; | |
3601 writeTypeInfo(s, a_type, options); | |
3602 s << ")"; | |
3603 } | |
3604 } | |
3605 | |
3606 if (a_type->isNativePointer() && a_type->typeEntry()->name() == "QString" && !a_type->isReference()) | |
3607 s << "&"; | |
3608 | |
3609 if ( ( !a_type->isPrimitive() | |
3610 || !java_function->conversionRule(TypeSystem::NativeCode, argument->argumentIndex()+1).isEmpty() ) | |
3611 && (!a_type->typeEntry()->isStructInD() || a_type->name() == "QModelIndex") ) { | |
3612 s << prefix; | |
3613 } | |
3614 | |
3615 if (options & VirtualDispatch) { | |
3616 if( argument->type()->isTargetLangString()) | |
3617 s << argument->indexedName() << ".utf16(), " << argument->indexedName() << ".size()"; | |
3618 else if (argument->type()->name() == "QModelIndex") | |
3619 s << "qtd_from_QModelIndex(" << argument->indexedName() << ")"; | |
3620 else | |
3621 s << argument->indexedName(); | |
3622 } else | |
3623 s << argument->indexedName(); | |
3624 } | |
3625 } | |
3626 | |
3627 | |
3628 QString CppImplGenerator::translateType(const AbstractMetaType *java_type, Option option, bool d_export) | |
3629 { | |
3630 if (!java_type) | |
3631 return "void"; | |
3632 | |
3633 const TypeEntry *type = java_type->typeEntry(); | |
3634 QString class_name = type->name(); | |
3635 QString qualified_class_name = fixCppTypeName(type->qualifiedCppName()); | |
3636 QString d_name = qualified_class_name; | |
3637 if (d_export) | |
3638 d_name = type->lookupName(); | |
3639 | |
3640 if (java_type->isPrimitive() | |
3641 || java_type->isTargetLangString() | |
3642 || java_type->isVariant() | |
3643 || java_type->isJObjectWrapper() | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3644 || java_type->isTargetLangChar()) { |
1 | 3645 return d_name; |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3646 } else if (java_type->isArray()) { |
39 | 3647 if (d_export) |
3648 return java_type->arrayElementType()->name() + "*"; | |
3649 else | |
3650 return java_type->arrayElementType()->typeEntry()->qualifiedCppName() + "*"; | |
1 | 3651 } 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
|
3652 || ((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
|
3653 return "int"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3654 } else if (java_type->isReference()) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3655 if (java_type->typeEntry()->isValue()) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3656 return "void*"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3657 else |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3658 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
|
3659 } else if (java_type->isNativePointer()) { |
190 | 3660 if (java_type->typeEntry()->isStructInD() && dVersion == 2 && java_type->isConstant() && d_export) |
3661 return "const(" + d_name + QString(java_type->indirections()-1, '*') + ")*"; | |
3662 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
|
3663 return "void*"; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3664 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
|
3665 return "int" + QString(java_type->indirections(), '*'); |
188 | 3666 else { |
3667 if (java_type->isConstant() && dVersion == 2 && d_export) | |
190 | 3668 return "const (" + d_name + QString(java_type->indirections(), '*') + ")"; |
188 | 3669 else |
3670 return d_name + QString(java_type->indirections(), '*'); | |
3671 } | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3672 } else { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
21
diff
changeset
|
3673 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
|
3674 } |
81 | 3675 } |
1 | 3676 |
3677 void CppImplGenerator::writeExtraIncludes(QTextStream &s, const AbstractMetaClass *java_class) | |
3678 { | |
3679 IncludeList includes = java_class->typeEntry()->extraIncludes(); | |
3680 qSort(includes.begin(), includes.end()); | |
3681 | |
3682 int used = 0; | |
3683 foreach (const Include &i, includes) { | |
3684 if (i.type != Include::TargetLangImport) { | |
3685 s << i.toString() << endl; | |
3686 ++used; | |
3687 } | |
3688 } | |
3689 | |
3690 | |
3691 if (used) | |
3692 s << endl; | |
3693 | |
3694 } | |
3695 | |
3696 | |
3697 void CppImplGenerator::writeDefaultConstructedValues_helper(QSet<QString> &values, | |
3698 const AbstractMetaFunction *func) | |
3699 { | |
3700 foreach (AbstractMetaArgument *arg, func->arguments()) { | |
3701 AbstractMetaType *type = arg->type(); | |
3702 if (func->typeReplaced(arg->argumentIndex()+1).isEmpty() && type->isValue() && hasDefaultConstructor(type)) | |
3703 values << type->typeEntry()->qualifiedCppName(); | |
3704 } | |
3705 } | |
3706 | |
3707 | |
3708 void CppImplGenerator::writeDefaultConstructedValues(QTextStream &s, const AbstractMetaClass *java_class) { | |
3709 | |
3710 QSet<QString> values; | |
3711 | |
3712 // Class functions, more or less copied from the logic in write(Class) above... | |
3713 AbstractMetaFunctionList class_funcs; | |
3714 | |
3715 // Add normal final functions | |
3716 foreach (AbstractMetaFunction *function, java_class->functionsInTargetLang()) { | |
3717 if (!function->isEmptyFunction()) | |
3718 class_funcs << function; | |
3719 } | |
3720 | |
3721 // Add abstract functions, I think... | |
3722 foreach (AbstractMetaFunction *function, java_class->queryFunctions(AbstractMetaClass::NormalFunctions | |
3723 | AbstractMetaClass::AbstractFunctions | |
3724 | AbstractMetaClass::NotRemovedFromTargetLang)) { | |
3725 if (function->implementingClass() != java_class) | |
3726 class_funcs << function; | |
3727 } | |
3728 | |
3729 // Signals (their c++ wrapper calls actually...) | |
3730 class_funcs += java_class->queryFunctions(AbstractMetaClass::Signals); | |
3731 | |
3732 // | |
3733 foreach (AbstractMetaFunction *f, class_funcs) { | |
3734 writeDefaultConstructedValues_helper(values, f); | |
3735 } | |
3736 | |
3737 foreach (AbstractMetaField *field, java_class->fields()) { | |
3738 writeDefaultConstructedValues_helper(values, field->setter()); | |
3739 } | |
3740 | |
3741 if (!values.isEmpty()) { | |
3742 s << endl << endl | |
3743 << "// Default constructed values used throughout final functions..." << endl; | |
3744 for (QSet<QString>::const_iterator it = values.constBegin(); it != values.constEnd(); ++it) { | |
3745 s << "Q_GLOBAL_STATIC(" << *it << ", default_" << QString(*it).replace("::", "_") | |
3746 << ");" << endl; | |
3747 } | |
3748 s << endl << endl; | |
3749 } | |
3750 } |