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