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