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