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