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