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