comparison generator/dgenerator.cpp @ 1:e78566595089

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