Mercurial > projects > qtd
annotate generator/dgenerator.cpp @ 187:34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
author | eldar |
---|---|
date | Sat, 11 Jul 2009 11:17:44 +0000 |
parents | 4fcf7ba2a791 |
children | 7dd099050621 |
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 "dgenerator.h" | |
43 #include "reporthandler.h" | |
44 #include "docparser.h" | |
45 #include "jumptable.h" | |
46 #include "fileout.h" | |
47 | |
48 #include <QtCore/QDir> | |
49 #include <QtCore/QTextStream> | |
50 #include <QtCore/QVariant> | |
51 #include <QtCore/QRegExp> | |
52 #include <QDebug> | |
53 | |
54 #include <iostream> | |
55 | |
56 | |
57 static Indentor INDENT; | |
58 | |
59 DGenerator::DGenerator() | |
60 : m_doc_parser(0), | |
61 m_docs_enabled(false), | |
62 m_native_jump_table(false), | |
63 m_recursive(0), | |
64 m_isRecursive(false) | |
65 { | |
66 excludedTypes << "long long" << "bool" << "int" << "QString" << "char" << "WId" | |
67 << "unsigned char" << "uint" << "double" << "short" << "float" | |
68 << "signed char" << "unsigned short" << "QBool" << "unsigned int" | |
69 << "Qt::HANDLE" << "QChar" << "java.lang.JObjectWrapper" << "void" | |
70 << "QLatin1String" << "unsigned long long" << "signed int" | |
71 << "signed short" << "Array" << "GLuint" << "GLenum" << "GLint" | |
72 << "unsigned long" << "ulong" << "long" << "QByteRef" | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
73 << "QStringList" << "QList" << "QVector" << "QPair" |
39 | 74 << "QSet" << "QStringRef" << "quintptr"; |
1 | 75 } |
76 | |
77 QString DGenerator::fileNameForClass(const AbstractMetaClass *d_class) const | |
78 { | |
79 return QString("%1.d").arg(d_class->name()); | |
80 } | |
81 | |
82 void DGenerator::writeFieldAccessors(QTextStream &s, const AbstractMetaField *field) | |
83 { | |
84 Q_ASSERT(field->isPublic() || field->isProtected()); | |
85 | |
86 const AbstractMetaClass *declaringClass = field->enclosingClass(); | |
87 | |
88 FieldModification mod = declaringClass->typeEntry()->fieldModification(field->name()); | |
89 | |
90 // Set function | |
91 if (mod.isWritable() && !field->type()->isConstant()) { | |
92 const AbstractMetaFunction *setter = field->setter(); | |
93 if (declaringClass->hasFunction(setter)) { | |
94 QString warning = | |
95 QString("class '%1' already has setter '%2' for public field '%3'") | |
96 .arg(declaringClass->name()).arg(setter->name()).arg(field->name()); | |
97 ReportHandler::warning(warning); | |
98 } else { | |
99 if (!notWrappedYet(setter)) // qtd2 | |
100 writeFunction(s, setter); | |
101 } | |
102 } | |
103 | |
104 // Get function | |
105 const AbstractMetaFunction *getter = field->getter(); | |
106 if (mod.isReadable()) { | |
107 if (declaringClass->hasFunction(getter)) { | |
108 QString warning = | |
109 QString("class '%1' already has getter '%2' for public field '%3'") | |
110 .arg(declaringClass->name()).arg(getter->name()).arg(field->name()); | |
111 ReportHandler::warning(warning); | |
112 } else { | |
113 if (!notWrappedYet(getter)) // qtd2 | |
114 writeFunction(s, getter); | |
115 } | |
116 } | |
117 } | |
118 | |
119 QString DGenerator::translateType(const AbstractMetaType *d_type, const AbstractMetaClass *context, Option option) | |
120 { | |
121 QString s; | |
122 | |
123 if (context != 0 && d_type != 0 && context->typeEntry()->isGenericClass() && d_type->originalTemplateType() != 0) | |
124 d_type = d_type->originalTemplateType(); | |
125 | |
126 if (!d_type) { | |
127 s = "void"; | |
81 | 128 } else if (d_type->typeEntry()->qualifiedCppName() == "QChar") { |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
129 s = "wchar" + QString(d_type->actualIndirections(), '*'); |
81 | 130 } else if (d_type->typeEntry() && d_type->typeEntry()->qualifiedCppName() == "QString") { |
1 | 131 s = "string"; |
132 } else if (d_type->isArray()) { | |
133 s = translateType(d_type->arrayElementType(), context) + "[]"; | |
134 } else if (d_type->isEnum() /* qtd2 || d_type->isFlags() */) { | |
135 if (( d_type->isEnum() && ((EnumTypeEntry *)d_type->typeEntry())->forceInteger() ) | |
136 || ( d_type->isFlags() && ((FlagsTypeEntry *)d_type->typeEntry())->forceInteger() ) ) { | |
137 if (option & BoxedPrimitive) | |
138 s = "java.lang.Integer"; | |
139 else | |
140 s = "int"; | |
141 } else { | |
142 if (option & EnumAsInts) | |
143 s = "int"; | |
144 else | |
145 s = d_type->typeEntry()->qualifiedTargetLangName(); | |
146 } | |
147 } else if (d_type->isFlags()) { // qtd2 begin | |
148 if (d_type->isFlags() && ((FlagsTypeEntry *)d_type->typeEntry())->forceInteger()) { | |
149 if (option & BoxedPrimitive) | |
150 s = "java.lang.Integer"; | |
151 else | |
152 s = "int"; | |
153 } else | |
154 s = "int"; | |
155 } else { | |
156 /* qtd if (d_type->isPrimitive() && (option & BoxedPrimitive)) { | |
157 s = static_cast<const PrimitiveTypeEntry *>(d_type->typeEntry())->javaObjectName(); | |
158 } else */ if (d_type->isVariant()) { | |
159 s = "QVariant"; | |
160 } else if (d_type->isNativePointer()) { | |
161 if (d_type->typeEntry()->isValue() && !d_type->typeEntry()->isStructInD()) | |
162 s = d_type->typeEntry()->lookupName(); | |
163 else if (d_type->typeEntry()->isEnum()) | |
164 s = "int" + QString(d_type->actualIndirections(), '*'); | |
165 else | |
166 s = d_type->typeEntry()->lookupName() + QString(d_type->actualIndirections(), '*'); | |
167 } else if (d_type->isContainer()) { | |
168 const ContainerTypeEntry* c_entry = static_cast<const ContainerTypeEntry*>(d_type->typeEntry()); | |
169 Q_ASSERT(c_entry); | |
170 | |
171 if ((option & SkipTemplateParameters) == 0) { | |
172 QList<AbstractMetaType *> args = d_type->instantiations(); | |
173 | |
174 if (args.size() == 1) // QVector or QList | |
175 s = translateType(args.at(0), context, BoxedPrimitive) + "[]"; | |
176 else if(args.size() == 2) { // all sorts of maps | |
177 s = translateType(args.at(1), context, BoxedPrimitive); // value | |
178 bool isMultiMap = static_cast<const ContainerTypeEntry *>(d_type->typeEntry())->type() == ContainerTypeEntry::MultiMapContainer; | |
179 if (isMultiMap) | |
180 s += "[]"; | |
181 s += "[" + translateType(args.at(0), context, BoxedPrimitive) + "]"; | |
182 } else { | |
183 s = d_type->typeEntry()->qualifiedTargetLangName(); | |
184 | |
185 for (int i=0; i<args.size(); ++i) { | |
186 if (i != 0) | |
187 s += ", "; | |
188 bool isMultiMap = static_cast<const ContainerTypeEntry *>(d_type->typeEntry())->type() == ContainerTypeEntry::MultiMapContainer | |
189 && i == 1; | |
190 if (isMultiMap) | |
191 s += "java.util.List<"; | |
192 s += translateType(args.at(i), context, BoxedPrimitive); | |
193 if (isMultiMap) | |
194 s += ">"; | |
195 } | |
196 s += '>'; | |
197 } | |
198 } | |
199 | |
200 } else { | |
201 const TypeEntry *type = d_type->typeEntry(); | |
202 if (type->designatedInterface()) | |
203 type = type->designatedInterface(); | |
204 if (type->isString()) | |
205 s = "string"; | |
206 else if (type->isObject()){ | |
207 s = type->name(); | |
208 } else { | |
209 s = type->lookupName(); | |
210 } | |
211 } | |
212 } | |
213 | |
214 return s; | |
215 } | |
216 | |
217 QString DGenerator::argumentString(const AbstractMetaFunction *d_function, | |
218 const AbstractMetaArgument *d_argument, | |
219 uint options) | |
220 { | |
221 QString modified_type = d_function->typeReplaced(d_argument->argumentIndex() + 1); | |
222 QString arg; | |
223 | |
224 AbstractMetaType *type = d_argument->type(); | |
225 // if argument is "QString &" ref attribute needed | |
226 if (type->typeEntry()->isValue() && type->isNativePointer() && type->typeEntry()->name() == "QString") | |
227 arg = "ref "; | |
228 | |
229 if (modified_type.isEmpty()) | |
230 arg += translateType(d_argument->type(), d_function->implementingClass(), (Option) options); | |
231 else | |
232 arg += modified_type.replace('$', '.'); | |
233 | |
234 if ((options & SkipName) == 0) { | |
235 arg += " "; | |
236 arg += d_argument->argumentName(); | |
237 } | |
238 | |
239 if (!d_argument->defaultValueExpression().isEmpty()) // qtd | |
240 arg += " = " + d_argument->defaultValueExpression(); | |
241 | |
242 return arg; | |
243 } | |
244 | |
245 void DGenerator::writeArgument(QTextStream &s, | |
246 const AbstractMetaFunction *d_function, | |
247 const AbstractMetaArgument *d_argument, | |
248 uint options) | |
249 { | |
250 s << argumentString(d_function, d_argument, options); | |
251 } | |
252 | |
253 | |
254 void DGenerator::writeIntegerEnum(QTextStream &s, const AbstractMetaEnum *d_enum) | |
255 { | |
256 const AbstractMetaEnumValueList &values = d_enum->values(); | |
257 | |
258 s << " public static class " << d_enum->name() << "{" << endl; | |
259 for (int i=0; i<values.size(); ++i) { | |
260 AbstractMetaEnumValue *value = values.at(i); | |
261 | |
262 if (d_enum->typeEntry()->isEnumValueRejected(value->name())) | |
263 continue; | |
264 | |
265 if (m_doc_parser) | |
266 s << m_doc_parser->documentation(value); | |
267 | |
268 s << " public static final int " << value->name() << " = " << value->value(); | |
269 s << ";"; | |
270 s << endl; | |
271 } | |
272 | |
273 s << " } // end of enum " << d_enum->name() << endl << endl; | |
274 } | |
275 | |
276 void DGenerator::writeEnumAlias(QTextStream &s, const AbstractMetaEnum *d_enum) | |
277 { | |
278 // aliases for enums to be used in easier way like QFont.Bold instead of QFont.Weight.Bold | |
279 s << QString(" alias %1 %2;").arg(d_enum->typeEntry()->qualifiedTargetLangName()).arg(d_enum->name()) << endl << endl; | |
280 const AbstractMetaEnumValueList &values = d_enum->values(); | |
281 for (int i=0; i<values.size(); ++i) { | |
282 AbstractMetaEnumValue *enum_value = values.at(i); | |
283 | |
284 if (d_enum->typeEntry()->isEnumValueRejected(enum_value->name())) | |
285 continue; | |
286 | |
287 s << QString(" alias %1.%2 %2;").arg(d_enum->typeEntry()->qualifiedTargetLangName()).arg(enum_value->name()) << endl; | |
288 } | |
289 s << endl; | |
290 } | |
291 | |
292 void DGenerator::writeEnum(QTextStream &s, const AbstractMetaEnum *d_enum) | |
293 { | |
294 if (m_doc_parser) { | |
295 s << m_doc_parser->documentation(d_enum); | |
296 } | |
297 | |
298 /* qtd | |
299 | |
300 if (d_enum->typeEntry()->forceInteger()) { | |
301 writeIntegerEnum(s, d_enum); | |
302 return; | |
303 } | |
304 | |
305 // Check if enums in QObjects are declared in the meta object. If not | |
306 if ( (d_enum->enclosingClass()->isQObject() || d_enum->enclosingClass()->isQtNamespace()) | |
307 && !d_enum->hasQEnumsDeclaration()) { | |
308 s << " @QtBlockedEnum" << endl; | |
309 } | |
310 */ | |
311 // Generates Java 1.5 type enums | |
312 s << " public enum " << d_enum->enclosingClass()->name() << "_" << d_enum->name() << " {" << endl; | |
313 const AbstractMetaEnumValueList &values = d_enum->values(); | |
314 EnumTypeEntry *entry = d_enum->typeEntry(); | |
315 | |
316 for (int i=0; i<values.size(); ++i) { | |
317 AbstractMetaEnumValue *enum_value = values.at(i); | |
318 | |
319 if (d_enum->typeEntry()->isEnumValueRejected(enum_value->name())) | |
320 continue; | |
321 | |
322 if (m_doc_parser) | |
323 s << m_doc_parser->documentation(enum_value); | |
324 | |
325 s << " " << enum_value->name() << " = " << enum_value->value(); | |
326 | |
327 if (i != values.size() - 1) { | |
328 AbstractMetaEnumValue *next_value = values.at(i+1); // qtd | |
329 if (!(d_enum->typeEntry()->isEnumValueRejected(next_value->name()) && i == values.size() - 2)) // qtd | |
330 s << "," << endl; | |
331 } | |
332 } | |
333 /* qtd | |
334 if (entry->isExtensible()) | |
335 s << " CustomEnum = 0"; | |
336 */ | |
337 s << endl << INDENT << "}" << endl << endl; // qtd | |
338 | |
339 | |
340 /* qtd s << ";" << endl << endl; | |
341 | |
342 s << " " << d_enum->name() << "(int value) { this.value = value; }" << endl | |
343 << " public int value() { return value; }" << endl | |
344 << endl; | |
345 | |
346 // Write out the createQFlags() function if its a QFlags enum | |
347 if (entry->flags()) { | |
348 FlagsTypeEntry *flags_entry = entry->flags(); | |
349 s << " public static " << flags_entry->targetLangName() << " createQFlags(" | |
350 << entry->targetLangName() << " ... values) {" << endl | |
351 << " return new " << flags_entry->targetLangName() << "(values);" << endl | |
352 << " }" << endl; | |
353 } | |
354 | |
355 // The resolve functions. The public one that returns the right | |
356 // type and an internal one that has a generic signature. Makes it | |
357 // easier to find the right one from JNI. | |
358 s << " public static " << d_enum->name() << " resolve(int value) {" << endl | |
359 << " return (" << d_enum->name() << ") resolve_internal(value);" << endl | |
360 << " }" << endl | |
361 << " private static Object resolve_internal(int value) {" << endl | |
362 << " switch (value) {" << endl; | |
363 | |
364 for (int i=0; i<values.size(); ++i) { | |
365 AbstractMetaEnumValue *e = values.at(i); | |
366 | |
367 if (d_enum->typeEntry()->isEnumValueRejected(e->name())) | |
368 continue; | |
369 | |
370 s << " case " << e->value() << ": return " << e->name() << ";" << endl; | |
371 } | |
372 | |
373 s << " }" << endl; | |
374 | |
375 if (entry->isExtensible()) { | |
376 s << " if (enumCache == null)" << endl | |
377 << " enumCache = new java.util.HashMap<Integer, " << d_enum->name() | |
378 << ">();" << endl | |
379 << " " << d_enum->name() << " e = enumCache.get(value);" << endl | |
380 << " if (e == null) {" << endl | |
381 << " e = (" << d_enum->name() << ") qt.GeneratorUtilities.createExtendedEnum(" | |
382 << "value, CustomEnum.ordinal(), " << d_enum->name() << ".class, CustomEnum.name());" | |
383 << endl | |
384 << " enumCache.put(value, e);" << endl | |
385 << " }" << endl | |
386 << " return e;" << endl; | |
387 } else { | |
388 s << " throw new qt.QNoSuchEnumValueException(value);" << endl; | |
389 } | |
390 | |
391 | |
392 s << " }" << endl; | |
393 | |
394 s << " private final int value;" << endl | |
395 << endl; | |
396 if (entry->isExtensible()) { | |
397 s << " private static java.util.HashMap<Integer, " << d_enum->name() | |
398 << "> enumCache;"; | |
399 } | |
400 s << " }" << endl; | |
401 */ | |
402 // Write out the QFlags if present... | |
403 /* FlagsTypeEntry *flags_entry = entry->flags(); | |
404 if (flags_entry) { | |
405 QString flagsName = flags_entry->targetLangName(); | |
406 s << INDENT << "alias QFlags!(" << d_enum->name() << ") " << flagsName << ";" << endl << endl; | |
407 }*/ | |
408 } | |
409 | |
410 void DGenerator::writePrivateNativeFunction(QTextStream &s, const AbstractMetaFunction *d_function) | |
411 { | |
412 int exclude_attributes = AbstractMetaAttributes::Public | AbstractMetaAttributes::Protected; | |
413 int include_attributes = 0; | |
414 | |
415 if (d_function->isEmptyFunction()) | |
416 exclude_attributes |= AbstractMetaAttributes::Native; | |
417 else | |
418 include_attributes |= AbstractMetaAttributes::Native; | |
419 | |
420 // if (!d_function->isConstructor()) | |
421 // include_attributes |= AbstractMetaAttributes::Static; | |
422 | |
423 writeFunctionAttributes(s, d_function, include_attributes, exclude_attributes, | |
424 EnumAsInts | ExternC | |
425 | (d_function->isEmptyFunction() | |
426 || d_function->isNormal() | |
427 || d_function->isSignal() ? 0 : SkipReturnType)); | |
428 | |
429 if (d_function->isConstructor()) | |
430 s << "void* "; | |
431 | |
432 | |
433 s << d_function->marshalledName(); | |
434 /* qtd | |
435 s << "("; | |
436 | |
437 AbstractMetaArgumentList arguments = d_function->arguments(); | |
438 | |
439 if (!d_function->isStatic() && !d_function->isConstructor()) | |
440 s << "void *__this__nativeId"; | |
441 for (int i=0; i<arguments.count(); ++i) { | |
442 const AbstractMetaArgument *arg = arguments.at(i); | |
443 | |
444 if (!d_function->argumentRemoved(i+1)) { | |
445 if (i > 0 || (!d_function->isStatic() && !d_function->isConstructor())) | |
446 s << ", "; | |
447 | |
448 if (!arg->type()->hasNativeId()) | |
449 writeArgument(s, d_function, arg, EnumAsInts); | |
450 else | |
451 s << "void *" << arg->argumentName(); | |
452 } | |
453 } | |
454 s << ")"; | |
455 */ | |
456 | |
457 CppImplGenerator::writeFinalFunctionArguments(s, d_function, true); // qtd | |
458 | |
459 // Make sure people don't call the private functions | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
460 // qtd remember name QNoImplementationException |
1 | 461 if (d_function->isEmptyFunction()) { |
462 s << endl | |
463 << INDENT << "{" << endl | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
464 << INDENT << " throw new Exception(\"No Implementation Exception\");" << endl |
1 | 465 << INDENT << "}" << endl << endl; |
466 } else { | |
467 s << ";" << endl; | |
468 } | |
469 } | |
470 | |
471 static QString function_call_for_ownership(TypeSystem::Ownership owner) | |
472 { | |
473 if (owner == TypeSystem::CppOwnership) { | |
474 return "__set_native_ownership(true)"; | |
475 } else /* qtd 2 if (owner == TypeSystem::TargetLangOwnership) */ { | |
476 return "__set_native_ownership(false)"; | |
477 }/* else if (owner == TypeSystem::DefaultOwnership) { | |
478 return "__no_real_delete = false"; | |
479 | |
480 } else { | |
481 Q_ASSERT(false); | |
482 return "bogus()"; | |
483 }*/ | |
484 } | |
485 | |
486 void DGenerator::writeOwnershipForContainer(QTextStream &s, TypeSystem::Ownership owner, | |
487 AbstractMetaType *type, const QString &arg_name) | |
488 { | |
489 Q_ASSERT(type->isContainer()); | |
490 | |
491 s << INDENT << "for (" << type->instantiations().at(0)->fullName() << " i : " | |
492 << arg_name << ")" << endl | |
493 << INDENT << " if (i != null) i." << function_call_for_ownership(owner) << ";" << endl; | |
494 | |
495 } | |
496 | |
497 void DGenerator::writeOwnershipForContainer(QTextStream &s, TypeSystem::Ownership owner, | |
498 AbstractMetaArgument *arg) | |
499 { | |
500 writeOwnershipForContainer(s, owner, arg->type(), arg->argumentName()); | |
501 } | |
502 | |
503 static FunctionModificationList get_function_modifications_for_class_hierarchy(const AbstractMetaFunction *d_function) | |
504 { | |
505 FunctionModificationList mods; | |
506 const AbstractMetaClass *cls = d_function->implementingClass(); | |
507 while (cls != 0) { | |
508 mods += d_function->modifications(cls); | |
509 | |
510 if (cls == cls->baseClass()) | |
511 break; | |
512 cls = cls->baseClass(); | |
513 } | |
514 return mods; | |
515 } | |
516 | |
517 void DGenerator::writeInjectedCode(QTextStream &s, const AbstractMetaFunction *d_function, | |
518 CodeSnip::Position position) | |
519 { | |
520 FunctionModificationList mods = get_function_modifications_for_class_hierarchy(d_function); | |
521 foreach (FunctionModification mod, mods) { | |
522 if (mod.snips.count() <= 0) | |
523 continue ; | |
524 | |
525 foreach (CodeSnip snip, mod.snips) { | |
526 if (snip.position != position) | |
527 continue ; | |
528 | |
529 if (snip.language != TypeSystem::TargetLangCode) | |
530 continue ; | |
531 | |
532 QString code; | |
533 QTextStream tmpStream(&code); | |
534 snip.formattedCode(tmpStream, INDENT); | |
535 ArgumentMap map = snip.argumentMap; | |
536 ArgumentMap::iterator it = map.begin(); | |
537 for (;it!=map.end();++it) { | |
538 int pos = it.key() - 1; | |
539 QString meta_name = it.value(); | |
540 | |
541 if (pos >= 0 && pos < d_function->arguments().count()) { | |
542 code = code.replace(meta_name, d_function->arguments().at(pos)->argumentName()); | |
543 } else { | |
544 QString debug = QString("argument map specifies invalid argument index %1" | |
545 "for function '%2'") | |
546 .arg(pos + 1).arg(d_function->name()); | |
547 ReportHandler::warning(debug); | |
548 } | |
549 | |
550 } | |
551 s << code << endl; | |
552 } | |
553 } | |
554 } | |
555 | |
556 | |
557 void DGenerator::writeJavaCallThroughContents(QTextStream &s, const AbstractMetaFunction *d_function, uint attributes) | |
558 { | |
559 Q_UNUSED(attributes); | |
560 writeInjectedCode(s, d_function, CodeSnip::Beginning); | |
561 /* qtd | |
562 if (d_function->implementingClass()->isQObject() | |
563 && !d_function->isStatic() | |
564 && !d_function->isConstructor() | |
565 && d_function->name() != QLatin1String("thread") | |
566 && d_function->name() != QLatin1String("disposeLater")) { | |
567 s << INDENT << "qt.GeneratorUtilities.threadCheck(this);" << endl; | |
568 } | |
569 */ | |
570 AbstractMetaArgumentList arguments = d_function->arguments(); | |
571 | |
572 if (!d_function->isConstructor()) { | |
573 TypeSystem::Ownership owner = d_function->ownership(d_function->implementingClass(), TypeSystem::TargetLangCode, -1); | |
574 if (owner != TypeSystem::InvalidOwnership) | |
575 s << INDENT << "this." << function_call_for_ownership(owner) << ";" << endl; | |
576 } | |
577 | |
578 for (int i=0; i<arguments.count(); ++i) { | |
579 AbstractMetaArgument *arg = arguments.at(i); | |
580 | |
581 if (!d_function->argumentRemoved(i+1)) { | |
582 TypeSystem::Ownership owner = d_function->ownership(d_function->implementingClass(), TypeSystem::TargetLangCode, i+1); | |
583 if (owner != TypeSystem::InvalidOwnership) { | |
584 s << INDENT << "if (" << arg->argumentName() << " !is null) {" << endl; | |
585 { | |
586 Indentation indent(INDENT); | |
587 if (arg->type()->isContainer()) | |
588 ;// qtd2 writeOwnershipForContainer(s, owner, arg); | |
589 else | |
590 s << INDENT << arg->argumentName() << "." << function_call_for_ownership(owner) << ";" << endl; | |
591 } | |
592 s << INDENT << "}" << endl; | |
593 } | |
594 /* | |
595 if (type->isArray()) { | |
596 s << INDENT << "if (" << arg->argumentName() << ".length != " << type->arrayElementCount() << ")" << endl | |
597 << INDENT << " " << "throw new IllegalArgumentException(\"Wrong number of elements in array. Found: \" + " | |
598 << arg->argumentName() << ".length + \", expected: " << type->arrayElementCount() << "\");" | |
599 << endl << endl; | |
600 } | |
601 | |
602 if (type->isEnum()) { | |
603 EnumTypeEntry *et = (EnumTypeEntry *) type->typeEntry(); | |
604 if (et->forceInteger()) { | |
605 if (!et->lowerBound().isEmpty()) { | |
606 s << INDENT << "if (" << arg->argumentName() << " < " << et->lowerBound() << ")" << endl | |
607 << INDENT << " throw new IllegalArgumentException(\"Argument " << arg->argumentName() | |
608 << " is less than lowerbound " << et->lowerBound() << "\");" << endl; | |
609 } | |
610 if (!et->upperBound().isEmpty()) { | |
611 s << INDENT << "if (" << arg->argumentName() << " > " << et->upperBound() << ")" << endl | |
612 << INDENT << " throw new IllegalArgumentException(\"Argument " << arg->argumentName() | |
613 << " is greated than upperbound " << et->upperBound() << "\");" << endl; | |
614 } | |
615 } | |
616 } | |
617 */ | |
618 } | |
619 } | |
620 | |
621 /* qtd2 | |
622 if (!d_function->isConstructor() && !d_function->isStatic()) { | |
623 s << INDENT << "if (nativeId() == 0)" << endl | |
624 << INDENT << " throw new QNoNativeResourcesException(\"Function call on incomplete object of type: \" +getClass().getName());" << endl; | |
625 } | |
626 */ | |
627 for (int i=0; i<arguments.size(); ++i) { | |
628 if (d_function->nullPointersDisabled(d_function->implementingClass(), i + 1)) { | |
629 s << INDENT << "/*if (" << arguments.at(i)->argumentName() << " is null)" << endl | |
630 << INDENT << " throw new NullPointerException(\"Argument '" << arguments.at(i)->argumentName() << "': null not expected.\"); */" << endl; | |
631 } | |
632 } | |
633 | |
634 QList<ReferenceCount> referenceCounts; | |
635 for (int i=0; i<arguments.size() + 1; ++i) { | |
636 referenceCounts = d_function->referenceCounts(d_function->implementingClass(), | |
637 i == 0 ? -1 : i); | |
638 | |
639 foreach (ReferenceCount refCount, referenceCounts) | |
640 writeReferenceCount(s, refCount, i == 0 ? "this" : arguments.at(i-1)->argumentName()); | |
641 } | |
642 | |
643 referenceCounts = d_function->referenceCounts(d_function->implementingClass(), 0); | |
644 AbstractMetaType *return_type = d_function->type(); | |
645 QString new_return_type = QString(d_function->typeReplaced(0)).replace('$', '.'); | |
646 bool has_return_type = new_return_type != "void" | |
647 && (!new_return_type.isEmpty() || return_type != 0); | |
648 // qtd TypeSystem::Ownership owner = d_function->ownership(d_function->implementingClass(), TypeSystem::TargetLangCode, 0); | |
649 | |
650 bool has_code_injections_at_the_end = false; | |
651 FunctionModificationList mods = get_function_modifications_for_class_hierarchy(d_function); | |
652 foreach (FunctionModification mod, mods) { | |
653 foreach (CodeSnip snip, mod.snips) { | |
654 if (snip.position == CodeSnip::End && snip.language == TypeSystem::TargetLangCode) { | |
655 has_code_injections_at_the_end = true; | |
656 break; | |
657 } | |
658 } | |
659 } | |
660 | |
661 // bool needs_return_variable = has_return_type | |
662 // && (owner != TypeSystem::InvalidOwnership || referenceCounts.size() > 0 || has_code_injections_at_the_end); | |
663 | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
664 if(return_type) { // qtd |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
665 if (return_type->isTargetLangString()) |
1 | 666 s << INDENT << "string res;" << endl; |
667 | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
668 if(return_type->name() == "QModelIndex") |
1 | 669 s << INDENT << "QModelIndex res;" << endl; |
81 | 670 else if (return_type->typeEntry()->isStructInD()) |
671 s << INDENT << return_type->name() << " res;" << endl; | |
1 | 672 |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
673 if(return_type->isContainer()) |
1 | 674 s << INDENT << this->translateType(d_function->type(), d_function->ownerClass(), NoOption) << " res;" << endl; |
675 } | |
676 | |
130
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
677 // returning string or a struct |
81 | 678 bool return_in_arg = return_type && (return_type->isTargetLangString() || |
679 return_type->name() == "QModelIndex" || | |
680 return_type->isContainer() || | |
681 return_type->typeEntry()->isStructInD()); | |
682 | |
130
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
683 // bool flag showing if we return value immediately, without any conversions |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
684 // which is commpon for primitive types, initially set up to return_in_arg, because in that case |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
685 // we don't need type conversions |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
686 bool returnImmediately = return_in_arg; |
81 | 687 |
1 | 688 s << INDENT; |
689 if ( (has_return_type && d_function->argumentReplaced(0).isEmpty() ) || d_function->isConstructor()) { //qtd | |
690 if(d_function->type() && d_function->type()->isQObject()) { // qtd | |
691 s << "void *__qt_return_value = "; | |
81 | 692 } else if(return_in_arg) // qtd |
1 | 693 ; |
81 | 694 else if (d_function->isConstructor()) { // qtd |
1 | 695 s << "void* __qt_return_value = "; |
81 | 696 } else if (return_type && return_type->isValue() && !return_type->typeEntry()->isStructInD()) { |
1 | 697 s << "void* __qt_return_value = "; |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
698 } else if (return_type && return_type->isVariant()) { |
1 | 699 s << "void* __qt_return_value = "; |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
700 } else if (return_type && ( return_type->isObject() || |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
701 (return_type->isNativePointer() && return_type->typeEntry()->isValue()) || |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
702 return_type->typeEntry()->isInterface()) ) { |
1 | 703 s << "void* __qt_return_value = "; |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
704 } else if (return_type && return_type->isArray()) { |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
705 s << return_type->arrayElementType()->name() + "* __qt_return_value = "; |
1 | 706 } else { |
130
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
707 returnImmediately = true; |
1 | 708 s << "return "; |
709 } | |
710 | |
711 if (return_type && return_type->isTargetLangEnum()) { | |
712 s << "cast(" << return_type->typeEntry()->qualifiedTargetLangName() << ") "; | |
713 }/* qtd2 flags else if (return_type && return_type->isTargetLangFlags()) { | |
714 s << "new " << return_type->typeEntry()->qualifiedTargetLangName() << "("; | |
715 }*/ | |
716 } | |
717 | |
718 bool useJumpTable = d_function->jumpTableId() != -1; | |
719 if (useJumpTable) { | |
720 // The native function returns the correct type, we only have | |
721 // java.lang.Object so we may have to cast... | |
722 QString signature = JumpTablePreprocessor::signature(d_function); | |
723 | |
724 // printf("return: %s::%s return=%p, replace-value=%s, replace-type=%s signature: %s\n", | |
725 // qPrintable(d_function->ownerClass()->name()), | |
726 // qPrintable(d_function->signature()), | |
727 // return_type, | |
728 // qPrintable(d_function->argumentReplaced(0)), | |
729 // qPrintable(new_return_type), | |
730 // qPrintable(signature)); | |
731 | |
732 if (has_return_type && signature.at(0) == 'L') { | |
733 if (new_return_type.length() > 0) { | |
734 // printf(" ---> replace-type: %s\n", qPrintable(new_return_type)); | |
735 s << "(" << new_return_type << ") "; | |
736 } else if (d_function->argumentReplaced(0).isEmpty()) { | |
737 // printf(" ---> replace-value\n"); | |
738 s << "(" << translateType(return_type, d_function->implementingClass()) << ") "; | |
739 } | |
740 } | |
741 | |
742 s << "JTbl." << JumpTablePreprocessor::signature(d_function) << "(" | |
743 << d_function->jumpTableId() << ", "; | |
744 | |
745 // Constructors and static functions don't have native id, but | |
746 // the functions expect them anyway, hence add '0'. Normal | |
747 // functions get their native ids added just below... | |
748 if (d_function->isConstructor() || d_function->isStatic()) | |
749 s << "0, "; | |
750 | |
751 } else { | |
752 /* qtd if (attributes & SuperCall) { | |
753 s << "super."; | |
754 }*/ | |
755 s << d_function->marshalledName() << "("; | |
756 } | |
757 | |
758 if (!d_function->isConstructor() && !d_function->isStatic()) | |
759 s << "nativeId"; | |
760 | |
761 if (d_function->isConstructor() && | |
762 ( d_function->implementingClass()->hasVirtualFunctions() | |
763 || d_function->implementingClass()->typeEntry()->isObject() ) ) { // qtd | |
764 s << "cast(void*) this"; | |
765 if (arguments.count() > 0) | |
766 s << ", "; | |
767 } | |
768 | |
769 if(return_in_arg) { // qtd | |
770 if (!d_function->isStatic() && !d_function->isConstructor()) // qtd | |
771 s << ", "; | |
772 s << "&res"; | |
773 } | |
774 | |
775 for (int i=0; i<arguments.count(); ++i) { | |
776 const AbstractMetaArgument *arg = arguments.at(i); | |
777 const AbstractMetaType *type = arg->type(); | |
778 const TypeEntry *te = type->typeEntry(); | |
779 | |
780 if (!d_function->argumentRemoved(i+1)) { | |
781 if (i > 0 || (!d_function->isStatic() && !d_function->isConstructor()) || return_in_arg) // qtd | |
782 s << ", "; | |
783 | |
784 // qtd | |
785 QString modified_type = d_function->typeReplaced(arg->argumentIndex() + 1); | |
786 if (!modified_type.isEmpty()) | |
787 modified_type = modified_type.replace('$', '.'); | |
788 | |
789 QString arg_name = arg->argumentName(); | |
790 | |
791 if (type->isVariant()) | |
792 s << arg_name << " is null ? null : " << arg_name << ".nativeId"; | |
793 else if (te->designatedInterface()) | |
794 s << arg_name << " is null ? null : " << arg_name << ".__ptr_" << te->designatedInterface()->name(); | |
795 else if (modified_type == "string" /* && type->fullName() == "char" */) { | |
796 s << "toStringz(" << arg_name << ")"; | |
81 | 797 } else if (type->isArray()) { |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
798 s << arg_name << ".ptr"; |
81 | 799 } else if(type->isContainer()) { |
1 | 800 const ContainerTypeEntry *cte = |
801 static_cast<const ContainerTypeEntry *>(te); | |
802 if(isLinearContainer(cte)) | |
803 s << QString("%1.ptr, %1.length").arg(arg_name); | |
81 | 804 } else if (type->typeEntry()->qualifiedCppName() == "QChar") { |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
805 s << arg_name; |
81 | 806 } else if (type->isTargetLangString() || (te && te->qualifiedCppName() == "QString")) { |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
807 s << arg_name; |
81 | 808 } else if (type->isTargetLangEnum() || type->isTargetLangFlags()) { |
1 | 809 s << arg_name; |
810 // qtd s << arg->argumentName() << ".value()"; | |
811 } else if (!type->hasNativeId() && !(te->isValue() && type->isNativePointer())) { // qtd2 hack for QStyleOption not being a nativeId based for some reason | |
812 s << arg_name; | |
813 } else if (te->isStructInD()) { | |
814 s << arg_name; | |
815 } else { | |
816 bool force_abstract = te->isComplex() && (((static_cast<const ComplexTypeEntry *>(te))->typeFlags() & ComplexTypeEntry::ForceAbstract) != 0); | |
817 if (!force_abstract) { | |
818 s << arg_name << " is null ? null : "; | |
819 } // else if (value type is abstract) then we will get a null pointer exception, which is all right | |
820 | |
821 s << arg_name << ".nativeId"; | |
822 } | |
823 } | |
824 } | |
825 | |
826 if (useJumpTable) { | |
827 if ((!d_function->isConstructor() && !d_function->isStatic()) || arguments.size() > 0) | |
828 s << ", "; | |
829 | |
830 if (d_function->isStatic()) | |
831 s << "null"; | |
832 else | |
833 s << "this"; | |
834 } | |
835 | |
836 s << ")"; | |
837 | |
838 if ( !d_function->argumentReplaced(0).isEmpty() ) { | |
839 s << ";" << endl; | |
840 s << INDENT << "return " << d_function->argumentReplaced(0) << ";" << endl; | |
841 return; | |
842 } | |
843 | |
844 // qtd2 if (return_type && (/* qtdreturn_type->isTargetLangEnum() ||*/ return_type->isTargetLangFlags())) | |
845 // s << ")"; | |
846 | |
847 s << ";" << endl; | |
848 | |
849 /* qtd2 | |
850 if (needs_return_variable) { | |
851 if (owner != TypeSystem::InvalidOwnership) { | |
852 s << INDENT << "if (__qt_return_value != null) {" << endl; | |
853 if (return_type->isContainer()) | |
854 writeOwnershipForContainer(s, owner, return_type, "__qt_return_value"); | |
855 else | |
856 s << INDENT << " __qt_return_value." << function_call_for_ownership(owner) << ";" << endl; | |
857 s << INDENT << "}" << endl; | |
858 } | |
859 s << INDENT << "return __qt_return_value;" << endl; | |
860 } | |
861 */ | |
862 if (d_function->isConstructor()) { | |
863 TypeSystem::Ownership owner = d_function->ownership(d_function->implementingClass(), TypeSystem::TargetLangCode, -1); | |
864 if (owner != TypeSystem::InvalidOwnership && d_function->isConstructor()) | |
865 s << INDENT << "this." << function_call_for_ownership(owner) << ";" << endl; | |
866 } | |
867 | |
130
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
868 // return value marschalling |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
869 if(return_type) { |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
870 if (!returnImmediately && !d_function->storeResult()) { |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
871 s << INDENT; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
872 QString modified_type = d_function->typeReplaced(0); |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
873 if (modified_type.isEmpty()) |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
874 s << translateType(d_function->type(), d_function->implementingClass()); |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
875 else |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
876 s << modified_type.replace('$', '.'); |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
877 s << " __d_return_value = "; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
878 } |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
879 |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
880 if ( ( has_return_type && d_function->argumentReplaced(0).isEmpty() )) // qtd |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
881 if(return_type->isQObject()) |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
882 s << "qtd_" << return_type->name() << "_from_ptr(__qt_return_value);" << endl; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
883 |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
884 if (return_type->isValue() && !return_type->typeEntry()->isStructInD()) |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
885 s << "new " << return_type->name() << "(__qt_return_value, false);" << endl; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
886 |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
887 if (return_type->isVariant()) |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
888 s << "new QVariant(__qt_return_value, false);" << endl; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
889 |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
890 if (return_type->isNativePointer() && return_type->typeEntry()->isValue()) |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
891 s << "new " << return_type->name() << "(__qt_return_value, true);" << endl; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
892 |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
893 if (return_type->isObject()) { |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
894 if(d_function->storeResult()) |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
895 s << INDENT << QString("__m_%1.nativeId = __qt_return_value;").arg(d_function->name()) << endl |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
896 << INDENT << QString("return __m_%1;").arg(d_function->name()) << endl; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
897 else |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
898 s << "qtd_" << return_type->name() << "_from_ptr(__qt_return_value);" << endl; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
899 s << endl; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
900 } |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
901 |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
902 if (return_type->isArray()) { |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
903 s << "__qt_return_value[0 .. " << return_type->arrayElementCount() << "];" << endl; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
904 } |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
905 |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
906 foreach (ReferenceCount referenceCount, referenceCounts) { |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
907 writeReferenceCount(s, referenceCount, "__d_return_value"); |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
908 } |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
909 |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
910 if (!returnImmediately && !d_function->storeResult()) |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
911 s << INDENT << "return __d_return_value;" << endl; |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
912 } |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
913 writeInjectedCode(s, d_function, CodeSnip::End); |
7ae9bc9d6935
reference counting for return values. applied to QMenu
eldar
parents:
112
diff
changeset
|
914 |
1 | 915 if(return_in_arg) // qtd |
916 s << INDENT << "return res;" << endl; | |
917 } | |
918 | |
919 void DGenerator::retrieveModifications(const AbstractMetaFunction *d_function, | |
920 const AbstractMetaClass *d_class, | |
921 uint *exclude_attributes, | |
922 uint *include_attributes) const | |
923 { | |
924 FunctionModificationList mods = d_function->modifications(d_class); | |
925 // printf("name: %s has %d mods\n", qPrintable(d_function->signature()), mods.size()); | |
926 foreach (FunctionModification mod, mods) { | |
927 if (mod.isAccessModifier()) { | |
928 // printf(" -> access mod to %x\n", mod.modifiers); | |
929 *exclude_attributes |= AbstractMetaAttributes::Public | |
930 | AbstractMetaAttributes::Protected | |
931 | AbstractMetaAttributes::Private | |
932 | AbstractMetaAttributes::Friendly; | |
933 | |
934 if (mod.isPublic()) | |
935 *include_attributes |= AbstractMetaAttributes::Public; | |
936 else if (mod.isProtected()) | |
937 *include_attributes |= AbstractMetaAttributes::Protected; | |
938 else if (mod.isPrivate()) | |
939 *include_attributes |= AbstractMetaAttributes::Private; | |
940 else if (mod.isFriendly()) | |
941 *include_attributes |= AbstractMetaAttributes::Friendly; | |
942 } | |
943 | |
944 if (mod.isFinal()) { | |
945 *include_attributes |= AbstractMetaAttributes::FinalInTargetLang; | |
946 } else if (mod.isNonFinal()) { | |
947 *exclude_attributes |= AbstractMetaAttributes::FinalInTargetLang; | |
948 } | |
949 } | |
950 | |
951 *exclude_attributes &= ~(*include_attributes); | |
952 } | |
953 | |
954 QString DGenerator::functionSignature(const AbstractMetaFunction *d_function, | |
955 uint included_attributes, uint excluded_attributes, | |
956 Option option, | |
957 int arg_count) | |
958 { | |
959 AbstractMetaArgumentList arguments = d_function->arguments(); | |
960 int argument_count = arg_count < 0 ? arguments.size() : arg_count; | |
961 | |
962 QString result; | |
963 QTextStream s(&result); | |
964 QString functionName = d_function->isConstructor() ? "this" : d_function->name(); // qtd | |
965 // The actual function | |
966 if (!(d_function->isEmptyFunction() || d_function->isNormal() || d_function->isSignal())) | |
967 option = Option(option | SkipReturnType); | |
968 writeFunctionAttributes(s, d_function, included_attributes, excluded_attributes, option); | |
969 | |
970 s << functionName << "("; | |
971 writeFunctionArguments(s, d_function, argument_count, option); | |
972 s << ")"; | |
973 | |
974 return result; | |
975 } | |
976 | |
977 void DGenerator::setupForFunction(const AbstractMetaFunction *d_function, | |
978 uint *included_attributes, | |
979 uint *excluded_attributes) const | |
980 { | |
981 *excluded_attributes |= d_function->ownerClass()->isInterface() || d_function->isConstructor() | |
982 ? AbstractMetaAttributes::Native | AbstractMetaAttributes::Final | |
983 : 0; | |
984 if (d_function->ownerClass()->isInterface()) | |
985 *excluded_attributes |= AbstractMetaAttributes::Abstract; | |
986 if (d_function->needsCallThrough()) | |
987 *excluded_attributes |= AbstractMetaAttributes::Native; | |
988 | |
989 const AbstractMetaClass *d_class = d_function->ownerClass(); | |
990 retrieveModifications(d_function, d_class, excluded_attributes, included_attributes); | |
991 } | |
992 | |
993 void DGenerator::writeReferenceCount(QTextStream &s, const ReferenceCount &refCount, | |
994 const QString &argumentName) | |
995 { | |
996 if (refCount.action == ReferenceCount::Ignore) | |
997 return; | |
998 | |
999 QString refCountVariableName = refCount.variableName; | |
1000 if (!refCount.declareVariable.isEmpty() && refCount.action != ReferenceCount::Set) { | |
1001 s << INDENT << "auto __rcTmp = " << refCountVariableName << ";" << endl; | |
1002 refCountVariableName = "__rcTmp"; | |
1003 } | |
1004 | |
1005 if (refCount.action != ReferenceCount::Set) { | |
1006 s << INDENT << "if (" << argumentName << " !is null"; | |
1007 | |
1008 if (!refCount.conditional.isEmpty()) | |
1009 s << " && " << refCount.conditional; | |
1010 | |
1011 s << ") {" << endl; | |
1012 } else { | |
1013 if (!refCount.conditional.isEmpty()) | |
1014 s << INDENT << "if (" << refCount.conditional << ") "; | |
1015 s << INDENT << "{" << endl; | |
1016 } | |
1017 | |
1018 { | |
1019 Indentation indent(INDENT); | |
1020 switch (refCount.action) { | |
1021 case ReferenceCount::Add: | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1022 s << INDENT << refCountVariableName << " ~= cast(Object) " << argumentName << ";" << endl; |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1023 break; |
1 | 1024 case ReferenceCount::AddAll: |
1025 s << INDENT << refCountVariableName << " ~= " << argumentName << ";" << endl; | |
1026 break; | |
1027 case ReferenceCount::Remove: | |
1028 s << INDENT << "remove(" << refCountVariableName | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1029 << ", cast(Object) " << argumentName << ");" << endl; |
1 | 1030 break; |
1031 case ReferenceCount::Set: | |
1032 { | |
1033 if (refCount.declareVariable.isEmpty()) | |
1034 s << INDENT << refCount.variableName << " = cast(Object) " << argumentName << ";" << endl; | |
1035 else | |
1036 s << INDENT << refCountVariableName << " = cast(Object) " << argumentName << ";" << endl; | |
1037 } | |
1038 default: | |
1039 break; | |
1040 }; | |
1041 } | |
1042 s << INDENT << "}" << endl; | |
1043 } | |
1044 | |
1045 void DGenerator::writeFunction(QTextStream &s, const AbstractMetaFunction *d_function, | |
1046 uint included_attributes, uint excluded_attributes) | |
1047 { | |
1048 s << endl; | |
1049 | |
1050 if (d_function->isModifiedRemoved(TypeSystem::TargetLangCode)) | |
1051 return ; | |
1052 QString functionName = d_function->name(); | |
1053 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
1054 | |
1055 if (!d_function->ownerClass()->isInterface()) { | |
1056 // qtd2 writeEnumOverload(s, d_function, included_attributes, excluded_attributes); | |
1057 // qtd writeFunctionOverloads(s, d_function, included_attributes, excluded_attributes); | |
1058 } | |
1059 /* qtd | |
1060 static QRegExp regExp("^(insert|set|take|add|remove|install).*"); | |
1061 | |
1062 if (regExp.exactMatch(d_function->name())) { | |
1063 AbstractMetaArgumentList arguments = d_function->arguments(); | |
1064 | |
1065 const AbstractMetaClass *c = d_function->implementingClass(); | |
1066 bool hasObjectTypeArgument = false; | |
1067 foreach (AbstractMetaArgument *argument, arguments) { | |
1068 TypeSystem::Ownership d_ownership = d_function->ownership(c, TypeSystem::TargetLangCode, argument->argumentIndex()+1); | |
1069 TypeSystem::Ownership shell_ownership = d_function->ownership(c, TypeSystem::ShellCode, argument->argumentIndex()+1); | |
1070 | |
1071 if (argument->type()->typeEntry()->isObject() | |
1072 && d_ownership == TypeSystem::InvalidOwnership | |
1073 && shell_ownership == TypeSystem::InvalidOwnership) { | |
1074 hasObjectTypeArgument = true; | |
1075 break; | |
1076 } | |
1077 } | |
1078 | |
1079 if (hasObjectTypeArgument | |
1080 && !d_function->isAbstract() | |
1081 && d_function->referenceCounts(d_function->implementingClass()).size() == 0) { | |
1082 m_reference_count_candidate_functions.append(d_function); | |
1083 } | |
1084 } | |
1085 | |
1086 | |
1087 if (m_doc_parser) { | |
1088 QString signature = functionSignature(d_function, | |
1089 included_attributes | NoBlockedSlot, | |
1090 excluded_attributes); | |
1091 s << m_doc_parser->documentationForFunction(signature) << endl; | |
1092 } | |
1093 | |
1094 const QPropertySpec *spec = d_function->propertySpec(); | |
1095 if (spec && d_function->modifiedName() == d_function->originalName()) { | |
1096 if (d_function->isPropertyReader()) { | |
1097 s << " @qt.QtPropertyReader(name=\"" << spec->name() << "\")" << endl; | |
1098 if (!spec->designable().isEmpty()) | |
1099 s << " @qt.QtPropertyDesignable(\"" << spec->designable() << "\")" << endl; | |
1100 } else if (d_function->isPropertyWriter()) { | |
1101 s << " @qt.QtPropertyWriter(name=\"" << spec->name() << "\")" << endl; | |
1102 } else if (d_function->isPropertyResetter()) { | |
1103 s << " @qt.QtPropertyResetter(name=\"" << spec->name() << "\")" | |
1104 << endl; | |
1105 } | |
1106 } | |
1107 */ | |
1108 s << functionSignature(d_function, included_attributes, excluded_attributes); | |
1109 | |
1110 if (d_function->isConstructor()) { | |
1111 writeConstructorContents(s, d_function); | |
1112 } else if (d_function->needsCallThrough() || d_function->isStatic()) { // qtd | |
1113 if (d_function->isAbstract()) { | |
1114 s << ";" << endl; | |
1115 } else { | |
1116 s << " {" << endl; | |
1117 { | |
1118 Indentation indent(INDENT); | |
1119 writeJavaCallThroughContents(s, d_function); | |
1120 } | |
1121 s << INDENT << "}" << endl; | |
1122 } | |
1123 | |
1124 /* qtd | |
1125 if (d_function->jumpTableId() == -1) { | |
1126 writePrivateNativeFunction(s, d_function); | |
1127 } | |
1128 */ | |
1129 } else { | |
1130 s << ";" << endl; | |
1131 } | |
1132 } | |
1133 | |
1134 static void write_equals_parts(QTextStream &s, const AbstractMetaFunctionList &lst, char prefix, bool *first) { | |
1135 foreach (AbstractMetaFunction *f, lst) { | |
1136 AbstractMetaArgument *arg = f->arguments().at(0); | |
1137 QString type = f->typeReplaced(1); | |
1138 if (type.isEmpty()) | |
1139 type = arg->type()->typeEntry()->qualifiedTargetLangName(); | |
1140 s << INDENT << (*first ? "if" : "else if") << " (other instanceof " << type << ")" << endl | |
1141 << INDENT << " return "; | |
1142 if (prefix != 0) s << prefix; | |
1143 s << f->name() << "((" << type << ") other);" << endl; | |
1144 *first = false; | |
1145 } | |
1146 } | |
1147 | |
1148 static void write_compareto_parts(QTextStream &s, const AbstractMetaFunctionList &lst, int value, bool *first) { | |
1149 foreach (AbstractMetaFunction *f, lst) { | |
1150 AbstractMetaArgument *arg = f->arguments().at(0); | |
1151 QString type = f->typeReplaced(1); | |
1152 if (type.isEmpty()) | |
1153 type = arg->type()->typeEntry()->qualifiedTargetLangName(); | |
1154 s << INDENT << (*first ? "if" : "else if") << " (other instanceof " << type << ") {" << endl | |
1155 << INDENT << " if (" << f->name() << "((" << type << ") other)) return " << value << ";" << endl | |
1156 << INDENT << " else return " << -value << ";" << endl | |
1157 << INDENT << "}" << endl; | |
1158 *first = false; | |
1159 } | |
1160 s << INDENT << "throw new ClassCastException();" << endl; | |
1161 } | |
1162 | |
1163 bool DGenerator::isComparable(const AbstractMetaClass *cls) const | |
1164 { | |
1165 AbstractMetaFunctionList eq_functions = cls->equalsFunctions(); | |
1166 AbstractMetaFunctionList neq_functions = cls->notEqualsFunctions(); | |
1167 | |
1168 // Write the comparable functions | |
1169 AbstractMetaFunctionList ge_functions = cls->greaterThanFunctions(); | |
1170 AbstractMetaFunctionList geq_functions = cls->greaterThanEqFunctions(); | |
1171 AbstractMetaFunctionList le_functions = cls->lessThanFunctions(); | |
1172 AbstractMetaFunctionList leq_functions = cls->lessThanEqFunctions(); | |
1173 | |
1174 bool hasEquals = eq_functions.size() || neq_functions.size(); | |
1175 bool isComparable = hasEquals | |
1176 ? ge_functions.size() || geq_functions.size() || le_functions.size() || leq_functions.size() | |
1177 : geq_functions.size() == 1 && leq_functions.size() == 1; | |
1178 | |
1179 return isComparable; | |
1180 } | |
1181 | |
1182 | |
1183 void DGenerator::writeJavaLangObjectOverrideFunctions(QTextStream &s, | |
1184 const AbstractMetaClass *cls) | |
1185 { | |
1186 AbstractMetaFunctionList eq_functions = cls->equalsFunctions(); | |
1187 AbstractMetaFunctionList neq_functions = cls->notEqualsFunctions(); | |
1188 | |
1189 if (eq_functions.size() || neq_functions.size()) { | |
1190 s << endl | |
1191 << INDENT << "@SuppressWarnings(\"unchecked\")" << endl | |
1192 << INDENT << "@Override" << endl | |
1193 << INDENT << "public boolean equals(Object other) {" << endl; | |
1194 bool first = true; | |
1195 write_equals_parts(s, eq_functions, (char) 0, &first); | |
1196 write_equals_parts(s, neq_functions, '!', &first); | |
1197 s << INDENT << " return false;" << endl | |
1198 << INDENT << "}" << endl << endl; | |
1199 } | |
1200 | |
1201 // Write the comparable functions | |
1202 AbstractMetaFunctionList ge_functions = cls->greaterThanFunctions(); | |
1203 AbstractMetaFunctionList geq_functions = cls->greaterThanEqFunctions(); | |
1204 AbstractMetaFunctionList le_functions = cls->lessThanFunctions(); | |
1205 AbstractMetaFunctionList leq_functions = cls->lessThanEqFunctions(); | |
1206 | |
1207 bool hasEquals = eq_functions.size() || neq_functions.size(); | |
1208 bool comparable = isComparable(cls); | |
1209 if (comparable) { | |
1210 s << INDENT << "public int compareTo(Object other) {" << endl; | |
1211 { | |
1212 Indentation indent(INDENT); | |
1213 if (hasEquals) { | |
1214 s << INDENT << "if (equals(other)) return 0;" << endl; | |
1215 bool first = false; | |
1216 if (le_functions.size()) { | |
1217 write_compareto_parts(s, le_functions, -1, &first); | |
1218 } else if (ge_functions.size()) { | |
1219 write_compareto_parts(s, ge_functions, 1, &first); | |
1220 } else if (leq_functions.size()) { | |
1221 write_compareto_parts(s, leq_functions, -1, &first); | |
1222 } else if (geq_functions.size()) { | |
1223 write_compareto_parts(s, geq_functions, 1, &first); | |
1224 } | |
1225 | |
1226 } else if (le_functions.size() == 1) { | |
1227 QString className = cls->typeEntry()->qualifiedTargetLangName(); | |
1228 s << INDENT << "if (operator_less((" << className << ") other)) return -1;" << endl | |
1229 << INDENT << "else if (((" << className << ") other).operator_less(this)) return 1;" << endl | |
1230 << INDENT << "else return 0;" << endl; | |
1231 | |
1232 } else if (geq_functions.size() == 1 && leq_functions.size()) { | |
1233 QString className = cls->typeEntry()->qualifiedTargetLangName(); | |
1234 s << INDENT << "boolean less = operator_less_or_equal((" << className << ") other);" << endl | |
1235 << INDENT << "boolean greater = operator_greater_or_equal((" << className << ") other);" << endl | |
1236 << INDENT << "if (less && greater) return 0;" << endl | |
1237 << INDENT << "else if (less) return -1;" << endl | |
1238 << INDENT << "else return 1;" << endl; | |
1239 } | |
1240 } | |
1241 | |
1242 s << INDENT << "}" << endl; | |
1243 } | |
1244 | |
1245 | |
1246 if (cls->hasHashFunction()) { | |
1247 AbstractMetaFunctionList hashcode_functions = cls->queryFunctionsByName("hashCode"); | |
1248 bool found = false; | |
1249 foreach (const AbstractMetaFunction *function, hashcode_functions) { | |
1250 if (function->actualMinimumArgumentCount() == 0) { | |
1251 found = true; | |
1252 break; | |
1253 } | |
1254 } | |
1255 | |
1256 if (!found) { | |
1257 s << endl | |
1258 << INDENT << "@Override" << endl | |
1259 << INDENT << "public int hashCode() {" << endl | |
1260 << INDENT << " if (nativeId() == 0)" << endl | |
1261 << INDENT << " throw new QNoNativeResourcesException(\"Function call on incomplete object of type: \" +getClass().getName());" << endl | |
1262 << INDENT << " return __qt_hashCode(nativeId());" << endl | |
1263 << INDENT << "}" << endl | |
1264 << INDENT << "native int __qt_hashCode(long __this_nativeId);" << endl; | |
1265 } | |
1266 } | |
1267 | |
1268 // Qt has a standard toString() conversion in QVariant? | |
1269 QVariant::Type type = QVariant::nameToType(cls->qualifiedCppName().toLatin1()); | |
1270 if (QVariant(type).canConvert(QVariant::String) && !cls->hasToStringCapability()) { | |
1271 AbstractMetaFunctionList tostring_functions = cls->queryFunctionsByName("toString"); | |
1272 bool found = false; | |
1273 foreach (const AbstractMetaFunction *function, tostring_functions) { | |
1274 if (function->actualMinimumArgumentCount() == 0) { | |
1275 found = true; | |
1276 break; | |
1277 } | |
1278 } | |
1279 | |
1280 if (!found) { | |
1281 s << endl | |
1282 << INDENT << "@Override" << endl | |
1283 << INDENT << "public String toString() {" << endl | |
1284 << INDENT << " if (nativeId() == 0)" << endl | |
1285 << INDENT << " throw new QNoNativeResourcesException(\"Function call on incomplete object of type: \" +getClass().getName());" << endl | |
1286 << INDENT << " return __qt_toString(nativeId());" << endl | |
1287 << INDENT << "}" << endl | |
1288 << INDENT << "native String __qt_toString(long __this_nativeId);" << endl; | |
1289 } | |
1290 } | |
1291 } | |
1292 | |
1293 void DGenerator::writeEnumOverload(QTextStream &s, const AbstractMetaFunction *d_function, | |
1294 uint include_attributes, uint exclude_attributes) | |
1295 { | |
1296 AbstractMetaArgumentList arguments = d_function->arguments(); | |
1297 | |
1298 if ((d_function->implementingClass() != d_function->declaringClass()) | |
1299 || ((!d_function->isNormal() && !d_function->isConstructor()) || d_function->isEmptyFunction() || d_function->isAbstract())) { | |
1300 return ; | |
1301 } | |
1302 | |
1303 | |
1304 int option = 0; | |
1305 if (d_function->isConstructor()) | |
1306 option = Option(option | SkipReturnType); | |
1307 else | |
1308 include_attributes |= AbstractMetaAttributes::FinalInTargetLang; | |
1309 | |
1310 int generate_enum_overload = -1; | |
1311 for (int i=0; i<arguments.size(); ++i) | |
1312 generate_enum_overload = arguments.at(i)->type()->isTargetLangFlags() ? i : -1; | |
1313 | |
1314 if (generate_enum_overload >= 0) { | |
1315 if (m_doc_parser) { | |
1316 // steal documentation from main function | |
1317 QString signature = functionSignature(d_function, | |
1318 include_attributes | NoBlockedSlot, | |
1319 exclude_attributes); | |
1320 s << m_doc_parser->documentationForFunction(signature) << endl; | |
1321 } | |
1322 | |
1323 s << endl; | |
1324 | |
1325 writeFunctionAttributes(s, d_function, include_attributes, exclude_attributes, option); | |
1326 s << d_function->name() << "("; | |
1327 if (generate_enum_overload > 0) { | |
1328 writeFunctionArguments(s, d_function, generate_enum_overload); | |
1329 s << ", "; | |
1330 } | |
1331 | |
1332 // Write the ellipsis convenience argument | |
1333 AbstractMetaArgument *affected_arg = arguments.at(generate_enum_overload); | |
1334 EnumTypeEntry *originator = ((FlagsTypeEntry *)affected_arg->type()->typeEntry())->originator(); | |
1335 | |
1336 s << originator->javaPackage() << "." << originator->javaQualifier() << "." << originator->targetLangName() | |
1337 << " ... " << affected_arg->argumentName() << ") {" << endl; | |
1338 | |
1339 s << " "; | |
1340 QString new_return_type = d_function->typeReplaced(0); | |
1341 if (new_return_type != "void" && (!new_return_type.isEmpty() || d_function->type() != 0)) | |
1342 s << "return "; | |
1343 | |
1344 if (d_function->isConstructor()) { | |
1345 s << "this"; | |
1346 } else { | |
1347 if (d_function->isStatic()) | |
1348 s << d_function->implementingClass()->fullName() << "."; | |
1349 else | |
1350 s << "this."; | |
1351 s << d_function->name(); | |
1352 } | |
1353 | |
1354 s << "("; | |
1355 for (int i=0; i<generate_enum_overload; ++i) { | |
1356 s << arguments.at(i)->argumentName() << ", "; | |
1357 } | |
1358 s << "new " << affected_arg->type()->fullName() << "(" << affected_arg->argumentName() << "));" << endl | |
1359 << " }" << endl; | |
1360 } | |
1361 } | |
1362 | |
1363 void DGenerator::writeInstantiatedType(QTextStream &s, const AbstractMetaType *abstractMetaType) const | |
1364 { | |
1365 Q_ASSERT(abstractMetaType != 0); | |
1366 | |
1367 const TypeEntry *type = abstractMetaType->typeEntry(); | |
1368 s << type->qualifiedTargetLangName(); | |
1369 | |
1370 if (abstractMetaType->hasInstantiations()) { | |
1371 s << "<"; | |
1372 QList<AbstractMetaType *> instantiations = abstractMetaType->instantiations(); | |
1373 for(int i=0; i<instantiations.size(); ++i) { | |
1374 if (i > 0) | |
1375 s << ", "; | |
1376 | |
1377 writeInstantiatedType(s, instantiations.at(i)); | |
1378 } | |
1379 s << ">"; | |
1380 } | |
1381 } | |
1382 | |
1383 void DGenerator::writeFunctionOverloads(QTextStream &s, const AbstractMetaFunction *d_function, | |
1384 uint include_attributes, uint exclude_attributes) | |
1385 { | |
1386 AbstractMetaArgumentList arguments = d_function->arguments(); | |
1387 int argument_count = arguments.size(); | |
1388 | |
1389 // We only create the overloads for the class that actually declares the function | |
1390 // unless this is an interface, in which case we create the overloads for all | |
1391 // classes that directly implement the interface. | |
1392 const AbstractMetaClass *decl_class = d_function->declaringClass(); | |
1393 if (decl_class->isInterface()) { | |
1394 AbstractMetaClassList interfaces = d_function->implementingClass()->interfaces(); | |
1395 foreach (AbstractMetaClass *iface, interfaces) { | |
1396 if (iface == decl_class) { | |
1397 decl_class = d_function->implementingClass(); | |
1398 break; | |
1399 } | |
1400 } | |
1401 } | |
1402 if (decl_class != d_function->implementingClass()) | |
1403 return; | |
1404 | |
1405 // Figure out how many functions we need to write out, | |
1406 // One extra for each default argument. | |
1407 int overload_count = 0; | |
1408 uint excluded_attributes = AbstractMetaAttributes::Abstract | |
1409 | AbstractMetaAttributes::Native | |
1410 | exclude_attributes; | |
1411 uint included_attributes = (d_function->isConstructor() ? 0 : AbstractMetaAttributes::Final) | include_attributes; | |
1412 | |
1413 for (int i=0; i<argument_count; ++i) { | |
1414 if (!arguments.at(i)->defaultValueExpression().isEmpty() && !d_function->argumentRemoved(i+1)) | |
1415 ++overload_count; | |
1416 } | |
1417 Q_ASSERT(overload_count <= argument_count); | |
1418 for (int i=0; i<overload_count; ++i) { | |
1419 int used_arguments = argument_count - i - 1; | |
1420 | |
1421 QString signature = functionSignature(d_function, included_attributes, | |
1422 excluded_attributes, | |
1423 d_function->isEmptyFunction() | |
1424 || d_function->isNormal() | |
1425 || d_function->isSignal() ? NoOption | |
1426 : SkipReturnType, | |
1427 used_arguments); | |
1428 | |
1429 s << endl; | |
1430 if (m_doc_parser) { | |
1431 s << m_doc_parser->documentationForFunction(signature) << endl; | |
1432 } | |
1433 | |
1434 s << signature << " {\n "; | |
1435 QString new_return_type = d_function->typeReplaced(0); | |
1436 if (new_return_type != "void" && (!new_return_type.isEmpty() || d_function->type())) | |
1437 s << "return "; | |
1438 if (d_function->isConstructor()) | |
1439 s << "this"; | |
1440 else | |
1441 s << d_function->name(); | |
1442 s << "("; | |
1443 | |
1444 int written_arguments = 0; | |
1445 for (int j=0; j<argument_count; ++j) { | |
1446 if (!d_function->argumentRemoved(j+1)) { | |
1447 if (written_arguments++ > 0) | |
1448 s << ", "; | |
1449 | |
1450 if (j < used_arguments) { | |
1451 s << arguments.at(j)->argumentName(); | |
1452 } else { | |
1453 AbstractMetaType *arg_type = 0; | |
1454 QString modified_type = d_function->typeReplaced(j+1); | |
1455 if (modified_type.isEmpty()) { | |
1456 arg_type = arguments.at(j)->type(); | |
1457 if (arg_type->isNativePointer()) { | |
1458 s << "(qt.QNativePointer)"; | |
1459 } else { | |
1460 const AbstractMetaType *abstractMetaType = arguments.at(j)->type(); | |
1461 const TypeEntry *type = abstractMetaType->typeEntry(); | |
1462 if (type->designatedInterface()) | |
1463 type = type->designatedInterface(); | |
1464 if (!type->isEnum() && !type->isFlags()) { | |
1465 s << "("; | |
1466 writeInstantiatedType(s, abstractMetaType); | |
1467 s << ")"; | |
1468 } | |
1469 } | |
1470 } else { | |
1471 s << "(" << modified_type.replace('$', '.') << ")"; | |
1472 } | |
1473 | |
1474 QString defaultExpr = arguments.at(j)->defaultValueExpression(); | |
1475 | |
1476 int pos = defaultExpr.indexOf("."); | |
1477 if (pos > 0) { | |
1478 QString someName = defaultExpr.left(pos); | |
1479 ComplexTypeEntry *ctype = | |
1480 TypeDatabase::instance()->findComplexType(someName); | |
1481 QString replacement; | |
1482 if (ctype != 0 && ctype->isVariant()) | |
1483 replacement = "qt.QVariant."; | |
1484 else if (ctype != 0) | |
1485 replacement = ctype->javaPackage() + "." + ctype->targetLangName() + "."; | |
1486 else | |
1487 replacement = someName + "."; | |
1488 defaultExpr = defaultExpr.replace(someName + ".", replacement); | |
1489 } | |
1490 | |
1491 if (arg_type != 0 && arg_type->isFlags()) { | |
1492 s << "new " << arg_type->fullName() << "(" << defaultExpr << ")"; | |
1493 } else { | |
1494 s << defaultExpr; | |
1495 } | |
1496 } | |
1497 } | |
1498 } | |
1499 s << ");\n }" << endl; | |
1500 } | |
1501 } | |
1502 | |
1503 const TypeEntry* DGenerator::fixedTypeEntry(const TypeEntry *type) | |
1504 { | |
1505 if (!type) | |
1506 return NULL; | |
1507 if (type->designatedInterface()) | |
1508 return type; | |
1509 else if (type->isEnum()) { | |
1510 const EnumTypeEntry *te = static_cast<const EnumTypeEntry *>(type); | |
1511 TypeEntry *ownerTe = TypeDatabase::instance()->findType(te->qualifier()); | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1512 if(ownerTe) |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1513 typeEntriesEnums << ownerTe; |
1 | 1514 return NULL; |
1515 // return ownerTe; | |
1516 } else if (type->isFlags()) { | |
1517 const FlagsTypeEntry *te = static_cast<const FlagsTypeEntry *>(type); | |
1518 TypeEntry *ownerTe = TypeDatabase::instance()->findType(te->qualifier()); | |
1519 return NULL; | |
1520 // return ownerTe; | |
1521 } else //if (type->isObject()) | |
1522 return type; | |
1523 // else return NULL; | |
1524 } | |
1525 | |
1526 void DGenerator::addInstantiations(const AbstractMetaType* d_type) | |
1527 { | |
1528 if (d_type->isContainer()) { | |
1529 QList<AbstractMetaType *> args = d_type->instantiations(); | |
1530 for (int i=0; i<args.size(); ++i) { | |
1531 const TypeEntry *type = fixedTypeEntry(args.at(i)->typeEntry()); | |
1532 if (type) | |
1533 typeEntries.insert(type); | |
1534 } | |
1535 } | |
1536 } | |
1537 | |
1538 void DGenerator::addTypeEntry(const AbstractMetaClass *d_class, const AbstractMetaFunction *function, QSet<const TypeEntry*> &typeEntries) | |
1539 { | |
1540 // If a method in an interface class is modified to be private, this should | |
1541 // not be present in the interface at all, only in the implementation. | |
1542 if (d_class->isInterface()) { | |
1543 uint includedAttributes = 0; | |
1544 uint excludedAttributes = 0; | |
1545 retrieveModifications(function, d_class, &excludedAttributes, &includedAttributes); | |
1546 if (includedAttributes & AbstractMetaAttributes::Private) | |
1547 return; | |
1548 } | |
1549 | |
1550 if (notWrappedYet(function)) // qtd2 | |
1551 return; | |
1552 | |
1553 // return type for function | |
1554 if (function->type()) { | |
1555 addInstantiations(function->type()); | |
1556 const TypeEntry *type = fixedTypeEntry(function->type()->typeEntry()); | |
1557 if (type) | |
1558 typeEntries.insert(type); | |
1559 } | |
1560 | |
1561 AbstractMetaArgumentList arguments = function->arguments(); | |
1562 for (int i=0; i<arguments.count(); ++i) { | |
1563 const AbstractMetaArgument *arg = arguments.at(i); | |
1564 addInstantiations(arg->type()); | |
1565 const TypeEntry *type = fixedTypeEntry(arg->type()->typeEntry()); | |
1566 if (type) | |
1567 typeEntries.insert(type); | |
1568 } | |
1569 } | |
1570 | |
1571 void DGenerator::fillRequiredImports(const AbstractMetaClass *d_class) | |
1572 { | |
1573 if (m_recursive < 2) { | |
1574 typeEntries.clear(); | |
1575 typeEntriesEnums.clear(); | |
1576 } | |
1577 | |
1578 // import for base class | |
1579 if(d_class->baseClass()) | |
1580 typeEntries << d_class->baseClass()->typeEntry(); | |
1581 | |
1582 //interfaces | |
1583 AbstractMetaClassList interfaces = d_class->interfaces(); | |
1584 if (!interfaces.isEmpty()) { | |
1585 for (int i=0; i<interfaces.size(); ++i) { | |
1586 AbstractMetaClass *iface = interfaces.at(i); | |
1587 InterfaceTypeEntry *te = (InterfaceTypeEntry*) iface->typeEntry(); | |
1588 typeEntries << te->origin(); | |
1589 } | |
1590 } | |
1591 | |
1592 AbstractMetaFunctionList d_funcs = d_class->functionsInTargetLang(); | |
1593 | |
1594 // in case of ConcreteWrapper - adding extra functions | |
1595 if (!d_class->isInterface() && d_class->isAbstract()) { | |
1596 AbstractMetaFunctionList functions_add = d_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NonEmptyFunctions | AbstractMetaClass::NotRemovedFromTargetLang); | |
1597 d_funcs << functions_add; | |
1598 } | |
1599 | |
1600 for (int i=0; i<d_funcs.size(); ++i) { | |
1601 AbstractMetaFunction *function = d_funcs.at(i); | |
1602 addTypeEntry(d_class, function, typeEntries); | |
1603 } | |
1604 | |
1605 // virtual dispatch | |
1606 AbstractMetaFunctionList virtualFunctions = d_class->virtualFunctions(); | |
1607 for (int i=0; i<virtualFunctions.size(); ++i) { | |
1608 AbstractMetaFunction *function = virtualFunctions.at(i); | |
1609 addTypeEntry(d_class, function, typeEntries); | |
1610 } | |
1611 | |
1612 AbstractMetaFieldList fields = d_class->fields(); | |
1613 foreach (const AbstractMetaField *field, fields) { | |
1614 if (field->wasPublic() || (field->wasProtected() && !d_class->isFinal())) { | |
1615 addTypeEntry(d_class, field->setter(), typeEntries); | |
1616 addTypeEntry(d_class, field->getter(), typeEntries); | |
1617 } | |
1618 } | |
1619 | |
1620 // signals | |
1621 AbstractMetaFunctionList signal_funcs = d_class->queryFunctions(AbstractMetaClass::Signals | |
1622 | AbstractMetaClass::Visible | |
1623 | AbstractMetaClass::NotRemovedFromTargetLang); | |
1624 for (int i=0; i<signal_funcs.size(); ++i) | |
1625 addTypeEntry(d_class, signal_funcs.at(i), typeEntries); | |
1626 | |
1627 if(d_class->isQObject() && d_class->name() != "QObject") | |
1628 typeEntries << TypeDatabase::instance()->findType("QObject"); | |
1629 | |
1630 if(m_recursive == 1) | |
1631 m_recursive++; | |
1632 } | |
1633 | |
1634 void DGenerator::writeImportString(QTextStream &s, const TypeEntry* typeEntry) | |
1635 { | |
1636 /* QString visibility = "private"; | |
1637 if (typeEntry->isNamespace() || typeEntry->name() == "QObject") | |
1638 visibility = "public"; | |
1639 if(d_class->baseClass() && d_class->baseClass()->typeEntry() == typeEntry) | |
1640 visibility = "public";*/ | |
1641 QString visibility = "public"; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
1642 s << QString("%1 import ").arg(visibility) << typeEntry->javaPackage() << "." << typeEntry->targetLangName() << ";" << endl; |
1 | 1643 } |
1644 | |
1645 void DGenerator::writeRequiredImports(QTextStream &s, const AbstractMetaClass *d_class) | |
1646 { | |
1647 foreach (const TypeEntry *typeEntry, typeEntriesEnums) { | |
1648 if (!excludedTypes.contains(typeEntry->name()) && d_class->typeEntry() != typeEntry | |
1649 && typeEntry->javaQualifier() != typeEntry->name() | |
1650 /*also*/ && !excludedTypes2.contains(typeEntry->name())) | |
1651 writeImportString(s, typeEntry); | |
1652 } | |
1653 | |
1654 foreach (const TypeEntry *typeEntry, typeEntries) { | |
1655 if (!excludedTypes.contains(typeEntry->name()) && d_class->typeEntry() != typeEntry | |
1656 && typeEntry->javaQualifier() != typeEntry->name() | |
1657 /*also*/ && !excludedTypes2.contains(typeEntry->name())) | |
1658 writeImportString(s, typeEntry); | |
1659 } | |
1660 excludedTypes2.clear(); | |
1661 } | |
1662 | |
1663 void DGenerator::writeDestructor(QTextStream &s, const AbstractMetaClass *d_class) | |
1664 { | |
1665 if (!d_class->hasConstructors()) | |
1666 return; | |
1667 | |
1668 s << endl; | |
1669 if (d_class->baseClassName().isEmpty()) { | |
1670 s << INDENT << "~this() { " << endl; | |
1671 { | |
1672 Indentation indent(INDENT); | |
1673 | |
185 | 1674 /* |
1 | 1675 if(d_class->name() == "QObject") |
1676 s << INDENT << "if(!__gc_managed)" << endl | |
183 | 1677 << INDENT << " remove(__gc_ref_list, this);" << endl |
1678 << INDENT << "if(!__no_real_delete && __gc_managed) {" << endl | |
1679 << INDENT << " __qobject_is_deleting = true;" << endl | |
1680 << INDENT << " scope(exit) __qobject_is_deleting = false;" << endl | |
1681 << INDENT << " __free_native_resources();" << endl | |
1682 << INDENT << "}" << endl; | |
185 | 1683 */ |
1684 if(d_class->name() == "QObject") | |
1685 s << INDENT << "if(!__no_real_delete) {" << endl | |
1686 << INDENT << " __qobject_is_deleting = true;" << endl | |
1687 << INDENT << " scope(exit) __qobject_is_deleting = false;" << endl | |
1688 << INDENT << " __free_native_resources();" << endl | |
1689 << INDENT << "}" << endl; | |
1 | 1690 else |
1691 s << INDENT << "if(!__no_real_delete)" << endl | |
1692 << INDENT << " __free_native_resources();" << endl; | |
1693 } | |
1694 s << INDENT << "}" << endl << endl; | |
1695 } | |
1696 | |
1697 s << INDENT << "protected void __free_native_resources() {" << endl; | |
1698 { | |
1699 Indentation indent(INDENT); | |
1700 s << INDENT << "qtd_" << d_class->name() << "_destructor(nativeId());" << endl; | |
1701 } | |
1702 s << INDENT << "}" << endl << endl; | |
1703 } | |
1704 | |
1705 void DGenerator::writeOwnershipMethods(QTextStream &s, const AbstractMetaClass *d_class) | |
1706 { | |
1707 s << INDENT << "void __set_native_ownership(bool ownership_)"; | |
1708 if (d_class->isInterface() || d_class->isNamespace()) | |
1709 s << ";"; | |
1710 else { | |
1711 s << " {" << endl | |
1712 << INDENT << " __no_real_delete = ownership_;" << endl | |
1713 << INDENT << "}" << endl << endl; | |
1714 } | |
1715 } | |
1716 | |
1717 void DGenerator::writeSignalHandlers(QTextStream &s, const AbstractMetaClass *d_class) | |
1718 { | |
1719 AbstractMetaFunctionList signal_funcs = signalFunctions(d_class); | |
1720 | |
1721 //TODO: linkage trivia should be abstracted away | |
1722 QString attr; | |
1723 | |
1724 s << "// signal handlers" << endl; | |
1725 foreach(AbstractMetaFunction *signal, signal_funcs) { | |
1726 QString sigExternName = signalExternName(d_class, signal); | |
1727 | |
1728 s << "private " << attr << "extern(C) void " << sigExternName << "_connect(void* native_id);" << endl; | |
1729 s << "private " << attr << "extern(C) void " << sigExternName << "_disconnect(void* native_id);" << endl; | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1730 /* |
1 | 1731 QString extra_args; |
1732 | |
1733 foreach (AbstractMetaArgument *argument, arguments) { | |
1734 if(argument->type()->isContainer()) { | |
1735 QString arg_name = argument->indexedName(); | |
1736 const AbstractMetaType *arg_type = argument->type(); | |
1737 QString type_string = translateType(argument->type(), signal->implementingClass(), BoxedPrimitive); | |
1738 extra_args += ", " + type_string + " " + arg_name; | |
1739 } | |
1740 } | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1741 */ |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1742 AbstractMetaArgumentList arguments = signal->arguments(); |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1743 |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1744 s << "private extern(C) void " << sigExternName << "_handle(void* d_entity, void** args) {" << endl; |
1 | 1745 { |
1746 Indentation indent(INDENT); | |
1747 s << INDENT << "auto d_object = cast(" << d_class->name() << ") d_entity;" << endl; | |
1748 int sz = arguments.count(); | |
1749 | |
1750 for (int j=0; j<sz; ++j) { | |
1751 AbstractMetaArgument *argument = arguments.at(j); | |
1752 QString arg_name = argument->indexedName(); | |
1753 AbstractMetaType *type = argument->type(); | |
1754 // if has QString argument we have to pass char* and str.length to QString constructor | |
1755 | |
1756 QString arg_ptr = QString("args[%1]").arg(argument->argumentIndex() + 1); | |
1757 | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1758 if (type->isContainer()) { |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1759 s << INDENT << translateType(type, signal->implementingClass(), BoxedPrimitive) << " " << arg_name << ";" << endl |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1760 << INDENT << fromCppContainerName(d_class, type) << "(" << arg_ptr << ", &" << arg_name << ");" << endl; |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1761 } else if (type->isTargetLangString()) { |
1 | 1762 s << INDENT << "auto " << arg_name << "_ptr = " << arg_ptr << ";" << endl |
1763 << INDENT << "string " << arg_name << " = QString.toNativeString(" << arg_name << "_ptr);"; | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1764 } else if(type->isPrimitive() || type->isEnum() || type->isFlags() || type->typeEntry()->isStructInD()) { |
1 | 1765 QString type_name = argument->type()->typeEntry()->qualifiedTargetLangName(); |
1766 if (type->isFlags()) | |
1767 type_name = "int"; | |
1768 s << INDENT << "auto " << arg_name << " = *(cast(" << type_name << "*)" << arg_ptr << ");"; | |
1769 } else if(type->isObject() || type->isQObject() | |
1770 || (type->typeEntry()->isValue() && type->isNativePointer()) | |
1771 || type->isValue()) { | |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1772 QString type_name = type->name(); |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1773 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(type->typeEntry()); |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1774 if(ctype->isAbstract()) |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1775 type_name = type_name + "_ConcreteWrapper"; |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1776 s << INDENT << "scope " << arg_name << " = new " << type_name |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1777 << "(cast(void*)(" << arg_ptr << "), true);" << endl |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1778 << INDENT << arg_name << ".__no_real_delete = true;"; |
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1779 } |
1 | 1780 s << endl; |
1781 } | |
1782 // s << INDENT << "Stdout(\"" << d_class->name() << "\", \"" << signal->name() << "\").newline;" << endl; | |
1783 s << INDENT << "d_object." << signal->name() << ".emit("; | |
1784 for (int j = 0; j<sz; ++j) { | |
1785 AbstractMetaArgument *argument = arguments.at(j); | |
1786 QString arg_name = argument->indexedName(); | |
1787 if (j != 0) | |
1788 s << ", "; | |
1789 s << arg_name; | |
1790 } | |
1791 | |
1792 s << ");" << endl; | |
1793 } | |
1794 s << "}" << endl; | |
1795 } | |
1796 } | |
1797 | |
1798 void DGenerator::write(QTextStream &s, const AbstractMetaClass *d_class) | |
1799 { | |
1800 ReportHandler::debugSparse("Generating class: " + d_class->fullName()); | |
1801 | |
1802 bool fakeClass = d_class->attributes() & AbstractMetaAttributes::Fake; | |
1803 | |
1804 if (m_docs_enabled) { | |
1805 m_doc_parser = new DocParser(m_doc_directory + "/" + d_class->name().toLower() + ".jdoc"); | |
1806 } | |
1807 if (!m_isRecursive) | |
1808 s << "module " << d_class->package() << "." << d_class->name() <<";" << endl << endl; | |
1809 | |
39 | 1810 /* |
1 | 1811 s << "// some type info" << endl; |
1812 QString hasVirtuals = d_class->hasVirtualFunctions() ? "has" : "doesn't have"; | |
1813 QString isFinal = d_class->isFinal() ? "is" : "is not"; | |
1814 QString isNativeId = d_class->typeEntry()->isNativeIdBased() ? "is" : "is not"; | |
1815 s << "// " << hasVirtuals << " virtual functions" << endl | |
1816 << "// " << isFinal << " final" << endl | |
1817 << "// " << isNativeId << " native id based" << endl << endl | |
1818 << "// " << d_class->generateShellClass() << " shell class" << endl | |
1819 << "// " << d_class->hasVirtualFunctions() << endl | |
1820 << "// " << d_class->hasProtectedFunctions() << endl | |
1821 << "// " << d_class->hasFieldAccessors() << endl | |
1822 << "// " << d_class->typeEntry()->isObject() << endl; | |
39 | 1823 */ |
1 | 1824 |
1825 const ComplexTypeEntry *ctype = d_class->typeEntry(); | |
1826 if (!ctype->addedTo.isEmpty() && !m_isRecursive) { | |
1827 ComplexTypeEntry *ctype_parent = TypeDatabase::instance()->findComplexType(ctype->addedTo); | |
1828 s << "public import " << ctype_parent->javaPackage() << "." << ctype_parent->name() << ";" << endl; | |
1829 return; | |
1830 } | |
1831 | |
1832 if(d_class->isInterface() && !m_isRecursive) { | |
1833 s << "public import " << ctype->javaPackage() << "." << ctype->qualifiedCppName() << ";" << endl; | |
1834 return; | |
1835 } | |
1836 AbstractMetaClassList includedClassesList; | |
1837 | |
1838 /* m_recursive is increasing by 1 each time we fill the import for a class | |
1839 if it equals to 0 or 1 imports Set is cleared before a filling cycle - if there | |
1840 is only one class as usual or if there are many classes in module, but before | |
1841 filling for first class we need to clear Set. Wow :) | |
1842 */ | |
1843 if(ctype->includedClasses.size() > 0) | |
1844 m_recursive = 1; | |
1845 else | |
1846 m_recursive = 0; | |
1847 | |
1848 foreach(QString child, ctype->includedClasses) { | |
1849 ComplexTypeEntry *ctype_child = TypeDatabase::instance()->findComplexType(child); | |
1850 foreach (AbstractMetaClass *cls, m_classes) { | |
1851 if ( cls->name() == ctype_child->name() ) { | |
1852 includedClassesList << cls; | |
1853 fillRequiredImports(cls); | |
1854 excludedTypes2 << cls->name(); | |
1855 } | |
1856 } | |
1857 } | |
1858 | |
1859 QString interface; | |
1860 if(d_class->typeEntry()->designatedInterface()) | |
1861 interface = d_class->typeEntry()->designatedInterface()->name(); | |
1862 | |
1863 if(d_class->typeEntry()->designatedInterface()) { | |
1864 foreach (AbstractMetaClass *cls, m_classes) { | |
1865 if ( cls->name() == interface ) { | |
1866 includedClassesList << cls; | |
1867 fillRequiredImports(cls); | |
1868 excludedTypes2 << cls->name(); | |
1869 } | |
1870 } | |
1871 } | |
1872 | |
1873 fillRequiredImports(d_class); | |
1874 excludedTypes2 << d_class->name(); | |
1875 if(ctype->includedClasses.size() > 0) | |
1876 m_recursive = 0; | |
1877 | |
1878 QList<Include> includes = d_class->typeEntry()->extraIncludes(); | |
1879 foreach (const Include &inc, includes) { | |
1880 if (inc.type == Include::TargetLangImport) { | |
1881 s << inc.toString() << endl; | |
1882 } | |
1883 } | |
1884 | |
1885 if (!m_isRecursive) { | |
1886 s << "public import qt.QGlobal;" << endl | |
1887 << "public import qt.core.Qt;" << endl | |
56
d5a6b6269f44
duic is working now on the first testcase, if there are any issues with it please report
eldar
parents:
39
diff
changeset
|
1888 << "private import qt.QtDObject;" << endl |
81 | 1889 << "private import qt.core.QString;" << endl |
1890 << "private import qt.qtd.Array;" << endl; | |
1 | 1891 if (d_class->isQObject()) { |
1892 s << "public import qt.Signal;" << endl; | |
1893 if (d_class->name() != "QObject") | |
1894 s << "public import qt.core.QObject;" << endl; | |
1895 } | |
1896 | |
1897 // qtd2 hack! | |
1898 if (d_class->name() == "QCoreApplication") | |
1899 s << "private import qt.core.ArrayOps;" << endl; | |
1900 else if (d_class->name() == "QApplication") | |
1901 s << "private import qt.gui.ArrayOps;" << endl; | |
1902 | |
187
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1903 /* |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1904 we don't need to import ArrayOps2 for anything else than QObjects, |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1905 for example if it is done in the namespaces, it may cause circular |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1906 imports forward references and shit. If ArrayOps2 is expanded later |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1907 for other usages - then restrict it just for namespaces/interfaces |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1908 */ |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1909 if(d_class->isQObject()) |
34fe79a9915b
fix forward references caused by unneeded import of ArrayOps2
eldar
parents:
185
diff
changeset
|
1910 s << "private import " << d_class->package() << ".ArrayOps2;" << endl; |
167
ae06da58ec25
fixed hacky conversion of arguments when emmitting a signal
eldar
parents:
152
diff
changeset
|
1911 |
1 | 1912 if (!d_class->enums().isEmpty()) |
1913 s << "public import " << d_class->package() << "." << d_class->name() << "_enum;" << endl << endl; | |
1914 | |
1915 s << "// automatic imports-------------" << endl; | |
1916 writeRequiredImports(s, d_class); | |
1917 s << endl; | |
1918 if (dPhobos) | |
1919 { | |
1920 s << "import std.stdio;" << endl | |
1921 << "import std.string;" << endl | |
1922 << "import std.utf;" << endl | |
1923 << "import core.memory;"; | |
1924 } | |
1925 else | |
1926 { | |
1927 s << "import tango.io.Stdout;" << endl | |
1928 << "import tango.stdc.stringz;" << endl | |
1929 << "import tango.text.convert.Utf;" << endl | |
1930 << "import tango.core.Memory;"; | |
1931 } | |
1932 s << endl << endl << endl; | |
1933 } | |
1934 | |
1935 if (m_doc_parser) { | |
1936 s << m_doc_parser->documentation(d_class) << endl << endl; | |
1937 } | |
1938 | |
1939 /* qtd s << "@QtJambiGeneratedClass" << endl; | |
1940 | |
1941 if ((d_class->typeEntry()->typeFlags() & ComplexTypeEntry::Deprecated) != 0) { | |
1942 s << "@Deprecated" << endl; | |
1943 } | |
1944 */ | |
1945 | |
1946 // Enums aliases outside of the class - hack | |
1947 if (!d_class->enums().isEmpty()) { | |
1948 QString fileName = QString("%1_enum.d").arg(d_class->name()); | |
1949 FileOut fileOut(outputDirectory() + "/" + subDirectoryForClass(d_class) + "/" + fileName); | |
1950 | |
1951 fileOut.stream << "module " << d_class->package() << "." << d_class->name() << "_enum;" << endl << endl; | |
1952 foreach (AbstractMetaEnum *d_enum, d_class->enums()) | |
1953 writeEnum(fileOut.stream, d_enum); | |
1954 } | |
1955 | |
1956 | |
1957 s << endl; | |
1958 | |
1959 if (d_class->isInterface()) { | |
1960 s << "public interface "; | |
1961 } else { | |
1962 if (d_class->isPublic()) | |
1963 s << "public "; | |
1964 // else friendly | |
1965 | |
1966 bool force_abstract = (d_class->typeEntry()->typeFlags() & ComplexTypeEntry::ForceAbstract) != 0; | |
1967 if (d_class->isFinal() && !force_abstract) | |
1968 s << "final "; | |
1969 if ((d_class->isAbstract() && !d_class->isNamespace()) || force_abstract) | |
1970 s << "abstract "; | |
1971 | |
1972 if (!d_class->typeEntry()->targetType().isEmpty()) { | |
1973 s << d_class->typeEntry()->targetType() << " "; | |
1974 } else if (d_class->isNamespace() && d_class->functionsInTargetLang().size() == 0) { | |
1975 s << "interface "; | |
1976 } else if (d_class->isNamespace()) { | |
1977 s << "class "; | |
1978 } else { | |
1979 s << "class "; | |
1980 } | |
1981 | |
1982 } | |
1983 | |
1984 const ComplexTypeEntry *type = d_class->typeEntry(); | |
1985 | |
1986 s << d_class->name(); | |
1987 | |
1988 if (type->isGenericClass()) { | |
1989 s << "<"; | |
1990 QList<TypeEntry *> templateArguments = d_class->templateBaseClass()->templateArguments(); | |
1991 for (int i=0; i<templateArguments.size(); ++i) { | |
1992 TypeEntry *templateArgument = templateArguments.at(i); | |
1993 if (i > 0) | |
1994 s << ", "; | |
1995 s << templateArgument->name(); | |
1996 } | |
1997 s << ">"; | |
1998 } | |
1999 | |
2000 if (!d_class->isNamespace() && !d_class->isInterface()) { | |
2001 if (!d_class->baseClassName().isEmpty()) { | |
2002 s << " : " << d_class->baseClass()->name(); | |
2003 } else { | |
2004 QString sc = type->defaultSuperclass(); | |
2005 if ((sc != d_class->name()) && !sc.isEmpty()) | |
2006 s << " : " << sc; | |
2007 } | |
2008 }/* qtd else if (d_class->isInterface()) { | |
2009 s << " extends QtJambiInterface"; | |
2010 }*/ | |
2011 | |
2012 // implementing interfaces... | |
2013 bool implements = false; | |
2014 AbstractMetaClassList interfaces = d_class->interfaces(); | |
2015 if (!interfaces.isEmpty()) { | |
2016 if (!d_class->isInterface()) | |
2017 s << ", "; | |
2018 else { | |
2019 implements = true; | |
2020 s << ": "; | |
2021 } | |
2022 for (int i=0; i<interfaces.size(); ++i) { | |
2023 AbstractMetaClass *iface = interfaces.at(i); | |
2024 if (i) s << ", "; | |
2025 s << iface->name(); | |
2026 } | |
2027 } | |
2028 /* qtd | |
2029 if (isComparable(d_class)) { | |
2030 if (!implements) { | |
2031 implements = true; | |
2032 s << endl << " implements "; | |
2033 } | |
2034 else | |
2035 s << "," << endl << " "; | |
2036 s << "java.lang.Comparable<Object>"; | |
2037 } | |
2038 | |
2039 if (d_class->hasCloneOperator()) { | |
2040 if (!implements) { | |
2041 implements = true; | |
2042 s << endl << " implements "; | |
2043 } | |
2044 else | |
2045 s << "," << endl << " "; | |
2046 s << "java.lang.Cloneable"; | |
2047 } | |
2048 */ | |
2049 s << endl << "{" << endl; | |
2050 | |
2051 Indentation indent(INDENT); | |
2052 | |
2053 // Define variables for reference count mechanism | |
2054 if (!d_class->isInterface() && !d_class->isNamespace()) { | |
2055 QHash<QString, int> variables; | |
2056 foreach (AbstractMetaFunction *function, d_class->functions()) { | |
2057 QList<ReferenceCount> referenceCounts = function->referenceCounts(d_class); | |
2058 foreach (ReferenceCount refCount, referenceCounts) { | |
2059 variables[refCount.variableName] |= refCount.action | |
2060 | refCount.access | |
2061 | (refCount.threadSafe ? ReferenceCount::ThreadSafe : 0) | |
2062 | (function->isStatic() ? ReferenceCount::Static : 0) | |
2063 | (refCount.declareVariable.isEmpty() ? ReferenceCount::DeclareVariable : 0); | |
2064 } | |
2065 } | |
2066 | |
2067 foreach (QString variableName, variables.keys()) { | |
2068 int actions = variables.value(variableName) & ReferenceCount::ActionsMask; | |
2069 // bool threadSafe = variables.value(variableName) & ReferenceCount::ThreadSafe; | |
2070 bool isStatic = variables.value(variableName) & ReferenceCount::Static; | |
2071 bool declareVariable = variables.value(variableName) & ReferenceCount::DeclareVariable; | |
2072 int access = variables.value(variableName) & ReferenceCount::AccessMask; | |
2073 | |
2074 if (actions == ReferenceCount::Ignore || !declareVariable) | |
2075 continue; | |
2076 | |
2077 if (((actions & ReferenceCount::Add) == 0) != ((actions & ReferenceCount::Remove) == 0)) { | |
2078 QString warn = QString("either add or remove specified for reference count variable '%1' in '%2' but not both") | |
2079 .arg(variableName).arg(d_class->fullName()); | |
2080 ReportHandler::warning(warn); | |
2081 } | |
2082 s << endl; | |
2083 /* qtd | |
2084 if (TypeDatabase::instance()->includeEclipseWarnings()) | |
2085 s << INDENT << "@SuppressWarnings(\"unused\")" << endl; | |
2086 */ | |
2087 if (actions != ReferenceCount::Set && actions != ReferenceCount::Ignore) { // qtd2 | |
2088 | |
2089 s << INDENT; | |
2090 switch (access) { | |
2091 case ReferenceCount::Private: | |
2092 s << "package "; break; // qtd | |
2093 case ReferenceCount::Protected: | |
2094 s << "protected "; break; | |
2095 case ReferenceCount::Public: | |
2096 s << "public "; break; | |
2097 default: | |
2098 s << "protected"; // friendly | |
2099 } | |
2100 | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2101 } // qtd2 |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2102 |
1 | 2103 if (isStatic) |
2104 s << "static "; | |
2105 | |
2106 if (actions != ReferenceCount::Set && actions != ReferenceCount::Ignore) { | |
2107 s << "Object[] " << variableName << ";" << endl; | |
2108 /* | |
2109 if (threadSafe) | |
2110 s << "java.util.Collections.synchronizedCollection("; | |
2111 s << "new java.util.ArrayList<Object>()"; | |
2112 if (threadSafe) | |
2113 s << ")"; | |
2114 s << ";" << endl;*/ | |
2115 } else if (actions != ReferenceCount::Ignore) { | |
2116 /* qtd2 if (threadSafe) | |
2117 s << "synchronized ";*/ | |
2118 s << "Object " << variableName << " = null;" << endl; | |
2119 } | |
2120 } | |
2121 s << endl; | |
2122 } | |
2123 | |
2124 /* qtd2 | |
2125 if (!d_class->isInterface() && (!d_class->isNamespace() || d_class->functionsInTargetLang().size() > 0) | |
2126 && (d_class->baseClass() == 0 || d_class->package() != d_class->baseClass()->package())) { | |
2127 s << endl | |
2128 << INDENT << "static {" << endl; | |
2129 | |
2130 if (d_class->isNamespace()) { | |
2131 s << INDENT << " qt.QtJambi_LibraryInitializer.init();" << endl; | |
2132 } | |
2133 | |
2134 s << INDENT << " " << d_class->package() << ".QtJambi_LibraryInitializer.init();" << endl | |
2135 << INDENT << "}" << endl; | |
2136 } | |
2137 */ | |
2138 | |
2139 // Enums aliaases | |
2140 foreach (AbstractMetaEnum *d_enum, d_class->enums()) | |
2141 writeEnumAlias(s, d_enum); | |
2142 | |
2143 if (!d_class->enums().isEmpty() && !d_class->functions().isEmpty()) | |
2144 s << endl; | |
2145 | |
2146 // Signals | |
2147 AbstractMetaFunctionList signal_funcs; | |
2148 | |
2149 signal_funcs = signalFunctions(d_class); | |
2150 if (signal_funcs.size()) | |
2151 { | |
2152 writeSignalConnectors(s, d_class, signal_funcs); | |
2153 foreach (AbstractMetaFunction *signal, signal_funcs) | |
2154 { | |
2155 if (d_class == signal->implementingClass()) | |
2156 writeSignal(s, signal); | |
2157 } | |
2158 } | |
2159 | |
2160 // Class has subclasses but also only private constructors | |
2161 if (!d_class->isFinalInTargetLang() && d_class->isFinalInCpp()) { | |
2162 s << endl << INDENT << "/**" << endl | |
2163 << INDENT << " * This constructor is a place holder intended to prevent" << endl | |
2164 << INDENT << " * users from subclassing the class. Certain classes can" << endl | |
2165 << INDENT << " * unfortunately only be subclasses internally. The constructor" << endl | |
2166 << INDENT << " * will indiscriminately throw an exception if called. If the" << endl | |
2167 << INDENT << " * exception is ignored, any use of the constructed object will" << endl | |
2168 << INDENT << " * cause an exception to occur." << endl << endl | |
2169 << INDENT << " * @throws QClassCannotBeSubclassedException" << endl | |
2170 << INDENT << " **/" << endl | |
2171 << INDENT << "protected " << d_class->name() << "() throws QClassCannotBeSubclassedException {" << endl | |
2172 << INDENT << " throw new QClassCannotBeSubclassedException(" << d_class->name() << ".class);" << endl | |
2173 << INDENT << "}" << endl << endl; | |
2174 } | |
2175 s << "// Functions" << endl; | |
2176 | |
2177 // Functions | |
2178 AbstractMetaFunctionList d_funcs = d_class->functionsInTargetLang(); | |
2179 for (int i=0; i<d_funcs.size(); ++i) { | |
2180 AbstractMetaFunction *function = d_funcs.at(i); | |
2181 | |
2182 // If a method in an interface class is modified to be private, this should | |
2183 // not be present in the interface at all, only in the implementation. | |
2184 if (d_class->isInterface()) { | |
2185 uint includedAttributes = 0; | |
2186 uint excludedAttributes = 0; | |
2187 retrieveModifications(function, d_class, &excludedAttributes, &includedAttributes); | |
2188 if (includedAttributes & AbstractMetaAttributes::Private) | |
2189 continue; | |
2190 } | |
2191 | |
2192 if (!notWrappedYet(function)) // qtd2 | |
2193 writeFunction(s, function); | |
2194 // s << function->minimalSignature() << endl; | |
2195 } | |
2196 if(d_class->isInterface()) | |
2197 s << endl << INDENT << "public void* __ptr_" << d_class->name() << "();" << endl << endl; | |
2198 | |
2199 | |
2200 s << "// Field accessors" << endl; | |
2201 // Field accessors | |
2202 AbstractMetaFieldList fields = d_class->fields(); | |
2203 foreach (const AbstractMetaField *field, fields) { | |
2204 if (field->wasPublic() || (field->wasProtected() && !d_class->isFinal())) | |
2205 writeFieldAccessors(s, field); | |
2206 } | |
2207 | |
2208 /* qtd | |
2209 | |
2210 // the static fromNativePointer function... | |
2211 if (!d_class->isNamespace() && !d_class->isInterface() && !fakeClass) { | |
2212 s << endl | |
2213 << INDENT << "public static native " << d_class->name() << " fromNativePointer(" | |
2214 << "QNativePointer nativePointer);" << endl; | |
2215 } | |
2216 | |
2217 if (d_class->isQObject()) { | |
2218 s << endl; | |
2219 if (TypeDatabase::instance()->includeEclipseWarnings()) | |
2220 s << INDENT << "@SuppressWarnings(\"unused\")" << endl; | |
2221 | |
2222 s << INDENT << "private static native long originalMetaObject();" << endl; | |
2223 } | |
2224 | |
2225 // The __qt_signalInitialization() function | |
2226 if (signal_funcs.size() > 0) { | |
2227 s << endl | |
2228 << INDENT << "@Override" << endl | |
2229 << INDENT << "@QtBlockedSlot protected boolean __qt_signalInitialization(String name) {" << endl | |
2230 << INDENT << " return (__qt_signalInitialization(nativeId(), name)" << endl | |
2231 << INDENT << " || super.__qt_signalInitialization(name));" << endl | |
2232 << INDENT << "}" << endl | |
2233 << endl | |
2234 << INDENT << "@QtBlockedSlot" << endl | |
2235 << INDENT << "private native boolean __qt_signalInitialization(long ptr, String name);" << endl; | |
2236 } | |
2237 */ | |
2238 // Add dummy constructor for use when constructing subclasses | |
2239 if (!d_class->isNamespace() && !d_class->isInterface() && !fakeClass) { | |
2240 s << endl | |
2241 << INDENT << "public " | |
2242 << "this"; | |
2243 | |
2244 if(d_class->name() == "QObject") | |
2245 { | |
2246 { | |
2247 Indentation indent(INDENT); | |
2248 s << "(void* native_id, bool gc_managed) {" << endl | |
185 | 2249 /* << INDENT << "if(!gc_managed)" << endl |
1 | 2250 << INDENT << " __gc_ref_list ~= this;" << endl |
185 | 2251 << INDENT << "__gc_managed = gc_managed;" << endl */ |
1 | 2252 << INDENT << "super(native_id);" << endl; |
2253 } | |
2254 } | |
2255 else { | |
2256 Indentation indent(INDENT); | |
2257 if(d_class->isQObject()) | |
2258 s << "(void* native_id, bool gc_managed) {" << endl | |
2259 << INDENT << "super(native_id, gc_managed);" << endl; | |
2260 else | |
2261 s << "(void* native_id, bool no_real_delete = false) {" << endl | |
2262 << INDENT << "super(native_id, no_real_delete);" << endl; | |
2263 } | |
2264 | |
2265 // customized store-result instances | |
2266 d_funcs = d_class->functionsInTargetLang(); | |
2267 for (int i=0; i<d_funcs.size(); ++i) { | |
2268 AbstractMetaFunction *d_function = d_funcs.at(i); | |
2269 uint included_attributes = 0; | |
2270 uint excluded_attributes = 0; | |
2271 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
2272 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
2273 bool isStatic = (attr & AbstractMetaAttributes::Static); | |
2274 | |
2275 if (!isStatic && (attr & AbstractMetaAttributes::Abstract)) | |
2276 continue; | |
2277 | |
2278 if(d_function->storeResult()) { | |
2279 QString type_name = d_function->type()->name(); | |
2280 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); | |
2281 if(ctype->isAbstract()) | |
2282 type_name = type_name + "_ConcreteWrapper"; | |
2283 | |
2284 s << INDENT << " __m_" << d_function->name() << " = new " | |
2285 << type_name << "(cast(void*)null);" << endl; | |
2286 if (d_function->type()->isQObject()) | |
2287 s << INDENT << " __m_" << d_function->name() << ".__no_real_delete = true;" << endl; | |
2288 } | |
2289 } | |
2290 | |
2291 // pointers to native interface objects for classes that implement interfaces | |
2292 // initializing | |
2293 interfaces = d_class->interfaces(); | |
2294 if (!interfaces.isEmpty()) { | |
2295 for (int i=0; i<interfaces.size(); ++i) { | |
2296 AbstractMetaClass *iface = interfaces.at(i); | |
2297 | |
2298 s << INDENT << " __m_ptr_" << iface->name() << " = qtd_" << d_class->name() << "_cast_to_" << iface->qualifiedCppName() | |
2299 << "(nativeId);" << endl; | |
2300 } | |
2301 } | |
2302 | |
2303 | |
2304 s << INDENT << "}" << endl << endl; | |
2305 | |
2306 /******************!!!DUBLICATE OF ABOVE!!!*********************/ | |
2307 for (int i=0; i<d_funcs.size(); ++i) { | |
2308 AbstractMetaFunction *d_function = d_funcs.at(i); | |
2309 uint included_attributes = 0; | |
2310 uint excluded_attributes = 0; | |
2311 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
2312 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
2313 bool isStatic = (attr & AbstractMetaAttributes::Static); | |
2314 | |
2315 if (!isStatic && (attr & AbstractMetaAttributes::Abstract)) | |
2316 continue; | |
2317 | |
2318 if(d_function->storeResult()) { | |
2319 QString type_name = d_function->type()->name(); | |
2320 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); | |
2321 if(ctype->isAbstract()) | |
2322 type_name = type_name + "_ConcreteWrapper"; | |
2323 | |
2324 s << INDENT << type_name << " __m_" << d_function->name() << ";" << endl; | |
2325 } | |
2326 } | |
2327 /***************************************************************/ | |
2328 | |
2329 // pointers to native interface objects for classes that implement interfaces | |
2330 // initializing | |
2331 interfaces = d_class->interfaces(); | |
2332 if (!interfaces.isEmpty()) { | |
2333 for (int i=0; i<interfaces.size(); ++i) { | |
2334 AbstractMetaClass *iface = interfaces.at(i); | |
2335 | |
2336 s << INDENT << "private void* __m_ptr_" << iface->name() << ";" << endl | |
2337 << INDENT << "public void* __ptr_" << iface->name() << "() { return __m_ptr_" << iface->name() << "; }" << endl << endl; | |
2338 } | |
2339 } | |
2340 | |
2341 writeDestructor(s, d_class); | |
2342 } | |
2343 | |
2344 /* qtd | |
2345 // Add a function that converts an array of the value type to a QNativePointer | |
2346 if (d_class->typeEntry()->isValue() && !fakeClass) { | |
2347 s << endl | |
2348 << INDENT << "public static native QNativePointer nativePointerArray(" << d_class->name() | |
2349 << " array[]);" << endl; | |
2350 } | |
2351 | |
2352 // write the cast to this function.... | |
2353 if (d_class->isInterface()) { | |
2354 s << endl | |
2355 << " public long __qt_cast_to_" | |
2356 << static_cast<const InterfaceTypeEntry *>(type)->origin()->targetLangName() | |
2357 << "(long ptr);" << endl; | |
2358 } else { | |
2359 foreach (AbstractMetaClass *cls, interfaces) { | |
2360 s << endl | |
2361 << " @QtBlockedSlot public native long __qt_cast_to_" | |
2362 << static_cast<const InterfaceTypeEntry *>(cls->typeEntry())->origin()->targetLangName() | |
2363 << "(long ptr);" << endl; | |
2364 } | |
2365 } | |
2366 */ | |
2367 | |
2368 /* qtd writeJavaLangObjectOverrideFunctions(s, d_class); | |
2369 */ | |
2370 writeOwnershipMethods(s, d_class); | |
2371 s << "// Injected code in class" << endl; | |
2372 writeExtraFunctions(s, d_class); | |
2373 // qtd2 writeToStringFunction(s, d_class); | |
2374 /* qtd | |
2375 if (d_class->hasCloneOperator()) { | |
2376 writeCloneFunction(s, d_class); | |
2377 } | |
2378 */ | |
2379 s << "}" << endl; | |
2380 | |
81 | 2381 /* ---------------- injected free code ----------------*/ |
2382 const ComplexTypeEntry *class_type = d_class->typeEntry(); | |
2383 Q_ASSERT(class_type); | |
2384 | |
2385 CodeSnipList code_snips = class_type->codeSnips(); | |
2386 foreach (const CodeSnip &snip, code_snips) { | |
2387 if (!d_class->isInterface() && snip.language == TypeSystem::TargetLangFreeCode) { | |
2388 s << endl; | |
2389 snip.formattedCode(s, INDENT); | |
2390 } | |
2391 } | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2392 /* --------------------------------------------------- */ |
14 | 2393 |
1 | 2394 interfaces = d_class->interfaces(); |
2395 if (!interfaces.isEmpty()) { | |
2396 for (int i=0; i<interfaces.size(); ++i) { | |
2397 AbstractMetaClass *iface = interfaces.at(i); | |
2398 | |
2399 s << INDENT << "private static extern (C) void*" << "qtd_" << d_class->name() << "_cast_to_" << iface->qualifiedCppName() | |
2400 << "(void* nativeId);" << endl; | |
2401 } | |
2402 } | |
2403 | |
2404 if (!d_class->isInterface() && d_class->isAbstract()) { | |
2405 s << endl; | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2406 |
1 | 2407 s << INDENT << "public class " << d_class->name() << "_ConcreteWrapper : " << d_class->name() << " {" << endl; |
2408 | |
2409 { | |
2410 Indentation indent(INDENT); | |
2411 s << INDENT << "public this(void* native_id, bool no_real_delete = true) {" << endl | |
2412 << INDENT << " super(native_id, no_real_delete);" << endl; | |
2413 | |
2414 | |
2415 | |
2416 | |
2417 /******************!!!DUBLICATE!!!*********************/ | |
2418 d_funcs = d_class->functionsInTargetLang(); | |
2419 for (int i=0; i<d_funcs.size(); ++i) { | |
2420 AbstractMetaFunction *d_function = d_funcs.at(i); | |
2421 uint included_attributes = 0; | |
2422 uint excluded_attributes = 0; | |
2423 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
2424 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
2425 // qtd bool isStatic = (attr & AbstractMetaAttributes::Static); | |
2426 | |
2427 if(d_function->storeResult()) { | |
2428 QString type_name = d_function->type()->name(); | |
2429 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); | |
2430 if(ctype->isAbstract()) | |
2431 type_name = type_name + "_ConcreteWrapper"; | |
2432 s << INDENT << " __m_" << d_function->name() << " = new " | |
2433 << type_name << "(cast(void*)null);" << endl; | |
2434 if (d_function->type()->isQObject()) | |
2435 s << INDENT << " __m_" << d_function->name() << ".__no_real_delete = true;" << endl; | |
2436 } | |
2437 } | |
2438 | |
2439 s << INDENT << "}" << endl << endl; | |
2440 | |
2441 for (int i=0; i<d_funcs.size(); ++i) { | |
2442 AbstractMetaFunction *d_function = d_funcs.at(i); | |
2443 uint included_attributes = 0; | |
2444 uint excluded_attributes = 0; | |
2445 setupForFunction(d_function, &included_attributes, &excluded_attributes); | |
2446 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
2447 // qtd bool isStatic = (attr & AbstractMetaAttributes::Static); | |
2448 | |
2449 if(d_function->storeResult()) { | |
2450 QString type_name = d_function->type()->name(); | |
2451 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(d_function->type()->typeEntry()); | |
2452 if(ctype->isAbstract()) | |
2453 type_name = type_name + "_ConcreteWrapper"; | |
2454 | |
2455 s << INDENT << d_function->type()->name() << " __m_" << d_function->name() << ";" << endl; | |
2456 } | |
2457 } | |
2458 /***************************************************************/ | |
2459 | |
2460 | |
2461 | |
2462 | |
2463 | |
2464 | |
2465 uint exclude_attributes = AbstractMetaAttributes::Native | AbstractMetaAttributes::Abstract; | |
2466 uint include_attributes = 0; | |
2467 AbstractMetaFunctionList functions = d_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NonEmptyFunctions | AbstractMetaClass::NotRemovedFromTargetLang); | |
2468 foreach (const AbstractMetaFunction *d_function, functions) { | |
2469 retrieveModifications(d_function, d_class, &exclude_attributes, &include_attributes); | |
2470 if (notWrappedYet(d_function)) | |
2471 continue; | |
2472 /* qtd s << endl | |
2473 << INDENT << "@Override" << endl; */ | |
2474 writeFunctionAttributes(s, d_function, include_attributes, exclude_attributes, | |
2475 d_function->isNormal() || d_function->isSignal() ? 0 : SkipReturnType); | |
2476 | |
2477 s << d_function->name() << "("; | |
2478 writeFunctionArguments(s, d_function, d_function->arguments().count()); | |
2479 s << ") {" << endl; | |
2480 { | |
2481 Indentation indent(INDENT); | |
2482 writeJavaCallThroughContents(s, d_function, SuperCall); | |
2483 } | |
2484 s << INDENT << "}" << endl; | |
2485 } | |
2486 } | |
2487 s << INDENT << "}" << endl << endl; | |
2488 } | |
2489 | |
2490 if (d_class->generateShellClass()) { // qtd2 | |
2491 if (d_class->hasVirtualFunctions() | |
2492 && (d_class->typeEntry()->isObject() || d_class->typeEntry()->isQObject()) ) | |
2493 s << endl << "extern (C) void *__" << d_class->name() << "_entity(void *q_ptr);" << endl << endl; | |
2494 } | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2495 |
110 | 2496 if (d_class->isQObject()) |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2497 writeQObjectFunctions(s, d_class); |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2498 |
110 | 2499 |
152 | 2500 // if (d_class->needsConversionFunc) |
110 | 2501 writeConversionFunction(s, d_class); |
1 | 2502 |
2503 if (d_class->hasConstructors()) | |
2504 s << "extern (C) void qtd_" << d_class->name() << "_destructor(void *ptr);" << endl << endl; | |
2505 | |
2506 // qtd | |
2507 s << endl << "// C wrappers" << endl; | |
2508 d_funcs = d_class->functionsInTargetLang(); | |
2509 if (!d_class->isInterface()) | |
2510 for (int i=0; i<d_funcs.size(); ++i) { | |
2511 AbstractMetaFunction *function = d_funcs.at(i); | |
2512 | |
2513 if (!notWrappedYet(function)) // qtd2 | |
2514 if (function->jumpTableId() == -1) | |
2515 writePrivateNativeFunction(s, function); | |
2516 } | |
2517 | |
2518 | |
2519 s << "// Just the private functions for abstract functions implemeneted in superclasses" << endl; | |
2520 // Just the private functions for abstract functions implemeneted in superclasses | |
2521 if (!d_class->isInterface() && d_class->isAbstract()) { | |
2522 d_funcs = d_class->queryFunctions(AbstractMetaClass::NormalFunctions | AbstractMetaClass::AbstractFunctions | AbstractMetaClass::NotRemovedFromTargetLang); | |
2523 foreach (AbstractMetaFunction *d_function, d_funcs) { | |
2524 if (d_function->implementingClass() != d_class) { | |
2525 s << endl; | |
2526 writePrivateNativeFunction(s, d_function); | |
2527 } | |
2528 } | |
2529 } | |
2530 | |
2531 | |
2532 foreach (const AbstractMetaField *field, fields) { | |
2533 if (field->wasPublic() || (field->wasProtected() && !d_class->isFinal())) | |
2534 writeNativeField(s, field); | |
2535 } | |
2536 s << endl << endl; | |
2537 | |
2538 // qtd | |
2539 s << endl << "// Virtual Dispatch functions" << endl; | |
2540 AbstractMetaFunctionList virtualFunctions = d_class->virtualFunctions(); | |
2541 for (int pos = 0; pos<virtualFunctions.size(); ++pos) { | |
2542 const AbstractMetaFunction *function = virtualFunctions.at(pos); | |
2543 if (!notWrappedYet(function)) // qtd2 | |
2544 writeShellVirtualFunction(s, function, d_class, pos); | |
2545 } | |
2546 | |
2547 //init callbacks from dll to D side | |
2548 if (cpp_shared) { | |
2549 bool shellClass = d_class->generateShellClass(); | |
2550 if (shellClass && !d_class->isInterface()) { | |
2551 QString initArgs = "void* virtuals"; | |
2552 if (d_class->isQObject()) | |
2553 initArgs += ", void* signals, void* qobj_del"; | |
2554 | |
2555 s << "private extern (C) void qtd_" << d_class->name() | |
2556 << QString("_initCallBacks(%1);").arg(initArgs) << endl << endl | |
2557 << "private bool init_flag_" << d_class->name() << " = false;" << endl | |
2558 << "void static_init_" << d_class->name() << "() {" << endl | |
2559 << INDENT << "init_flag_" << d_class->name() << " = true;" << endl << endl | |
2560 | |
2561 // virtual functions | |
2562 << INDENT << "void*[" << virtualFunctions.size() << "] virt_arr;" << endl; | |
2563 for (int pos = 0; pos<virtualFunctions.size(); ++pos) { | |
2564 const AbstractMetaFunction *function = virtualFunctions.at(pos); | |
2565 if (!notWrappedYet(function)) // qtd2 | |
2566 s << INDENT << "virt_arr[" << pos << "] = &" << function->marshalledName() << "_dispatch;" <<endl; | |
2567 } | |
2568 if (virtualFunctions.size() == 0) | |
2569 initArgs = "null"; | |
2570 else | |
2571 initArgs = "virt_arr.ptr"; | |
2572 | |
2573 // signals | |
2574 if (d_class->isQObject()) { | |
2575 AbstractMetaFunctionList signal_funcs = signalFunctions(d_class); | |
2576 s << endl << INDENT << "void*[" << signal_funcs.size() << "] sign_arr;" << endl; | |
2577 for(int i = 0; i < signal_funcs.size(); i++) { | |
2578 AbstractMetaFunction *signal = signal_funcs.at(i); | |
2579 s << INDENT << "sign_arr[" << i << "] = &" << signalExternName(d_class, signal) << "_handle_in_d;" << endl; | |
2580 } | |
2581 if(signal_funcs.size() == 0) | |
2582 initArgs += ", null"; | |
2583 else | |
2584 initArgs += ", sign_arr.ptr"; | |
2585 | |
2586 // QObject_delete | |
2587 s << endl << INDENT << "void *qobj_del;" << endl | |
2588 << INDENT << "qobj_del = &qtd_D_" << d_class->name() << "_delete;" << endl; | |
2589 initArgs += ", qobj_del"; | |
2590 } | |
2591 | |
2592 s << INDENT << "qtd_" << d_class->name() << QString("_initCallBacks(%1);").arg(initArgs) << endl | |
2593 << "}" << endl << endl; | |
2594 } | |
2595 } | |
2596 | |
2597 writeSignalHandlers(s, d_class); | |
2598 s << endl; | |
2599 | |
2600 if (m_docs_enabled) { | |
2601 delete m_doc_parser; | |
2602 m_doc_parser = 0; | |
2603 } | |
2604 | |
2605 // qtd multiple classes | |
2606 foreach (AbstractMetaClass *cls, includedClassesList) { | |
2607 m_isRecursive = true; | |
2608 write(s, cls); | |
2609 m_isRecursive = false; | |
2610 } | |
2611 } | |
2612 | |
110 | 2613 void DGenerator::writeConversionFunction(QTextStream &s, const AbstractMetaClass *d_class) |
2614 { | |
2615 const ComplexTypeEntry *ctype = d_class->typeEntry(); | |
152 | 2616 if(!ctype->isQObject() && !ctype->isObject()) |
2617 return; | |
110 | 2618 QString class_name = ctype->name(); |
111 | 2619 QString return_type_name = class_name; |
2620 if(ctype->designatedInterface()) | |
2621 return_type_name = ctype->designatedInterface()->name(); | |
2622 s << return_type_name << " qtd_" << class_name << "_from_ptr(void* __qt_return_value) {" << endl; | |
110 | 2623 |
2624 if(ctype->isQObject()) { | |
2625 QString type_name = class_name; | |
2626 if(ctype->isAbstract()) | |
2627 type_name = type_name + "_ConcreteWrapper"; | |
2628 | |
2629 s << INDENT << "if (__qt_return_value is null)" << endl | |
2630 << INDENT << " return null;" << endl | |
2631 << INDENT << "void* d_obj = qtd_" << class_name << "_d_pointer(__qt_return_value);" << endl | |
2632 << INDENT << "if (d_obj is null) {" << endl | |
183 | 2633 << INDENT << " auto new_obj = new " << type_name << "(__qt_return_value, false);" << endl |
110 | 2634 << INDENT << " qtd_" << class_name << "_create_link(new_obj.nativeId, cast(void*) new_obj);" << endl |
2635 << INDENT << " new_obj.__no_real_delete = true;" << endl | |
2636 << INDENT << " return new_obj;" << endl | |
2637 << INDENT << "} else" << endl | |
2638 << INDENT << " return cast(" << class_name << ") d_obj;" << endl; | |
111 | 2639 } else if (ctype->isObject()) { |
110 | 2640 QString type_name = class_name; |
2641 if(ctype->isAbstract()) | |
152 | 2642 type_name = ctype->targetLangName() + "_ConcreteWrapper"; |
110 | 2643 |
2644 // if class has virtual functions then it has classname_entity function so | |
2645 // we can look for D Object pointer. otherwise create new wrapper | |
2646 if (d_class->hasVirtualFunctions()) { | |
152 | 2647 s << INDENT << "void* d_obj = __" << ctype->targetLangName() << "_entity(__qt_return_value);" << endl |
110 | 2648 << INDENT << "if (d_obj !is null) {" << endl |
2649 << INDENT << " auto d_obj_ref = cast (Object) d_obj;" << endl | |
2650 << INDENT << " return cast(" << return_type_name << ") d_obj_ref;" << endl | |
2651 << INDENT << "} else {" << endl | |
2652 << INDENT << " auto return_value = new " << type_name << "(__qt_return_value, true);" << endl | |
2653 << INDENT << " return_value.__no_real_delete = true;" << endl | |
2654 << INDENT << " return return_value;" << endl | |
152 | 2655 << INDENT << "}" << endl; |
110 | 2656 } else { |
2657 s << INDENT << "auto return_value = new " << type_name << "(__qt_return_value, true);" << endl | |
2658 << INDENT << "return_value.__no_real_delete = true;" << endl | |
2659 << INDENT << "return return_value;" << endl; | |
2660 } | |
111 | 2661 } |
2662 s << "}" << endl << endl; | |
110 | 2663 } |
2664 | |
2665 | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2666 void DGenerator::writeQObjectFunctions(QTextStream &s, const AbstractMetaClass *d_class) |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2667 { |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2668 s << "extern(C) void* qtd_" << d_class->name() << "_d_pointer(void *obj);" << endl |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2669 << "extern(C) void qtd_" << d_class->name() << "_create_link(void *obj, void* d_obj);" << endl << endl; |
110 | 2670 s << "private extern (C) void qtd_D_" << d_class->name() << "_delete(void *d_ptr) {" << endl |
2671 << " auto d_ref = cast(QObject) d_ptr;" << endl | |
2672 << " d_ref.__no_real_delete = true;" << endl | |
183 | 2673 << " if(!d_ref.__qobject_is_deleting)" |
2674 << " delete d_ref;" << endl | |
110 | 2675 << "}" << endl << endl; |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2676 } |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
2677 |
1 | 2678 /* |
2679 void DGenerator::writeMarshallFunction(QTextStream &s, const AbstractMetaClass *d_class) | |
2680 { | |
2681 | |
2682 } | |
2683 */ | |
2684 void DGenerator::marshallFromCppToD(QTextStream &s, const ComplexTypeEntry* ctype) | |
2685 { | |
2686 if(ctype->isQObject()) { | |
2687 QString type_name = ctype->name(); | |
152 | 2688 s << "return qtd_" << type_name << "_from_ptr(__qt_return_value);" << endl; |
2689 } else if (ctype->isValue() && !ctype->isStructInD()) { | |
1 | 2690 s << INDENT << "return new " << ctype->name() << "(__qt_return_value, false);" << endl; |
152 | 2691 } else if (ctype->isVariant()) { |
1 | 2692 s << INDENT << "return new QVariant(__qt_return_value, false);" << endl; |
152 | 2693 } else if (ctype->name() == "QModelIndex" || ctype->isStructInD()) { |
1 | 2694 s << INDENT << "return __qt_return_value;" << endl; |
152 | 2695 } else if (ctype->isObject()) { |
1 | 2696 QString type_name = ctype->name(); |
152 | 2697 s << "return qtd_" << type_name << "_from_ptr(__qt_return_value);" << endl; |
1 | 2698 } |
2699 } | |
2700 | |
2701 void DGenerator::writeNativeField(QTextStream &s, const AbstractMetaField *field) | |
2702 { | |
2703 Q_ASSERT(field->isPublic() || field->isProtected()); | |
2704 | |
2705 const AbstractMetaClass *declaringClass = field->enclosingClass(); | |
2706 | |
2707 FieldModification mod = declaringClass->typeEntry()->fieldModification(field->name()); | |
2708 | |
2709 // Set function | |
2710 if (mod.isWritable() && !field->type()->isConstant()) { | |
2711 const AbstractMetaFunction *setter = field->setter(); | |
2712 if (declaringClass->hasFunction(setter)) { | |
2713 QString warning = | |
2714 QString("class '%1' already has setter '%2' for public field '%3'") | |
2715 .arg(declaringClass->name()).arg(setter->name()).arg(field->name()); | |
2716 ReportHandler::warning(warning); | |
2717 } else { | |
2718 if (!notWrappedYet(setter)) | |
2719 writePrivateNativeFunction(s, setter); | |
2720 } | |
2721 } | |
2722 | |
2723 // Get function | |
2724 const AbstractMetaFunction *getter = field->getter(); | |
2725 if (mod.isReadable()) { | |
2726 if (declaringClass->hasFunction(getter)) { | |
2727 QString warning = | |
2728 QString("class '%1' already has getter '%2' for public field '%3'") | |
2729 .arg(declaringClass->name()).arg(getter->name()).arg(field->name()); | |
2730 ReportHandler::warning(warning); | |
2731 } else { | |
2732 if (!notWrappedYet(getter)) | |
2733 writePrivateNativeFunction(s, getter); | |
2734 } | |
2735 } | |
2736 } | |
2737 | |
2738 void DGenerator::writeSignalConnectors(QTextStream &s, const AbstractMetaClass *d_class, AbstractMetaFunctionList signal_funcs) | |
2739 { | |
2740 s << INDENT << "private static SlotConnector[" << signal_funcs.size() << "] __slotConnectors;" << endl; | |
2741 s << INDENT << "private static SlotConnector[" << signal_funcs.size() << "] __slotDisconnectors;" << endl << endl; | |
2742 | |
2743 s << INDENT << "protected void onSignalHandlerCreated(ref SignalHandler sh) {" << endl; | |
2744 { | |
2745 Indentation indent(INDENT); | |
2746 s << INDENT << "sh.firstSlotConnected = &onFirstSlot;" << endl; | |
2747 s << INDENT << "sh.lastSlotDisconnected = &onLastSlot;" << endl << endl; | |
2748 | |
2749 for(int i = 0; i < signal_funcs.size(); i++) { | |
2750 AbstractMetaFunction *signal = signal_funcs.at(i); | |
2751 QString sigExternName = signalExternName(d_class, signal); | |
2752 s << INDENT << "__slotConnectors[" << i << "] = &" << sigExternName << "_connect;" << endl; | |
2753 s << INDENT << "__slotDisconnectors[" << i << "] = &" << sigExternName << "_disconnect;" << endl; | |
2754 } | |
2755 } | |
2756 s << INDENT << "}" << endl << endl; | |
2757 | |
2758 s << INDENT << "private final void onFirstSlot(int signalId) {" << endl; | |
2759 { | |
2760 Indentation indent(INDENT); | |
2761 s << INDENT << "if (signalId < __slotConnectors.length) {" << endl; | |
2762 s << INDENT << " __slotConnectors[signalId](nativeId);" << endl; | |
2763 s << INDENT << "}" << endl; | |
2764 } | |
2765 s << INDENT << "}" << endl; | |
2766 | |
2767 s << INDENT << "private final void onLastSlot(int signalId) {" << endl; | |
2768 { | |
2769 Indentation indent(INDENT); | |
2770 s << INDENT << "if (signalId < __slotDisconnectors.length) {" << endl; | |
2771 s << INDENT << " __slotDisconnectors[signalId](nativeId);" << endl; | |
2772 s << INDENT << "}" << endl; | |
2773 } | |
2774 s << INDENT << "}" << endl; | |
2775 } | |
2776 | |
2777 void DGenerator::writeSignal(QTextStream &s, const AbstractMetaFunction *d_function) | |
2778 { | |
2779 Q_ASSERT(d_function->isSignal()); | |
2780 | |
2781 AbstractMetaArgumentList arguments = d_function->arguments(); | |
2782 int sz = arguments.count(); | |
2783 | |
2784 s << INDENT << "mixin Signal!(\"" << d_function->name() << "\""; | |
2785 | |
2786 if (sz > 0) { | |
2787 for (int i=0; i<sz; ++i) { | |
2788 s << ", "; | |
2789 | |
2790 QString modifiedType = d_function->typeReplaced(i+1); | |
2791 | |
2792 if (modifiedType.isEmpty()) | |
2793 s << translateType(arguments.at(i)->type(), d_function->implementingClass(), BoxedPrimitive); | |
2794 else | |
2795 s << modifiedType; | |
2796 } | |
2797 } | |
2798 | |
2799 s << ");" << endl; | |
2800 } | |
2801 | |
2802 void DGenerator::writeShellVirtualFunction(QTextStream &s, const AbstractMetaFunction *d_function, | |
2803 const AbstractMetaClass *implementor, int id) | |
2804 { | |
2805 Q_UNUSED(id); | |
2806 Q_UNUSED(implementor); | |
2807 | |
2808 s << "private extern(C) "; | |
2809 CppImplGenerator::writeVirtualDispatchFunction(s, d_function, true); | |
2810 s << "{" << endl; | |
2811 | |
2812 const AbstractMetaClass *own_class = d_function->ownerClass(); | |
2813 | |
2814 s << INDENT << "auto d_object = cast(" << own_class->name() << ") d_entity;" << endl; | |
2815 | |
2816 // the function arguments | |
2817 AbstractMetaArgumentList arguments = d_function->arguments(); | |
2818 foreach (const AbstractMetaArgument *argument, arguments) | |
2819 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
2820 QString arg_name = argument->indexedName(); | |
2821 AbstractMetaType *type = argument->type(); | |
2822 // if has QString argument we have to pass char* and str.length to QString constructor | |
2823 { | |
2824 if(type->isEnum()) | |
2825 s << INDENT << "auto " << arg_name << "_enum = cast(" | |
2826 << type->typeEntry()->qualifiedTargetLangName() << ") " << arg_name << ";"; | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2827 else if (type->typeEntry()->qualifiedCppName() == "QChar") |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2828 s << INDENT << "auto " << arg_name << "_d_ref = cast(wchar" << QString(type->actualIndirections(), '*') |
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2829 << ") " << arg_name << ";"; |
1 | 2830 else if (type->isTargetLangString()) |
2831 s << INDENT << "string " << arg_name << "_d_ref = toString(" | |
2832 << arg_name << "[0.." << arg_name << "_size]);"; | |
2833 else if (type->typeEntry()->isValue() && type->isNativePointer() && type->typeEntry()->name() == "QString") { | |
33
cf8a415f3f32
gui package is fully wrapped now. lots of fixes in the generator
eldar
parents:
14
diff
changeset
|
2834 s << INDENT << "auto " << arg_name << "_d_qstr = QString(" << arg_name << ", true);" << endl |
1 | 2835 << INDENT << "string " << arg_name << "_d_ref = " << arg_name << "_d_qstr.toNativeString();"; |
2836 } else if(type->isVariant()) | |
2837 s << INDENT << "scope " << arg_name << "_d_ref = new QVariant(" << arg_name << ", true);"; | |
2838 else if (type->typeEntry()->isStructInD()) | |
2839 continue; | |
2840 else if (!type->hasNativeId() && !(type->typeEntry()->isValue() && type->isNativePointer())) | |
2841 continue; | |
2842 else if(type->isObject() | |
2843 || (type->typeEntry()->isValue() && type->isNativePointer()) | |
2844 || type->isValue() || type->isVariant()) { | |
2845 QString type_name = type->typeEntry()->name(); | |
2846 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(type->typeEntry()); | |
2847 if(ctype->isAbstract()) | |
2848 type_name = type_name + "_ConcreteWrapper"; | |
2849 s << INDENT << "scope " << arg_name << "_d_ref = new " << type_name << "(" << arg_name << ", true);"; | |
2850 } | |
2851 else if (type->isQObject()) { | |
2852 QString type_name = type->name(); | |
2853 const ComplexTypeEntry *ctype = static_cast<const ComplexTypeEntry *>(type->typeEntry()); | |
2854 if(ctype->isAbstract()) | |
2855 type_name = type_name + "_ConcreteWrapper"; | |
184
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
2856 |
183 | 2857 s << INDENT << "scope " << arg_name << "_so = new StackObject!(" << type_name << ");" << endl |
2858 << INDENT << "auto " << arg_name << "_d_ref = " << arg_name << "_so(" << arg_name <<", true);" << endl | |
2859 << INDENT << arg_name << "_d_ref.__no_real_delete = true;"; | |
184
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
2860 /* |
1 | 2861 s << INDENT << "scope " << arg_name << "_d_ref = new " << type_name << "(" << arg_name <<", true);" << endl |
2862 << INDENT << arg_name << "_d_ref.__no_real_delete = true;"; | |
184
7d9db724ee1d
QObject is now non GC'ed, to better integrate to Qt memory management
eldar
parents:
183
diff
changeset
|
2863 */ |
1 | 2864 } |
2865 s << endl; | |
2866 } | |
2867 } | |
2868 | |
2869 s << INDENT; | |
2870 AbstractMetaType *return_type = d_function->type(); | |
2871 QString new_return_type = QString(d_function->typeReplaced(0)).replace('$', '.'); | |
2872 bool has_return_type = new_return_type != "void" | |
2873 && (!new_return_type.isEmpty() || return_type != 0); | |
2874 if(has_return_type) { | |
2875 AbstractMetaType *f_type = d_function->type(); | |
2876 if(f_type && (f_type->isObject() || f_type->isQObject() || f_type->isVariant() || | |
2877 (f_type->isValue() && !f_type->typeEntry()->isStructInD()))) | |
2878 { | |
2879 QString f_type_name = f_type->name(); | |
2880 if(f_type->typeEntry()->designatedInterface()) | |
2881 f_type_name = f_type->typeEntry()->designatedInterface()->name(); | |
2882 s << f_type_name << " ret_value = "; | |
2883 } | |
2884 else if (f_type && f_type->isTargetLangString()) | |
2885 s << "string _d_str = "; | |
81 | 2886 else if (f_type && (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD())) |
1 | 2887 s << "*__d_return_value = "; |
2888 else | |
2889 s << "auto return_value = "; | |
2890 } | |
2891 s << "d_object." << d_function->name() << "("; | |
2892 | |
2893 uint nativeArgCount = 0; | |
2894 foreach (const AbstractMetaArgument *argument, arguments) | |
2895 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
2896 QString arg_name = argument->indexedName(); | |
2897 const AbstractMetaType *type = argument->type(); | |
2898 | |
2899 if (nativeArgCount > 0) | |
2900 s << "," << " "; | |
2901 | |
2902 QString modified_type = d_function->typeReplaced(argument->argumentIndex() + 1); | |
2903 if (!modified_type.isEmpty()) | |
2904 modified_type = modified_type.replace('$', '.'); | |
2905 | |
2906 if (modified_type == "string" /* && type->fullName() == "char" */) | |
2907 s << "fromStringz(" << arg_name << ")"; | |
2908 else { | |
2909 if(type->isContainer() | |
2910 || (type->isReference() && type->typeEntry()->isStructInD())) | |
2911 s << "*"; | |
2912 s << arg_name; | |
2913 } | |
2914 if (type->typeEntry()->isStructInD()) ; | |
2915 else if (type->isQObject() || type->isObject() | |
2916 || (type->typeEntry()->isValue() && type->isNativePointer()) | |
2917 || type->isValue() | |
2918 || type->isTargetLangString() || type->isVariant()) | |
2919 s << "_d_ref"; | |
2920 else if(type->isEnum()) | |
2921 s << "_enum"; | |
2922 | |
2923 nativeArgCount++; | |
2924 } | |
2925 s << ");" << endl; | |
2926 | |
2927 // check for arguments that may return value | |
2928 foreach (const AbstractMetaArgument *argument, arguments) | |
2929 if (!d_function->argumentRemoved(argument->argumentIndex() + 1)) { | |
2930 QString arg_name = argument->indexedName(); | |
2931 AbstractMetaType *type = argument->type(); | |
2932 | |
2933 if (type->typeEntry()->isValue() && type->isNativePointer() && type->typeEntry()->name() == "QString") | |
2934 s << INDENT << arg_name << "_d_qstr.assign(" << arg_name << "_d_ref);" << endl; | |
2935 } | |
2936 | |
2937 if(has_return_type) { | |
2938 AbstractMetaType *f_type = d_function->type(); | |
2939 if(f_type) { | |
2940 if(f_type->isObject() || f_type->isQObject() || f_type->isVariant() || | |
2941 (f_type->isValue() && !f_type->typeEntry()->isStructInD())) { | |
2942 QString native_id = "nativeId"; | |
2943 if (f_type->typeEntry()->designatedInterface()) | |
2944 native_id = "__ptr_" + f_type->typeEntry()->designatedInterface()->name(); | |
2945 s << INDENT << "return ret_value is null? null : ret_value." << native_id << ";" << endl; | |
2946 } else if (f_type->isTargetLangString()) | |
104 | 2947 s << INDENT << "*ret_str = _d_str;" << endl; |
1 | 2948 else if (f_type->isContainer()) |
2949 s << INDENT << "*__d_arr_ptr = return_value.ptr;" << endl | |
2950 << INDENT << "*__d_arr_size = return_value.length;" << endl; | |
81 | 2951 else if (f_type->name() == "QModelIndex" || f_type->typeEntry()->isStructInD()) |
1 | 2952 ; |
2953 else | |
2954 s << INDENT << "return return_value;" << endl; | |
2955 } else | |
2956 s << INDENT << "return return_value;" << endl; | |
2957 | |
2958 } | |
2959 | |
2960 s << "}" << endl << endl; | |
2961 } | |
2962 | |
2963 void DGenerator::generate() | |
2964 { | |
2965 // qtd | |
2966 // code for including classses in 1 module for avoiding circular imports | |
2967 foreach (AbstractMetaClass *cls, m_classes) { | |
2968 const ComplexTypeEntry *ctype = cls->typeEntry(); | |
2969 | |
2970 if (!cls->isInterface() && cls->isAbstract()) { | |
2971 ComplexTypeEntry *ctype_m = (ComplexTypeEntry *)ctype; | |
2972 ctype_m->setAbstract(true); | |
2973 } | |
2974 | |
2975 foreach(QString child, ctype->includedClasses) { | |
2976 ComplexTypeEntry *ctype_child = TypeDatabase::instance()->findComplexType(child); | |
2977 ctype_child->addedTo = cls->name(); | |
2978 } | |
2979 | |
2980 foreach (AbstractMetaFunction *function, cls->functions()) | |
2981 function->checkStoreResult(); | |
152 | 2982 /* we don't need this anymore |
110 | 2983 // generate QObject conversion functions only those that are required |
2984 AbstractMetaFunctionList d_funcs = cls->functionsInTargetLang(); | |
2985 for (int i=0; i<d_funcs.size(); ++i) { | |
2986 AbstractMetaType *f_type = d_funcs.at(i)->type(); | |
2987 if (!f_type) | |
2988 continue; | |
2989 if (f_type->isQObject() || f_type->isObject()) { | |
2990 const ComplexTypeEntry* cte = static_cast<const ComplexTypeEntry *>(f_type->typeEntry()); | |
2991 AbstractMetaClass* d_class = ClassFromEntry::get(cte); | |
2992 if (d_class) | |
2993 d_class->needsConversionFunc = true; | |
2994 } | |
152 | 2995 }*/ |
1 | 2996 } |
2997 | |
2998 Generator::generate(); | |
2999 | |
3000 { | |
3001 const AbstractMetaClass *last_class = 0; | |
3002 QFile file("mjb_nativepointer_api.log"); | |
3003 if (file.open(QFile::WriteOnly)) { | |
3004 QTextStream s(&file); | |
3005 | |
3006 AbstractMetaFunctionList nativepointer_functions; | |
3007 for (int i=0; i<m_nativepointer_functions.size(); ++i) { | |
3008 AbstractMetaFunction *f = const_cast<AbstractMetaFunction *>(m_nativepointer_functions[i]); | |
3009 if (f->ownerClass() == f->declaringClass() || f->isFinal()) | |
3010 nativepointer_functions.append(f); | |
3011 } | |
3012 | |
3013 s << "Number of public or protected functions with QNativePointer API: " << nativepointer_functions.size() << endl; | |
3014 foreach (const AbstractMetaFunction *f, nativepointer_functions) { | |
3015 if (last_class != f->ownerClass()) { | |
3016 last_class = f->ownerClass(); | |
3017 s << endl << endl<< "Class " << last_class->name() << ":" << endl; | |
3018 s << "---------------------------------------------------------------------------------" | |
3019 << endl; | |
3020 } | |
3021 | |
3022 s << f->minimalSignature() << endl; | |
3023 } | |
3024 | |
3025 m_nativepointer_functions.clear(); } | |
3026 } | |
3027 | |
3028 { | |
3029 const AbstractMetaClass *last_class = 0; | |
3030 QFile file("mjb_object_type_usage.log"); | |
3031 if (file.open(QFile::WriteOnly)) { | |
3032 QTextStream s(&file); | |
3033 | |
3034 AbstractMetaFunctionList resettable_object_functions; | |
3035 for (int i=0; i<m_resettable_object_functions.size(); ++i) { | |
3036 AbstractMetaFunction *f = const_cast<AbstractMetaFunction *>(m_resettable_object_functions[i]); | |
3037 if (f->ownerClass() == f->declaringClass() || f->isFinal()) | |
3038 resettable_object_functions.append(f); | |
3039 } | |
3040 | |
3041 s << "Number of public or protected functions that return a non-QObject object type, or that are virtual and take a non-QObject object type argument: " << resettable_object_functions.size() << endl; | |
3042 foreach (const AbstractMetaFunction *f, resettable_object_functions) { | |
3043 if (last_class != f->ownerClass()) { | |
3044 last_class = f->ownerClass(); | |
3045 s << endl << endl<< "Class " << last_class->name() << ":" << endl; | |
3046 s << "---------------------------------------------------------------------------------" | |
3047 << endl; | |
3048 } | |
3049 | |
3050 s << f->minimalSignature() << endl; | |
3051 } | |
3052 | |
3053 m_resettable_object_functions.clear(); } | |
3054 } | |
3055 | |
3056 { | |
3057 QFile file("mjb_reference_count_candidates.log"); | |
3058 if (file.open(QFile::WriteOnly)) { | |
3059 QTextStream s(&file); | |
3060 | |
3061 s << "The following functions have a signature pattern which may imply that" << endl | |
3062 << "they need to apply reference counting to their arguments (" | |
3063 << m_reference_count_candidate_functions.size() << " functions) : " << endl; | |
3064 | |
3065 foreach (const AbstractMetaFunction *f, m_reference_count_candidate_functions) { | |
3066 s << f->implementingClass()->fullName() << " : " << f->minimalSignature() << endl; | |
3067 } | |
3068 } | |
3069 file.close(); | |
3070 } | |
3071 } | |
3072 | |
3073 void DGenerator::writeFunctionAttributes(QTextStream &s, const AbstractMetaFunction *d_function, | |
3074 uint included_attributes, uint excluded_attributes, | |
3075 uint options) | |
3076 { | |
3077 uint attr = d_function->attributes() & (~excluded_attributes) | included_attributes; | |
3078 | |
3079 if ((attr & AbstractMetaAttributes::Public) || (attr & AbstractMetaAttributes::Protected)) { | |
3080 | |
3081 // Does the function use native pointer API? | |
3082 bool nativePointer = d_function->type() && d_function->type()->isNativePointer() | |
3083 && d_function->typeReplaced(0).isEmpty(); | |
3084 | |
3085 // Does the function need to be considered for resetting the Java objects after use? | |
3086 bool resettableObject = false; | |
3087 | |
3088 if (!nativePointer | |
3089 && d_function->type() | |
3090 && d_function->type()->hasInstantiations() | |
3091 && d_function->typeReplaced(0).isEmpty()) { | |
3092 | |
3093 QList<AbstractMetaType *> instantiations = d_function->type()->instantiations(); | |
3094 | |
3095 foreach (const AbstractMetaType *type, instantiations) { | |
3096 if (type && type->isNativePointer()) { | |
3097 nativePointer = true; | |
3098 break; | |
3099 } | |
3100 } | |
3101 | |
3102 } | |
3103 | |
3104 AbstractMetaArgumentList arguments = d_function->arguments(); | |
3105 if (!nativePointer || (!resettableObject && !d_function->isFinal())) { | |
3106 foreach (const AbstractMetaArgument *argument, arguments) { | |
3107 if (!d_function->argumentRemoved(argument->argumentIndex()+1) | |
3108 && d_function->typeReplaced(argument->argumentIndex()+1).isEmpty()) { | |
3109 | |
3110 if (argument->type()->isNativePointer()) { | |
3111 | |
3112 nativePointer = true; | |
3113 if (resettableObject) break ; | |
3114 | |
3115 } else if (!d_function->isFinalInTargetLang() | |
3116 && argument->type()->isObject() | |
3117 && !argument->type()->isQObject() | |
3118 && !d_function->resetObjectAfterUse(argument->argumentIndex()+1) | |
3119 && d_function->ownership(d_function->declaringClass(), TypeSystem::ShellCode, argument->argumentIndex()+1) == TypeSystem::InvalidOwnership) { | |
3120 | |
3121 resettableObject = true; | |
3122 if (nativePointer) break ; | |
3123 | |
3124 } else if (argument->type()->hasInstantiations()) { | |
3125 | |
3126 QList<AbstractMetaType *> instantiations = argument->type()->instantiations(); | |
3127 foreach (AbstractMetaType *type, instantiations) { | |
3128 if (type && type->isNativePointer()) { | |
3129 nativePointer = true; | |
3130 if (resettableObject) break; | |
3131 } else if (!d_function->isFinal() | |
3132 && type | |
3133 && type->isObject() | |
3134 && !type->isQObject() | |
3135 && !d_function->resetObjectAfterUse(argument->argumentIndex()+1)) { | |
3136 resettableObject = true; | |
3137 if (nativePointer) break ; | |
3138 } | |
3139 } | |
3140 | |
3141 if (nativePointer && resettableObject) | |
3142 break; | |
3143 | |
3144 } | |
3145 } | |
3146 } | |
3147 } | |
3148 | |
3149 if (nativePointer && !m_nativepointer_functions.contains(d_function)) | |
3150 m_nativepointer_functions.append(d_function); | |
3151 if (resettableObject && !m_resettable_object_functions.contains(d_function)) | |
3152 m_resettable_object_functions.append(d_function); | |
3153 } | |
3154 | |
3155 if ((options & SkipAttributes) == 0) { | |
3156 if (d_function->isEmptyFunction() | |
3157 || d_function->isDeprecated()) s << INDENT << "deprecated "; | |
3158 /* | |
3159 bool needsSuppressUnusedWarning = TypeDatabase::instance()->includeEclipseWarnings() | |
3160 && d_function->isSignal() | |
3161 && (((excluded_attributes & AbstractMetaAttributes::Private) == 0) | |
3162 && (d_function->isPrivate() | |
3163 || ((included_attributes & AbstractMetaAttributes::Private) != 0))); | |
3164 | |
3165 if (needsSuppressUnusedWarning && d_function->needsSuppressUncheckedWarning()) { | |
3166 s << INDENT<< "@SuppressWarnings({\"unchecked\", \"unused\"})" << endl; | |
3167 } else if (d_function->needsSuppressUncheckedWarning()) { | |
3168 s << INDENT<< "@SuppressWarnings(\"unchecked\")" << endl; | |
3169 } else if (needsSuppressUnusedWarning) { | |
3170 s << INDENT<< "@SuppressWarnings(\"unused\")" << endl; | |
3171 } | |
3172 | |
3173 if (!(attr & NoBlockedSlot) | |
3174 && !d_function->isConstructor() | |
3175 && !d_function->isSlot() | |
3176 && !d_function->isSignal() | |
3177 && !d_function->isStatic() | |
3178 && !(included_attributes & AbstractMetaAttributes::Static)) | |
3179 s << INDENT << "@QtBlockedSlot" << endl; | |
3180 */ | |
3181 if (!(options & ExternC)) | |
3182 s << INDENT; | |
3183 | |
3184 if (attr & AbstractMetaAttributes::Public) s << "public "; | |
3185 else if (attr & AbstractMetaAttributes::Protected) s << "protected "; | |
3186 else if (attr & AbstractMetaAttributes::Private) s << "private "; | |
3187 else if (attr & AbstractMetaAttributes::Native) s << "private extern(C) "; | |
3188 bool isStatic = (attr & AbstractMetaAttributes::Static); | |
3189 | |
3190 if (attr & AbstractMetaAttributes::Native) ; | |
3191 else if (!isStatic && (attr & AbstractMetaAttributes::FinalInTargetLang)) s << "final "; | |
3192 else if (!isStatic && (attr & AbstractMetaAttributes::Abstract)) s << "abstract "; | |
3193 | |
3194 if (isStatic && !(options & ExternC)) s << "static "; | |
3195 } | |
3196 | |
3197 if ((options & SkipReturnType) == 0) { | |
3198 QString modified_type = d_function->typeReplaced(0); | |
3199 if (options & ExternC) { | |
3200 uint options = 0x0004; // qtd externC | |
3201 s << CppImplGenerator::jniReturnName(d_function, options, true) << " "; | |
3202 } | |
3203 else if (modified_type.isEmpty()) | |
3204 s << translateType(d_function->type(), d_function->implementingClass(), (Option) options); | |
3205 else | |
3206 s << modified_type.replace('$', '.'); | |
3207 s << " "; | |
3208 } | |
3209 | |
3210 } | |
3211 | |
3212 void DGenerator::writeConstructorContents(QTextStream &s, const AbstractMetaFunction *d_function) | |
3213 { | |
3214 // Write constructor | |
3215 s << " {" << endl; | |
3216 { | |
3217 Indentation indent(INDENT); | |
3218 bool shellClass = d_function->ownerClass()->generateShellClass(); | |
3219 if (cpp_shared) { | |
3220 if (shellClass && !d_function->ownerClass()->isInterface()) | |
3221 s << INDENT << "if (!init_flag_" << d_function->ownerClass()->name() << ")" << endl | |
3222 << INDENT << " static_init_" << d_function->ownerClass()->name() << "();" << endl << endl; | |
3223 } | |
3224 writeJavaCallThroughContents(s, d_function); | |
3225 | |
3226 // Write out expense checks if present... | |
3227 const AbstractMetaClass *d_class = d_function->implementingClass(); | |
3228 const ComplexTypeEntry *te = d_class->typeEntry(); | |
3229 if (te->expensePolicy().isValid()) { | |
3230 s << endl; | |
3231 const ExpensePolicy &ep = te->expensePolicy(); | |
3232 s << INDENT << "qt.GeneratorUtilities.countExpense(" << d_class->fullName() | |
3233 << ".class, " << ep.cost << ", " << ep.limit << ");" << endl; | |
3234 } | |
3235 | |
3236 foreach (CodeSnip snip, te->codeSnips()) { | |
3237 if (snip.language == TypeSystem::Constructors) { | |
3238 snip.formattedCode(s, INDENT); | |
3239 } | |
3240 } | |
3241 | |
3242 if(d_function->implementingClass()->isQObject()) | |
3243 { | |
3244 bool hasParentArg = false; | |
3245 AbstractMetaArgumentList arguments = d_function->arguments(); | |
37 | 3246 int arg_index = 0; |
1 | 3247 for (int i=0; i<arguments.count(); ++i) { |
3248 const AbstractMetaArgument *arg = arguments.at(i); | |
37 | 3249 if (arg->argumentName().contains("parent", Qt::CaseInsensitive)) { |
3250 arg_index = i; | |
1 | 3251 hasParentArg = true; |
37 | 3252 } |
1 | 3253 } |
3254 | |
37 | 3255 const AbstractMetaArgument *arg = arguments.at(arg_index); |
1 | 3256 // QString ctor_call = d_function->implementingClass()->name() == "QObject"? "this" : "super"; |
3257 QString ctor_call = "this"; | |
3258 if (hasParentArg) { | |
37 | 3259 s << INDENT << "bool gc_managed = " << arg->argumentName() << " is null ? true : false;" << endl |
1 | 3260 << INDENT << ctor_call << "(__qt_return_value, gc_managed);" << endl; |
3261 } else { | |
3262 s << INDENT << ctor_call << "(__qt_return_value, true);" << endl; | |
3263 } | |
105
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
3264 |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
3265 // creating a link object associated with the current QObject for signal handling and metadata |
3aa118a9ae71
each QObject now has associated child QObject for handling signals. fixes #15
eldar
parents:
104
diff
changeset
|
3266 s << INDENT << "qtd_" << d_function->ownerClass()->name() << "_create_link(this.nativeId, cast(void*) this);" << endl; |
1 | 3267 } |
3268 else | |
35 | 3269 s << INDENT << "this(__qt_return_value);" << endl; |
1 | 3270 } |
3271 s << INDENT << "}" << endl << endl; | |
3272 | |
3273 /* qtd // Write native constructor | |
3274 if (d_function->jumpTableId() == -1) | |
3275 writePrivateNativeFunction(s, d_function); | |
3276 */ | |
3277 } | |
3278 | |
3279 void DGenerator::writeFunctionArguments(QTextStream &s, const AbstractMetaFunction *d_function, | |
3280 int argument_count, uint options) | |
3281 { | |
3282 AbstractMetaArgumentList arguments = d_function->arguments(); | |
3283 | |
3284 if (argument_count == -1) | |
3285 argument_count = arguments.size(); | |
3286 | |
3287 for (int i=0; i<argument_count; ++i) { | |
3288 if (!d_function->argumentRemoved(i+1)) { | |
3289 if (i != 0) | |
3290 s << ", "; | |
3291 writeArgument(s, d_function, arguments.at(i), options); | |
3292 } | |
3293 } | |
3294 } | |
3295 | |
3296 | |
3297 void DGenerator::writeExtraFunctions(QTextStream &s, const AbstractMetaClass *d_class) | |
3298 { | |
3299 const ComplexTypeEntry *class_type = d_class->typeEntry(); | |
3300 Q_ASSERT(class_type); | |
3301 | |
3302 CodeSnipList code_snips = class_type->codeSnips(); | |
3303 foreach (const CodeSnip &snip, code_snips) { | |
3304 if ((!d_class->isInterface() && snip.language == TypeSystem::TargetLangCode) | |
3305 || (d_class->isInterface() && snip.language == TypeSystem::Interface)) { | |
3306 s << endl; | |
3307 snip.formattedCode(s, INDENT); | |
3308 } | |
3309 } | |
3310 } | |
3311 | |
3312 | |
3313 void DGenerator::writeToStringFunction(QTextStream &s, const AbstractMetaClass *d_class) | |
3314 { | |
3315 bool generate = d_class->hasToStringCapability() && !d_class->hasDefaultToStringFunction(); | |
3316 bool core = d_class->package() == QLatin1String("qt.core"); | |
3317 bool qevent = false; | |
3318 | |
3319 const AbstractMetaClass *cls = d_class; | |
3320 while (cls) { | |
3321 if (cls->name() == "QEvent") { | |
3322 qevent = true; | |
3323 break; | |
3324 } | |
3325 cls = cls->baseClass(); | |
3326 } | |
3327 | |
3328 if (generate || qevent) { | |
3329 | |
3330 if (qevent && core) { | |
3331 s << endl | |
3332 << " @Override" << endl | |
3333 << " public String toString() {" << endl | |
3334 << " return getClass().getSimpleName() + \"(type=\" + type().name() + \")\";" << endl | |
3335 << " }" << endl; | |
3336 } else { | |
3337 s << endl | |
3338 << " @Override" << endl | |
3339 << " public String toString() {" << endl | |
3340 << " if (nativeId() == 0)" << endl | |
3341 << " throw new QNoNativeResourcesException(\"Function call on incomplete object of type: \" +getClass().getName());" << endl | |
3342 << " return __qt_toString(nativeId());" << endl | |
3343 << " }" << endl | |
3344 << " native String __qt_toString(long __this_nativeId);" << endl; | |
3345 } | |
3346 } | |
3347 } | |
3348 | |
3349 void DGenerator::writeCloneFunction(QTextStream &s, const AbstractMetaClass *d_class) | |
3350 { | |
3351 s << endl | |
3352 << " @Override" << endl | |
3353 << " public " << d_class->name() << " clone() {" << endl | |
3354 << " if (nativeId() == 0)" << endl | |
3355 << " throw new QNoNativeResourcesException(\"Function call on incomplete object of type: \" +getClass().getName());" << endl | |
3356 << " return __qt_clone(nativeId());" << endl | |
3357 << " }" << endl | |
3358 << " native " << d_class->name() << " __qt_clone(long __this_nativeId);" << endl; | |
3359 } | |
3360 | |
3361 ClassFromEntry* ClassFromEntry::m_instance = NULL; | |
3362 | |
3363 ClassFromEntry::ClassFromEntry() | |
3364 { | |
110 | 3365 } |
3366 | |
3367 AbstractMetaClass* ClassFromEntry::get(const TypeEntry *ctype) | |
3368 { | |
3369 if(!m_instance) | |
3370 return NULL; | |
3371 | |
3372 return m_instance->classFromEntry[ctype]; | |
3373 } | |
3374 | |
3375 void ClassFromEntry::construct(const AbstractMetaClassList &classes) | |
3376 { | |
3377 if(!m_instance) { | |
3378 m_instance = new ClassFromEntry; | |
3379 m_instance->setClasses(classes); | |
3380 m_instance->buildHash(); | |
3381 } | |
3382 } | |
3383 | |
3384 void ClassFromEntry::buildHash() | |
3385 { | |
1 | 3386 foreach (AbstractMetaClass *cls, m_classes) { |
3387 const ComplexTypeEntry *ctype = cls->typeEntry(); | |
3388 classFromEntry[ctype] = cls; | |
3389 } | |
3390 } | |
3391 | |
110 | 3392 void ClassFromEntry::print(QTextStream &s) |
1 | 3393 { |
110 | 3394 s << "_fuck_" << m_instance->m_classes.size(); |
3395 foreach (AbstractMetaClass *cls, m_instance->m_classes) { | |
3396 s << cls->name() << endl; | |
3397 } | |
1 | 3398 } |
110 | 3399 |