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