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