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