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