annotate generator/dgenerator.cpp @ 363:3b0545d4d479

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